KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > 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.ruby.rubyproject.ui.customizer;
21
22 import java.awt.event.MouseEvent JavaDoc;
23 import java.awt.event.MouseListener JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.ListSelectionModel JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import javax.swing.event.ChangeEvent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import javax.swing.event.ListSelectionEvent JavaDoc;
32 import javax.swing.event.ListSelectionListener JavaDoc;
33 import org.netbeans.modules.ruby.rubyproject.RubyProjectUtil;
34 //import org.netbeans.modules.javacore.JMManager;
35
import org.openide.awt.Mnemonics;
36 import org.openide.awt.MouseUtils;
37 import org.openide.filesystems.FileObject;
38 import org.openide.util.NbBundle;
39 import org.openide.util.RequestProcessor;
40
41 /** Browses and allows to choose a project's main class.
42  *
43  * @author Jiri Rechtacek
44  */

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

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

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

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

210
211     // Variables declaration - do not modify//GEN-BEGIN:variables
212
private javax.swing.JLabel JavaDoc jLabel1;
213     private javax.swing.JList JavaDoc jMainClassList;
214     private javax.swing.JScrollPane JavaDoc jScrollPane1;
215     // End of variables declaration//GEN-END:variables
216

217     // Maybe useless renderer (fit if wanted to reneder Icons) // XXX
218
// private static final class MainClassRenderer extends DefaultListCellRenderer {
219
// public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
220
// String displayName;
221
// if (value instanceof String) {
222
// displayName = (String) value;
223
// } if (value instanceof FileObject) {
224
// displayName = ((FileObject)value).getName ();
225
// } else {
226
// displayName = value.toString ();
227
// }
228
// return super.getListCellRendererComponent (list, displayName, index, isSelected, cellHasFocus);
229
// }
230
// }
231
//
232
}
233
Popular Tags