KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > 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.bluej;
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.openide.awt.Mnemonics;
41 import org.openide.awt.MouseUtils;
42 import org.openide.filesystems.FileObject;
43 import org.openide.util.NbBundle;
44 import org.openide.util.RequestProcessor;
45
46 /** Browses and allows to choose a project's main class.
47  *
48  * @author Jiri Rechtacek
49  * @author Milos Kleint copied from j2se project type to bluej one
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     private String JavaDoc selectedClass;
58             
59     /** Creates new form MainClassChooser */
60     public MainClassChooser (FileObject[] sourcesRoots) {
61         this (sourcesRoots, null);
62     }
63     
64     public MainClassChooser (FileObject[] sourcesRoots, String JavaDoc subtitle, boolean showArguments) {
65         this (sourcesRoots, subtitle);
66         if (!showArguments) {
67             lblArguments.setVisible(false);
68             txtArguments.setVisible(false);
69         }
70     }
71
72     public MainClassChooser (FileObject[] sourcesRoots, String JavaDoc subtitle) {
73         dialogSubtitle = subtitle;
74         initComponents();
75         initClassesView (sourcesRoots);
76     }
77     
78     private void initClassesView (final FileObject[] sourcesRoots) {
79         possibleMainClasses = null;
80         jMainClassList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
81         jMainClassList.setListData (getWarmupList ());
82         jMainClassList.addListSelectionListener (new ListSelectionListener JavaDoc () {
83             public void valueChanged (ListSelectionEvent JavaDoc evt) {
84                 if (changeListener != null) {
85                     changeListener.stateChanged (new ChangeEvent JavaDoc (evt));
86                 }
87             }
88         });
89         // support for double click to finish dialog with selected class
90
jMainClassList.addMouseListener (new MouseListener JavaDoc () {
91             public void mouseClicked (MouseEvent JavaDoc e) {
92                 if (MouseUtils.isDoubleClick (e)) {
93                     if (getSelectedMainClass () != null) {
94                         if (changeListener != null) {
95                             changeListener.stateChanged (new ChangeEvent JavaDoc (e));
96                         }
97                     }
98                 }
99             }
100             public void mousePressed (MouseEvent JavaDoc e) {}
101             public void mouseReleased (MouseEvent JavaDoc e) {}
102             public void mouseEntered (MouseEvent JavaDoc e) {}
103             public void mouseExited (MouseEvent JavaDoc e) {}
104         });
105         
106         RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
107             public void run () {
108                 
109                 possibleMainClasses = SourceUtils.getMainClasses(sourcesRoots);
110                 if (possibleMainClasses.isEmpty ()) {
111                     SwingUtilities.invokeLater( new Runnable JavaDoc () {
112                         public void run () {
113                             jMainClassList.setListData (new String JavaDoc[] { NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_NO_CLASSES_NODE") } ); // NOI18N
114
}
115                     });
116                 } else {
117                     final ElementHandle<TypeElement>[] arr = possibleMainClasses.toArray(new ElementHandle[possibleMainClasses.size()]);
118                     // #46861, sort name of classes
119
Arrays.sort (arr, new MainClassComparator());
120                     SwingUtilities.invokeLater(new Runnable JavaDoc () {
121                         public void run () {
122                             jMainClassList.setListData (arr);
123                             jMainClassList.setSelectedIndex (0);
124                         }
125                     });
126                 }
127             }
128         });
129         
130         if (dialogSubtitle != null) {
131             Mnemonics.setLocalizedText (jLabel1, dialogSubtitle);
132         }
133     }
134     
135     private Object JavaDoc[] getWarmupList () {
136           return new Object JavaDoc[] {NbBundle.getMessage (MainClassChooser.class, "LBL_ChooseMainClass_WARMUP_MESSAGE")};
137     }
138     
139     private boolean isValidMainClassName (Object JavaDoc value) {
140         return (possibleMainClasses != null) && (possibleMainClasses.contains (value));
141     }
142
143
144     /** Returns the selected main class.
145      *
146      * @return name of class or null if no class with the main method is selected
147      */

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

178     public static boolean hasMainMethod (FileObject classFO) {
179         return BluejActionProvider.hasMainMethod (classFO);
180     }
181
182     /** This method is called from within the constructor to
183      * initialize the form.
184      * WARNING: Do NOT modify this code. The content of this method is
185      * always regenerated by the Form Editor.
186      */

187     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
188
private void initComponents() {
189         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
190
191         jLabel1 = new javax.swing.JLabel JavaDoc();
192         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
193         jMainClassList = new javax.swing.JList JavaDoc();
194         lblArguments = new javax.swing.JLabel JavaDoc();
195         txtArguments = new javax.swing.JTextField JavaDoc();
196
197         setPreferredSize(new java.awt.Dimension JavaDoc(380, 300));
198         getAccessibleContext().setAccessibleDescription(null);
199         jLabel1.setLabelFor(jMainClassList);
200
201         jScrollPane1.setMinimumSize(new java.awt.Dimension JavaDoc(100, 200));
202         jScrollPane1.setViewportView(jMainClassList);
203         jMainClassList.getAccessibleContext().setAccessibleDescription(null);
204
205         lblArguments.setText(org.openide.util.NbBundle.getMessage(MainClassChooser.class, "LBL_Run_Arguments"));
206
207         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
208         this.setLayout(layout);
209         layout.setHorizontalGroup(
210             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
211             .add(layout.createSequentialGroup()
212                 .addContainerGap()
213                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
214                     .add(jLabel1)
215                     .add(layout.createSequentialGroup()
216                         .add(4, 4, 4)
217                         .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
218                     .add(layout.createSequentialGroup()
219                         .add(lblArguments)
220                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
221                         .add(txtArguments, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)))
222                 .addContainerGap())
223         );
224         layout.setVerticalGroup(
225             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
226             .add(layout.createSequentialGroup()
227                 .addContainerGap()
228                 .add(jLabel1)
229                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
230                 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 154, Short.MAX_VALUE)
231                 .add(27, 27, 27)
232                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
233                     .add(lblArguments)
234                     .add(txtArguments, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
235                 .add(61, 61, 61))
236         );
237     }// </editor-fold>//GEN-END:initComponents
238

239     
240  
241
242     // Variables declaration - do not modify//GEN-BEGIN:variables
243
private javax.swing.JLabel JavaDoc jLabel1;
244     private javax.swing.JList JavaDoc jMainClassList;
245     private javax.swing.JScrollPane JavaDoc jScrollPane1;
246     private javax.swing.JLabel JavaDoc lblArguments;
247     private javax.swing.JTextField JavaDoc txtArguments;
248     // End of variables declaration//GEN-END:variables
249

250     
251     void setSelectedMainClass(String JavaDoc clazz) {
252         this.selectedClass = clazz;
253     }
254
255     void setArguments(String JavaDoc args) {
256         txtArguments.setText(args);
257     }
258     
259     private static final class MainClassRenderer extends DefaultListCellRenderer JavaDoc {
260         public Component JavaDoc getListCellRendererComponent (JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
261             String JavaDoc displayName;
262             if (value instanceof String JavaDoc) {
263                 displayName = (String JavaDoc) value;
264             } if (value instanceof ElementHandle) {
265                 displayName = ((ElementHandle)value).getQualifiedName();
266             } else {
267                 displayName = value.toString ();
268             }
269             return super.getListCellRendererComponent (list, displayName, index, isSelected, cellHasFocus);
270         }
271     }
272     
273     private static class MainClassComparator implements Comparator JavaDoc<ElementHandle> {
274             
275         public int compare(ElementHandle arg0, ElementHandle arg1) {
276             return arg0.getQualifiedName().compareTo(arg1.getQualifiedName());
277         }
278     }
279     
280 }
281
Popular Tags