Package org.onemind.swingweb.mapinput.peerdelegate.javax.swing

Source Code of org.onemind.swingweb.mapinput.peerdelegate.javax.swing.JTreeInputDelegate$TreeEvent

/*
* Copyright (C) 2004 TiongHiang Lee
*
* This library 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 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not,  write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Email: thlee@onemindsoft.org
*/

package org.onemind.swingweb.mapinput.peerdelegate.javax.swing;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Map;
import java.util.logging.Logger;
import javax.swing.JTree;
import javax.swing.tree.*;
import org.onemind.awtbridge.input.BridgeInputContext;
import org.onemind.awtbridge.input.InputException;
import org.onemind.awtbridge.peer.BridgeComponentPeer;
import org.onemind.commons.java.util.StringUtils;
import org.onemind.swingweb.mapinput.peerdelegate.MapInputDelegate;
/**
* The JTable input delegate
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
public class JTreeInputDelegate extends JComponentInputDelegate
{

    /** the logger * */
    private static final Logger _logger = Logger.getLogger(JTreeInputDelegate.class.getName());

    /** the instance **/
    public static MapInputDelegate INSTANCE = new JTreeInputDelegate();

    protected static abstract class TreeEvent extends AWTEvent implements ActiveEvent
    {

        private TreePath _path;

        public TreeEvent(JTree tree, TreePath path)
        {
            super(tree, 0);
            _path = path;
        }

        public final TreePath getPath()
        {
            return _path;
        }
    }

    /**
     * {@inheritDoc}
     */
    public void processInput(BridgeComponentPeer peer, BridgeInputContext context, Map inputForm) throws InputException
    {
        if (hasEvent(peer, inputForm)) //quick check for table visibility
        {
            JTree tree = (JTree) peer.getComponentObject();
            TreeModel model = tree.getModel();
            String pathString = (String) inputForm.get(peer.getId());
            String actionKey = peer.getId() + "-action";
            String action = (String) inputForm.get(actionKey);
            if (!StringUtils.isNullOrEmpty(action))
            {
                if (action.equals("selected"))
                {
                    TreePath path = getTreePath(model, pathString);
                    TreeEvent evt = new TreeEvent(tree, path)
                    {

                        public void dispatch()
                        {
                            ((JTree) getSource()).setSelectionPath(getPath());
                        }
                    };
                    postEvent(context, evt);
                } else if (action.equals("selected-doubleclicked"))
                {
                    TreePath path = getTreePath(model, pathString);
                   
                    TreeEvent evt = new TreeEvent(tree, path)
                    {
                        public void dispatch()
                        {
                            JTree tree = (JTree)getSource();
                            TreePath path = getPath();
                            tree.setSelectionPath(path);
                            if (tree.isExpanded(path)){
                                tree.collapsePath(path);
                            } else {
                                tree.expandPath(path);
                            }                       
                            MouseListener[] listeners = tree.getMouseListeners();
                            Rectangle rec = tree.getPathBounds(path);
                            for (int i=0; i<listeners.length; i++){
                                listeners[i].mousePressed(
                                        new MouseEvent(tree, MouseEvent.MOUSE_CLICKED, System.currentTimeMillis(), 0, (int)rec.getX(), (int)rec.getY(), 2, false)
                                );
                            }
                        }
                    };
                    postEvent(context, evt);
                } else if (action.equals("addSelected"))
                {
                    TreeSelectionModel smodel = tree.getSelectionModel();
                    TreePath path = getTreePath(model, pathString);
                    if (smodel.isPathSelected(path))
                    {
                        TreeEvent evt = new TreeEvent(tree, path)
                        {

                            public void dispatch()
                            {
                                ((JTree) getSource()).removeSelectionPath(getPath());
                            }
                        };
                        postEvent(context, evt);
                    } else
                    {
                        TreeEvent evt = new TreeEvent(tree, path)
                        {

                            public void dispatch()
                            {
                                ((JTree) getSource()).removeSelectionPath(getPath());
                            }
                        };
                        postEvent(context, evt);
                    }
                    tree.setSelectionPath(path);
                } else if (action.equals("expanded"))
                {
                    TreePath path = getTreePath(model, pathString);
                    TreeEvent evt = new TreeEvent(tree, path)
                    {

                        public void dispatch()
                        {
                            ((JTree) getSource()).expandPath(getPath());
                        }
                    };
                    postEvent(context, evt);
                } else if (action.equals("collapsed"))
                {
                    TreePath path = getTreePath(model, pathString);
                    TreeEvent evt = new TreeEvent(tree, path)
                    {

                        public void dispatch()
                        {
                            ((JTree) getSource()).collapsePath(getPath());
                        }
                    };
                    postEvent(context, evt);
                } else
                {
                    _logger.warning("Unsupported action for JTree: " + action);
                }
            } else
            {
                _logger.warning("Input indicates JTree " + peer.getId() + " has event but no action in key " + actionKey + ""
                        + " was detected");
                _logger.warning("Form values: " + inputForm);
            }
        }
    }

    /**
     * Get the treepath as representing by the path string
     * @param model the model
     * @param pathString the path string
     * @return the treepath
     */
    private TreePath getTreePath(TreeModel model, String pathString)
    {
        String[] path = pathString.split("_");
        Object[] pathObjects = new Object[path.length];
        Object current = null;
        for (int i = 0; i < path.length; i++) //traverse the tree
        {
            if (current == null)
            {
                current = model.getRoot();
            } else
            {
                int index = Integer.parseInt(path[i]);
                current = model.getChild(current, index);
            }
            pathObjects[i] = current;
        }
        return new TreePath(pathObjects);
    }
}
TOP

Related Classes of org.onemind.swingweb.mapinput.peerdelegate.javax.swing.JTreeInputDelegate$TreeEvent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.