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

jaxx.runtime.swing.editor.SimpleTimeEditor Maven / Gradle / Ivy

There is a newer version: 3.0-alpha-1
Show newest version
package jaxx.runtime.swing.editor;

/*-
 * #%L
 * JAXX :: Widgets
 * %%
 * Copyright (C) 2008 - 2017 CodeLutin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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 Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import jaxx.runtime.JAXXBinding;
import jaxx.runtime.JAXXContext;
import jaxx.runtime.JAXXObject;
import jaxx.runtime.JAXXObjectDescriptor;
import jaxx.runtime.JAXXUtil;
import jaxx.runtime.SwingUtil;
import jaxx.runtime.binding.DefaultJAXXBinding;
import jaxx.runtime.binding.SimpleJAXXObjectBinding;
import jaxx.runtime.context.DefaultJAXXContext;
import jaxx.runtime.swing.Table;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import static org.nuiton.i18n.I18n.t;

public class SimpleTimeEditor extends Table implements JAXXObject {

    /*-----------------------------------------------------------------------*/
    /*------------------ Constants for all public bindings ------------------*/
    /*-----------------------------------------------------------------------*/

    public static final String BINDING_HOUR_ENABLED = "hour.enabled";
    public static final String BINDING_HOUR_VALUE = "hour.value";
    public static final String BINDING_MINUTE_ENABLED = "minute.enabled";
    public static final String BINDING_MINUTE_MODEL_VALUE = "minuteModel.value";

    /*-----------------------------------------------------------------------*/
    /*------------------------- Other static fields -------------------------*/
    /*-----------------------------------------------------------------------*/

    private static final String $jaxxObjectDescriptor = "H4sIAAAAAAAAAI1Uu24TQRS9cfzASSwegRCUIJmnbBStHZBoUpAoieVEdpBwkCLcMPaO7InGs8PubLJgBfEJfAL0NEh0VIiCmoIG8QsIUdAi7uyuvXZiwJZfO/eec1/nzpvvkHBsuLJPPM+wXaFYhxrba3t79xv7tKk2qNO0mVSWDcFrIgaxOkyb/XNHwbV6RcMLIbywbnWkJagYQK9UYMpRTzl12pQqBZeHEU3HKdT65hVPunaPtZ/UKNZXP3/EXpovXscAPInZpbCU7P9QUSXxCsSYqeAcRjogBU5EC9OwmWhhvhl9ts6J4+yQDn0CzyFVgaQkNpIpuDp+yT6Hj/ekgtT1XdLgtKjg1lCiziGGNajJEGrUWEdyuovnm/6BlD5BUkGiY5mUKyiMD65qRMQw3WHCVbQa8CzqKr2QoCaZENTeIKE5AqXaRJic2gqWxw9cDjARS7xtuUhxfjDmdhhUu6T7nklOGpSXcTRDvhV9eswzKEc/XQwtNiwMJYnqMiJ1ReOfqEPCdvFYwXz9pCAfoCmQ4vwxKWpC3/p7bvbL+2/vSj39xTH2hZGuA+uDupC2JbGZTIc+HYjPVYwXqkSu1CHtUI675+/W4ojEaqEZk8N4ZzXc0HCjTJw2UiRSXz98nHv8eRJiJZjiFjFLRPtvQVq1beyCxU1P3lv1M5o5PIXfZ/AzGanrRlfQw2w4mR2306CBinLFpSy+b99Zyi7nj9D/gHAXW7rQ9YFGiyo9/sA3ny1k7xbRK0WFljwuWqbLnM3gIZc/8rC1iyNa26+vkf70a7b2drXX3gks99Jf3aMWJx5BkgnOBPUXPNzdkQs9LR3qmla0o6O2FkLpylDKV/3vmyc6GFMw27Zs9swSivA1zlqi498WySb+6O2JK+rh84xOPtyasudqNsOvT/8rQl//YwTMNAmnwiR2iVFu6lFEelgPTUZ1a+fh7ubAwPLdcKENh6pqdCHk+nPUl0Aur4d0MrvBK2SMFHuqynQHcP/UxYiGBFCM9ge2rEmYsgYAAA==";
    private static final Log log = LogFactory.getLog(SimpleTimeEditor.class);
    private static final long serialVersionUID = 1L;

    /*-----------------------------------------------------------------------*/
    /*--------------------------- Internal states ---------------------------*/
    /*-----------------------------------------------------------------------*/

    protected List $activeBindings = new ArrayList();
    protected Map $bindingSources = new HashMap();
    protected final Map $bindings = new TreeMap();
    protected Map $objectMap = new HashMap();
    protected Map $previousValues = new HashMap();
    private boolean allComponentsCreated;
    protected final JAXXContext delegateContext = new DefaultJAXXContext();

    /*-----------------------------------------------------------------------*/
    /*------------------------ Protected components  ------------------------*/
    /*-----------------------------------------------------------------------*/

    protected SimpleTimeEditorHandler handler;
    protected JSpinner hour;
    protected JLabel labelH;
    protected JSpinner minute;
    protected SpinnerDateModel minuteModel;
    protected SimpleTimeEditorModel model;

    /*-----------------------------------------------------------------------*/
    /*------------------------- Private components  -------------------------*/
    /*-----------------------------------------------------------------------*/

    private SimpleTimeEditor $Table0 = this;

    /*-----------------------------------------------------------------------*/
    /*---------------------- Raw body code from script ----------------------*/
    /*-----------------------------------------------------------------------*/

    public void init() { handler.init(); }
    public void setBean(Serializable bean) { model.setBean(bean); }
    public void setProperty(String property ) { model.setProperty(property); }
    public void setDate(Date date) { model.setDate(date); }

    /*-----------------------------------------------------------------------*/
    /*---------------------------- Constructors  ----------------------------*/
    /*-----------------------------------------------------------------------*/

    public SimpleTimeEditor() {
        $initialize();
    }

    public SimpleTimeEditor(JAXXContext parentContext) {
        JAXXUtil.initContext(this, parentContext);
        $initialize();
    }

    /*-----------------------------------------------------------------------*/
    /*--------------------------- Statics methods ---------------------------*/
    /*-----------------------------------------------------------------------*/

    public static JAXXObjectDescriptor $getJAXXObjectDescriptor() {
        return JAXXUtil.decodeCompressedJAXXObjectDescriptor($jaxxObjectDescriptor);
    }

    /*-----------------------------------------------------------------------*/
    /*---------------------- JAXXObject implementation ----------------------*/
    /*-----------------------------------------------------------------------*/

    @Override
    public void applyDataBinding(String $binding) {
        if (allComponentsCreated && $bindings.containsKey($binding)) {
            getDataBinding($binding).applyDataBinding();
        }
        processDataBinding($binding);
    }

    @Override
    public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
        super.firePropertyChange(propertyName, oldValue, newValue);
    }

    @Override
    public Map get$objectMap() {
        return $objectMap;
    }

    @Override
    public JAXXBinding getDataBinding(String bindingId) {
        return $bindings.get(bindingId);
    }

    @Override
    public JAXXBinding[] getDataBindings() {
        return $bindings.values().toArray(new JAXXBinding[$bindings.size()]);
    }

    @Override
    public Object getObjectById(String id) {
        return $objectMap.get(id);
    }

    @Override
    public void processDataBinding(String $binding, boolean $force) {
        if (!$force && $activeBindings.contains($binding)) { 
            return;
        }
        $activeBindings.add($binding);
        try {
            if (allComponentsCreated && $bindings.containsKey($binding)) {
                getDataBinding($binding).processDataBinding();
            }
        } finally {
            $activeBindings.remove($binding);
        }
    }

    @Override
    public void processDataBinding(String $binding) {
        processDataBinding($binding, false);
    }

    @Override
    public void registerDataBinding(JAXXBinding binding) {
        $bindings.put(binding.getId(), binding);
    }

    @Override
    public void removeDataBinding(String $binding) {
        if (allComponentsCreated && $bindings.containsKey($binding)) {
            getDataBinding($binding).removeDataBinding();
        }
    }

    /*-----------------------------------------------------------------------*/
    /*--------------------- JAXXContext implementation  ---------------------*/
    /*-----------------------------------------------------------------------*/

    @Override
    public  T getContextValue(Class clazz) {
        return delegateContext.getContextValue(clazz, null);
    }

    @Override
    public  T getContextValue(Class clazz, String name) {
        return delegateContext.getContextValue(clazz, name);
    }

    @Override
    public JAXXContext getDelegateContext() {
        return delegateContext;
    }

    @Override
    public  O getParentContainer(Class clazz) {
        return SwingUtil.getParentContainer(this, clazz);
    }

    @Override
    public  O getParentContainer(Object source, Class clazz) {
        return SwingUtil.getParentContainer(source, clazz);
    }

    @Override
    public  void removeContextValue(Class clazz) {
        delegateContext.removeContextValue(clazz, null);
    }

    @Override
    public  void removeContextValue(Class clazz, String name) {
        delegateContext.removeContextValue(clazz, name);
    }

    @Override
    public  void setContextValue(T o) {
        delegateContext.setContextValue(o, null);
    }

    @Override
    public  void setContextValue(T o, String name) {
        delegateContext.setContextValue(o, name);
    }

    /*-----------------------------------------------------------------------*/
    /*---------------------------- Event methods ----------------------------*/
    /*-----------------------------------------------------------------------*/

    public void doStateChanged__on__hour(ChangeEvent event) {
        if (log.isDebugEnabled()) {
            log.debug(event);
        }
        handler.updateTimeModelFromHour((Integer)hour.getValue());
    }

    public void doStateChanged__on__minute(ChangeEvent event) {
        if (log.isDebugEnabled()) {
            log.debug(event);
        }
        handler.updateTimeModelFromMinute(minuteModel.getDate());
    }

    /*-----------------------------------------------------------------------*/
    /*----------------------- Public acessor methods  -----------------------*/
    /*-----------------------------------------------------------------------*/

    public SimpleTimeEditorHandler getHandler() {
        return handler;
    }

    public JSpinner getHour() {
        return hour;
    }

    public JLabel getLabelH() {
        return labelH;
    }

    public JSpinner getMinute() {
        return minute;
    }

    public SpinnerDateModel getMinuteModel() {
        return minuteModel;
    }

    public SimpleTimeEditorModel getModel() {
        return model;
    }

    /*-----------------------------------------------------------------------*/
    /*--------------------- Components creation methods ---------------------*/
    /*-----------------------------------------------------------------------*/

    protected void createHandler() {
        $objectMap.put("handler", handler = new SimpleTimeEditorHandler(this));
    }

    protected void createHour() {
        $objectMap.put("hour", hour = new JSpinner());
        
        hour.setName("hour");
        hour.addChangeListener(JAXXUtil.getEventListener(ChangeListener.class, "stateChanged", this, "doStateChanged__on__hour"));
    }

    protected void createLabelH() {
        $objectMap.put("labelH", labelH = new JLabel());
        
        labelH.setName("labelH");
        labelH.setHorizontalAlignment(0);
        labelH.setText(t("timeeditor.H"));
    }

    protected void createMinute() {
        $objectMap.put("minute", minute = new JSpinner());
        
        minute.setName("minute");
        minute.addChangeListener(JAXXUtil.getEventListener(ChangeListener.class, "stateChanged", this, "doStateChanged__on__minute"));
    }

    protected void createMinuteModel() {
        $objectMap.put("minuteModel", minuteModel = new SpinnerDateModel());
    }

    protected void createModel() {
        $objectMap.put("model", model = new SimpleTimeEditorModel());
    }

    /*-----------------------------------------------------------------------*/
    /*------------------------ Internal jaxx methods ------------------------*/
    /*-----------------------------------------------------------------------*/

    private void $completeSetup() {
        allComponentsCreated = true;
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        // inline complete setup of $Table0
        add(hour, new GridBagConstraints(0, 0, 1, 1, 0.5, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0));
        add(labelH, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0));
        add(minute, new GridBagConstraints(2, 0, 1, 1, 0.5, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0));
        
        // apply 4 data bindings
        JAXXUtil.applyDataBinding(this, $bindings.keySet());
        
        // apply 3 property setters
        minuteModel.setCalendarField(java.util.Calendar.MINUTE);
        hour.setModel(new SpinnerNumberModel(0, 0, 23, 1));
        minute.setModel(minuteModel);
    }

    private void $initialize() {
        if (allComponentsCreated) {
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        $objectMap.put("$Table0", $Table0);
        createModel();
        createMinuteModel();
        createHandler();
        createHour();
        createLabelH();
        createMinute();
        // inline creation of $Table0
        setName("$Table0");
        
        // registers 4 data bindings
        $registerDefaultBindings();
        $completeSetup();
    }

    private void $registerDefaultBindings() {
        // register 4 data bindings
        registerDataBinding(new DefaultJAXXBinding(this, BINDING_MINUTE_MODEL_VALUE, true) {
        
            @Override
            public void applyDataBinding() {
                if (model != null) {
                    model.addPropertyChangeListener("date", this);
                }
            }
        
            @Override
            public void processDataBinding() {
                if (model != null) {
                    minuteModel.setValue(handler.setMinuteModel(model.getDate()));
                }
            }
        
            @Override
            public void removeDataBinding() {
                if (model != null) {
                    model.removePropertyChangeListener("date", this);
                }
            }
        });
        registerDataBinding(new DefaultJAXXBinding(this, BINDING_HOUR_VALUE, true) {
        
            @Override
            public void applyDataBinding() {
                if (model != null) {
                    model.addPropertyChangeListener("timeModel", this);
                }
            }
        
            @Override
            public void processDataBinding() {
                if (model != null) {
                    hour.setValue(model.getTimeModel() / 60);
                }
            }
        
            @Override
            public void removeDataBinding() {
                if (model != null) {
                    model.removePropertyChangeListener("timeModel", this);
                }
            }
        });
        registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_HOUR_ENABLED, true ,"enabled") {
        
            @Override
            public void processDataBinding() {
                hour.setEnabled(isEnabled());
            }
        });
        registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_MINUTE_ENABLED, true ,"enabled") {
        
            @Override
            public void processDataBinding() {
                minute.setEnabled(isEnabled());
            }
        });
    }

}