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.ui;
042:
043: import org.netbeans.lib.profiler.client.ClientUtils;
044: import org.netbeans.lib.profiler.ui.UIUtils;
045: import org.netbeans.lib.profiler.ui.components.HTMLTextArea;
046: import org.openide.DialogDescriptor;
047: import org.openide.util.NbBundle;
048: import java.awt.*;
049: import javax.swing.*;
050: import javax.swing.border.EmptyBorder;
051: import javax.swing.event.DocumentEvent;
052: import javax.swing.event.DocumentListener;
053:
054: /**
055: * The panel to enter method info (class/method name and signature) manually
056: *
057: * @author Tomas Hurka
058: * @author Ian Formanek
059: */
060: public final class ManualMethodSelect extends JPanel {
061: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
062:
063: private final class MethodSelectDocumentListener implements
064: DocumentListener {
065: //~ Methods --------------------------------------------------------------------------------------------------------------
066:
067: public void changedUpdate(final DocumentEvent e) {
068: updateEnabledState();
069: }
070:
071: public void insertUpdate(final DocumentEvent e) {
072: updateEnabledState();
073: }
074:
075: public void removeUpdate(final DocumentEvent e) {
076: updateEnabledState();
077: }
078: }
079:
080: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
081:
082: // -----
083: // I18N String constants
084: private static final String SELECT_METHODS_DIALOG_CAPTION = NbBundle
085: .getMessage(ManualMethodSelect.class,
086: "ManualMethodSelect_SelectMethodsDialogCaption" //NOI18N
087: );
088: private static final String OK_BUTTON_TEXT = NbBundle
089: .getMessage(ManualMethodSelect.class,
090: "ManualMethodSelect_OKButtonText"); //NOI18N
091: private static final String CLASS_NAME_LABEL_TEXT = NbBundle
092: .getMessage(ManualMethodSelect.class,
093: "ManualMethodSelect_ClassNameLabelText"); //NOI18N
094: private static final String METHOD_NAME_LABEL_TEXT = NbBundle
095: .getMessage(ManualMethodSelect.class,
096: "ManualMethodSelect_MethodNameLabelText"); //NOI18N
097: private static final String METHOD_SIGNATURE_LABEL_TEXT = NbBundle
098: .getMessage(ManualMethodSelect.class,
099: "ManualMethodSelect_MethodSignatureLabelText"); //NOI18N
100: private static final String HINT_MSG = NbBundle.getMessage(
101: ManualMethodSelect.class, "ManualMethodSelect_HintMsg"); //NOI18N
102: private static final String CLASS_NAME_ACCESS_NAME = NbBundle
103: .getMessage(ManualMethodSelect.class,
104: "ManualMethodSelect_ClassNameAccessName"); //NOI18N
105: private static final String METHOD_NAME_ACCESS_NAME = NbBundle
106: .getMessage(ManualMethodSelect.class,
107: "ManualMethodSelect_MethodNameAccessName"); //NOI18N
108: private static final String METHOD_SIGNATURE_ACCESS_NAME = NbBundle
109: .getMessage(ManualMethodSelect.class,
110: "ManualMethodSelect_MethodSignatureAccessName"); //NOI18N
111: // -----
112: private static ManualMethodSelect mms;
113: private static JButton okButton = new JButton(OK_BUTTON_TEXT);
114:
115: //~ Instance fields ----------------------------------------------------------------------------------------------------------
116:
117: private HTMLTextArea hintArea;
118:
119: // -- Private implementation ------------------------------------------------------
120: private JTextField className;
121: private JTextField methodName;
122: private JTextField methodSignature;
123: private MethodSelectDocumentListener documentListener;
124:
125: //~ Constructors -------------------------------------------------------------------------------------------------------------
126:
127: /**
128: * Creates new form CodeFragmentSelect
129: */
130: private ManualMethodSelect() {
131: documentListener = new MethodSelectDocumentListener();
132:
133: JLabel classNameLabel = new JLabel();
134: className = new JTextField();
135:
136: JLabel methodNameLabel = new JLabel();
137: methodName = new JTextField();
138:
139: JLabel methodSignatureLabel = new JLabel();
140: methodSignature = new JTextField();
141: hintArea = new HTMLTextArea() {
142: public Dimension getPreferredSize() { // Workaround to force the text area not to consume horizontal space to fit the contents to just one line
143:
144: return new Dimension(1, super .getPreferredSize().height);
145: }
146: };
147:
148: setLayout(new GridBagLayout());
149:
150: GridBagConstraints labelConstraints = new GridBagConstraints();
151: GridBagConstraints fieldConstraints = new GridBagConstraints();
152:
153: labelConstraints.insets = new Insets(5, 0, 0, 0);
154: labelConstraints.anchor = GridBagConstraints.WEST;
155:
156: fieldConstraints.insets = new Insets(5, 5, 0, 0);
157: fieldConstraints.weightx = 1.0;
158: fieldConstraints.fill = GridBagConstraints.HORIZONTAL;
159:
160: classNameLabel.setLabelFor(className);
161: org.openide.awt.Mnemonics.setLocalizedText(classNameLabel,
162: CLASS_NAME_LABEL_TEXT);
163: labelConstraints.gridx = 0;
164: labelConstraints.gridy = 0;
165: add(classNameLabel, labelConstraints);
166:
167: className.getAccessibleContext().setAccessibleName(
168: CLASS_NAME_ACCESS_NAME);
169: className.setPreferredSize(new Dimension(290, className
170: .getPreferredSize().height));
171: fieldConstraints.gridx = 1;
172: fieldConstraints.gridy = 0;
173: add(className, fieldConstraints);
174:
175: methodNameLabel.setLabelFor(methodName);
176: org.openide.awt.Mnemonics.setLocalizedText(methodNameLabel,
177: METHOD_NAME_LABEL_TEXT);
178: labelConstraints.gridx = 0;
179: labelConstraints.gridy = 1;
180: add(methodNameLabel, labelConstraints);
181:
182: methodName.getAccessibleContext().setAccessibleName(
183: METHOD_NAME_ACCESS_NAME);
184: fieldConstraints.gridx = 1;
185: fieldConstraints.gridy = 1;
186: add(methodName, fieldConstraints);
187:
188: methodSignatureLabel.setLabelFor(methodSignature);
189: org.openide.awt.Mnemonics.setLocalizedText(
190: methodSignatureLabel, METHOD_SIGNATURE_LABEL_TEXT);
191: labelConstraints.gridx = 0;
192: labelConstraints.gridy = 2;
193: add(methodSignatureLabel, labelConstraints);
194:
195: methodSignature.getAccessibleContext().setAccessibleName(
196: METHOD_SIGNATURE_ACCESS_NAME);
197: fieldConstraints.gridx = 1;
198: fieldConstraints.gridy = 2;
199: add(methodSignature, fieldConstraints);
200:
201: Color panelBackground = UIManager.getColor("Panel.background"); //NOI18N
202: Color hintBackground = UIUtils.getSafeColor(panelBackground
203: .getRed() - 10, panelBackground.getGreen() - 10,
204: panelBackground.getBlue() - 10);
205:
206: // fill space
207: GridBagConstraints constraints = new GridBagConstraints();
208: constraints.gridx = 0;
209: constraints.gridy = 3;
210: constraints.gridwidth = GridBagConstraints.REMAINDER;
211: constraints.weighty = 1.0;
212: constraints.anchor = GridBagConstraints.NORTHWEST;
213: constraints.fill = GridBagConstraints.BOTH;
214: add(new JPanel(), constraints);
215:
216: // hintArea
217: hintArea.setText(HINT_MSG); // NOI18N
218: hintArea.setEnabled(false);
219: hintArea.setDisabledTextColor(Color.darkGray);
220: hintArea.setBackground(hintBackground);
221: hintArea.setBorder(BorderFactory.createMatteBorder(10, 10, 10,
222: 10, hintBackground));
223: constraints = new GridBagConstraints();
224: constraints.gridx = 0;
225: constraints.gridy = 4;
226: constraints.gridwidth = GridBagConstraints.REMAINDER;
227: constraints.insets = new Insets(18, 0, 0, 0);
228: constraints.anchor = GridBagConstraints.NORTHWEST;
229: constraints.fill = GridBagConstraints.BOTH;
230: add(hintArea, constraints);
231:
232: setBorder(new EmptyBorder(12, 12, 0, 12));
233:
234: className.getDocument().addDocumentListener(documentListener);
235: methodName.getDocument().addDocumentListener(documentListener);
236: methodSignature.getDocument().addDocumentListener(
237: documentListener);
238:
239: updateEnabledState();
240: }
241:
242: //~ Methods ------------------------------------------------------------------------------------------------------------------
243:
244: public static ClientUtils.SourceCodeSelection selectMethod() {
245: if (mms == null) {
246: mms = new ManualMethodSelect();
247: }
248:
249: final DialogDescriptor dd = new DialogDescriptor(mms,
250: SELECT_METHODS_DIALOG_CAPTION, true, new Object[] {
251: okButton, DialogDescriptor.CANCEL_OPTION },
252: okButton, DialogDescriptor.BOTTOM_ALIGN, null, null);
253:
254: final Dialog d = ProfilerDialogs.createDialog(dd);
255: d.pack(); // To properly layout HTML hint area
256: d.setVisible(true);
257:
258: if (dd.getValue() == okButton) {
259: return mms.getSelectedMethod();
260: } else {
261: return null;
262: }
263: }
264:
265: private String getClassName() {
266: return className.getText().trim(); //NOI18N
267: }
268:
269: private String getMethodName() {
270: String ret = methodName.getText().trim();
271:
272: if ("".equals(ret)) {
273: return null; //NOI18N
274: } else {
275: return ret;
276: }
277: }
278:
279: private String getMethodSignature() {
280: String ret = methodSignature.getText().trim();
281:
282: if ("".equals(ret)) {
283: return null; //NOI18N
284: } else {
285: return ret;
286: }
287: }
288:
289: private ClientUtils.SourceCodeSelection getSelectedMethod() {
290: return new ClientUtils.SourceCodeSelection(getClassName(),
291: getMethodName(), getMethodSignature());
292: }
293:
294: private void updateEnabledState() {
295: boolean enabled = true;
296:
297: // package/class name cannot be empty
298: if ("".equals(className.getText().trim())) {
299: enabled = false; //NOI18N
300: }
301: // method name cannot be empty
302: else if ("".equals(methodName.getText().trim())) {
303: enabled = false; //NOI18N
304: }
305: // method signature cannot be empty
306: else if ("".equals(methodSignature.getText().trim())) {
307: enabled = false; //NOI18N
308: }
309: // try to format method as a kind of heuristics if provided info is correct
310: // TODO: className, methodName and methodSignature should be validated separately and exactly!
311: else {
312: try {
313: // new MethodNameFormatter(getClassName(), getMethodName(), getMethodSignature()).getFullFormattedClassAndMethod();
314: methodSignature.setForeground(UIManager
315: .getColor("Label.foreground")); // NOI18N // formatting method could fail only because of incorrect signature
316: } catch (Exception e) {
317: methodSignature.setForeground(Color.red); // formatting method could fail only because of incorrect signature
318: enabled = false;
319: }
320: }
321:
322: okButton.setEnabled(enabled);
323: }
324: }
|