KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > debugger > 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.netbeans.modules.ant.debugger;
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.text.Collator JavaDoc;
25 import java.util.*;
26 import javax.swing.DefaultComboBoxModel JavaDoc;
27 import javax.swing.JEditorPane JavaDoc;
28 import javax.swing.KeyStroke JavaDoc;
29 import javax.swing.text.EditorKit JavaDoc;
30 import org.apache.tools.ant.module.AntSettings;
31 import org.apache.tools.ant.module.api.AntProjectCookie;
32 import org.apache.tools.ant.module.api.AntTargetExecutor;
33 import org.apache.tools.ant.module.api.support.TargetLister;
34 import org.openide.awt.Mnemonics;
35 import org.openide.execution.ExecutorTask;
36 import org.openide.filesystems.FileObject;
37 import org.openide.util.NbBundle;
38
39 /**
40  * Panel for advanced Ant target invocation.
41  * @author Jesse Glick
42  */

43 final class AdvancedActionPanel extends javax.swing.JPanel JavaDoc {
44     
45     /** File attribute storing last-run target(s). Format: space-separated list. */
46     private static final String JavaDoc ATTR_TARGETS = "org.apache.tools.ant.module.preferredTargets"; // NOI18N
47
/** File attribute storing last-run properties. Format: newline-delimited name=value pairs. */
48     private static final String JavaDoc ATTR_PROPERTIES = "org.apache.tools.ant.module.preferredProperties"; // NOI18N
49
/** File attribute storing last-run verbosity. Format: int. */
50     private static final String JavaDoc ATTR_VERBOSITY = "org.apache.tools.ant.module.preferredVerbosity"; // NOI18N
51

52     private static final String JavaDoc[] VERBOSITIES = {
53         /* #45482: this one is really useless:
54         NbBundle.getMessage(AdvancedActionPanel.class, "LBL_verbosity_err"),
55          */

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

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

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

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

282     /**
283      * Try to run the selected target(s).
284      */

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