KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > tools > OverridePanel2


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.tools;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyVetoException JavaDoc;
27 import java.text.FieldPosition JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Comparator JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.ListIterator JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.TreeSet JavaDoc;
37 import javax.swing.SwingUtilities JavaDoc;
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 /**
53  *
54  * @author Vitezslav Stejskal
55  */

56 public class OverridePanel2 extends javax.swing.JPanel JavaDoc
57 implements PropertyChangeListener JavaDoc, ExplorerManager.Provider {
58
59     public static final String JavaDoc PROP_VALID = "OverridePanel2.valid"; //NOI18N
60

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")); //NOI18N
65
private static final ElementFormat METHOD_ELEMENT_FORMAT = new ElementFormat(NbBundle.getMessage(OverridePanel2.class, "FMT_DefaultMethodFormat2")); //NOI18N
66
private static final ElementFormat METHOD_ELEMENT_TOOLTIP1_FORMAT = new ElementFormat(NbBundle.getMessage(OverridePanel2.class, "FMT_DefaultMethodFormat_Tooltip1")); //NOI18N
67
private static final ElementFormat METHOD_ELEMENT_TOOLTIP2_FORMAT = new ElementFormat(NbBundle.getMessage(OverridePanel2.class, "FMT_DefaultMethodFormat_Tooltip2")); //NOI18N
68

69     private JMIInheritanceSupport support;
70     private HashMap JavaDoc /*<JMIInheritanceSupport.MethodKey, Method>*/ selection = new HashMap JavaDoc();
71     private BeanTreeView view;
72     private boolean lastFired;
73     private ExplorerManager manager = new ExplorerManager();
74     
75     /** Creates new form OverridePanel2 */
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 JavaDoc getMethods() {
99         return selection.values();
100     }
101     
102     public Map JavaDoc 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     /** This method is called from within the constructor to
115      * initialize the form.
116      * WARNING: Do NOT modify this code. The content of this method is
117      * always regenerated by the Form Editor.
118      */

119     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
120
private void initComponents() {
121         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
122
123         lblTop = new javax.swing.JLabel JavaDoc();
124         chkShowClasses = new javax.swing.JCheckBox JavaDoc();
125         chkAbstractOnly = new javax.swing.JCheckBox JavaDoc();
126         lblList = new javax.swing.JLabel JavaDoc();
127         panel = new javax.swing.JPanel JavaDoc();
128         chkSuperCalls = new javax.swing.JCheckBox JavaDoc();
129         chkJavadoc = new javax.swing.JCheckBox JavaDoc();
130
131         setLayout(new java.awt.GridBagLayout JavaDoc());
132
133         lblTop.setText(org.openide.util.NbBundle.getBundle(OverridePanel2.class).getString("LBL_OverridePanel2_Top"));
134         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
135         gridBagConstraints.gridwidth = 2;
136         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
137         gridBagConstraints.weightx = 1.0;
138         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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 JavaDoc();
143         gridBagConstraints.gridx = 0;
144         gridBagConstraints.gridy = 1;
145         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
146         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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 JavaDoc();
151         gridBagConstraints.gridx = 1;
152         gridBagConstraints.gridy = 1;
153         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
154         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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 JavaDoc();
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 JavaDoc(0, 12, 3, 12);
164         add(lblList, gridBagConstraints);
165
166         panel.setLayout(new java.awt.BorderLayout JavaDoc());
167
168         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(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 JavaDoc();
181         gridBagConstraints.gridx = 0;
182         gridBagConstraints.gridy = 4;
183         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
184         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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 JavaDoc();
189         gridBagConstraints.gridx = 1;
190         gridBagConstraints.gridy = 4;
191         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
192         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 12, 12);
193         add(chkJavadoc, gridBagConstraints);
194
195     }
196     // </editor-fold>//GEN-END:initComponents
197

198     
199     // Variables declaration - do not modify//GEN-BEGIN:variables
200
private javax.swing.JCheckBox JavaDoc chkAbstractOnly;
201     private javax.swing.JCheckBox JavaDoc chkJavadoc;
202     private javax.swing.JCheckBox JavaDoc chkShowClasses;
203     private javax.swing.JCheckBox JavaDoc chkSuperCalls;
204     private javax.swing.JLabel JavaDoc lblList;
205     private javax.swing.JLabel JavaDoc lblTop;
206     private javax.swing.JPanel JavaDoc panel;
207     // End of variables declaration//GEN-END:variables
208

209     private void initA11Y() {
210         this.getAccessibleContext().setAccessibleDescription(getString("ACSD_OverridePanel2")); //NOI18N
211
chkShowClasses.setMnemonic(getString("CTL_OverridePanel2_ShowClasses").charAt(0)); //NOI18N
212
chkShowClasses.getAccessibleContext().setAccessibleDescription(getString("ACSD_OverridePanel2_ShowClasses")); //NOI18N
213
chkAbstractOnly.setMnemonic(getString("CTL_OverridePanel2_AbstractOnly").charAt(0)); //NOI18N
214
chkAbstractOnly.getAccessibleContext().setAccessibleDescription(getString("ACSD_OverridePanel2_AbstractOnly")); //NOI18N
215
chkSuperCalls.setMnemonic(getString("CTL_OverridePanel2_SuperCalls").charAt(0)); //NOI18N
216
chkSuperCalls.getAccessibleContext().setAccessibleDescription(getString("ACSD_OverridePanel2_SuperCalls")); //NOI18N
217
chkJavadoc.setMnemonic(getString("CTL_OverridePanel2_Javadoc").charAt(0)); //NOI18N
218
chkJavadoc.getAccessibleContext().setAccessibleDescription(getString("ACSD_OverridePanel2_Javadoc")); //NOI18N
219
lblList.setDisplayedMnemonic(getString("CTL_OverridePanel2_View").charAt(0)); //NOI18N
220
view.getAccessibleContext().setAccessibleDescription(getString("ACSD_OverridePanel2_View")); //NOI18N
221
}
222     
223     private static String JavaDoc getString(String JavaDoc 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     // ---- PropertyChangeListener implemenetation
235

236     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
237         if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
238             Node n[] = getExplorerManager().getSelectedNodes();
239             HashMap JavaDoc methods = new HashMap JavaDoc();
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     // ---- private implementation
258

259     private Collection JavaDoc computeClasses() {
260         ArrayList JavaDoc result = new ArrayList JavaDoc();
261         TreeSet JavaDoc ifaces = new TreeSet JavaDoc(CLASS_ELEMENTS_COMPARATOR);
262
263         support.getClasses(result, true, false);
264         support.getClasses(ifaces, false, true);
265         result.addAll(ifaces);
266         ListIterator JavaDoc iter = result.listIterator();
267         while(iter.hasNext()) {
268             Object JavaDoc 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 JavaDoc computeMethods(ClassDefinition cle, boolean abstractOnly, boolean deep) {
281         TreeSet JavaDoc result = new TreeSet JavaDoc(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 JavaDoc selectedNodes = new ArrayList JavaDoc();
294         
295         // expand whole tree
296
view.expandAll();
297
298         // find previously selected nodes
299
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 JavaDoc 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"); // NOI18N
331
setDisplayName(NbBundle.getMessage(WaitNode.class, "LBL_WaitNode_DisplayName")); //NOI18N
332
setName(getClass().getName());
333         }
334     } // End of WaitNode class
335

336     private final class Ch extends Children.Keys implements PropertyChangeListener JavaDoc, ActionListener JavaDoc {
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 JavaDoc 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 { // key instanceof MethodElement
365
return new Node [] { new MEN((Method) key) };
366             }
367         }
368
369         // ---- PropertyChangeListener implementation
370

371         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
372             update();
373         }
374         
375         // ---- ActionListener implementation
376

377         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
378             if (e.getSource() == chkAbstractOnly && chkShowClasses.isSelected()) {
379                 return;
380             }
381             
382             update();
383         }
384         
385         // ---- private implementation
386

387         private void update() {
388             if (support.isFinished()) {
389                 getExplorerManager().removePropertyChangeListener(OverridePanel2.this);
390                 // hot fix, workaround #40491, unselect nodes to avoid IAE (An explored context must be within the root context)
391
try {
392                     getExplorerManager ().setSelectedNodes (new Node[0]);
393                 } catch (PropertyVetoException JavaDoc 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 JavaDoc() {
402                     public void run() {
403                         updateSelection();
404                     }
405                 });
406             } else {
407                 setKeys(new Object JavaDoc [] { WAIT_NODE });
408             }
409         }
410     } // End of Ch class
411

412     private final class ClassElementCh extends Children.Keys implements ActionListener JavaDoc {
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 JavaDoc key) {
431             // key instanceof MethodElement
432
MethodNode node = new MethodNode((Method) key, false);
433             node.setElementFormat(METHOD_ELEMENT_FORMAT);
434             return new Node [] { node };
435         }
436         
437         // ---- ActionListener
438

439         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
440             update();
441         }
442         
443         // ---- private implementation
444

445         private void update() {
446             setKeys(computeMethods(cle, chkAbstractOnly.isSelected(), false));
447         }
448     } // End of ClassElementCh
449

450     private static class ClassElementsComparator implements Comparator JavaDoc {
451         public ClassElementsComparator () {}
452         public int compare(Object JavaDoc value1, Object JavaDoc 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 JavaDoc o) {
466             return o instanceof ClassElementsComparator;
467         }
468     } // End of ClassElementsComparator class
469

470     /** Comparator that orders methods as follows:
471      * 1. sorts them in ascending alphabetical order
472      * 2. methods with the same name, but less arguments are preceding methods with more arguments
473      * 3. methods with the same number of arguments are sorted by their argument declarations
474      */

475     private static final class MethodElementsComparator implements Comparator JavaDoc {
476         public MethodElementsComparator () {}
477         public int compare(Object JavaDoc a,Object JavaDoc 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                 // compare parameter names/types.
494
Iterator JavaDoc iterA = ma.getParameters().iterator();
495                 Iterator JavaDoc iterB = mb.getParameters().iterator();
496                 while (iterA.hasNext()) {
497                     Parameter parA = (Parameter) iterA.next();
498                     Parameter parB = (Parameter) iterB.next();
499                     String JavaDoc parAName = parA.getName();
500                     String JavaDoc parBName = parB.getName();
501                     String JavaDoc sA = parA.getType().getName() + (parAName != null ? " " + parAName : ""); // NOI18N
502
String JavaDoc sB = parB.getType().getName() + (parBName != null ? " " + parBName : ""); // NOI18N
503
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 JavaDoc o) {
514             return o instanceof MethodElementsComparator;
515         }
516     } // End of MethodElementsComparator class
517

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 JavaDoc getShortDescription() {
525             StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
526             
527             METHOD_ELEMENT_TOOLTIP1_FORMAT.format(element, buff, new FieldPosition JavaDoc(0));
528             buff.append(" "); //NOI18N
529
buff.append(((Method)element).getDeclaringClass().getName());
530             buff.append("."); //NOI18N
531
METHOD_ELEMENT_TOOLTIP2_FORMAT.format(element, buff, new FieldPosition JavaDoc(0));
532             
533             return buff.toString();
534         }
535     } // End of MEN class
536
}
537
Popular Tags