001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.modules.profiler.utils;
042:
043: import org.openide.awt.Mnemonics;
044: import org.openide.awt.MouseUtils;
045: import org.openide.filesystems.FileObject;
046: import org.openide.util.NbBundle;
047: import org.openide.util.RequestProcessor;
048: import java.awt.event.MouseEvent;
049: import java.awt.event.MouseListener;
050: import java.util.Arrays;
051: import java.util.Collection;
052: import javax.swing.JPanel;
053: import javax.swing.ListSelectionModel;
054: import javax.swing.event.ChangeEvent;
055: import javax.swing.event.ChangeListener;
056: import javax.swing.event.ListSelectionEvent;
057: import javax.swing.event.ListSelectionListener;
058: import org.netbeans.modules.profiler.projectsupport.utilities.SourceUtils;
059:
060: /**
061: * Browses and allows to choose a project's main class.
062: *
063: * @author Tomas Hurka
064: * @author Jiri Rechtacek
065: */
066: public class MainClassChooser extends JPanel {
067: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
068:
069: // Used only from unit tests to suppress check of main method. If value
070: // is different from null it will be returned instead.
071: public static Boolean unitTestingSupport_hasMainMethodResult = null;
072:
073: //~ Instance fields ----------------------------------------------------------------------------------------------------------
074:
075: private javax.swing.JLabel jLabel1;
076: private javax.swing.JList jMainClassList;
077: private javax.swing.JScrollPane jScrollPane1;
078: private ChangeListener changeListener;
079: private Collection<String> possibleMainClasses;
080: private String dialogSubtitle = null;
081:
082: //~ Constructors -------------------------------------------------------------------------------------------------------------
083:
084: /**
085: * Creates new form MainClassChooser
086: */
087: public MainClassChooser(FileObject[] sourcesRoots) {
088: this (sourcesRoots, null);
089: }
090:
091: public MainClassChooser(FileObject[] sourcesRoots, String subtitle) {
092: dialogSubtitle = subtitle;
093: initComponents();
094: initClassesView(sourcesRoots);
095: }
096:
097: //~ Methods ------------------------------------------------------------------------------------------------------------------
098:
099: /**
100: * Returns the selected main class.
101: *
102: * @return name of class or null if no class with the main method is selected
103: */
104: public String getSelectedMainClass() {
105: if (isValidMainClassName(jMainClassList.getSelectedValue())) {
106: return (String) jMainClassList.getSelectedValue();
107: } else {
108: return null;
109: }
110: }
111:
112: public void addChangeListener(ChangeListener l) {
113: changeListener = l;
114: }
115:
116: /**
117: * Checks if given file object contains the main method.
118: *
119: * @param classFO file object represents java
120: * @return false if parameter is null or doesn't contain SourceCookie
121: * or SourceCookie doesn't contain the main method
122: */
123: public static boolean hasMainMethod(FileObject classFO) {
124: // return MDRUtils.hasMainMethod(classFO);
125: return false;
126: }
127:
128: public void removeChangeListener(ChangeListener l) {
129: changeListener = null;
130: }
131:
132: private boolean isValidMainClassName(Object value) {
133: return (possibleMainClasses != null)
134: && (possibleMainClasses.contains(value));
135: }
136:
137: private Object[] getWarmupList() {
138: return new Object[] { NbBundle.getMessage(
139: MainClassChooser.class,
140: "LBL_ChooseMainClass_WARMUP_MESSAGE") }; // NOI18N
141: }
142:
143: private void initClassesView(final FileObject[] sourcesRoots) {
144: possibleMainClasses = null;
145: jMainClassList
146: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
147: jMainClassList.setListData(getWarmupList());
148: RequestProcessor.getDefault().post(new Runnable() {
149: public void run() {
150: possibleMainClasses = SourceUtils
151: .findMainClasses(sourcesRoots);
152:
153: if (possibleMainClasses.isEmpty()) {
154: jMainClassList
155: .setListData(new String[] { NbBundle
156: .getMessage(MainClassChooser.class,
157: "LBL_ChooseMainClass_NO_CLASSES_NODE") }); // NOI18N
158: } else {
159: Object[] arr = possibleMainClasses.toArray();
160: // #46861, sort name of classes
161: Arrays.sort(arr);
162: jMainClassList.setListData(arr);
163: jMainClassList.setSelectedIndex(0);
164: }
165: }
166: });
167: jMainClassList
168: .addListSelectionListener(new ListSelectionListener() {
169: public void valueChanged(ListSelectionEvent evt) {
170: if (changeListener != null) {
171: changeListener
172: .stateChanged(new ChangeEvent(evt));
173: }
174: }
175: });
176: // support for double click to finish dialog with selected class
177: jMainClassList.addMouseListener(new MouseListener() {
178: public void mouseClicked(MouseEvent e) {
179: if (MouseUtils.isDoubleClick(e)) {
180: if (getSelectedMainClass() != null) {
181: if (changeListener != null) {
182: changeListener
183: .stateChanged(new ChangeEvent(e));
184: }
185: }
186: }
187: }
188:
189: public void mousePressed(MouseEvent e) {
190: }
191:
192: public void mouseReleased(MouseEvent e) {
193: }
194:
195: public void mouseEntered(MouseEvent e) {
196: }
197:
198: public void mouseExited(MouseEvent e) {
199: }
200: });
201:
202: if (dialogSubtitle != null) {
203: Mnemonics.setLocalizedText(jLabel1, dialogSubtitle);
204: }
205: }
206:
207: private void initComponents() {
208: java.awt.GridBagConstraints gridBagConstraints;
209:
210: jLabel1 = new javax.swing.JLabel();
211: jScrollPane1 = new javax.swing.JScrollPane();
212: jMainClassList = new javax.swing.JList();
213:
214: setLayout(new java.awt.GridBagLayout());
215:
216: setPreferredSize(new java.awt.Dimension(380, 300));
217: getAccessibleContext().setAccessibleDescription(
218: NbBundle.getBundle(MainClassChooser.class).getString(
219: "AD_MainClassChooser")); // NOI18N
220: jLabel1.setLabelFor(jMainClassList);
221: Mnemonics.setLocalizedText(jLabel1, NbBundle.getBundle(
222: MainClassChooser.class).getString(
223: "CTL_AvaialableMainClasses")); // NOI18N
224: gridBagConstraints = new java.awt.GridBagConstraints();
225: gridBagConstraints.gridx = 0;
226: gridBagConstraints.gridy = 0;
227: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
228: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
229: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
230: gridBagConstraints.weightx = 1.0;
231: gridBagConstraints.insets = new java.awt.Insets(12, 12, 2, 12);
232: add(jLabel1, gridBagConstraints);
233:
234: jScrollPane1.setMinimumSize(new java.awt.Dimension(100, 200));
235: jScrollPane1.setViewportView(jMainClassList);
236: jMainClassList.getAccessibleContext().setAccessibleDescription(
237: NbBundle.getBundle(MainClassChooser.class).getString(
238: "AD_jMainClassList")); // NOI18N
239:
240: gridBagConstraints = new java.awt.GridBagConstraints();
241: gridBagConstraints.gridx = 0;
242: gridBagConstraints.gridy = 1;
243: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
244: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
245: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
246: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
247: gridBagConstraints.weightx = 1.0;
248: gridBagConstraints.weighty = 1.0;
249: gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 12);
250: add(jScrollPane1, gridBagConstraints);
251: }
252: }
|