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