001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.designtree;
016:
017: import java.awt.event.ActionEvent;
018: import java.awt.event.MouseAdapter;
019: import java.awt.event.MouseEvent;
020: import java.util.ArrayList;
021:
022: import javax.swing.JPopupMenu;
023: import javax.swing.JTree;
024: import javax.swing.SwingUtilities;
025: import javax.swing.tree.DefaultMutableTreeNode;
026: import javax.swing.tree.TreeModel;
027: import javax.swing.tree.TreePath;
028:
029: import com.metaboss.applications.designstudio.Application;
030: import com.metaboss.applications.designstudio.BaseAction;
031: import com.metaboss.applications.designstudio.BaseUserObject;
032: import com.metaboss.applications.designstudio.components.SeparatorAction;
033:
034: /* Design Tree Mouse Adapter class */
035:
036: public class TreeMouseAdapter extends MouseAdapter {
037: private JTree mTree = null;
038:
039: public TreeMouseAdapter(JTree pTree) {
040: mTree = pTree;
041: }
042:
043: public void mousePressed(MouseEvent e) {
044: if (SwingUtilities.isLeftMouseButton(e)) {
045: BaseUserObject lObject = getUserObject(e.getX(), e.getY());
046: if (lObject != null) {
047: switch (e.getClickCount()) {
048: //case 1:
049: //Application.fireObjectSelected(lObject);
050: // break;
051: case 2:
052: try {
053: lObject.editObject();
054: } catch (Exception ex) {
055: ex.printStackTrace();
056: Application.showError(ex.getMessage());
057: }
058: break;
059: }
060: }
061: } else if (e.isPopupTrigger())
062: showPopUp(e.getX(), e.getY());
063: super .mousePressed(e);
064: }
065:
066: public void mouseReleased(MouseEvent e) {
067: if (e.isPopupTrigger())
068: showPopUp(e.getX(), e.getY());
069: super .mouseReleased(e);
070: }
071:
072: private BaseUserObject getUserObject(int X, int Y) {
073: //TreePath selPath = mTree.getPathForLocation(X, Y);
074: TreePath selPath = mTree.getSelectionPath();
075: BaseUserObject lObject = null;
076: if (selPath != null) {
077: DefaultMutableTreeNode lNode = (DefaultMutableTreeNode) selPath
078: .getLastPathComponent();
079: try {
080: lObject = (BaseUserObject) lNode.getUserObject();
081: } catch (Throwable t) {
082: lObject = null;
083: }
084: }
085: return lObject;
086: }
087:
088: private void showPopUp(int X, int Y) {
089: BaseUserObject lObject = getUserObject(X, Y);
090: if (lObject != null) {
091: JPopupMenu lMenu = new JPopupMenu();
092: ArrayList lList = Application.mainFrame
093: .getActionActionsAsList(lObject);
094: if (lList != null) {
095: TreePath selPath = mTree.getSelectionPath();
096: if (selPath != null) {
097: DefaultMutableTreeNode lNode = (DefaultMutableTreeNode) selPath
098: .getLastPathComponent();
099: if (lNode != null && !lNode.isLeaf()) {
100: lList.add(0, new ExpandTreeAction(lNode));
101: lList.add(1, new CollapseTreeAction(lNode));
102: if (lList.size() > 2)
103: lList.add(2, new SeparatorAction());
104: }
105: }
106: Application.fillActions(lList.toArray(), null, null,
107: lMenu);
108: lMenu.show(mTree, X, Y);
109: }
110: }
111: }
112:
113: /* Actions */
114:
115: public class ExpandTreeAction extends BaseAction {
116: private DefaultMutableTreeNode mNode = null;
117:
118: public ExpandTreeAction(DefaultMutableTreeNode pNode) {
119: super ("Expand All Child Nodes", "Expand All Child Nodes",
120: Application.EXPANDTREE_ICON);
121: mNode = pNode;
122: }
123:
124: public void actionPerformed(ActionEvent arg0) {
125: if (mNode != null && mTree != null) {
126: TreeModel lModel = mTree.getModel();
127: if (lModel != null
128: && (lModel instanceof MetaBossPackageTreeModel)) {
129: MetaBossPackageTreeModel lMetabossModel = (MetaBossPackageTreeModel) lModel;
130: lMetabossModel.expandAll(mTree, mNode);
131: }
132: }
133: }
134: }
135:
136: public class CollapseTreeAction extends BaseAction {
137: private DefaultMutableTreeNode mNode = null;
138:
139: public CollapseTreeAction(DefaultMutableTreeNode pNode) {
140: super ("Collapse All Child Nodes",
141: "Collapse All Child Nodes",
142: Application.COLLAPSETREE_ICON);
143: mNode = pNode;
144: }
145:
146: public void actionPerformed(ActionEvent arg0) {
147: if (mNode != null && mTree != null) {
148: TreeModel lModel = mTree.getModel();
149: if (lModel != null
150: && (lModel instanceof MetaBossPackageTreeModel)) {
151: MetaBossPackageTreeModel lMetabossModel = (MetaBossPackageTreeModel) lModel;
152: lMetabossModel.collapseAll(mTree, mNode);
153: }
154: }
155: }
156: }
157: }
|