1 19 20 package org.netbeans.modules.java.tools; 21 22 import java.awt.BorderLayout ; 23 import java.awt.event.ActionListener ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyVetoException ; 27 import java.text.FieldPosition ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Collections ; 31 import java.util.Comparator ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.ListIterator ; 35 import java.util.Map ; 36 import java.util.TreeSet ; 37 import javax.swing.SwingUtilities ; 38 import org.netbeans.api.mdr.MDRepository; 39 import org.netbeans.jmi.javamodel.*; 40 import org.netbeans.modules.java.ui.nodes.elements.ClassNode; 41 import org.netbeans.modules.java.ui.nodes.elements.ElementFormat; 42 import org.netbeans.modules.java.ui.nodes.elements.MethodNode; 43 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 44 import org.openide.ErrorManager; 45 import org.openide.explorer.ExplorerManager; 46 import org.openide.explorer.view.BeanTreeView; 47 import org.openide.nodes.AbstractNode; 48 import org.openide.nodes.Children; 49 import org.openide.nodes.Node; 50 import org.openide.util.NbBundle; 51 52 56 public class OverridePanel2 extends javax.swing.JPanel 57 implements PropertyChangeListener , ExplorerManager.Provider { 58 59 public static final String PROP_VALID = "OverridePanel2.valid"; 61 private static final Node WAIT_NODE = new WaitNode(); 62 private static final ClassElementsComparator CLASS_ELEMENTS_COMPARATOR = new ClassElementsComparator(); 63 private static final MethodElementsComparator METHOD_ELEMENTS_COMPARATOR = new MethodElementsComparator(); 64 private static final ElementFormat CLASS_ELEMENT_FORMAT = new ElementFormat(NbBundle.getMessage(OverridePanel2.class, "FMT_DefaultClassFormat2")); private static final ElementFormat METHOD_ELEMENT_FORMAT = new ElementFormat(NbBundle.getMessage(OverridePanel2.class, "FMT_DefaultMethodFormat2")); private static final ElementFormat METHOD_ELEMENT_TOOLTIP1_FORMAT = new ElementFormat(NbBundle.getMessage(OverridePanel2.class, "FMT_DefaultMethodFormat_Tooltip1")); private static final ElementFormat METHOD_ELEMENT_TOOLTIP2_FORMAT = new ElementFormat(NbBundle.getMessage(OverridePanel2.class, "FMT_DefaultMethodFormat_Tooltip2")); 69 private JMIInheritanceSupport support; 70 private HashMap selection = new HashMap (); 71 private BeanTreeView view; 72 private boolean lastFired; 73 private ExplorerManager manager = new ExplorerManager(); 74 75 76 public OverridePanel2(JMIInheritanceSupport support) { 77 this.support = support; 78 79 initComponents(); 80 chkShowClasses.setSelected(false); 81 chkSuperCalls.setSelected(true); 82 83 view = new BeanTreeView(); 84 view.setPopupAllowed(false); 85 view.setRootVisible(false); 86 view.setDefaultActionAllowed(false); 87 lblList.setLabelFor(view); 88 panel.add(view, BorderLayout.CENTER); 89 getExplorerManager().setRootContext(new AbstractNode(new Ch())); 90 getExplorerManager().addPropertyChangeListener(this); 91 initA11Y(); 92 } 93 94 public ExplorerManager getExplorerManager() { 95 return manager; 96 } 97 98 public Collection getMethods() { 99 return selection.values(); 100 } 101 102 public Map getSelection() { 103 return selection; 104 } 105 106 public boolean isGenerateSuperCalls() { 107 return chkSuperCalls.isSelected(); 108 } 109 110 public boolean isGenerateJavadoc() { 111 return chkJavadoc.isSelected(); 112 } 113 114 119 private void initComponents() { 121 java.awt.GridBagConstraints gridBagConstraints; 122 123 lblTop = new javax.swing.JLabel (); 124 chkShowClasses = new javax.swing.JCheckBox (); 125 chkAbstractOnly = new javax.swing.JCheckBox (); 126 lblList = new javax.swing.JLabel (); 127 panel = new javax.swing.JPanel (); 128 chkSuperCalls = new javax.swing.JCheckBox (); 129 chkJavadoc = new javax.swing.JCheckBox (); 130 131 setLayout(new java.awt.GridBagLayout ()); 132 133 lblTop.setText(org.openide.util.NbBundle.getBundle(OverridePanel2.class).getString("LBL_OverridePanel2_Top")); 134 gridBagConstraints = new java.awt.GridBagConstraints (); 135 gridBagConstraints.gridwidth = 2; 136 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 137 gridBagConstraints.weightx = 1.0; 138 gridBagConstraints.insets = new java.awt.Insets (12, 12, 12, 12); 139 add(lblTop, gridBagConstraints); 140 141 chkShowClasses.setText(org.openide.util.NbBundle.getBundle(OverridePanel2.class).getString("LBL_OverridePanel2_ShowClasses")); 142 gridBagConstraints = new java.awt.GridBagConstraints (); 143 gridBagConstraints.gridx = 0; 144 gridBagConstraints.gridy = 1; 145 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 146 gridBagConstraints.insets = new java.awt.Insets (0, 12, 6, 0); 147 add(chkShowClasses, gridBagConstraints); 148 149 chkAbstractOnly.setText(org.openide.util.NbBundle.getBundle(OverridePanel2.class).getString("LBL_OverridePanel2_AbstractOnly")); 150 gridBagConstraints = new java.awt.GridBagConstraints (); 151 gridBagConstraints.gridx = 1; 152 gridBagConstraints.gridy = 1; 153 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 154 gridBagConstraints.insets = new java.awt.Insets (0, 6, 6, 12); 155 add(chkAbstractOnly, gridBagConstraints); 156 157 lblList.setText(org.openide.util.NbBundle.getMessage(OverridePanel2.class, "LBL_OverridePanel2_View")); 158 gridBagConstraints = new java.awt.GridBagConstraints (); 159 gridBagConstraints.gridx = 0; 160 gridBagConstraints.gridy = 2; 161 gridBagConstraints.gridwidth = 2; 162 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 163 gridBagConstraints.insets = new java.awt.Insets (0, 12, 3, 12); 164 add(lblList, gridBagConstraints); 165 166 panel.setLayout(new java.awt.BorderLayout ()); 167 168 gridBagConstraints = new java.awt.GridBagConstraints (); 169 gridBagConstraints.gridx = 0; 170 gridBagConstraints.gridy = 3; 171 gridBagConstraints.gridwidth = 2; 172 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 173 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 174 gridBagConstraints.weightx = 1.0; 175 gridBagConstraints.weighty = 1.0; 176 gridBagConstraints.insets = new java.awt.Insets (0, 12, 6, 12); 177 add(panel, gridBagConstraints); 178 179 chkSuperCalls.setText(org.openide.util.NbBundle.getBundle(OverridePanel2.class).getString("LBL_OverridePanel2_SuperCalls")); 180 gridBagConstraints = new java.awt.GridBagConstraints (); 181 gridBagConstraints.gridx = 0; 182 gridBagConstraints.gridy = 4; 183 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 184 gridBagConstraints.insets = new java.awt.Insets (0, 12, 12, 0); 185 add(chkSuperCalls, gridBagConstraints); 186 187 chkJavadoc.setText(org.openide.util.NbBundle.getBundle(OverridePanel2.class).getString("LBL_OverridePanel2_Javadoc")); 188 gridBagConstraints = new java.awt.GridBagConstraints (); 189 gridBagConstraints.gridx = 1; 190 gridBagConstraints.gridy = 4; 191 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 192 gridBagConstraints.insets = new java.awt.Insets (0, 6, 12, 12); 193 add(chkJavadoc, gridBagConstraints); 194 195 } 196 198 199 private javax.swing.JCheckBox chkAbstractOnly; 201 private javax.swing.JCheckBox chkJavadoc; 202 private javax.swing.JCheckBox chkShowClasses; 203 private javax.swing.JCheckBox chkSuperCalls; 204 private javax.swing.JLabel lblList; 205 private javax.swing.JLabel lblTop; 206 private javax.swing.JPanel panel; 207 209 private void initA11Y() { 210 this.getAccessibleContext().setAccessibleDescription(getString("ACSD_OverridePanel2")); chkShowClasses.setMnemonic(getString("CTL_OverridePanel2_ShowClasses").charAt(0)); chkShowClasses.getAccessibleContext().setAccessibleDescription(getString("ACSD_OverridePanel2_ShowClasses")); chkAbstractOnly.setMnemonic(getString("CTL_OverridePanel2_AbstractOnly").charAt(0)); chkAbstractOnly.getAccessibleContext().setAccessibleDescription(getString("ACSD_OverridePanel2_AbstractOnly")); chkSuperCalls.setMnemonic(getString("CTL_OverridePanel2_SuperCalls").charAt(0)); chkSuperCalls.getAccessibleContext().setAccessibleDescription(getString("ACSD_OverridePanel2_SuperCalls")); chkJavadoc.setMnemonic(getString("CTL_OverridePanel2_Javadoc").charAt(0)); chkJavadoc.getAccessibleContext().setAccessibleDescription(getString("ACSD_OverridePanel2_Javadoc")); lblList.setDisplayedMnemonic(getString("CTL_OverridePanel2_View").charAt(0)); view.getAccessibleContext().setAccessibleDescription(getString("ACSD_OverridePanel2_View")); } 222 223 private static String getString(String key) { 224 return NbBundle.getMessage(OverridePanel2.class, key); 225 } 226 227 public void addNotify() { 228 super.addNotify(); 229 lastFired = !selection.isEmpty(); 230 firePropertyChange(PROP_VALID, !lastFired, lastFired); 231 view.requestFocus(); 232 } 233 234 236 public void propertyChange(PropertyChangeEvent evt) { 237 if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) { 238 Node n[] = getExplorerManager().getSelectedNodes(); 239 HashMap methods = new HashMap (); 240 241 for (int i = 0; i < n.length; i++) { 242 Method mte = (Method) n[i].getLookup().lookup(Method.class); 243 if (mte != null) { 244 methods.put(new JMIInheritanceSupport.MethodKey(mte), mte); 245 } 246 } 247 248 selection = methods; 249 250 if (lastFired != !selection.isEmpty()) { 251 lastFired = !selection.isEmpty(); 252 firePropertyChange(PROP_VALID, !lastFired, lastFired); 253 } 254 } 255 } 256 257 259 private Collection computeClasses() { 260 ArrayList result = new ArrayList (); 261 TreeSet ifaces = new TreeSet (CLASS_ELEMENTS_COMPARATOR); 262 263 support.getClasses(result, true, false); 264 support.getClasses(ifaces, false, true); 265 result.addAll(ifaces); 266 ListIterator iter = result.listIterator(); 267 while(iter.hasNext()) { 268 Object cls = iter.next(); 269 if (cls instanceof ParameterizedType) { 270 cls = ((ParameterizedType) cls).getDefinition(); 271 } 272 if (cls instanceof UnresolvedClass) { 273 iter.remove(); 274 } 275 } 276 277 return result; 278 } 279 280 private Collection computeMethods(ClassDefinition cle, boolean abstractOnly, boolean deep) { 281 TreeSet result = new TreeSet (METHOD_ELEMENTS_COMPARATOR); 282 283 if (deep) { 284 return support.getAllMethods(result, abstractOnly); 285 } else { 286 return support.getMethods(result, cle, abstractOnly); 287 } 288 } 289 290 private void updateSelection() { 291 Node root = getExplorerManager().getRootContext(); 292 Node ch[] = root.getChildren().getNodes(true); 293 ArrayList selectedNodes = new ArrayList (); 294 295 view.expandAll(); 297 298 if (chkShowClasses.isSelected()) { 300 for (int i = 0; i < ch.length; i++) { 301 Node chch[] = ch[i].getChildren().getNodes(true); 302 for (int j = 0; j < chch.length; j++) { 303 Method mte = (Method) chch[j].getLookup().lookup(Method.class); 304 if (mte != null && selection.containsKey(new JMIInheritanceSupport.MethodKey(mte))) { 305 selectedNodes.add(chch[j]); 306 } 307 } 308 } 309 } else { 310 for (int i = 0; i < ch.length; i++) { 311 Method mte = (Method) ch[i].getLookup().lookup(Method.class); 312 if (mte != null && selection.containsKey(new JMIInheritanceSupport.MethodKey(mte))) { 313 selectedNodes.add(ch[i]); 314 } 315 } 316 } 317 318 try { 319 getExplorerManager().setSelectedNodes((Node []) selectedNodes.toArray(new Node[selectedNodes.size()])); 320 } catch (PropertyVetoException e) { 321 ErrorManager.getDefault().notify(e); 322 } 323 324 getExplorerManager().addPropertyChangeListener(this); 325 } 326 327 private static final class WaitNode extends AbstractNode { 328 public WaitNode() { 329 super(Children.LEAF); 330 setIconBase("org/openide/src/resources/wait"); setDisplayName(NbBundle.getMessage(WaitNode.class, "LBL_WaitNode_DisplayName")); setName(getClass().getName()); 333 } 334 } 336 private final class Ch extends Children.Keys implements PropertyChangeListener , ActionListener { 337 338 public Ch() { 339 support.reset(); 340 } 341 342 protected void addNotify() { 343 update(); 344 chkShowClasses.addActionListener(this); 345 chkAbstractOnly.addActionListener(this); 346 support.addPropertyChangeListener(this); 347 } 348 349 protected void removeNotify() { 350 chkShowClasses.removeActionListener(this); 351 chkAbstractOnly.removeActionListener(this); 352 support.removePropertyChangeListener(this); 353 setKeys(Collections.EMPTY_LIST); 354 } 355 356 protected Node[] createNodes(Object key) { 357 if (key instanceof Node) { 358 return new Node [] { (Node)key }; 359 } else if (key instanceof JavaClass) { 360 ClassNode node = new ClassNode((JavaClass) key, 361 new ClassElementCh((JavaClass) key), false); 362 node.setElementFormat(CLASS_ELEMENT_FORMAT); 363 return new Node [] { node }; 364 } else { return new Node [] { new MEN((Method) key) }; 366 } 367 } 368 369 371 public void propertyChange(PropertyChangeEvent evt) { 372 update(); 373 } 374 375 377 public void actionPerformed(java.awt.event.ActionEvent e) { 378 if (e.getSource() == chkAbstractOnly && chkShowClasses.isSelected()) { 379 return; 380 } 381 382 update(); 383 } 384 385 387 private void update() { 388 if (support.isFinished()) { 389 getExplorerManager().removePropertyChangeListener(OverridePanel2.this); 390 try { 392 getExplorerManager ().setSelectedNodes (new Node[0]); 393 } catch (PropertyVetoException pve) { 394 ErrorManager.getDefault ().notify (ErrorManager.WARNING, pve); 395 } 396 397 setKeys(chkShowClasses.isSelected() ? 398 computeClasses() : 399 computeMethods(support.getRootClass(), chkAbstractOnly.isSelected(), true)); 400 401 SwingUtilities.invokeLater(new Runnable () { 402 public void run() { 403 updateSelection(); 404 } 405 }); 406 } else { 407 setKeys(new Object [] { WAIT_NODE }); 408 } 409 } 410 } 412 private final class ClassElementCh extends Children.Keys implements ActionListener { 413 414 private ClassDefinition cle; 415 416 public ClassElementCh(ClassDefinition cle) { 417 this.cle = cle; 418 } 419 420 protected void addNotify() { 421 update(); 422 chkAbstractOnly.addActionListener(this); 423 } 424 425 protected void removeNotify() { 426 chkAbstractOnly.removeActionListener(this); 427 setKeys(Collections.EMPTY_LIST); 428 } 429 430 protected Node[] createNodes(Object key) { 431 MethodNode node = new MethodNode((Method) key, false); 433 node.setElementFormat(METHOD_ELEMENT_FORMAT); 434 return new Node [] { node }; 435 } 436 437 439 public void actionPerformed(java.awt.event.ActionEvent e) { 440 update(); 441 } 442 443 445 private void update() { 446 setKeys(computeMethods(cle, chkAbstractOnly.isSelected(), false)); 447 } 448 } 450 private static class ClassElementsComparator implements Comparator { 451 public ClassElementsComparator () {} 452 public int compare(Object value1, Object value2) { 453 ClassDefinition v1 = (ClassDefinition)value1; 454 ClassDefinition v2 = (ClassDefinition)value2; 455 456 boolean v1_interf = v1 instanceof JavaClass && ((JavaClass) v1).isInterface(); 457 boolean v2_interf = v2 instanceof JavaClass && ((JavaClass) v2).isInterface(); 458 459 if (v1_interf != v2_interf) { 460 return v1_interf ? 1 : -1; 461 } 462 return v1.getName().compareTo(v2.getName()); 463 } 464 465 public boolean equals(Object o) { 466 return o instanceof ClassElementsComparator; 467 } 468 } 470 475 private static final class MethodElementsComparator implements Comparator { 476 public MethodElementsComparator () {} 477 public int compare(Object a,Object b) { 478 MDRepository repos = JavaMetamodel.getDefaultRepository(); 479 repos.beginTrans(false); 480 try { 481 Method ma = (Method)a; 482 Method mb = (Method)b; 483 int result; 484 485 if (ma == mb) 486 return 0; 487 result = ma.getName().compareTo(mb.getName()); 488 if (result != 0) 489 return result; 490 result = ma.getParameters().size() - mb.getParameters().size(); 491 if (result != 0) 492 return result; 493 Iterator iterA = ma.getParameters().iterator(); 495 Iterator iterB = mb.getParameters().iterator(); 496 while (iterA.hasNext()) { 497 Parameter parA = (Parameter) iterA.next(); 498 Parameter parB = (Parameter) iterB.next(); 499 String parAName = parA.getName(); 500 String parBName = parB.getName(); 501 String sA = parA.getType().getName() + (parAName != null ? " " + parAName : ""); String sB = parB.getType().getName() + (parBName != null ? " " + parBName : ""); result = sA.compareTo(sB); 504 if (result != 0) 505 return result; 506 } 507 return 0; 508 } finally { 509 repos.endTrans(false); 510 } 511 } 512 513 public boolean equals(Object o) { 514 return o instanceof MethodElementsComparator; 515 } 516 } 518 public static final class MEN extends MethodNode { 519 public MEN(Method m) { 520 super(m, false); 521 setElementFormat(METHOD_ELEMENT_FORMAT); 522 } 523 524 public String getShortDescription() { 525 StringBuffer buff = new StringBuffer (); 526 527 METHOD_ELEMENT_TOOLTIP1_FORMAT.format(element, buff, new FieldPosition (0)); 528 buff.append(" "); buff.append(((Method)element).getDeclaringClass().getName()); 530 buff.append("."); METHOD_ELEMENT_TOOLTIP2_FORMAT.format(element, buff, new FieldPosition (0)); 532 533 return buff.toString(); 534 } 535 } } 537 | Popular Tags |