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

Groovy example source code file (JComponentProperties.java)

This example Groovy source code file (JComponentProperties.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

abstractjcomponentbinding, abstractjcomponentbinding, awt, bean, componentlistener, event, fullbinding, fullbinding, javabean, jcomponent, jcomponentproperties, map, propertychangelistener, string, swing, targetbinding, targetbinding, triggerbinding, triggerbinding, util

The Groovy JComponentProperties.java source code

/*
 * Copyright 2009 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 java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JComponent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Andres Almiray
 * @since Groovy 1.6
 */
public class JComponentProperties {
    public static Map<String, TriggerBinding> getSyntheticProperties() {
        Map<String, TriggerBinding> result = new HashMap();
        result.put(JComponent.class.getName() + "#size",
            new TriggerBinding() {
                public FullBinding createBinding(SourceBinding source, TargetBinding target) {
                    return new AbstractJComponentBinding((PropertyBinding) source, target, "size") {
                        public void componentResized(ComponentEvent event) {
                            update();
                        }
                    };
                }
            });
        result.put(JComponent.class.getName() + "#width",
            new TriggerBinding() {
                public FullBinding createBinding(SourceBinding source, TargetBinding target) {
                    return new AbstractJComponentBinding((PropertyBinding) source, target, "width") {
                        public void componentResized(ComponentEvent event) {
                            update();
                        }
                    };
                }
            });
        result.put(JComponent.class.getName() + "#height",
            new TriggerBinding() {
                public FullBinding createBinding(SourceBinding source, TargetBinding target) {
                    return new AbstractJComponentBinding((PropertyBinding) source, target, "height") {
                        public void componentResized(ComponentEvent event) {
                            update();
                        }
                    };
                }
            });
        result.put(JComponent.class.getName() + "#bounds",
            new TriggerBinding() {
                public FullBinding createBinding(SourceBinding source, TargetBinding target) {
                    return new AbstractJComponentBinding((PropertyBinding) source, target, "bounds") {
                        public void componentResized(ComponentEvent event) {
                            update();
                        }
                        public void componentMoved(ComponentEvent event) {
                            update();
                        }
                    };
                }
            });
        result.put(JComponent.class.getName() + "#x",
            new TriggerBinding() {
                public FullBinding createBinding(SourceBinding source, TargetBinding target) {
                    return new AbstractJComponentBinding((PropertyBinding) source, target, "x") {
                        public void componentMoved(ComponentEvent event) {
                            update();
                        }
                    };
                }
            });
        result.put(JComponent.class.getName() + "#y",
            new TriggerBinding() {
                public FullBinding createBinding(SourceBinding source, TargetBinding target) {
                    return new AbstractJComponentBinding((PropertyBinding) source, target, "y") {
                        public void componentMoved(ComponentEvent event) {
                            update();
                        }
                    };
                }
            });
        result.put(JComponent.class.getName() + "#visible",
            new TriggerBinding() {
                public FullBinding createBinding(SourceBinding source, TargetBinding target) {
                    return new AbstractJComponentBinding((PropertyBinding) source, target, "visible") {
                        public void componentHidden(ComponentEvent event) {
                            update();
                        }
                        public void componentShown(ComponentEvent event) {
                            update();
                        }
                    };
                }
            });
        return result;
    }
}

abstract class AbstractJComponentBinding extends AbstractSyntheticBinding implements PropertyChangeListener, ComponentListener {
    JComponent boundComponent;
    String propertyName;

    public AbstractJComponentBinding(PropertyBinding source, TargetBinding target, String propertyName) {
        super(source, target, JComponent.class, propertyName);
        source.setNonChangeCheck(true);
    }

    public synchronized void syntheticBind() {
        boundComponent = (JComponent) ((PropertyBinding)sourceBinding).getBean();
        boundComponent.addPropertyChangeListener(propertyName, this);
        boundComponent.addComponentListener(this);
    }

    public synchronized void syntheticUnbind() {
        boundComponent.removePropertyChangeListener(propertyName, this);
        boundComponent.removeComponentListener(this);
        boundComponent = null;
    }

    public void propertyChange(PropertyChangeEvent event) {
        update();
        ((JComponent)event.getOldValue()).removeComponentListener(this);
        ((JComponent)event.getNewValue()).addComponentListener(this);
    }

    public void componentHidden(ComponentEvent event) {}
    public void componentShown(ComponentEvent event) {}
    public void componentMoved(ComponentEvent event) {}
    public void componentResized(ComponentEvent event) {}
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy JComponentProperties.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.