Package DisplayProject.actions

Source Code of DisplayProject.actions.TreeField

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject.actions;

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.tree.TreePath;

import DisplayProject.ArrayFieldModel;
import DisplayProject.DisplayNode;
import DisplayProject.JListView;
import DisplayProject.TableSorter;
import DisplayProject.UIutils;
import DisplayProject.controls.ListView;
import DisplayProject.controls.OutlineField;
import Framework.ParameterHolder;

/**
* Provides several EDT safe operations on a JTree, OutlienField and ListView
*
*/
@SuppressWarnings("deprecation")
public class TreeField extends PendingAction {

    private static final int SET_CURRENT = 1;
    private static final int LOCATE_NODE = 2;
    private int function;
    private DisplayNode value;
    public TreeField(Component pComponent, DisplayNode pValue, int function) {
        super(pComponent);
        this.value = pValue;
        this.function = function;
    }
    public DisplayNode getValue() {
        return this.value;
    }
  public void performAction() {
        switch (function){
        case SET_CURRENT:
            if (this._component instanceof JTree){
                if (this.value != null){
                    JTree jt = ((JTree)this._component);
                    jt.setSelectionPath(new TreePath(this.value));
                    jt.scrollPathToVisible(new TreePath(this.value));
                }
            } else if (this._component instanceof JListView) {
                ((JListView)this._component).setCurrentNode(this.value);
            } else if (this._component instanceof ListView){
                ((ListView)this._component).setCurrentNode(this.value);
            }else // it is an outline field
              ((OutlineField)this._component).setCurrentNode(this.value);
            break;
        case LOCATE_NODE:
            break;
        }
    }
    public static void setCurrentNode(Component comp, DisplayNode value){
        ActionMgr.addAction(new TreeField(comp, value, SET_CURRENT));
    }

    public static DisplayNode locateNode(Component comp, int x, int y, ParameterHolder column){
        // Outline fields are just fancy JTrees. Craig Mitchell 27/04/2007.
        if (comp instanceof OutlineField) {
            comp = ((OutlineField)comp).getTree();
        }
        if (comp instanceof JTree) {
            column.setInt(1);

            // CraigM: 20/03/2008 - Corrected to not always match a node
            TreePath tp = ((JTree)comp).getPathForLocation(x, y);
           
            if (tp == null || tp.getLastPathComponent() == null) {
              column.setInt(0);
              return null;
            }
            return (DisplayNode)tp.getLastPathComponent();
        }
        else if (comp instanceof JListView)
            // not implemented
            return null;
        else // It's a ListView
            return (DisplayNode)((ArrayFieldModel)((ListView)comp).getTableModel()).locateNode((UIutils.milsToPixels(x)) / 10, UIutils.milsToPixels((y) / 10), column);
    }


    public static DisplayNode getCurrentNode(Component comp){
        TreeField action = ActionMgr.getAction(comp, TreeField.class);
        if (action != null)  {
            return action.getValue();
        }
        if (comp instanceof JTree){
            JTree tree = (JTree)comp;
            TreePath[] paths = tree.getSelectionPaths();
            if ((paths == null) || (paths[0] == null))
                return null;
            else
                return (DisplayNode)paths[0].getLastPathComponent();
        }
        if (comp instanceof ListView){
            return ((ListView)comp).getCurrentNode();
        }
        if (comp instanceof OutlineField){
            return ((OutlineField)comp).getCurrentNode();
        }
        if (comp instanceof JListView){
            return ((JListView)comp).getCurrentNode();
        }
        if (comp instanceof JTable)
            return ((ArrayFieldModel)((TableSorter)((JTable)comp).getModel()).getTableModel()).getCurrentRow();
        else // it is a JListView
            return ((DisplayNode)((JListView)comp).getCurrentNode());

    }


}
TOP

Related Classes of DisplayProject.actions.TreeField

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.