KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > 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.java.j2seproject.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.java.j2seproject.J2SEProjectUtil;
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                 
100                 possibleMainClasses = SourceUtils.getMainClasses(sourcesRoots);
101                 if (possibleMainClasses.isEmpty ()) {
102                     SwingUtilities.invokeLater( new Runnable JavaDoc () {
103                         public void run () {
104                             jMainClassList.setListData (new String JavaDoc[] { NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_NO_CLASSES_NODE") } ); // NOI18N
105
}
106                     });
107                 } else {
108                     final ElementHandle<TypeElement>[] arr = possibleMainClasses.toArray(new ElementHandle[possibleMainClasses.size()]);
109                     // #46861, sort name of classes
110
Arrays.sort (arr, new MainClassComparator());
111                     SwingUtilities.invokeLater(new Runnable JavaDoc () {
112                         public void run () {
113                             jMainClassList.setListData (arr);
114                             jMainClassList.setSelectedIndex (0);
115                         }
116                     });
117                 }
118             }
119         });
120         
121         if (dialogSubtitle != null) {
122             Mnemonics.setLocalizedText (jLabel1, dialogSubtitle);
123         }
124     }
125     
126     private Object JavaDoc[] getWarmupList () {
127 // return JMManager.getManager().isScanInProgress() ?
128
// new Object[] {NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_SCANNING_MESSAGE")}:
129
// new Object[] {NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_WARMUP_MESSAGE")}; // NOI18N
130
return new Object JavaDoc[] {NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_WARMUP_MESSAGE")};
131     }
132     
133     private boolean isValidMainClassName (Object JavaDoc value) {
134         return (possibleMainClasses != null) && (possibleMainClasses.contains (value));
135     }
136
137
138     /** Returns the selected main class.
139      *
140      * @return name of class or null if no class with the main method is selected
141      */

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

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

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

218
219     // Variables declaration - do not modify//GEN-BEGIN:variables
220
private javax.swing.JLabel JavaDoc jLabel1;
221     private javax.swing.JList JavaDoc jMainClassList;
222     private javax.swing.JScrollPane JavaDoc jScrollPane1;
223     // End of variables declaration//GEN-END:variables
224

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