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

fr.ird.observe.client.util.tree.BeanTreeHeader Maven / Gradle / Ivy

package fr.ird.observe.client.util.tree;

/*-
 * #%L
 * ObServe Toolkit :: Common Client
 * %%
 * Copyright (C) 2008 - 2017 IRD, Ultreia.io
 * %%
 * This program 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.
 * 
 * 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 Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.JTree;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.binding.SimpleJAXXObjectBinding;
import org.nuiton.jaxx.runtime.context.DefaultJAXXContext;
import org.nuiton.jaxx.runtime.swing.SwingUtil;
import static org.nuiton.i18n.I18n.t;

public class BeanTreeHeader extends JPanel implements JAXXObject {

    /*-----------------------------------------------------------------------*/
    /*---------------- Constants for all javaBean properties ----------------*/
    /*-----------------------------------------------------------------------*/

    public static final String PROPERTY_LABEL_TEXT = "labelText";
    public static final String PROPERTY_SHOW_FOLD_ACTIONS = "showFoldActions";
    public static final String PROPERTY_SHOW_SELECT_ACTIONS = "showSelectActions";
    public static final String PROPERTY_TREE = "tree";

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

    public static final String BINDING_COLAPSE_ALL_VISIBLE = "colapseAll.visible";
    public static final String BINDING_EXPAND_ALL_VISIBLE = "expandAll.visible";
    public static final String BINDING_LABEL_TEXT = "label.text";
    public static final String BINDING_SELECT_ALL_VISIBLE = "selectAll.visible";
    public static final String BINDING_UNSELECT_ALL_VISIBLE = "unselectAll.visible";

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

    private static final String $jaxxObjectDescriptor = "H4sIAAAAAAAAANVYTW8TRxgeG38kdgikpVTtgdIESqqWNQEEbVNRnDgGp86HsBGIHNLx7sQZmOwsM7OJLQsOPfTWP9BDr71U/AfUU6VeeuU/VOIn8M6u7bUTr70uSGktZWPNvvPMM++339//QUkp0Jdc1A3bpYrbxmPcaBjCtRXdI8Zq/uHDjdpjYqoCkaagjuIC+Z9YHMW3UNbqrkuFjK0yIOV8pJxGyrWRcst8z+E2sXuAFssoI1WTEblLiFJoPnSzKWWu0pVcbDiuaJ81kPWgs/76aeFVjBR+iSPUcIB+Fq79xRgAwa0TZRSnlkLvlR/jfZxj2K4DOUHtOlzopF5bZljKdbxHnqLnKF1GKQcLAFPo8r9SjwfnQTUchSYurG5im7AroO4dYVBhGbwmidgnhskoIBiuosxQghBjiWC7Cl/uEmwR4TgeSkqhSYZrhFVJAzid1owNfQvDv0Ugdkru8oMiZ1beVJTbYOCZQHiJcwbwgfSMlq4QBs7SltcvZgKBhObUxmgY8gDOMlY1Pf1+siuW9MiBfvvkynqxXzCtgEINC4XO9EPC8hIW/cIZkzPsSJJnAP1+n/ySq8AeWu7jo3fuqow0HGxbsF0vfDJKWvp66Jc+qqGOfNa1o+4Q6LMwt4UwMYIwCTw2toWSwoVlhebCI1QH2T2Q8sNrLjy89DGe4I8vn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0ZjefU/bnCf5bXjZXk9Gsvro2pbQvmTkha0HuXO5GRgPe5ML5wRXUX8UMGaMKEZ0uMJqEB6qlGljj+dmevMdrx5jr/F6AjnGTPUoNLdN+4Yl0vKn3QcZvLpICbdoUgYj2BsMjYN/0dNJBrd2Ukoja7E2JbpTGYiWaZnjBNGpXfUM4pMTOd33OTQjqDpby34GX2+Rm344VK/dQQ6pp+ZCIgJanrTrtjtwRjnRmLozStDEGbfGuHiWyPMA8IbhMBpnN4WAAA=";
    private static final Log log = LogFactory.getLog(BeanTreeHeader.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 JButton colapseAll;
    protected JButton expandAll;
    protected BeanTreeHeaderHandler handler;
    protected JLabel label;
    protected String labelText;
    protected JButton selectAll;
    protected Boolean showFoldActions;
    protected Boolean showSelectActions;
    protected JToolBar toolbar;
    protected JTree tree;
    protected JButton unselectAll;

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

    private BeanTreeHeader $JPanel0;

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

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

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

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

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

    public BeanTreeHeader() {
        $initialize();
    }

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

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

    public BeanTreeHeader(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 doActionPerformed__on__colapseAll(ActionEvent event) {
        if (log.isDebugEnabled()) {
            log.debug(event);
        }
        handler.collapseAll();
    }

    public void doActionPerformed__on__expandAll(ActionEvent event) {
        if (log.isDebugEnabled()) {
            log.debug(event);
        }
        handler.expandAll();
    }

    public void doActionPerformed__on__selectAll(ActionEvent event) {
        if (log.isDebugEnabled()) {
            log.debug(event);
        }
        handler.selectAll();
    }

    public void doActionPerformed__on__unselectAll(ActionEvent event) {
        if (log.isDebugEnabled()) {
            log.debug(event);
        }
        handler.unselectAll();
    }

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

    public JButton getColapseAll() {
        return colapseAll;
    }

    public JButton getExpandAll() {
        return expandAll;
    }

    public BeanTreeHeaderHandler getHandler() {
        return handler;
    }

    public JLabel getLabel() {
        return label;
    }

    public String getLabelText() {
        return labelText;
    }

    public JButton getSelectAll() {
        return selectAll;
    }

    public Boolean getShowFoldActions() {
        return showFoldActions;
    }

    public Boolean getShowSelectActions() {
        return showSelectActions;
    }

    public JToolBar getToolbar() {
        return toolbar;
    }

    public JTree getTree() {
        return tree;
    }

    public JButton getUnselectAll() {
        return unselectAll;
    }

    public Boolean isShowFoldActions() {
        return showFoldActions !=null && showFoldActions;
    }

    public Boolean isShowSelectActions() {
        return showSelectActions !=null && showSelectActions;
    }

    /*-----------------------------------------------------------------------*/
    /*----------------------- Public mutator methods  -----------------------*/
    /*-----------------------------------------------------------------------*/

    public void setLabelText(String labelText) {
        String oldValue = this.labelText;
        this.labelText = labelText;
        firePropertyChange(PROPERTY_LABEL_TEXT, oldValue, labelText);
    }

    public void setShowFoldActions(Boolean showFoldActions) {
        Boolean oldValue = this.showFoldActions;
        this.showFoldActions = showFoldActions;
        firePropertyChange(PROPERTY_SHOW_FOLD_ACTIONS, oldValue, showFoldActions);
    }

    public void setShowSelectActions(Boolean showSelectActions) {
        Boolean oldValue = this.showSelectActions;
        this.showSelectActions = showSelectActions;
        firePropertyChange(PROPERTY_SHOW_SELECT_ACTIONS, oldValue, showSelectActions);
    }

    public void setTree(JTree tree) {
        JTree oldValue = this.tree;
        this.tree = tree;
        firePropertyChange(PROPERTY_TREE, oldValue, tree);
    }

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

    protected void addChildrenToToolbar() {
        toolbar.add(colapseAll);
        toolbar.add(expandAll);
        toolbar.add(selectAll);
        toolbar.add(unselectAll);
    }

    protected void createColapseAll() {
        $objectMap.put("colapseAll", colapseAll = new JButton());
        
        colapseAll.setName("colapseAll");
        colapseAll.setFocusable(false);
        colapseAll.setToolTipText(t("observe.tree.action.collapseAll.tip"));
        colapseAll.setFocusPainted(false);
        colapseAll.addActionListener(JAXXUtil.getEventListener(ActionListener.class, "actionPerformed", this, "doActionPerformed__on__colapseAll"));
    }

    protected void createExpandAll() {
        $objectMap.put("expandAll", expandAll = new JButton());
        
        expandAll.setName("expandAll");
        expandAll.setFocusable(false);
        expandAll.setToolTipText(t("observe.tree.action.expandAll.tip"));
        expandAll.setFocusPainted(false);
        expandAll.addActionListener(JAXXUtil.getEventListener(ActionListener.class, "actionPerformed", this, "doActionPerformed__on__expandAll"));
    }

    protected void createLabel() {
        $objectMap.put("label", label = new JLabel());
        
        label.setName("label");
    }

    protected void createLabelText() {
        $objectMap.put("labelText", labelText = null);
    }

    protected void createSelectAll() {
        $objectMap.put("selectAll", selectAll = new JButton());
        
        selectAll.setName("selectAll");
        selectAll.setFocusable(false);
        selectAll.setToolTipText(t("observe.tree.action.selectAll.tip"));
        selectAll.setFocusPainted(false);
        selectAll.addActionListener(JAXXUtil.getEventListener(ActionListener.class, "actionPerformed", this, "doActionPerformed__on__selectAll"));
    }

    protected void createShowFoldActions() {
        $objectMap.put("showFoldActions", showFoldActions = true);
    }

    protected void createShowSelectActions() {
        $objectMap.put("showSelectActions", showSelectActions = true);
    }

    protected void createToolbar() {
        $objectMap.put("toolbar", toolbar = new JToolBar());
        
        toolbar.setName("toolbar");
        toolbar.setBorderPainted(false);
        toolbar.setOpaque(false);
        toolbar.setFloatable(false);
    }

    protected void createTree() {
        $objectMap.put("tree", tree = new JTree());
        
        tree.setName("tree");
    }

    protected void createUnselectAll() {
        $objectMap.put("unselectAll", unselectAll = new JButton());
        
        unselectAll.setName("unselectAll");
        unselectAll.setFocusable(false);
        unselectAll.setToolTipText(t("observe.tree.action.unselectAll.tip"));
        unselectAll.setFocusPainted(false);
        unselectAll.addActionListener(JAXXUtil.getEventListener(ActionListener.class, "actionPerformed", this, "doActionPerformed__on__unselectAll"));
    }

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

    protected void $initialize() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        $JPanel0 = this;
        handler = new BeanTreeHeaderHandler();
        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);
        createLabelText();
        createShowFoldActions();
        createShowSelectActions();
        createTree();
        createLabel();
        createToolbar();
        createColapseAll();
        createExpandAll();
        createSelectAll();
        createUnselectAll();
        // inline creation of $JPanel0
        setName("$JPanel0");
        setLayout(new BorderLayout());
    }

    protected void $initialize_02_registerDataBindings() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        // register 5 data bindings
        registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_LABEL_TEXT, true ,"labelText") {
        
            @Override
            public void processDataBinding() {
                label.setText(t(getLabelText()));
            }
        });
        registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_COLAPSE_ALL_VISIBLE, true ,"showFoldActions") {
        
            @Override
            public void processDataBinding() {
                colapseAll.setVisible(isShowFoldActions());
            }
        });
        registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_EXPAND_ALL_VISIBLE, true ,"showFoldActions") {
        
            @Override
            public void processDataBinding() {
                expandAll.setVisible(isShowFoldActions());
            }
        });
        registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_SELECT_ALL_VISIBLE, true ,"showSelectActions") {
        
            @Override
            public void processDataBinding() {
                selectAll.setVisible(isShowSelectActions());
            }
        });
        registerDataBinding(new SimpleJAXXObjectBinding(this, BINDING_UNSELECT_ALL_VISIBLE, true ,"showSelectActions") {
        
            @Override
            public void processDataBinding() {
                unselectAll.setVisible(isShowSelectActions());
            }
        });
    }

    protected void $initialize_03_finalizeCreateComponents() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        // inline complete setup of $JPanel0
        add(tree);
        add(label, BorderLayout.CENTER);
        add(toolbar, BorderLayout.EAST);
        addChildrenToToolbar();
    }

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

    protected void $initialize_05_setProperties() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        
        // apply 5 property setters
        colapseAll.setIcon(SwingUtil.getUIManagerActionIcon("collapse"));
        expandAll.setIcon(SwingUtil.getUIManagerActionIcon("expand"));
        selectAll.setIcon(SwingUtil.getUIManagerActionIcon("select"));
        unselectAll.setIcon(SwingUtil.getUIManagerActionIcon("unselect"));
    }

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

}