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

jaxx.runtime.swing.config.ConfigCallBackUIHandler Maven / Gradle / Ivy

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

/*
 * #%L
 * JAXX :: Config
 * %%
 * Copyright (C) 2008 - 2014 Code Lutin, Tony Chemit
 * %%
 * 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 jaxx.runtime.SwingUtil;
import jaxx.runtime.swing.config.model.CallBackEntry;
import jaxx.runtime.swing.config.model.CallBackMap;
import jaxx.runtime.swing.config.model.OptionModel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import java.awt.Component;
import java.awt.Window;
import java.util.List;
import java.util.Map;

import static org.nuiton.i18n.I18n.t;

/**
 * callBackUI handler
 *
 * @author Tony Chemit - [email protected]
 * @since 2.5.11
 */
public class ConfigCallBackUIHandler {

    /** Logger */
    private static final Log log =
            LogFactory.getLog(ConfigCallBackUIHandler.class);

    public void init(ConfigCallBackUI ui) {

        // build tree model
        DefaultMutableTreeNode root = new DefaultMutableTreeNode();

        CallBackMap forSaved = ui.getContextValue(
                CallBackMap.class, ConfigUIHandler.CALLBACKS_WITH_OPTIONS);

        for (Map.Entry> e :
                forSaved.entrySet()) {

            CallBackEntry callBackEntry = e.getKey();
            List options = e.getValue();

            DefaultMutableTreeNode callBackNode;
            callBackNode = new DefaultMutableTreeNode(callBackEntry, true);

            root.add(callBackNode);
            for (OptionModel o : options) {

                DefaultMutableTreeNode optionkNode;
                optionkNode = new DefaultMutableTreeNode(o, false);

                callBackNode.add(optionkNode);
            }
        }

        JTree tree = ui.getDetectedCallBack();

        tree.setModel(new DefaultTreeModel(root));

        SwingUtil.expandTree(tree);

        tree.setCellRenderer(new DefaultTreeCellRenderer() {
            @Override
            public Component getTreeCellRendererComponent(JTree tree,
                                                          Object value,
                                                          boolean sel,
                                                          boolean expanded,
                                                          boolean leaf,
                                                          int row,
                                                          boolean hasFocus) {
                if (log.isDebugEnabled()) {
                    log.debug("value = " + value.getClass());
                }
                if (value == null) {
                    return super.getTreeCellRendererComponent(
                            tree,
                            value,
                            sel,
                            expanded,
                            leaf,
                            row,
                            hasFocus);
                }

                if (!(value instanceof DefaultMutableTreeNode)) {
                    return super.getTreeCellRendererComponent(
                            tree,
                            value,
                            sel,
                            expanded,
                            leaf,
                            row,
                            hasFocus);
                }

                DefaultMutableTreeNode n = (DefaultMutableTreeNode) value;
                value = n.getUserObject();

                if (value instanceof CallBackEntry) {
                    CallBackEntry v = (CallBackEntry) value;
                    if (log.isDebugEnabled()) {
                        log.debug("callBackEntry detected " + v.getName());
                    }
                    value = t(v.getDescription());
                } else if (value instanceof OptionModel) {

                    OptionModel v = (OptionModel) value;
                    if (log.isDebugEnabled()) {
                        log.debug("option detected " + v.getKey());
                    }
                    value = v.getKey() + " (" + t(v.getDescription()) + ")";
                }

                JLabel rendererComponent;
                rendererComponent = (JLabel)
                        super.getTreeCellRendererComponent(
                                tree,
                                value,
                                sel,
                                expanded,
                                leaf,
                                row,
                                hasFocus);

                value = n.getUserObject();

                if (value instanceof CallBackEntry) {

                    CallBackEntry v = (CallBackEntry) value;
                    rendererComponent.setIcon(v.getIcon());
                }
                return rendererComponent;
            }
        });
    }

    public void doAction(final ConfigCallBackUI ui) {
        log.info("Launch callBacks...");
        Window parent = ui.getContextValue(Window.class, "parent");
        if (parent != null) {
            log.info("dispose parent window...");
            parent.dispose();
        }

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

                List callBacks;
                callBacks = ui.getContextValue(
                        List.class,
                        ConfigUIHandler.CALLBACKS);
                for (CallBackEntry e : callBacks) {
                    if (log.isInfoEnabled()) {
                        log.info("launch callBack " + t(e.getDescription()));
                    }
                    e.getAction().run();
                }
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy