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: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.ant.debugger;
043:
044: import java.io.ByteArrayInputStream;
045: import java.io.ByteArrayOutputStream;
046: import java.io.IOException;
047: import java.text.Collator;
048: import java.util.*;
049: import javax.swing.DefaultComboBoxModel;
050: import javax.swing.JEditorPane;
051: import javax.swing.KeyStroke;
052: import javax.swing.text.EditorKit;
053: import org.apache.tools.ant.module.AntSettings;
054: import org.apache.tools.ant.module.api.AntProjectCookie;
055: import org.apache.tools.ant.module.api.AntTargetExecutor;
056: import org.apache.tools.ant.module.api.support.TargetLister;
057: import org.openide.awt.Mnemonics;
058: import org.openide.execution.ExecutorTask;
059: import org.openide.filesystems.FileObject;
060: import org.openide.util.NbBundle;
061:
062: /**
063: * Panel for advanced Ant target invocation.
064: * @author Jesse Glick
065: */
066: final class AdvancedActionPanel extends javax.swing.JPanel {
067:
068: /** File attribute storing last-run target(s). Format: space-separated list. */
069: private static final String ATTR_TARGETS = "org.apache.tools.ant.module.preferredTargets"; // NOI18N
070: /** File attribute storing last-run properties. Format: newline-delimited name=value pairs. */
071: private static final String ATTR_PROPERTIES = "org.apache.tools.ant.module.preferredProperties"; // NOI18N
072: /** File attribute storing last-run verbosity. Format: int. */
073: private static final String ATTR_VERBOSITY = "org.apache.tools.ant.module.preferredVerbosity"; // NOI18N
074:
075: private static final String[] VERBOSITIES = {
076: /* #45482: this one is really useless:
077: NbBundle.getMessage(AdvancedActionPanel.class, "LBL_verbosity_err"),
078: */
079: NbBundle.getMessage(AdvancedActionPanel.class,
080: "LBL_verbosity_warn"),
081: NbBundle.getMessage(AdvancedActionPanel.class,
082: "LBL_verbosity_info"),
083: NbBundle.getMessage(AdvancedActionPanel.class,
084: "LBL_verbosity_verbose"),
085: NbBundle.getMessage(AdvancedActionPanel.class,
086: "LBL_verbosity_debug"), };
087: private static final int[] VERBOSITY_LEVELS = {
088: // no Project.MSG_ERR exposed in GUI
089: 1 /*Project.MSG_WARN*/, 2 /*Project.MSG_INFO*/,
090: 3 /*Project.MSG_VERBOSE*/, 4 /*Project.MSG_DEBUG*/, };
091:
092: private final AntProjectCookie project;
093: private final Set/*<TargetLister.Target>*/allTargets;
094: private String defaultTarget = null;
095:
096: public AdvancedActionPanel(AntProjectCookie project,
097: Set/*<TargetLister.Target>*/allTargets) {
098: this .project = project;
099: this .allTargets = allTargets;
100: initComponents();
101: Mnemonics.setLocalizedText(targetLabel, NbBundle.getMessage(
102: AdvancedActionPanel.class,
103: "AdvancedActionsPanel.targetLabel.text"));
104: Mnemonics
105: .setLocalizedText(
106: targetDescriptionLabel,
107: NbBundle
108: .getMessage(AdvancedActionPanel.class,
109: "AdvancedActionsPanel.targetDescriptionLabel.text"));
110: Mnemonics.setLocalizedText(propertiesLabel, NbBundle
111: .getMessage(AdvancedActionPanel.class,
112: "AdvancedActionsPanel.propertiesLabel.text"));
113: Mnemonics.setLocalizedText(verbosityLabel, NbBundle.getMessage(
114: AdvancedActionPanel.class,
115: "AdvancedActionsPanel.verbosityLabel.text"));
116: // Hack; EditorKit does not permit "fallback" kits, so we have to
117: // mimic what the IDE itself does:
118: EditorKit kit = propertiesPane.getEditorKit();
119: String clazz = kit.getClass().getName();
120: if (clazz.equals("javax.swing.text.DefaultEditorKit") || // NOI18N
121: clazz.equals("javax.swing.JEditorPane$PlainEditorKit")) { // NOI18N
122: propertiesPane.setEditorKit(JEditorPane
123: .createEditorKitForContentType("text/plain")); // NOI18N
124: }
125: // Make ENTER run OK, not change the combo box.
126: targetComboBox.getInputMap().remove(
127: KeyStroke.getKeyStroke("ENTER")); // NOI18N
128: initializeFields();
129: }
130:
131: private void initializeFields() {
132: FileObject script = project.getFileObject();
133: assert script != null : "No file found for " + project;
134: String initialTargets = (String) script
135: .getAttribute(ATTR_TARGETS);
136: SortedSet/*<String>*/relevantTargets = new TreeSet(Collator
137: .getInstance());
138: Iterator it = allTargets.iterator();
139: while (it.hasNext()) {
140: TargetLister.Target target = (TargetLister.Target) it
141: .next();
142: if (!target.isOverridden() && !target.isInternal()) {
143: relevantTargets.add(target.getName());
144: if (defaultTarget == null && target.isDefault()) {
145: defaultTarget = target.getName();
146: }
147: }
148: }
149: targetComboBox.setModel(new DefaultComboBoxModel(
150: relevantTargets.toArray()));
151: if (initialTargets != null) {
152: targetComboBox.setSelectedItem(initialTargets);
153: } else {
154: targetComboBox.setSelectedItem(defaultTarget);
155: }
156: // Initialize description field:
157: targetComboBoxActionPerformed(null);
158: String initialProperties = (String) script
159: .getAttribute(ATTR_PROPERTIES);
160: if (initialProperties == null) {
161: Properties props = new Properties();
162: props.putAll(AntSettings.getProperties());
163: ByteArrayOutputStream baos = new ByteArrayOutputStream();
164: try {
165: props.store(baos, null);
166: String text = baos.toString("ISO-8859-1"); // NOI18N
167: // Strip the annoying initial comment:
168: initialProperties = text.replaceFirst("^#.*\n", ""); // NOI18N
169: } catch (IOException e) {
170: assert false : e;
171: }
172: }
173: propertiesPane.setText(initialProperties);
174: Integer verbosity = (Integer) script
175: .getAttribute(ATTR_VERBOSITY);
176: if (verbosity == null) {
177: verbosity = new Integer(AntSettings.getVerbosity());
178: }
179: verbosityComboBox
180: .setModel(new DefaultComboBoxModel(VERBOSITIES));
181: for (int i = 0; i < VERBOSITY_LEVELS.length; i++) {
182: if (VERBOSITY_LEVELS[i] == verbosity.intValue()) {
183: verbosityComboBox.setSelectedItem(VERBOSITIES[i]);
184: break;
185: }
186: }
187: }
188:
189: /** This method is called from within the constructor to
190: * initialize the form.
191: * WARNING: Do NOT modify this code. The content of this method is
192: * always regenerated by the Form Editor.
193: */
194: private void initComponents() {//GEN-BEGIN:initComponents
195: java.awt.GridBagConstraints gridBagConstraints;
196:
197: targetLabel = new javax.swing.JLabel();
198: targetComboBox = new javax.swing.JComboBox();
199: targetDescriptionLabel = new javax.swing.JLabel();
200: targetDescriptionField = new javax.swing.JTextField();
201: propertiesLabel = new javax.swing.JLabel();
202: propertiesScrollPane = new javax.swing.JScrollPane();
203: propertiesPane = new javax.swing.JEditorPane();
204: verbosityLabel = new javax.swing.JLabel();
205: verbosityComboBox = new javax.swing.JComboBox();
206:
207: setLayout(new java.awt.GridBagLayout());
208:
209: targetLabel.setText("Select target(s) to run:");
210: targetLabel.setLabelFor(targetComboBox);
211: gridBagConstraints = new java.awt.GridBagConstraints();
212: gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
213: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
214: add(targetLabel, gridBagConstraints);
215:
216: targetComboBox.setEditable(true);
217: targetComboBox.setModel(new javax.swing.DefaultComboBoxModel(
218: new String[] { "sampleTarget1", "sampleTarget2",
219: "sampleTarget3" }));
220: targetComboBox
221: .addActionListener(new java.awt.event.ActionListener() {
222: public void actionPerformed(
223: java.awt.event.ActionEvent evt) {
224: targetComboBoxActionPerformed(evt);
225: }
226: });
227:
228: gridBagConstraints = new java.awt.GridBagConstraints();
229: gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
230: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
231: add(targetComboBox, gridBagConstraints);
232:
233: targetDescriptionLabel.setText("Target description:");
234: gridBagConstraints = new java.awt.GridBagConstraints();
235: gridBagConstraints.gridx = 0;
236: gridBagConstraints.gridy = 1;
237: gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
238: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
239: add(targetDescriptionLabel, gridBagConstraints);
240:
241: targetDescriptionField.setEditable(false);
242: targetDescriptionField.setText("Sample description here.");
243: gridBagConstraints = new java.awt.GridBagConstraints();
244: gridBagConstraints.gridx = 1;
245: gridBagConstraints.gridy = 1;
246: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
247: gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
248: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
249: add(targetDescriptionField, gridBagConstraints);
250:
251: propertiesLabel.setText("Special Ant properties:");
252: propertiesLabel.setLabelFor(propertiesPane);
253: gridBagConstraints = new java.awt.GridBagConstraints();
254: gridBagConstraints.gridx = 0;
255: gridBagConstraints.gridy = 2;
256: gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
257: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
258: add(propertiesLabel, gridBagConstraints);
259:
260: propertiesScrollPane.setPreferredSize(new java.awt.Dimension(
261: 400, 150));
262: propertiesScrollPane.setMinimumSize(new java.awt.Dimension(400,
263: 150));
264: propertiesPane
265: .setText("# This is sample text for GUI design.\nsomeprop1=someval1\nsomeprop2=someval2\nsomeprop3=someval3\n");
266: propertiesPane.setContentType("text/x-properties");
267: propertiesScrollPane.setViewportView(propertiesPane);
268:
269: gridBagConstraints = new java.awt.GridBagConstraints();
270: gridBagConstraints.gridx = 1;
271: gridBagConstraints.gridy = 2;
272: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
273: gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
274: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
275: add(propertiesScrollPane, gridBagConstraints);
276:
277: verbosityLabel.setText("Verbosity level:");
278: verbosityLabel.setLabelFor(verbosityComboBox);
279: gridBagConstraints = new java.awt.GridBagConstraints();
280: gridBagConstraints.gridx = 0;
281: gridBagConstraints.gridy = 3;
282: gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
283: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
284: add(verbosityLabel, gridBagConstraints);
285:
286: verbosityComboBox
287: .setModel(new javax.swing.DefaultComboBoxModel(
288: new String[] { "Errors only [SAMPLE]",
289: "Normal [SAMPLE]", "Verbose [SAMPLE]" }));
290: gridBagConstraints = new java.awt.GridBagConstraints();
291: gridBagConstraints.gridx = 1;
292: gridBagConstraints.gridy = 3;
293: gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
294: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
295: add(verbosityComboBox, gridBagConstraints);
296:
297: }//GEN-END:initComponents
298:
299: private void targetComboBoxActionPerformed(
300: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_targetComboBoxActionPerformed
301: String selection = (String) targetComboBox.getSelectedItem();
302: if (selection == null) {
303: // Why? Not sure. #45097.
304: selection = "";
305: }
306: StringTokenizer tok = new StringTokenizer(selection, " ,"); // NOI18N
307: List/*<String>*/targetsL = Collections.list(tok);
308: String description = "";
309: if (targetsL.size() == 1) {
310: String targetName = (String) targetsL.get(0);
311: Iterator it = allTargets.iterator();
312: while (it.hasNext()) {
313: TargetLister.Target target = (TargetLister.Target) it
314: .next();
315: if (!target.isOverridden()
316: && target.getName().equals(targetName)) {
317: description = target.getElement().getAttribute(
318: "description"); // NOI18N
319: // may still be "" if not defined
320: break;
321: }
322: }
323: }
324: targetDescriptionField.setText(description);
325: }//GEN-LAST:event_targetComboBoxActionPerformed
326:
327: // Variables declaration - do not modify//GEN-BEGIN:variables
328: private javax.swing.JLabel propertiesLabel;
329: private javax.swing.JEditorPane propertiesPane;
330: private javax.swing.JScrollPane propertiesScrollPane;
331: private javax.swing.JComboBox targetComboBox;
332: private javax.swing.JTextField targetDescriptionField;
333: private javax.swing.JLabel targetDescriptionLabel;
334: private javax.swing.JLabel targetLabel;
335: private javax.swing.JComboBox verbosityComboBox;
336: private javax.swing.JLabel verbosityLabel;
337:
338: // End of variables declaration//GEN-END:variables
339:
340: /**
341: * Try to run the selected target(s).
342: */
343: public void run() throws IOException {
344: // Read settings from the dialog.
345: String[] targets;
346: String selected = (String) targetComboBox.getSelectedItem();
347: if (selected == null) {
348: // Run default target.
349: targets = null;
350: } else {
351: StringTokenizer tok = new StringTokenizer(selected, " ,"); // NOI18N
352: List/*<String>*/targetsL = Collections.list(tok);
353: if (targetsL.isEmpty()) {
354: // Run default target.
355: targets = null;
356: } else {
357: targets = (String[]) targetsL
358: .toArray(new String[targetsL.size()]);
359: }
360: }
361: Properties props = new Properties();
362: ByteArrayInputStream bais = new ByteArrayInputStream(
363: propertiesPane.getText().getBytes("ISO-8859-1"));
364: props.load(bais);
365: int verbosity = 2;
366: String verbosityString = (String) verbosityComboBox
367: .getSelectedItem();
368: for (int i = 0; i < VERBOSITIES.length; i++) {
369: if (VERBOSITIES[i].equals(verbosityString)) {
370: verbosity = VERBOSITY_LEVELS[i];
371: break;
372: }
373: }
374: // Remember these settings for next time.
375: // Wherever the values used match the default, remove the attribute.
376: FileObject script = project.getFileObject();
377: assert script != null;
378: if (targets == null
379: || (targets.length == 1 && targets[0]
380: .equals(defaultTarget)) || targets.length == 0) {
381: script.setAttribute(ATTR_TARGETS, null);
382: } else {
383: StringBuffer targetsSpaceSep = new StringBuffer();
384: for (int i = 0; i < targets.length; i++) {
385: if (i > 0) {
386: targetsSpaceSep.append(' ');
387: }
388: targetsSpaceSep.append(targets[i]);
389: }
390: script.setAttribute(ATTR_TARGETS, targetsSpaceSep
391: .toString());
392: }
393: if (props.equals(AntSettings.getProperties())) {
394: script.setAttribute(ATTR_PROPERTIES, null);
395: } else {
396: script.setAttribute(ATTR_PROPERTIES, propertiesPane
397: .getText());
398: }
399: if (verbosity == AntSettings.getVerbosity()) {
400: script.setAttribute(ATTR_VERBOSITY, null);
401: } else {
402: script.setAttribute(ATTR_VERBOSITY, new Integer(verbosity));
403: }
404: // Actually run the target(s).
405: DebuggerAntLogger.getDefault().debugFile(project.getFile());
406: AntTargetExecutor.Env env = new AntTargetExecutor.Env();
407: env.setProperties(props);
408: env.setVerbosity(verbosity);
409: AntTargetExecutor executor = AntTargetExecutor
410: .createTargetExecutor(env);
411: ExecutorTask executorTask = executor.execute(project, targets);
412: DebuggerAntLogger.getDefault().fileExecutor(project.getFile(),
413: executorTask);
414: }
415:
416: }
|