All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.util.swing.BoundInlineEditLabel Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2008-2013 LimeTri. All rights reserved.
 *
 * AgroSense is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
 * this software, see the FLOSS License Exception
 * .
 *
 * AgroSense is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AgroSense.  If not, see .
 */
package nl.cloudfarming.client.util.swing;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Timon Veenstra
 */
public class BoundInlineEditLabel extends InlineEditLabel  implements Serializable{

    private static final String METHOD_ADD_PCL = "addPropertyChangeListener";
    private static final String METHOD_REMOVE_PCL = "removePropertyChangeListener";
    private Object bean;
    private String property;
    private static final Logger LOGGER = Logger.getLogger("nl.cloudfarming.client.util.swing");

    public void setBoundProperty(final Object bean, final String property) {
        this.bean = bean;
        this.property = property;
        initialize();
    }

    private void initialize() {

        setValue(getBoundValue());

        try {
            Method addPCLMethod = bean.getClass().getDeclaredMethod(METHOD_ADD_PCL, PropertyChangeListener.class);
            addPCLMethod.invoke(bean, new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (property.equals(evt.getPropertyName())) {
                        setValue(getValue());
                    }
                }
            });
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) {
            LOGGER.log(Level.WARNING, "Failure in adding listener to bound value on bean, {0}", ex.getMessage());
        }
    }

    public void setBoundValue(String newValue) {
        try {
            StringBuilder sb = new StringBuilder("set");
            sb.append(property.substring(0, 1).toUpperCase());
            sb.append(property.substring(1, property.length()));
            Method setMethod = bean.getClass().getDeclaredMethod(sb.toString(), String.class);
            setMethod.invoke(bean, newValue);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) {
            LOGGER.log(Level.WARNING, "Failure in changing value of bound property on bean, {0}", ex.getMessage());
        }
    }

    public String getBoundValue() {
        try {
            StringBuilder sb = new StringBuilder("get");
            sb.append(property.substring(0, 1).toUpperCase());
            sb.append(property.substring(1, property.length()));
            Method getMethod = bean.getClass().getDeclaredMethod(sb.toString());
            Object value = getMethod.invoke(bean);
            return (value != null) ? value.toString() : null;
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) {
            LOGGER.log(Level.WARNING, "Failure in retrieving bound value from bean, {0}", ex.getMessage());
        }
        return null;
    }

    public Object getBean() {
        return bean;
    }

    public void setBean(Object bean) {
        this.bean = bean;
        if (this.property != null){
            initialize();
        }
    }

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
        if (this.bean != null){
            initialize();
        }        
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy