KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > nodes > AdvancedActionPanel


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.apache.tools.ant.module.nodes;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.text.Collator JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.SortedSet JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32 import java.util.TreeSet JavaDoc;
33 import javax.swing.DefaultComboBoxModel JavaDoc;
34 import javax.swing.JEditorPane JavaDoc;
35 import javax.swing.KeyStroke JavaDoc;
36 import javax.swing.text.EditorKit JavaDoc;
37 import org.apache.tools.ant.module.AntSettings;
38 import org.apache.tools.ant.module.api.AntProjectCookie;
39 import org.apache.tools.ant.module.api.support.TargetLister;
40 import org.apache.tools.ant.module.run.TargetExecutor;
41 import org.openide.awt.Mnemonics;
42 import org.openide.filesystems.FileObject;
43 import org.openide.util.NbBundle;
44 import org.openide.util.NbCollections;
45
46 /**
47  * Panel for advanced Ant target invocation.
48  * @author Jesse Glick
49  */

50 final class AdvancedActionPanel extends javax.swing.JPanel JavaDoc {
51     
52     /** File attribute storing last-run target(s). Format: space-separated list. */
53     private static final String JavaDoc ATTR_TARGETS = "org.apache.tools.ant.module.preferredTargets"; // NOI18N
54
/** File attribute storing last-run properties. Format: newline-delimited name=value pairs. */
55     private static final String JavaDoc ATTR_PROPERTIES = "org.apache.tools.ant.module.preferredProperties"; // NOI18N
56
/** File attribute storing last-run verbosity. Format: int. */
57     private static final String JavaDoc ATTR_VERBOSITY = "org.apache.tools.ant.module.preferredVerbosity"; // NOI18N
58

59     private static final String JavaDoc[] VERBOSITIES = {
60         /* #45482: this one is really useless:
61         NbBundle.getMessage(AdvancedActionPanel.class, "LBL_verbosity_err"),
62          */

63         NbBundle.getMessage(AdvancedActionPanel.class, "LBL_verbosity_warn"),
64         NbBundle.getMessage(AdvancedActionPanel.class, "LBL_verbosity_info"),
65         NbBundle.getMessage(AdvancedActionPanel.class, "LBL_verbosity_verbose"),
66         NbBundle.getMessage(AdvancedActionPanel.class, "LBL_verbosity_debug"),
67     };
68     private static final int[] VERBOSITY_LEVELS = {
69         // no Project.MSG_ERR exposed in GUI
70
1 /*Project.MSG_WARN*/,
71         2 /*Project.MSG_INFO*/,
72         3 /*Project.MSG_VERBOSE*/,
73         4 /*Project.MSG_DEBUG*/,
74     };
75     
76     private final AntProjectCookie project;
77     private final Set JavaDoc<TargetLister.Target> allTargets;
78     private String JavaDoc defaultTarget = null;
79     
80     public AdvancedActionPanel(AntProjectCookie project, Set JavaDoc<TargetLister.Target> allTargets) {
81         this.project = project;
82         this.allTargets = allTargets;
83         initComponents();
84         Mnemonics.setLocalizedText(targetLabel, NbBundle.getMessage(AdvancedActionPanel.class, "AdvancedActionsPanel.targetLabel.text"));
85         Mnemonics.setLocalizedText(targetDescriptionLabel, NbBundle.getMessage(AdvancedActionPanel.class, "AdvancedActionsPanel.targetDescriptionLabel.text"));
86         Mnemonics.setLocalizedText(propertiesLabel, NbBundle.getMessage(AdvancedActionPanel.class, "AdvancedActionsPanel.propertiesLabel.text"));
87         Mnemonics.setLocalizedText(verbosityLabel, NbBundle.getMessage(AdvancedActionPanel.class, "AdvancedActionsPanel.verbosityLabel.text"));
88         // Hack; EditorKit does not permit "fallback" kits, so we have to
89
// mimic what the IDE itself does:
90
EditorKit JavaDoc kit = propertiesPane.getEditorKit();
91         String JavaDoc clazz = kit.getClass().getName();
92         if (clazz.equals("javax.swing.text.DefaultEditorKit") || // NOI18N
93
clazz.equals("javax.swing.JEditorPane$PlainEditorKit")) { // NOI18N
94
propertiesPane.setEditorKit(JEditorPane.createEditorKitForContentType("text/plain")); // NOI18N
95
}
96         // Make ENTER run OK, not change the combo box.
97
targetComboBox.getInputMap().remove(KeyStroke.getKeyStroke("ENTER")); // NOI18N
98
initializeFields();
99     }
100     
101     private void initializeFields() {
102         FileObject script = project.getFileObject();
103         assert script != null : "No file found for " + project;
104         String JavaDoc initialTargets = (String JavaDoc) script.getAttribute(ATTR_TARGETS);
105         SortedSet JavaDoc<String JavaDoc> relevantTargets = new TreeSet JavaDoc<String JavaDoc>(Collator.getInstance());
106         for (TargetLister.Target target : allTargets) {
107             if (!target.isOverridden() && !target.isInternal()) {
108                 relevantTargets.add(target.getName());
109                 if (defaultTarget == null && target.isDefault()) {
110                     defaultTarget = target.getName();
111                 }
112             }
113         }
114         targetComboBox.setModel(new DefaultComboBoxModel JavaDoc(relevantTargets.toArray()));
115         if (initialTargets != null) {
116             targetComboBox.setSelectedItem(initialTargets);
117         } else {
118             targetComboBox.setSelectedItem(defaultTarget);
119         }
120         // Initialize description field:
121
targetComboBoxActionPerformed(null);
122         String JavaDoc initialProperties = (String JavaDoc) script.getAttribute(ATTR_PROPERTIES);
123         if (initialProperties == null) {
124             Properties JavaDoc props = new Properties JavaDoc();
125             props.putAll(AntSettings.getProperties());
126             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
127             try {
128                 props.store(baos, null);
129                 String JavaDoc text = baos.toString("ISO-8859-1"); // NOI18N
130
// Strip the annoying initial comment:
131
initialProperties = text.replaceFirst("^#.*\n", ""); // NOI18N
132
} catch (IOException JavaDoc e) {
133                 assert false : e;
134             }
135         }
136         propertiesPane.setText(initialProperties);
137         Integer JavaDoc verbosity = (Integer JavaDoc) script.getAttribute(ATTR_VERBOSITY);
138         if (verbosity == null) {
139             verbosity = AntSettings.getVerbosity();
140         }
141         verbosityComboBox.setModel(new DefaultComboBoxModel JavaDoc(VERBOSITIES));
142         for (int i = 0; i < VERBOSITY_LEVELS.length; i++) {
143             if (VERBOSITY_LEVELS[i] == verbosity) {
144                 verbosityComboBox.setSelectedItem(VERBOSITIES[i]);
145                 break;
146             }
147         }
148     }
149     
150     /** This method is called from within the constructor to
151      * initialize the form.
152      * WARNING: Do NOT modify this code. The content of this method is
153      * always regenerated by the Form Editor.
154      */

155     private void initComponents() {//GEN-BEGIN:initComponents
156
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
157
158         targetLabel = new javax.swing.JLabel JavaDoc();
159         targetComboBox = new javax.swing.JComboBox JavaDoc();
160         targetDescriptionLabel = new javax.swing.JLabel JavaDoc();
161         targetDescriptionField = new javax.swing.JTextField JavaDoc();
162         propertiesLabel = new javax.swing.JLabel JavaDoc();
163         propertiesScrollPane = new javax.swing.JScrollPane JavaDoc();
164         propertiesPane = new javax.swing.JEditorPane JavaDoc();
165         verbosityLabel = new javax.swing.JLabel JavaDoc();
166         verbosityComboBox = new javax.swing.JComboBox JavaDoc();
167
168         setLayout(new java.awt.GridBagLayout JavaDoc());
169
170         targetLabel.setText("Select target(s) to run:");
171         targetLabel.setLabelFor(targetComboBox);
172         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
173         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 5, 5);
174         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
175         add(targetLabel, gridBagConstraints);
176
177         targetComboBox.setEditable(true);
178         targetComboBox.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(new String JavaDoc[] { "sampleTarget1", "sampleTarget2", "sampleTarget3" }));
179         targetComboBox.addActionListener(new java.awt.event.ActionListener JavaDoc() {
180             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
181                 targetComboBoxActionPerformed(evt);
182             }
183         });
184
185         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
186         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 5, 5);
187         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
188         add(targetComboBox, gridBagConstraints);
189
190         targetDescriptionLabel.setText("Target description:");
191         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
192         gridBagConstraints.gridx = 0;
193         gridBagConstraints.gridy = 1;
194         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 5, 5);
195         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
196         add(targetDescriptionLabel, gridBagConstraints);
197
198         targetDescriptionField.setEditable(false);
199         targetDescriptionField.setText("Sample description here.");
200         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
201         gridBagConstraints.gridx = 1;
202         gridBagConstraints.gridy = 1;
203         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
204         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 5, 5);
205         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
206         add(targetDescriptionField, gridBagConstraints);
207
208         propertiesLabel.setText("Special Ant properties:");
209         propertiesLabel.setLabelFor(propertiesPane);
210         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
211         gridBagConstraints.gridx = 0;
212         gridBagConstraints.gridy = 2;
213         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 5, 5);
214         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
215         add(propertiesLabel, gridBagConstraints);
216
217         propertiesScrollPane.setPreferredSize(new java.awt.Dimension JavaDoc(400, 150));
218         propertiesScrollPane.setMinimumSize(new java.awt.Dimension JavaDoc(400, 150));
219         propertiesPane.setText("# This is sample text for GUI design.\nsomeprop1=someval1\nsomeprop2=someval2\nsomeprop3=someval3\n");
220         propertiesPane.setContentType("text/x-properties");
221         propertiesScrollPane.setViewportView(propertiesPane);
222
223         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
224         gridBagConstraints.gridx = 1;
225         gridBagConstraints.gridy = 2;
226         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
227         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 5, 5);
228         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
229         add(propertiesScrollPane, gridBagConstraints);
230
231         verbosityLabel.setText("Verbosity level:");
232         verbosityLabel.setLabelFor(verbosityComboBox);
233         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
234         gridBagConstraints.gridx = 0;
235         gridBagConstraints.gridy = 3;
236         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 5, 5);
237         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
238         add(verbosityLabel, gridBagConstraints);
239
240         verbosityComboBox.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(new String JavaDoc[] { "Errors only [SAMPLE]", "Normal [SAMPLE]", "Verbose [SAMPLE]" }));
241         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
242         gridBagConstraints.gridx = 1;
243         gridBagConstraints.gridy = 3;
244         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 5, 5);
245         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
246         add(verbosityComboBox, gridBagConstraints);
247
248     }//GEN-END:initComponents
249

250     private void targetComboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_targetComboBoxActionPerformed
251
String JavaDoc selection = (String JavaDoc) targetComboBox.getSelectedItem();
252         if (selection == null) {
253             // Why? Not sure. #45097.
254
selection = "";
255         }
256         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(selection, " ,"); // NOI18N
257
List JavaDoc<String JavaDoc> targetsL = Collections.list(NbCollections.checkedEnumerationByFilter(tok, String JavaDoc.class, true));
258         String JavaDoc description = "";
259         if (targetsL.size() == 1) {
260             String JavaDoc targetName = targetsL.get(0);
261             for (TargetLister.Target target : allTargets) {
262                 if (!target.isOverridden() && target.getName().equals(targetName)) {
263                     description = target.getElement().getAttribute("description"); // NOI18N
264
// may still be "" if not defined
265
break;
266                 }
267             }
268         }
269         targetDescriptionField.setText(description);
270     }//GEN-LAST:event_targetComboBoxActionPerformed
271

272     
273     // Variables declaration - do not modify//GEN-BEGIN:variables
274
private javax.swing.JLabel JavaDoc propertiesLabel;
275     private javax.swing.JEditorPane JavaDoc propertiesPane;
276     private javax.swing.JScrollPane JavaDoc propertiesScrollPane;
277     private javax.swing.JComboBox JavaDoc targetComboBox;
278     private javax.swing.JTextField JavaDoc targetDescriptionField;
279     private javax.swing.JLabel JavaDoc targetDescriptionLabel;
280     private javax.swing.JLabel JavaDoc targetLabel;
281     private javax.swing.JComboBox JavaDoc verbosityComboBox;
282     private javax.swing.JLabel JavaDoc verbosityLabel;
283     // End of variables declaration//GEN-END:variables
284

285     /**
286      * Try to run the selected target(s).
287      */

288     public void run() throws IOException JavaDoc {
289         // Read settings from the dialog.
290
String JavaDoc selection = (String JavaDoc) targetComboBox.getSelectedItem();
291         String JavaDoc[] targets = null; // default target unless otherwise specified
292
if (selection != null) {
293             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(selection, " ,"); // NOI18N
294
List JavaDoc<String JavaDoc> targetsL = Collections.list(NbCollections.checkedEnumerationByFilter(tok, String JavaDoc.class, true));
295             if (!targetsL.isEmpty()) {
296                 targets = targetsL.toArray(new String JavaDoc[targetsL.size()]);
297             }
298         }
299         Properties JavaDoc props = new Properties JavaDoc();
300         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(propertiesPane.getText().getBytes("ISO-8859-1"));
301         props.load(bais);
302         int verbosity = 2;
303         String JavaDoc verbosityString = (String JavaDoc) verbosityComboBox.getSelectedItem();
304         for (int i = 0; i < VERBOSITIES.length; i++) {
305             if (VERBOSITIES[i].equals(verbosityString)) {
306                 verbosity = VERBOSITY_LEVELS[i];
307                 break;
308             }
309         }
310         // Remember these settings for next time.
311
// Wherever the values used match the default, remove the attribute.
312
FileObject script = project.getFileObject();
313         assert script != null;
314         if (targets == null || (targets.length == 1 && targets[0].equals(defaultTarget))) {
315             script.setAttribute(ATTR_TARGETS, null);
316         } else {
317             StringBuffer JavaDoc targetsSpaceSep = new StringBuffer JavaDoc();
318             for (int i = 0; i < targets.length; i++) {
319                 if (i > 0) {
320                     targetsSpaceSep.append(' ');
321                 }
322                 targetsSpaceSep.append(targets[i]);
323             }
324             script.setAttribute(ATTR_TARGETS, targetsSpaceSep.toString());
325         }
326         if (props.equals(AntSettings.getProperties())) {
327             script.setAttribute(ATTR_PROPERTIES, null);
328         } else {
329             script.setAttribute(ATTR_PROPERTIES, propertiesPane.getText());
330         }
331         if (verbosity == AntSettings.getVerbosity()) {
332             script.setAttribute(ATTR_VERBOSITY, null);
333         } else {
334             script.setAttribute(ATTR_VERBOSITY, verbosity);
335         }
336         // Actually run the target(s).
337
TargetExecutor exec = new TargetExecutor(project, targets);
338         exec.setProperties(NbCollections.checkedMapByCopy(props, String JavaDoc.class, String JavaDoc.class, true));
339         exec.setVerbosity(verbosity);
340         exec.execute();
341     }
342     
343 }
344
Popular Tags