JRadioButtonHandler.java :  » XML-UI » JAXX » jaxx » tags » swing » Java Open Source

Java Open Source » XML UI » JAXX 
JAXX » jaxx » tags » swing » JRadioButtonHandler.java
/*
 * Copyright 2006 Ethan Nicholas. All rights reserved.
 * Use is subject to license terms.
 */
package jaxx.tags.swing;

import javax.swing.*;
import javax.swing.event.*;

import jaxx.*;
import jaxx.compiler.*;
import jaxx.reflect.*;
import jaxx.runtime.swing.*;
import jaxx.tags.*;
import jaxx.types.*;

public class JRadioButtonHandler extends DefaultComponentHandler {
    private static final String VALUE_PROPERTY = "value";
    private static final String BUTTON_GROUP_PROPERTY = "buttonGroup";
    
    public JRadioButtonHandler(ClassDescriptor beanClass) {
        super(beanClass);
        if (!ClassDescriptorLoader.getClassDescriptor(AbstractButton.class).isAssignableFrom(beanClass))
            throw new IllegalArgumentException(getClass().getName() + " does not support the class " + beanClass.getName());
    }


    protected void configureProxyEventInfo() {
        super.configureProxyEventInfo();
        addProxyEventInfo("isSelected", ChangeListener.class, "model");
    }


    public ClassDescriptor getPropertyType(CompiledObject object, String name, JAXXCompiler compiler) throws CompilerException {
        if (name.equals(BUTTON_GROUP_PROPERTY))
            return null; // accepts either a String or a ButtonGroup
        else if (name.equals(VALUE_PROPERTY))
            return ClassDescriptorLoader.getClassDescriptor(Object.class);
        else
            return super.getPropertyType(object, name, compiler);
    }            


    public boolean isMemberBound(String name) throws UnsupportedAttributeException {
        if (name.equals(BUTTON_GROUP_PROPERTY) || name.equals(VALUE_PROPERTY))
            return false;
        return super.isMemberBound(name);
    }
    

    // handle buttonGroup assignment in addition block rather than initialization block
    public void setProperty(CompiledObject object, String name, Object value, JAXXCompiler compiler){
        if (name.equals(BUTTON_GROUP_PROPERTY))
            object.appendAdditionCode(getSetPropertyCode(object.getJavaCode(), name, TypeManager.getJavaCode(value), compiler));
        else
            super.setProperty(object, name, value, compiler);
    }


    public String getSetPropertyCode(String id, String name, String valueCode, JAXXCompiler compiler) throws CompilerException {
        if (name.equals(BUTTON_GROUP_PROPERTY)) {
            if (valueCode.startsWith("\"") && valueCode.endsWith("\"")) {
                valueCode = valueCode.substring(1, valueCode.length() - 1);
                CompiledObject buttonGroup = compiler.getCompiledObject(valueCode);
                if (buttonGroup == null) {
                    buttonGroup = new CompiledObject(valueCode, ClassDescriptorLoader.getClassDescriptor(JAXXButtonGroup.class), compiler);
                    compiler.registerCompiledObject(buttonGroup);
                }
            }
            return "{ javax.swing.ButtonGroup $buttonGroup = " + valueCode + "; " + id + ".putClientProperty(\"$buttonGroup\", $buttonGroup); $buttonGroup.add(" + id + "); }\n";
        }
        else if (name.equals(VALUE_PROPERTY))
            return "{ " + id + ".putClientProperty(\"$value\", " + valueCode + ");  Object $buttonGroup = " + id + ".getClientProperty(\"$buttonGroup\");" + 
                        " if ($buttonGroup instanceof jaxx.runtime.swing.JAXXButtonGroup) { ((jaxx.runtime.swing.JAXXButtonGroup) $buttonGroup).updateSelectedValue(); } }\n";
        else
            return super.getSetPropertyCode(id, name, valueCode, compiler);
    }    
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.