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

org.nuiton.jaxx.widgets.temperature.TemperatureEditor Maven / Gradle / Ivy

There is a newer version: 3.0.0-RC-12
Show newest version
package org.nuiton.jaxx.widgets.temperature;

/*-
 * #%L
 * JAXX :: Widgets Temperature
 * %%
 * Copyright (C) 2008 - 2020 Code Lutin, Ultreia.io
 * %%
 * 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 io.ultreia.java4all.jaxx.JaxxWidgetsTemperatureI18nEnumHelper;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nuiton.jaxx.runtime.JAXXBinding;
import org.nuiton.jaxx.runtime.JAXXContext;
import org.nuiton.jaxx.runtime.JAXXObject;
import org.nuiton.jaxx.runtime.JAXXObjectDescriptor;
import org.nuiton.jaxx.runtime.JAXXUtil;
import org.nuiton.jaxx.runtime.bean.BeanScopeAware;
import org.nuiton.jaxx.runtime.binding.DefaultJAXXBinding;
import org.nuiton.jaxx.runtime.context.DefaultJAXXContext;
import org.nuiton.jaxx.runtime.swing.JAXXButtonGroup;
import org.nuiton.jaxx.runtime.swing.SwingUtil;
import org.nuiton.jaxx.widgets.number.NumberEditor;

public class TemperatureEditor extends JPanel implements BeanScopeAware, JAXXObject {

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

    public static final String BINDING_C_FORMAT_SELECTED = "cFormat.selected";
    public static final String BINDING_EDITOR_NUMBER_VALUE = "editor.numberValue";
    public static final String BINDING_F_FORMAT_SELECTED = "fFormat.selected";
    public static final String BINDING_MODEL_FORMAT = "model.format";

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

    private static final String $jaxxObjectDescriptor = "H4sIAAAAAAAAAM1VT28TRxR/NthOYtI/oLYcOESFQgLpmByQqqYUQmJDIiexnAXS5gBj79jZaL0znZklG1lw4MCNL9BDr70gvgPihMSFa75DJT5C3+ysvQ6xSUCqVB9217Pv/d57v/d7b1/8AzklYZbLNglCT/OA7NAoIjIMtNdhZGVhc3O9scOaeomppvSE5hLsL5OF7BYU3f650kC2qohUskglg1RKkEqLvCN4wIIBoPkqTCi95zO1zZjWMD3SualUaaNvOR+JUCaxhmY9LNbbZ3P7Gbb0ZxYgEph+Acu+8gkAadUnq5D1XA2nqzv0ES35NGhjctIL2ljQpDlb9KlSa7TD/oAnUKhCXlCJYBp+/Cx6YrgYKhIaxi6s1GjA/Ksarn2Y/67ntplWRLOOYJLqUDLipM9lF02lEDFYXkOuw13ma/j5s3BWjXMKlmfxqT7Mag8tCDsNJslafLMYxne8D1FscdmhOi4P+TVURkTtIrPE1nzQvNCsxPYazh4wrVPX47dCjQkYy9OpQ8s6mL/fHXw1ZkPfuo1NGqWKBB5HwoLfljwUSICEi6NcULgkFW6qocwW5GSIxxrOj54ZI/s6WlnBnx8teBMmNnz68on8e/f9fk/leUxu6iivgYFGyQnJsc/aM7l9aSUeas8vrVIxvwXjivm4DeJpv/TxzDcSS8wes/jaIBGDRO5QtY1oucL+q9ffPnx3ArIVmPA5dSvU2C/DuN6WyBj33UjcuBknd2p3DK9fmTRRaq2k71e70wOytM2d6XWSoOhsDsy9R/2QTc88jpDHSx/nsZ/2y/X3b/56/sOrHpcZrOLCcTxTPnO/Q94LfC9g8c5I1sHQHVEUioUuT2d92PRDb2ZFMnffx9eLh0gyw9RIRXpA3+Mt3gwVbfgoyFyL+grvJzWLkM+l7goWdd/O6wCzy3M/BWUc3zsYGScY31Zpg/mHySeLM48R9ZGhW8M33SEG+L6oOfcdTzhx0JVjB+3tRo8Ho0KPqaTjGi4Piz51/fpUzKDBs4exLAyNl+Mmm6dZSLfL/5bqylFUV/4zqivHoLryCVS3jkd1TsOE/Yg4ewKLnuxWcG1o0jQjghlN2pc1XA1MBhrOdTfMyr5rls5SeXF5daE696C24Djl+popIFl1e7jzu8M/b6RWX6+V685vD5zyKj4sOHfrZXQthIpteO0gbWvRxr5n23G22698AHhE+cmX86jqzeLz6R5HrcEXv7hU06mGF7hY4K+HUDPmOnEkojlumEtzOMIZRPgXCkl70yMKAAA=";
    private static final Logger log = LogManager.getLogger(TemperatureEditor.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();
    protected final JAXXContext delegateContext = new DefaultJAXXContext();

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

    protected JRadioButton cFormat;
    protected NumberEditor editor;
    protected JRadioButton fFormat;
    protected JAXXButtonGroup formatBG;
    protected JPanel formatPanel;
    protected TemperatureEditorHandler handler;
    protected TemperatureEditorModel model;

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

    private TemperatureEditor $JPanel0;

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

    public void init(JLabel label) { handler.init(this, label); }
    
    @Override
    public Object getBean() { return model.getBean(); }
    
    @Override
    public void setBean(Object bean) { model.setBean(bean); }
    
    public void setConfig(TemperatureEditorConfig config) { model.setConfig(config); }
    public void setStorageTemperature(Float temperature) { model.setStorageTemperature(temperature); }
    public void setTemperature(Float temperature) { model.setTemperature(temperature); }
    public void setFormat(TemperatureFormat format) { model.setFormat(format); }

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

    public TemperatureEditor(LayoutManager param0, boolean param1) {
        super(param0 ,param1);
        $initialize();
    }

    public TemperatureEditor(JAXXContext parentContext, LayoutManager param1, boolean param2) {
        super(param1 ,param2);
        JAXXUtil.initContext(this, parentContext);
        $initialize();
    }

    public TemperatureEditor(LayoutManager param0) {
        super(param0);
        $initialize();
    }

    public TemperatureEditor(JAXXContext parentContext, LayoutManager param1) {
        super(param1);
        JAXXUtil.initContext(this, parentContext);
        $initialize();
    }

    public TemperatureEditor() {
        $initialize();
    }

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

    public TemperatureEditor(boolean param0) {
        super(param0);
        $initialize();
    }

    public TemperatureEditor(JAXXContext parentContext, boolean param1) {
        super(param1);
        JAXXUtil.initContext(this, parentContext);
        $initialize();
    }

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

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

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

    @Override
    public void applyDataBinding(String $binding) {
        if ($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 ($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 ($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 doFocusGained__on__$JPanel0(FocusEvent event) {
        if (log.isDebugEnabled()) {
            log.debug(event);
        }
        editor.requestFocusInWindow();
    }

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

    public JRadioButton getCFormat() {
        return cFormat;
    }

    public NumberEditor getEditor() {
        return editor;
    }

    public JRadioButton getFFormat() {
        return fFormat;
    }

    public JAXXButtonGroup getFormatBG() {
        return formatBG;
    }

    public JPanel getFormatPanel() {
        return formatPanel;
    }

    public TemperatureEditorHandler getHandler() {
        return handler;
    }

    public TemperatureEditorModel getModel() {
        return model;
    }

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

    protected void addChildrenToCFormat() {
        { ButtonGroup $buttonGroup = formatBG; cFormat.putClientProperty("$buttonGroup", $buttonGroup); $buttonGroup.add(cFormat); }
    }

    protected void addChildrenToFFormat() {
        { ButtonGroup $buttonGroup = formatBG; fFormat.putClientProperty("$buttonGroup", $buttonGroup); $buttonGroup.add(fFormat); }
    }

    protected void addChildrenToFormatPanel() {
        formatPanel.add(cFormat);
        formatPanel.add(fFormat);
    }

    protected void createCFormat() {
        $objectMap.put("cFormat", cFormat = new JRadioButton());
        
        cFormat.setName("cFormat");
        cFormat.setFocusable(false);
    }

    protected void createEditor() {
        $objectMap.put("editor", editor = new NumberEditor());
        
        editor.setName("editor");
        editor.setUseSign(false);
    }

    protected void createFFormat() {
        $objectMap.put("fFormat", fFormat = new JRadioButton());
        
        fFormat.setName("fFormat");
        fFormat.setFocusable(false);
    }

    protected void createFormatBG() {
        $objectMap.put("formatBG", formatBG = new JAXXButtonGroup());
    }

    protected void createFormatPanel() {
        $objectMap.put("formatPanel", formatPanel = new JPanel());
        
        formatPanel.setName("formatPanel");
        formatPanel.setLayout(new GridLayout(1, 0, 2, 0));
    }

    protected void createModel() {
        $objectMap.put("model", model = getContextValue(TemperatureEditorModel.class));
    }

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

    protected void $initialize() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        $JPanel0 = this;
        handler = new TemperatureEditorHandler();
        handler.beforeInit(this);
        $initialize_01_createComponents();
        $initialize_02_registerDataBindings();
        $initialize_03_finalizeCreateComponents();
        $initialize_04_applyDataBindings();
        $initialize_05_setProperties();
        $initialize_06_finalizeInitialize();
        handler.afterInit(this);
    }

    protected void $initialize_01_createComponents() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        $objectMap.put("$JPanel0", $JPanel0);
        createModel();
        createEditor();
        createFormatPanel();
        createCFormat();
        createFFormat();
        createFormatBG();
        // inline creation of $JPanel0
        setName("$JPanel0");
        setLayout(new BorderLayout());
        $JPanel0.addFocusListener(JAXXUtil.getEventListener(FocusListener.class, "focusGained", this, "doFocusGained__on__$JPanel0"));
    }

    protected void $initialize_02_registerDataBindings() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        // register 4 data bindings
        registerDataBinding(new DefaultJAXXBinding(this, BINDING_MODEL_FORMAT, true) {
        
            @Override
            public void applyDataBinding() {
                if (formatBG != null) {
                    formatBG.addPropertyChangeListener("selectedValue", this);
                }
            }
        
            @Override
            public void processDataBinding() {
                if (formatBG != null) {
                    model.setFormat((TemperatureFormat)formatBG.getSelectedValue());
                }
            }
        
            @Override
            public void removeDataBinding() {
                if (formatBG != null) {
                    formatBG.removePropertyChangeListener("selectedValue", this);
                }
            }
        });
        registerDataBinding(new DefaultJAXXBinding(this, BINDING_EDITOR_NUMBER_VALUE, true) {
        
            @Override
            public void applyDataBinding() {
                if (model != null) {
                    model.addPropertyChangeListener("temperature", this);
                }
            }
        
            @Override
            public void processDataBinding() {
                if (model != null) {
                    editor.setNumberValue(model.getTemperature());
                }
            }
        
            @Override
            public void removeDataBinding() {
                if (model != null) {
                    model.removePropertyChangeListener("temperature", this);
                }
            }
        });
        registerDataBinding(new DefaultJAXXBinding(this, BINDING_C_FORMAT_SELECTED, true) {
        
            @Override
            public void applyDataBinding() {
                if (model != null) {
                    model.addPropertyChangeListener("format", this);
                }
            }
        
            @Override
            public void processDataBinding() {
                if (model != null) {
                    cFormat.setSelected(TemperatureFormat.C == model.getFormat());
                }
            }
        
            @Override
            public void removeDataBinding() {
                if (model != null) {
                    model.removePropertyChangeListener("format", this);
                }
            }
        });
        registerDataBinding(new DefaultJAXXBinding(this, BINDING_F_FORMAT_SELECTED, true) {
        
            @Override
            public void applyDataBinding() {
                if (model != null) {
                    model.addPropertyChangeListener("format", this);
                }
            }
        
            @Override
            public void processDataBinding() {
                if (model != null) {
                    fFormat.setSelected(TemperatureFormat.F == model.getFormat());
                }
            }
        
            @Override
            public void removeDataBinding() {
                if (model != null) {
                    model.removePropertyChangeListener("format", this);
                }
            }
        });
    }

    protected void $initialize_03_finalizeCreateComponents() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        // inline complete setup of $JPanel0
        add(editor, BorderLayout.CENTER);
        add(formatPanel, BorderLayout.EAST);
        addChildrenToFormatPanel();
        addChildrenToCFormat();
        addChildrenToFFormat();
    }

    protected void $initialize_04_applyDataBindings() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        
        // apply 4 data bindings
        JAXXUtil.applyDataBinding(this, $bindings.keySet());
    }

    protected void $initialize_05_setProperties() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        
        // apply 11 property setters
        editor.setNumberType(Float.class);
        editor.setNumberPattern(SwingUtil.DECIMAL1_PATTERN);
        editor.setProperty(TemperatureEditorModel.PROPERTY_TEMPERATURE);
        cFormat.setText(JaxxWidgetsTemperatureI18nEnumHelper.getLabel(TemperatureFormat.C));
        { cFormat.putClientProperty("$value", TemperatureFormat.C);  Object $buttonGroup = cFormat.getClientProperty("$buttonGroup"); if ($buttonGroup instanceof JAXXButtonGroup) { ((JAXXButtonGroup) $buttonGroup).updateSelectedValue(); } }
        
        cFormat.setToolTipText(JaxxWidgetsTemperatureI18nEnumHelper.getDescription(TemperatureFormat.C));
        fFormat.setText(JaxxWidgetsTemperatureI18nEnumHelper.getLabel(TemperatureFormat.F));
        { fFormat.putClientProperty("$value", TemperatureFormat.F);  Object $buttonGroup = fFormat.getClientProperty("$buttonGroup"); if ($buttonGroup instanceof JAXXButtonGroup) { ((JAXXButtonGroup) $buttonGroup).updateSelectedValue(); } }
        
        fFormat.setToolTipText(JaxxWidgetsTemperatureI18nEnumHelper.getDescription(TemperatureFormat.F));
    }

    protected void $initialize_06_finalizeInitialize() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
    }

}