1 6 7 package org.netbeans.test.editor.app.gui; 8 9 import java.awt.event.ActionEvent ; 10 import java.awt.event.ActionListener ; 11 import java.awt.event.MouseEvent ; 12 import java.beans.PropertyChangeEvent ; 13 import java.beans.PropertyChangeListener ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Set ; 19 import java.util.Vector ; 20 import javax.swing.JMenu ; 21 import javax.swing.JMenuItem ; 22 import javax.swing.JPopupMenu ; 23 import javax.swing.JSeparator ; 24 import javax.swing.tree.DefaultTreeModel ; 25 import javax.swing.tree.TreeModel ; 26 import javax.swing.tree.TreePath ; 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 39 public class TreeDialog extends javax.swing.JDialog implements PropertyChangeListener { 40 41 TestEditorFrame main; 42 HashMap popups; 43 int mx,my; 44 String oldString=""; 45 46 47 public TreeDialog(TestEditorFrame parent) { 48 super(parent,false); 49 main=parent; 50 initComponents(); 51 newMenu=new JMenu ("New"); 52 sep1=new JSeparator (); 53 sep2=new JSeparator (); 54 sep3=new JSeparator (); 55 popups=new HashMap (); 56 57 setSize(parent.getWidth()/3,parent.getHeight()); 58 setLocation(parent.getX()-getWidth(), parent.getY()); 59 } 60 61 66 private void initComponents() { popupM = new javax.swing.JPopupMenu (); 68 jPanel1 = new javax.swing.JPanel (); 69 jScrollPane1 = new javax.swing.JScrollPane (); 70 tree = new javax.swing.JTree (); 71 72 setTitle("Test Editor Tree [Default]"); 73 addWindowListener(new java.awt.event.WindowAdapter () { 74 public void windowClosing(java.awt.event.WindowEvent evt) { 75 closeDialog(evt); 76 } 77 }); 78 79 jPanel1.setLayout(new java.awt.BorderLayout ()); 80 81 tree.addMouseListener(new java.awt.event.MouseAdapter () { 82 public void mouseClicked(java.awt.event.MouseEvent 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 } 96 private void mouseClicked(java.awt.event.MouseEvent evt) { if (evt.getModifiers() == MouseEvent.BUTTON3_MASK) { 99 mx=evt.getX(); 100 my=evt.getY(); 101 new Thread () { 102 public void run() { 103 TreePath [] 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 o=sels[i].getLastPathComponent(); 108 selected[i]=(TestNodeDelegate)o; 109 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 } 129 130 private void closeDialog(java.awt.event.WindowEvent evt) { setVisible(false); 132 dispose(); 133 } 135 public void setRootContext(TestNodeDelegate root) { 136 Object rt=((DefaultTreeModel )(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 )(tree.getModel())).setRoot(root); 144 ((DefaultTreeModel )(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 if (selected.length == 1) { 160 Vector 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 it=types.iterator();it.hasNext();) { 167 act=(TreeNodeAction)(it.next()); 168 newMenu.add(getMenu(act,selected)); 169 } 170 } 171 } 172 Vector acts=mergeActions(selected); 174 176 Vector cooks=cookieActions(selected); 177 if (cooks != null && cooks.size() > 0) { 178 TreeNodeAction act; 179 for (Iterator 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 it=acts.iterator();it.hasNext();) { 188 act=(TreeNodeAction)(it.next()); 189 popupM.add(getMenu(act,selected)); 190 } 191 } 192 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 cookieActions(TestNodeDelegate[] selected) { 201 Set v1,v2,v3; 202 Object 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 (); 207 for (Iterator iti=v2.iterator();iti.hasNext();) { 208 o=iti.next(); 209 for (Iterator 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 v=new Vector (); 220 for (Iterator it=v2.iterator();it.hasNext();) { 221 v.addAll(ActionRegistry.getDefault().getActions((Class )(it.next()))); 222 } 223 return v; 224 } 225 226 private Vector mergeActions(TestNodeDelegate[] selected) { 227 Vector v1,v2,v3; 228 TreeNodeAction a1,a2; 229 230 v2=new Vector (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 (); 235 for (int j=0;j < v2.size();j++) { 236 a2=(TreeNodeAction)(v2.get(j)); 237 for (Iterator 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 getMenu(TreeNodeAction action,TestNodeDelegate[] selected) { 251 MenuItem 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 menu=new JMenuItem (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 (perfor,menu); 265 registerMenu(action.getClass(),it); 266 perfor.setSelected(selected); 267 return menu; 268 } 269 } 270 271 private void registerMenu(Class action, MenuItem menu) { 272 popups.put(action, menu); 273 } 274 275 private MenuItem getMenu(Class action) { 276 return (MenuItem )(popups.get(action)); 277 } 278 279 public TreeModel getTreeModel() { 280 return tree.getModel(); 281 } 282 283 288 public void propertyChange(PropertyChangeEvent evt) { 289 Object rt=((DefaultTreeModel )(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 menuItem; 304 305 public MenuItem(ActionPerformer perf,JMenuItem menu) { 306 performer=perf; 307 menuItem=menu; 308 } 309 } 310 311 static class ActionPerformer implements ActionListener { 312 313 TreeNodeAction action; 314 TestNodeDelegate[] selected=null; 315 316 public ActionPerformer(TreeNodeAction act) { 317 action=act; 318 } 319 320 public void actionPerformed(ActionEvent ev) { 321 new Thread () { 322 public void run() { 323 action.performAction(selected); 324 } 325 }.start(); 326 } 327 328 332 public TestNodeDelegate[] getSelected() { 333 return this.selected; 334 } 335 336 340 public void setSelected(TestNodeDelegate[] selected) { 341 this.selected = selected; 342 } 343 344 } 345 346 private javax.swing.JScrollPane jScrollPane1; 348 private javax.swing.JTree tree; 349 private javax.swing.JPopupMenu popupM; 350 private javax.swing.JPanel jPanel1; 351 private JMenu newMenu; 353 private JSeparator sep1; 354 private JSeparator sep2; 355 private JSeparator sep3; 356 } 357 | Popular Tags |