KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > nbprefuse > NodeExpansionMouseControl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * NodeExpansionMouseControl.java
22  *
23  * Created on January 17, 2006, 12:50 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.nbprefuse;
30
31
32 import java.awt.event.ActionEvent JavaDoc;
33 import java.awt.event.ActionListener JavaDoc;
34 import java.awt.event.MouseEvent JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import javax.swing.JMenuItem JavaDoc;
37 import javax.swing.JPopupMenu JavaDoc;
38 import org.netbeans.modules.xml.nbprefuse.util.GraphUtilities;
39 import org.openide.util.NbBundle;
40 import prefuse.Visualization;
41 import prefuse.controls.ControlAdapter;
42 import prefuse.visual.AggregateItem;
43 import prefuse.visual.NodeItem;
44 import prefuse.visual.VisualItem;
45
46 /**
47  *
48  * @author Jeri Lockhart
49  */

50 public final class NodeExpansionMouseControl extends ControlAdapter {
51     
52     private JPopupMenu JavaDoc popup;
53     private VisualItem vItem;
54     private JMenuItem JavaDoc menuItem;
55     
56     public NodeExpansionMouseControl(final Visualization viz, final String JavaDoc activity) {
57         popup = new JPopupMenu JavaDoc();
58         menuItem = new JMenuItem JavaDoc();
59         menuItem.addActionListener(new ActionListener JavaDoc() {
60             public void actionPerformed(ActionEvent JavaDoc e){
61                 if (vItem != null && vItem instanceof VisualItem){
62                     NodeItem fileNode = null;
63                     if (vItem.canGetBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE) &&
64                             vItem.getBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE)){
65                         
66                         fileNode = findFileNode(vItem.getInt(AnalysisConstants.ID),
67                                 AggregateItem.class.cast(vItem));
68                     } else {
69                         fileNode = NodeItem.class.cast(vItem);
70                     }
71                     if (fileNode.canGetBoolean(AnalysisConstants.IS_FILE_NODE) &&
72                             fileNode.getBoolean(AnalysisConstants.IS_FILE_NODE)){
73                         GraphUtilities.expandCollapseFileNode(fileNode);
74                         if (activity != null){
75                             viz.run(activity);
76                         }
77                     }
78                 }
79             }
80         });
81         popup.add(menuItem);
82         
83     }
84     
85     public void itemReleased(VisualItem gi, MouseEvent JavaDoc e) {
86         
87         super.itemReleased(gi, e);
88         vItem = gi;
89         maybeShowPopup(e);
90     }
91     
92     public void itemPressed(VisualItem gi, MouseEvent JavaDoc e) {
93         
94         super.itemPressed(gi, e);
95         vItem = gi;
96         maybeShowPopup(e);
97     }
98     
99     
100     
101     private void maybeShowPopup(MouseEvent JavaDoc e) {
102         if (e.isPopupTrigger()) {
103             NodeItem fileNode = null;
104             if (vItem.canGetBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE) &&
105                     vItem.getBoolean(AnalysisConstants.IS_FILE_GROUP_AGGREGATE)){
106                 fileNode = findFileNode(vItem.getInt(AnalysisConstants.ID),
107                         AggregateItem.class.cast(vItem));
108             }
109             if (vItem.canGetBoolean(AnalysisConstants.IS_FILE_NODE) &&
110                     vItem.getBoolean(AnalysisConstants.IS_FILE_NODE)){
111                 fileNode = NodeItem.class.cast(vItem);
112             }
113             if (fileNode == null){
114                 return;
115             }
116             boolean isExpanded = fileNode.getBoolean(AnalysisConstants.IS_EXPANDED);
117             menuItem.setText(NbBundle.getMessage(NodeExpansionMouseControl.class,(isExpanded?"LBL_collapse":"LBL_expand")));
118             popup.show(e.getComponent(),
119                     e.getX(), e.getY());
120         }
121     }
122     
123     /**
124      * Finds the schema File Node in the AggregateItem
125      * The AggregateItem contains the schema file node and
126      * the SchemaComponent nodes for the file
127      *
128      */

129     private NodeItem findFileNode(final int aggregateItemFileGroup, final VisualItem item ) {
130         NodeItem fileNode = null;
131         AggregateItem agIt = AggregateItem.class.cast(item);
132         Iterator JavaDoc agItems = agIt.items();
133         while(agItems.hasNext()){
134             VisualItem agItem = (VisualItem)agItems.next();
135             int fileGroup = -1;
136             if (agItem.canGetInt(AnalysisConstants.FILE_NODE_FILE_GROUP)) {
137                 fileGroup = agItem.getInt(AnalysisConstants.FILE_NODE_FILE_GROUP);
138                 if (fileGroup == aggregateItemFileGroup &&
139                         agItem.canGetBoolean(AnalysisConstants.IS_EXPANDED)){
140                     fileNode = NodeItem.class.cast(agItem);
141                     break;
142                 }
143             }
144         }
145         return fileNode;
146     }
147     
148 }
149
150
151
Popular Tags