KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > app > gui > TreeDialog


1 /*
2  * TreeDialog.java
3  *
4  * Created on November 13, 2002, 4:15 PM
5  */

6
7 package org.netbeans.test.editor.app.gui;
8
9 import java.awt.event.ActionEvent JavaDoc;
10 import java.awt.event.ActionListener JavaDoc;
11 import java.awt.event.MouseEvent JavaDoc;
12 import java.beans.PropertyChangeEvent JavaDoc;
13 import java.beans.PropertyChangeListener JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.Vector JavaDoc;
20 import javax.swing.JMenu JavaDoc;
21 import javax.swing.JMenuItem JavaDoc;
22 import javax.swing.JPopupMenu JavaDoc;
23 import javax.swing.JSeparator JavaDoc;
24 import javax.swing.tree.DefaultTreeModel JavaDoc;
25 import javax.swing.tree.TreeModel JavaDoc;
26 import javax.swing.tree.TreePath JavaDoc;
27 import org.netbeans.test.editor.app.core.TestNode;
28 import org.netbeans.test.editor.app.core.actions.ActionRegistry;
29 import org.netbeans.test.editor.app.gui.actions.TestPropertiesAction;
30 import org.netbeans.test.editor.app.gui.actions.TreeNodeAction;
31 import org.netbeans.test.editor.app.gui.tree.ActionsCache;
32 import org.netbeans.test.editor.app.gui.tree.TestGroupNodeDelegate;
33 import org.netbeans.test.editor.app.gui.tree.TestNodeDelegate;
34
35 /**
36  *
37  * @author eh103527
38  */

39 public class TreeDialog extends javax.swing.JDialog JavaDoc implements PropertyChangeListener JavaDoc {
40     
41     TestEditorFrame main;
42     HashMap JavaDoc popups;
43     int mx,my;
44     String JavaDoc oldString="";
45     
46     /** Creates new form TreeDialog */
47     public TreeDialog(TestEditorFrame parent) {
48         super(parent,false);
49         main=parent;
50         initComponents();
51         newMenu=new JMenu JavaDoc("New");
52         sep1=new JSeparator JavaDoc();
53         sep2=new JSeparator JavaDoc();
54         sep3=new JSeparator JavaDoc();
55         popups=new HashMap JavaDoc();
56         
57         setSize(parent.getWidth()/3,parent.getHeight());
58         setLocation(parent.getX()-getWidth(), parent.getY());
59     }
60     
61     /** This method is called from within the constructor to
62      * initialize the form.
63      * WARNING: Do NOT modify this code. The content of this method is
64      * always regenerated by the Form Editor.
65      */

66     private void initComponents() {//GEN-BEGIN:initComponents
67
popupM = new javax.swing.JPopupMenu JavaDoc();
68         jPanel1 = new javax.swing.JPanel JavaDoc();
69         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
70         tree = new javax.swing.JTree JavaDoc();
71
72         setTitle("Test Editor Tree [Default]");
73         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
74             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
75                 closeDialog(evt);
76             }
77         });
78
79         jPanel1.setLayout(new java.awt.BorderLayout JavaDoc());
80
81         tree.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
82             public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
83                 TreeDialog.this.mouseClicked(evt);
84             }
85         });
86
87         jScrollPane1.setViewportView(tree);
88
89         jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
90
91         getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
92
93         pack();
94     }//GEN-END:initComponents
95

96     private void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_mouseClicked
97
// Add your handling code here:
98
if (evt.getModifiers() == MouseEvent.BUTTON3_MASK) {
99             mx=evt.getX();
100             my=evt.getY();
101             new Thread JavaDoc() {
102                 public void run() {
103                     TreePath JavaDoc[] sels=tree.getSelectionPaths();
104                     TestNodeDelegate[] selected=new TestNodeDelegate[sels.length];
105                     TestNode t,parent=null;
106                     for (int i=0;i < sels.length;i++) {
107                         Object JavaDoc o=sels[i].getLastPathComponent();
108                         selected[i]=(TestNodeDelegate)o;
109                         //you couldn't have selected nodes from another parent
110
t=selected[i].getTestNode().getOwner();
111                         if (parent == null) {
112                             parent=t;
113                         } else {
114                             if (parent != t) {
115                                 selected=null;
116                                 break;
117                             }
118                         }
119                     }
120                     generatePopup(selected);
121                     popupM.pack();
122                     popupM.show(tree,mx,my);
123                     popupM.repaint();
124                 }
125             }.start();
126         }
127     }//GEN-LAST:event_mouseClicked
128

129     /** Closes the dialog */
130     private void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_closeDialog
131
setVisible(false);
132         dispose();
133     }//GEN-LAST:event_closeDialog
134

135     public void setRootContext(TestNodeDelegate root) {
136         Object JavaDoc rt=((DefaultTreeModel JavaDoc)(tree.getModel())).getRoot();
137         if (rt != null && rt instanceof TestGroupNodeDelegate) {
138             TestNode test=((TestNodeDelegate)rt).getTestNode();
139             if (test != null) {
140                 test.removePropertyChangeListener(this);
141             }
142         }
143         ((DefaultTreeModel JavaDoc)(tree.getModel())).setRoot(root);
144         ((DefaultTreeModel JavaDoc)(tree.getModel())).nodeStructureChanged(root);
145         TestNode test=root.getTestNode();
146         test.addPropertyChangeListener(this);
147         oldString=test.toString();
148         setTitle("Test ["+oldString+"]");
149     }
150     
151     public void close() {
152         closeDialog(null);
153     }
154     
155     protected void generatePopup(TestNodeDelegate[] selected) {
156         popupM.removeAll();
157         if (selected == null) return;
158         //New could be only on one node
159
if (selected.length == 1) {
160             Vector JavaDoc types=ActionRegistry.getDefault().getNewTypes(selected[0].getTestNode().getClass());
161             if (types != null && types.size() > 0) {
162                 popupM.add(newMenu);
163                 popupM.add(sep1);
164                 newMenu.removeAll();
165                 TreeNodeAction act;
166                 for (Iterator JavaDoc it=types.iterator();it.hasNext();) {
167                     act=(TreeNodeAction)(it.next());
168                     newMenu.add(getMenu(act,selected));
169                 }
170             }
171         }
172         //merged node actions (enabled)
173
Vector JavaDoc acts=mergeActions(selected);
174         //merged cookie actions
175

176         Vector JavaDoc cooks=cookieActions(selected);
177         if (cooks != null && cooks.size() > 0) {
178             TreeNodeAction act;
179             for (Iterator JavaDoc it=cooks.iterator();it.hasNext();) {
180                 act=(TreeNodeAction)(it.next());
181                 popupM.add(getMenu(act,selected));
182             }
183             popupM.add(sep2);
184         }
185         if (acts.size() > 0) {
186             TreeNodeAction act;
187             for (Iterator JavaDoc it=acts.iterator();it.hasNext();) {
188                 act=(TreeNodeAction)(it.next());
189                 popupM.add(getMenu(act,selected));
190             }
191         }
192         //Properties could be only on one node
193
if (selected.length == 1) {
194             TreeNodeAction act=ActionsCache.getDefault().getAction(selected[0].getTestNode().getClass(),TestPropertiesAction.class);
195             popupM.add(sep3);
196             popupM.add(getMenu(act,selected));
197         }
198     }
199     
200     private Vector JavaDoc cookieActions(TestNodeDelegate[] selected) {
201         Set JavaDoc v1,v2,v3;
202         Object JavaDoc o,o2;
203         v2=selected[0].getTestNode().getCookieSet().keySet();
204         for(int i=1;i < selected.length;i++) {
205             v1=selected[i].getTestNode().getCookieSet().keySet();
206             v3=new HashSet JavaDoc();
207             for (Iterator JavaDoc iti=v2.iterator();iti.hasNext();) {
208                 o=iti.next();
209                 for (Iterator JavaDoc it=v1.iterator();it.hasNext();) {
210                     o2=it.next();
211                     if (o.getClass().equals(o2.getClass())) {
212                         v3.add(o);
213                         break;
214                     }
215                 }
216             }
217             v2=v3;
218         }
219         Vector JavaDoc v=new Vector JavaDoc();
220         for (Iterator JavaDoc it=v2.iterator();it.hasNext();) {
221             v.addAll(ActionRegistry.getDefault().getActions((Class JavaDoc)(it.next())));
222         }
223         return v;
224     }
225     
226     private Vector JavaDoc mergeActions(TestNodeDelegate[] selected) {
227         Vector JavaDoc v1,v2,v3;
228         TreeNodeAction a1,a2;
229         
230         v2=new Vector JavaDoc(ActionsCache.getDefault().getActions(selected[0].getTestNode().getClass()));
231         
232         for(int i=1;i < selected.length;i++) {
233             v1=ActionsCache.getDefault().getActions(selected[i].getTestNode().getClass());
234             v3=new Vector JavaDoc();
235             for (int j=0;j < v2.size();j++) {
236                 a2=(TreeNodeAction)(v2.get(j));
237                 for (Iterator JavaDoc it=v1.iterator();it.hasNext();) {
238                     a1=(TreeNodeAction)(it.next());
239                     if (a1.getClass().equals(a2.getClass())) {
240                         v3.add(a1);
241                         break;
242                     }
243                 }
244             }
245             v2=v3;
246         }
247         return v2;
248     }
249     
250     private JMenuItem JavaDoc getMenu(TreeNodeAction action,TestNodeDelegate[] selected) {
251         MenuItem JavaDoc it=getMenu(action.getClass());
252         if (it != null) {
253             it.performer.setSelected(selected);
254             it.menuItem.setText(action.getName());
255             it.menuItem.setEnabled(action.enable(selected));
256             return it.menuItem;
257         } else {
258             JMenuItem JavaDoc menu=new JMenuItem JavaDoc(action.getName());
259             TreeDialog.ActionPerformer perfor=new TreeDialog.ActionPerformer(action);
260             menu.addActionListener(perfor);
261             menu.setToolTipText(action.getHelpCtx());
262             menu.setText(action.getName());
263             menu.setEnabled(action.enable(selected));
264             it=new MenuItem JavaDoc(perfor,menu);
265             registerMenu(action.getClass(),it);
266             perfor.setSelected(selected);
267             return menu;
268         }
269     }
270     
271     private void registerMenu(Class JavaDoc action, MenuItem JavaDoc menu) {
272         popups.put(action, menu);
273     }
274     
275     private MenuItem JavaDoc getMenu(Class JavaDoc action) {
276         return (MenuItem JavaDoc)(popups.get(action));
277     }
278     
279     public TreeModel JavaDoc getTreeModel() {
280         return tree.getModel();
281     }
282     
283     /** This method gets called when a bound property is changed.
284      * @param evt A PropertyChangeEvent object describing the event source
285      * and the property that has changed.
286      *
287      */

288     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
289         Object JavaDoc rt=((DefaultTreeModel JavaDoc)(tree.getModel())).getRoot();
290         if (rt != null) {
291             TestNode test=((TestNodeDelegate)rt).getTestNode();
292             if (test != null) {
293                 if (test.toString().compareTo(oldString) != 0) {
294                     oldString=test.toString();
295                     setTitle("Test ["+oldString+"]");
296                 }
297             }
298         }
299     }
300     
301     static class MenuItem {
302         public ActionPerformer performer;
303         public JMenuItem JavaDoc menuItem;
304         
305         public MenuItem(ActionPerformer perf,JMenuItem JavaDoc menu) {
306             performer=perf;
307             menuItem=menu;
308         }
309     }
310     
311     static class ActionPerformer implements ActionListener JavaDoc {
312         
313         TreeNodeAction action;
314         TestNodeDelegate[] selected=null;
315         
316         public ActionPerformer(TreeNodeAction act) {
317             action=act;
318         }
319         
320         public void actionPerformed(ActionEvent JavaDoc ev) {
321             new Thread JavaDoc() {
322                 public void run() {
323                     action.performAction(selected);
324                 }
325             }.start();
326         }
327         
328         /** Getter for property selected.
329          * @return Value of property selected.
330          *
331          */

332         public TestNodeDelegate[] getSelected() {
333             return this.selected;
334         }
335         
336         /** Setter for property selected.
337          * @param selected New value of property selected.
338          *
339          */

340         public void setSelected(TestNodeDelegate[] selected) {
341             this.selected = selected;
342         }
343         
344     }
345     
346     // Variables declaration - do not modify//GEN-BEGIN:variables
347
private javax.swing.JScrollPane JavaDoc jScrollPane1;
348     private javax.swing.JTree JavaDoc tree;
349     private javax.swing.JPopupMenu JavaDoc popupM;
350     private javax.swing.JPanel JavaDoc jPanel1;
351     // End of variables declaration//GEN-END:variables
352
private JMenu JavaDoc newMenu;
353     private JSeparator JavaDoc sep1;
354     private JSeparator JavaDoc sep2;
355     private JSeparator JavaDoc sep3;
356 }
357
Popular Tags