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

org.nuiton.jaxx.demo.DemoTab Maven / Gradle / Ivy

There is a newer version: 3.0.5
Show newest version
package org.nuiton.jaxx.demo;

/*-
 * #%L
 * JAXX :: Demo
 * %%
 * Copyright (C) 2008 - 2018 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 java.awt.Container;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
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.DefaultJAXXBinding;
import org.nuiton.jaxx.runtime.context.DefaultJAXXContext;
import org.nuiton.jaxx.runtime.swing.SwingUtil;
import org.nuiton.jaxx.runtime.swing.TabInfo;
import org.nuiton.jaxx.runtime.swing.TabInfoPropertyChangeListener;
import static org.nuiton.i18n.I18n.t;

public class DemoTab extends JTabbedPane implements JAXXObject {

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

    public static final String BINDING_SOURCE_TABS_SOURCES = "sourceTabs.sources";
    public static final String BINDING_TAB_DEMO_PANEL_TITLE = "tabDemoPanel.title";

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

    private static final String $jaxxObjectDescriptor = "H4sIAAAAAAAAAJ1TPW8TQRAdX3CMnQQCQlQRsuR8IAF3ojbiQ7EiggxBmCLCDWvfYq913l129/DRpKCg4w9Q0NKg/IeIComG1v8BKT+B2fXFZ4IvAq7YO83NvHn75s2Xn1DUCm4K1fN5zIzg/oAkia9ibtiQ+o8e7O/vdQa0axpUdxWTRiiYPAUPvDYshdO4NuC3m4gUTJACixSkSMG2GErBKZ8Bqjehos3biOo+pcbA9dzirtZBa5pZT2Ss0l5zWc/r9f397XGBNj56AIlE+vbaN/4BILv1uSZ4LDRwuTkgb0gQEd5DcorxHl5oxca2I6L1EzKkr+EASk1YlEQhmIFb/yWPg3NQiTSwYASea6eph3Qo/AYez0lHSpe+aKBsw08Jp5GBa7klLsFWlKeFFS1i1aUIhmOt5la2XJb+vXbZkE4j67uRp7IeoWY+ttjlr0TG+UJaPoN9Bf8q2MwDQnv4mT2ySRXaUFQxhg3U8p1pzfUMsya2quXbyrZxie8OD9Tn0fH4xEsekvtDo9NVM2uDg5VKSKoMs9wuTowUGxYFj4mst6GsaYQ753Zq62zmrTQT2SOLSxbJt0j+Q6L7iFYsjY++Xn35YwG8HahEgoQ7xObvQtn0FSomojCR9+47csuj83iuWpoGVhjviiGOyPkP53InJIZUO4yHGLyboFhbZ4s15Xa4d/zt04eNoxPBCkh1/W8qM9GKL2CR8Yhx6tYv3ay567YkNY1Dka3NvEUq2PeqTG235s7qPBlKOvNhLYnta91dwX5tTmF+AQg6Tt5KBQAA";
    private static final Log log = LogFactory.getLog(DemoTab.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 DemoPanel demoPanel;
    protected DemoSources sourceTabs;
    protected TabInfo tabDemoPanel;
    protected TabInfo tabDemoSources;
    protected DemoTab top;

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

    private void $afterCompleteSetup() {
        SwingUtilities.invokeLater(new Runnable() {
    
            @Override
            public void run() {
                if (demoPanel.getSources().length == 0) {
                   top.remove(sourceTabs);
                   return;
                }
                sourceTabs.init();
            }
        });
    }

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

    public DemoTab(int param0) {
        super(param0);
        $initialize();
    }

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

    public DemoTab(int param0, int param1) {
        super(param0 ,param1);
        $initialize();
    }

    public DemoTab(JAXXContext parentContext, int param1, int param2) {
        super(param1 ,param2);
        JAXXUtil.initContext(this, parentContext);
        $initialize();
    }

    public DemoTab() {
        $initialize();
    }

    public DemoTab(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 ($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);
    }

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

    public DemoPanel getDemoPanel() {
        return demoPanel;
    }

    public DemoSources getSourceTabs() {
        return sourceTabs;
    }

    public TabInfo getTabDemoPanel() {
        return tabDemoPanel;
    }

    public TabInfo getTabDemoSources() {
        return tabDemoSources;
    }

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

    protected void addChildrenToTop() {
        add(demoPanel);
        add(sourceTabs);
        tabDemoPanel.setTabIndex(0);
        tabDemoPanel.addPropertyChangeListener(new TabInfoPropertyChangeListener(top, 0));
        tabDemoSources.setTabIndex(1);
        tabDemoSources.addPropertyChangeListener(new TabInfoPropertyChangeListener(top, 1));
        top.setTitleAt(1, t("Sources"));
    }

    protected void createDemoPanel() {
        $objectMap.put("demoPanel", demoPanel = getContextValue(DemoPanel.class, "content"));
        
        demoPanel.setName("demoPanel");
    }

    protected void createSourceTabs() {
        $objectMap.put("sourceTabs", sourceTabs = new DemoSources(demoPanel));
        
        sourceTabs.setName("sourceTabs");
    }

    protected void createTabDemoPanel() {
        $objectMap.put("tabDemoPanel", tabDemoPanel = new TabInfo("tabDemoPanel"));
    }

    protected void createTabDemoSources() {
        $objectMap.put("tabDemoSources", tabDemoSources = new TabInfo("tabDemoSources"));
        
        tabDemoSources.setTitle(t("Sources"));
    }

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

    protected void $initialize() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        top = this;
        $initialize_01_createComponents();
        $initialize_02_registerDataBindings();
        $initialize_03_finalizeCreateComponents();
        $initialize_04_applyDataBindings();
        $initialize_05_setProperties();
        $initialize_06_finalizeInitialize();
    }

    protected void $initialize_01_createComponents() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        $objectMap.put("top", top);
        createDemoPanel();
        createSourceTabs();
        createTabDemoPanel();
        createTabDemoSources();
        // inline creation of top
        setName("top");
    }

    protected void $initialize_02_registerDataBindings() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        // register 2 data bindings
        registerDataBinding(new DefaultJAXXBinding(this, BINDING_TAB_DEMO_PANEL_TITLE, true) {
        
            @Override
            public void applyDataBinding() {
                if (demoPanel != null) {
                    demoPanel.addPropertyChangeListener("demoTabTitle", this);
                }
            }
        
            @Override
            public void processDataBinding() {
                if (demoPanel != null) {
                    tabDemoPanel.setTitle(demoPanel.getDemoTabTitle());
                }
            }
        
            @Override
            public void removeDataBinding() {
                if (demoPanel != null) {
                    demoPanel.removePropertyChangeListener("demoTabTitle", this);
                }
            }
        });
        registerDataBinding(new DefaultJAXXBinding(this, BINDING_SOURCE_TABS_SOURCES, true) {
        
            @Override
            public void applyDataBinding() {
                if (demoPanel != null) {
                    demoPanel.addPropertyChangeListener("sources", this);
                }
            }
        
            @Override
            public void processDataBinding() {
                if (demoPanel != null) {
                    sourceTabs.setSources(Arrays.asList(demoPanel.getSources()));
                }
            }
        
            @Override
            public void removeDataBinding() {
                if (demoPanel != null) {
                    demoPanel.removePropertyChangeListener("sources", this);
                }
            }
        });
    }

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

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

    protected void $initialize_05_setProperties() {
        if (log.isDebugEnabled()) {
            log.debug(this);
        }
        
        // apply 1 property setters
        sourceTabs.setIncomingClass(demoPanel.getClass());
    }

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

}