alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (JComboBoxProperties.java)

This example Groovy source code file (JComboBoxProperties.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

abstractsyntheticbinding, awt, bean, event, fullbinding, fullbinding, gui, itemlistener, jcombobox, jcombobox, jcomboboxelementsbinding, jcomboboxselectedelementbinding, jcomboboxselectedindexbinding, propertychangelistener, swing, targetbinding, targetbinding, triggerbinding, triggerbinding, util

The Groovy JComboBoxProperties.java source code

/*
 * Copyright 2007-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy.swing.binding;

import org.codehaus.groovy.binding.*;

import javax.swing.*;
import javax.swing.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Map;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

/**
 * @author <a href="mailto:shemnon@yahoo.com">Danno Ferrin
 * @version $Revision: 7953 $
 * @since Groovy 1.1
 */
public class JComboBoxProperties {
    public static Map<String, TriggerBinding> getSyntheticProperties() {
        Map<String, TriggerBinding> result = new HashMap();

        // to match property name
        result.put(JComboBox.class.getName() + "#selectedItem", new TriggerBinding() {
            public FullBinding createBinding(SourceBinding source, TargetBinding target) {
                return new JComboBoxSelectedElementBinding((PropertyBinding) source, target, "selectedItem");
            }
        });
        // to match JSR-295
        result.put(JComboBox.class.getName() + "#selectedElement", new TriggerBinding() {
            public FullBinding createBinding(SourceBinding source, TargetBinding target) {
                return new JComboBoxSelectedElementBinding((PropertyBinding) source, target, "selectedElement");
            }
        });

        result.put(JComboBox.class.getName() + "#selectedIndex", new TriggerBinding() {
            public FullBinding createBinding(SourceBinding source, TargetBinding target) {
                return new JComboBoxSelectedIndexBinding((PropertyBinding) source, target);
            }
        });


        // to match JSR-295
        result.put(JComboBox.class.getName() + "#elements", new TriggerBinding() {
            public FullBinding createBinding(SourceBinding source, TargetBinding target) {
                return new JComboBoxElementsBinding((PropertyBinding) source, target);
            }
        });

        return result;
    }
}


class JComboBoxSelectedElementBinding extends AbstractSyntheticBinding implements PropertyChangeListener, ItemListener {
    JComboBox boundComboBox;

    public JComboBoxSelectedElementBinding(PropertyBinding source, TargetBinding target, String propertyName) {
        super(source, target, JComboBox.class, propertyName);
    }

    public synchronized void syntheticBind() {
        boundComboBox = (JComboBox) ((PropertyBinding)sourceBinding).getBean();
        boundComboBox.addPropertyChangeListener("model", this);
        boundComboBox.addItemListener(this);
    }

    public synchronized void syntheticUnbind() {
        boundComboBox.removePropertyChangeListener("model", this);
        boundComboBox.removeItemListener(this);
        boundComboBox = null;
    }

    public void setTargetBinding(TargetBinding target) {
        super.setTargetBinding(target);
    }

    public void propertyChange(PropertyChangeEvent event) {
        update();
    }

    public void itemStateChanged(ItemEvent e) {
        update();
    }

}

class JComboBoxSelectedIndexBinding extends AbstractSyntheticBinding implements PropertyChangeListener, ItemListener {
    JComboBox boundComboBox;

    public JComboBoxSelectedIndexBinding(PropertyBinding source, TargetBinding target) {
        super(source, target, JComboBox.class, "selectedIndex");
    }

    public synchronized void syntheticBind() {
        boundComboBox = (JComboBox) ((PropertyBinding)sourceBinding).getBean();
        boundComboBox.addPropertyChangeListener("model", this);
        boundComboBox.addItemListener(this);
    }

    public synchronized void syntheticUnbind() {
        boundComboBox.removePropertyChangeListener("model", this);
        boundComboBox.removeItemListener(this);
        boundComboBox = null;
    }

    public void setTargetBinding(TargetBinding target) {
        super.setTargetBinding(target);
    }

    public void propertyChange(PropertyChangeEvent event) {
        update();
    }

    public void itemStateChanged(ItemEvent e) {
        update();
    }

}

class JComboBoxElementsBinding extends AbstractSyntheticBinding implements ListDataListener, PropertyChangeListener {
    JComboBox boundComboBox;

    public JComboBoxElementsBinding(PropertyBinding propertyBinding, TargetBinding target) {
        super(propertyBinding, target, JComboBox.class, "elements");
    }

    protected void syntheticBind() {
        boundComboBox = (JComboBox) ((PropertyBinding)sourceBinding).getBean();
        boundComboBox.addPropertyChangeListener("model", this);
        boundComboBox.getModel().addListDataListener(this);
    }

    protected void syntheticUnbind() {
        boundComboBox.removePropertyChangeListener("model", this);
        boundComboBox.getModel().removeListDataListener(this);
    }

    public void propertyChange(PropertyChangeEvent event) {
        update();
        ((ComboBoxModel) event.getOldValue()).removeListDataListener(this);
        ((ComboBoxModel) event.getNewValue()).addListDataListener(this);
    }

    public void intervalAdded(ListDataEvent e) {
        update();
    }

    public void intervalRemoved(ListDataEvent e) {
        update();
    }

    public void contentsChanged(ListDataEvent e) {
        update();
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy JComboBoxProperties.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.