KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > ui > customizer > MainClassChooser


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.j2ee.clientproject.ui.customizer;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.MouseEvent JavaDoc;
24 import java.awt.event.MouseListener JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Comparator JavaDoc;
28 import javax.lang.model.element.TypeElement;
29 import javax.swing.DefaultListCellRenderer JavaDoc;
30 import javax.swing.JList JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.ListSelectionModel JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34 import javax.swing.event.ChangeEvent JavaDoc;
35 import javax.swing.event.ChangeListener JavaDoc;
36 import javax.swing.event.ListSelectionEvent JavaDoc;
37 import javax.swing.event.ListSelectionListener JavaDoc;
38 import org.netbeans.api.java.source.ElementHandle;
39 import org.netbeans.api.java.source.SourceUtils;
40 import org.netbeans.modules.j2ee.clientproject.AppClientProjectUtil;
41 import org.openide.awt.Mnemonics;
42 import org.openide.awt.MouseUtils;
43 import org.openide.filesystems.FileObject;
44 import org.openide.util.NbBundle;
45 import org.openide.util.RequestProcessor;
46
47 /** Browses and allows to choose a project's main class.
48  *
49  * @author Jiri Rechtacek
50  */

51 public class MainClassChooser extends JPanel JavaDoc {
52
53     private ChangeListener JavaDoc changeListener;
54     private String JavaDoc dialogSubtitle = null;
55     private Collection JavaDoc<ElementHandle<TypeElement>> possibleMainClasses;
56             
57     /** Creates new form MainClassChooser */
58     public MainClassChooser (FileObject[] sourcesRoots) {
59         this (sourcesRoots, null);
60     }
61
62     public MainClassChooser (FileObject[] sourcesRoots, String JavaDoc subtitle) {
63         dialogSubtitle = subtitle;
64         initComponents();
65         jMainClassList.setCellRenderer(new MainClassRenderer());
66         initClassesView (sourcesRoots);
67     }
68     
69     private void initClassesView (final FileObject[] sourcesRoots) {
70         possibleMainClasses = null;
71         jMainClassList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
72         jMainClassList.setListData (getWarmupList ());
73         jMainClassList.addListSelectionListener (new ListSelectionListener JavaDoc () {
74             public void valueChanged (ListSelectionEvent JavaDoc evt) {
75                 if (changeListener != null) {
76                     changeListener.stateChanged (new ChangeEvent JavaDoc (evt));
77                 }
78             }
79         });
80         // support for double click to finish dialog with selected class
81
jMainClassList.addMouseListener (new MouseListener JavaDoc () {
82             public void mouseClicked (MouseEvent JavaDoc e) {
83                 if (MouseUtils.isDoubleClick (e)) {
84                     if (getSelectedMainClass () != null) {
85                         if (changeListener != null) {
86                             changeListener.stateChanged (new ChangeEvent JavaDoc (e));
87                         }
88                     }
89                 }
90             }
91             public void mousePressed (MouseEvent JavaDoc e) {}
92             public void mouseReleased (MouseEvent JavaDoc e) {}
93             public void mouseEntered (MouseEvent JavaDoc e) {}
94             public void mouseExited (MouseEvent JavaDoc e) {}
95         });
96         
97         RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
98             public void run () {
99                 possibleMainClasses = SourceUtils.getMainClasses(sourcesRoots);
100                 if (possibleMainClasses.isEmpty ()) {
101                     SwingUtilities.invokeLater( new Runnable JavaDoc () {
102                         public void run () {
103                             jMainClassList.setListData (new String JavaDoc[] { NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_NO_CLASSES_NODE") } ); // NOI18N
104
}
105                     });
106                 } else {
107                     final ElementHandle<TypeElement>[] arr = possibleMainClasses.toArray(new ElementHandle[possibleMainClasses.size()]);
108                     // #46861, sort name of classes
109
Arrays.sort (arr, new MainClassComparator());
110                     SwingUtilities.invokeLater(new Runnable JavaDoc () {
111                         public void run () {
112                             jMainClassList.setListData (arr);
113                             jMainClassList.setSelectedIndex (0);
114                         }
115                     });
116                 }
117             }
118         });
119         
120         if (dialogSubtitle != null) {
121             Mnemonics.setLocalizedText (jLabel1, dialogSubtitle);
122         }
123     }
124     
125     private Object JavaDoc[] getWarmupList () {
126         return new Object JavaDoc[] {NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_WARMUP_MESSAGE")}; // NOI18N
127
}
128     
129     private boolean isValidMainClassName (Object JavaDoc value) {
130         return (possibleMainClasses != null) && (possibleMainClasses.contains (value));
131     }
132
133
134     /** Returns the selected main class.
135      *
136      * @return name of class or null if no class with the main method is selected
137      */

138     public String JavaDoc getSelectedMainClass () {
139         if (isValidMainClassName (jMainClassList.getSelectedValue ())) {
140             return ((ElementHandle)jMainClassList.getSelectedValue()).getQualifiedName();
141         } else {
142             return null;
143         }
144     }
145     
146     public void addChangeListener (ChangeListener JavaDoc l) {
147         changeListener = l;
148     }
149     
150     public void removeChangeListener (ChangeListener JavaDoc l) {
151         changeListener = null;
152     }
153     
154     // Used only from unit tests to suppress check of main method. If value
155
// is different from null it will be returned instead.
156
public static Boolean JavaDoc unitTestingSupport_hasMainMethodResult = null;
157     
158     /** Checks if given file object contains the main method.
159      *
160      * @param classFO file object represents java
161      * @return false if parameter is null or doesn't contain SourceCookie
162      * or SourceCookie doesn't contain the main method
163      */

164     public static boolean hasMainMethod (FileObject classFO) {
165         return AppClientProjectUtil.hasMainMethod (classFO);
166     }
167
168     /** This method is called from within the constructor to
169      * initialize the form.
170      * WARNING: Do NOT modify this code. The content of this method is
171      * always regenerated by the Form Editor.
172      */

173     private void initComponents() {//GEN-BEGIN:initComponents
174
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
175
176         jLabel1 = new javax.swing.JLabel JavaDoc();
177         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
178         jMainClassList = new javax.swing.JList JavaDoc();
179
180         setLayout(new java.awt.GridBagLayout JavaDoc());
181
182         setPreferredSize(new java.awt.Dimension JavaDoc(380, 300));
183         getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("AD_MainClassChooser"));
184         jLabel1.setLabelFor(jMainClassList);
185         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("CTL_AvaialableMainClasses"));
186         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
187         gridBagConstraints.gridx = 0;
188         gridBagConstraints.gridy = 0;
189         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
190         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
191         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
192         gridBagConstraints.weightx = 1.0;
193         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 2, 12);
194         add(jLabel1, gridBagConstraints);
195
196         jScrollPane1.setMinimumSize(new java.awt.Dimension JavaDoc(100, 200));
197         jScrollPane1.setViewportView(jMainClassList);
198         jMainClassList.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(MainClassChooser.class).getString("AD_jMainClassList"));
199
200         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
201         gridBagConstraints.gridx = 0;
202         gridBagConstraints.gridy = 1;
203         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
204         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
205         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
206         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
207         gridBagConstraints.weightx = 1.0;
208         gridBagConstraints.weighty = 1.0;
209         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 12);
210         add(jScrollPane1, gridBagConstraints);
211
212     }//GEN-END:initComponents
213

214
215     // Variables declaration - do not modify//GEN-BEGIN:variables
216
private javax.swing.JLabel JavaDoc jLabel1;
217     private javax.swing.JList JavaDoc jMainClassList;
218     private javax.swing.JScrollPane JavaDoc jScrollPane1;
219     // End of variables declaration//GEN-END:variables
220

221     private static final class MainClassRenderer extends DefaultListCellRenderer JavaDoc {
222         public Component JavaDoc getListCellRendererComponent (JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
223             String JavaDoc displayName;
224             if (value instanceof String JavaDoc) {
225                 displayName = (String JavaDoc) value;
226             } if (value instanceof ElementHandle) {
227                 displayName = ((ElementHandle)value).getQualifiedName();
228             } else {
229                 displayName = value.toString ();
230             }
231             return super.getListCellRendererComponent (list, displayName, index, isSelected, cellHasFocus);
232         }
233     }
234     
235     private static class MainClassComparator implements Comparator JavaDoc<ElementHandle> {
236             
237         public int compare(ElementHandle arg0, ElementHandle arg1) {
238             return arg0.getQualifiedName().compareTo(arg1.getQualifiedName());
239         }
240     }
241
242 }
243
Popular Tags