KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > AntCustomizer


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;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.beans.PropertyEditor JavaDoc;
27 import java.beans.PropertyEditorManager JavaDoc;
28 import java.io.File JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.util.regex.Pattern JavaDoc;
34 import javax.swing.DefaultComboBoxModel JavaDoc;
35 import javax.swing.JFileChooser JavaDoc;
36 import javax.swing.JPanel JavaDoc;
37 import javax.swing.SwingUtilities JavaDoc;
38 import org.openide.DialogDescriptor;
39 import org.openide.DialogDisplayer;
40 import org.openide.NotifyDescriptor;
41 import org.openide.execution.NbClassPath;
42 import org.openide.util.NbBundle;
43 import org.openide.util.NbCollections;
44
45 /**
46  * Implementation of one panel in Options Dialog.
47  * @author Jan Jancura, Jesse Glick
48  */

49 public class AntCustomizer extends JPanel JavaDoc implements ActionListener JavaDoc {
50     
51     private List JavaDoc<File JavaDoc> classpath;
52     private Map JavaDoc<String JavaDoc,String JavaDoc> properties;
53     private boolean changed = false;
54     private boolean listen = false;
55     private File JavaDoc originalAntHome;
56
57     public AntCustomizer() {
58         initComponents();
59         bAntHome.addActionListener (this);
60         ((DefaultComboBoxModel JavaDoc) cbVerbosity.getModel()).removeAllElements(); // just have prototype for form editor
61
cbVerbosity.addItem(NbBundle.getMessage(AntCustomizer.class, "LBL_verbosity_warn"));
62         cbVerbosity.addItem(NbBundle.getMessage(AntCustomizer.class, "LBL_verbosity_info"));
63         cbVerbosity.addItem(NbBundle.getMessage(AntCustomizer.class, "LBL_verbosity_verbose"));
64         cbVerbosity.addItem(NbBundle.getMessage(AntCustomizer.class, "LBL_verbosity_debug"));
65         bProperties.addActionListener (this);
66         bClasspath.addActionListener (this);
67         cbSaveFiles.addActionListener (this);
68         cbReuseOutput.addActionListener (this);
69         cbAlwaysShowOutput.addActionListener (this);
70         cbVerbosity.addActionListener (this);
71     }
72     
73     void update () {
74         listen = false;
75         classpath = AntSettings.getExtraClasspath();
76         properties = AntSettings.getProperties();
77         originalAntHome = AntSettings.getAntHome();
78             
79         tfAntHome.setText(originalAntHome != null ? originalAntHome.toString() : null);
80         cbSaveFiles.setSelected(AntSettings.getSaveAll());
81         cbReuseOutput.setSelected(AntSettings.getAutoCloseTabs());
82         cbAlwaysShowOutput.setSelected(AntSettings.getAlwaysShowOutput());
83         cbVerbosity.setSelectedIndex(AntSettings.getVerbosity() - 1);
84         lAntVersion.setText("(" + AntSettings.getAntVersion() + ")");
85         changed = false;
86         initialized = true;
87         listen = true;
88     }
89     
90     private boolean initialized = false;
91     
92     void applyChanges () {
93         if (!initialized) return;
94         String JavaDoc antHome = tfAntHome.getText ().trim ();
95         AntSettings.setAntHome(new File JavaDoc(antHome));
96         if (AntSettings.getAutoCloseTabs() != cbReuseOutput.isSelected()) {
97             AntSettings.setAutoCloseTabs(cbReuseOutput.isSelected());
98         }
99         if (AntSettings.getSaveAll() != cbSaveFiles.isSelected()) {
100             AntSettings.setSaveAll(cbSaveFiles.isSelected());
101         }
102         if (AntSettings.getAlwaysShowOutput() != cbAlwaysShowOutput.isSelected()) {
103             AntSettings.setAlwaysShowOutput(cbAlwaysShowOutput.isSelected());
104         }
105         if (AntSettings.getVerbosity() != cbVerbosity.getSelectedIndex() + 1) {
106             AntSettings.setVerbosity(cbVerbosity.getSelectedIndex() + 1);
107         }
108         if (!AntSettings.getProperties().equals(properties)) {
109             AntSettings.setProperties(properties);
110         }
111         if (!AntSettings.getExtraClasspath().equals(classpath)) {
112             AntSettings.setExtraClasspath(classpath);
113         }
114         changed = false;
115     }
116     
117     void cancel () {
118         if (AntSettings.getAntHome() != originalAntHome) {
119             AntSettings.setAntHome(originalAntHome);
120         }
121         changed = false;
122     }
123     
124     boolean dataValid () {
125         return true;
126     }
127     
128     boolean isChanged () {
129         return changed;
130     }
131     
132     public void actionPerformed (ActionEvent JavaDoc e) {
133         if (!listen) return;
134         Object JavaDoc o = e.getSource ();
135         if (o == cbAlwaysShowOutput) {
136             changed = true;
137         } else
138         if (o == cbReuseOutput) {
139             changed = true;
140         } else
141         if (o == cbSaveFiles) {
142             changed = true;
143         } else
144         if (o == cbVerbosity) {
145             changed = true;
146         } else
147         if (o == bAntHome) {
148             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc (tfAntHome.getText ());
149             chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
150             int r = chooser.showDialog (
151                 SwingUtilities.getWindowAncestor (this),
152                 NbBundle.getMessage(AntCustomizer.class, "Select_Directory")
153             );
154             if (r == JFileChooser.APPROVE_OPTION) {
155                 File JavaDoc file = chooser.getSelectedFile ();
156                 if (!new File JavaDoc (new File JavaDoc (file, "lib"), "ant.jar").isFile ()) {
157                     DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
158                         NbBundle.getMessage(AntCustomizer.class, "Not_a_ant_home", file),
159                         NotifyDescriptor.Message.WARNING_MESSAGE
160                     ));
161                     return;
162                 }
163                 tfAntHome.setText (file.getAbsolutePath ());
164                 AntSettings.setAntHome(file);
165                 lAntVersion.setText("(" + AntSettings.getAntVersion() + ")");
166                 changed = true;
167             }
168         } else
169         if (o == bClasspath) {
170             PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(NbClassPath.class);
171             editor.setValue(new NbClassPath(classpath.toArray(new File JavaDoc[classpath.size()])));
172             Component JavaDoc customEditor = editor.getCustomEditor ();
173             DialogDescriptor dd = new DialogDescriptor (
174                 customEditor,
175                 NbBundle.getMessage(AntCustomizer.class, "Classpath_Editor_Title")
176             );
177             Dialog JavaDoc dialog = DialogDisplayer.getDefault ().createDialog (dd);
178             dialog.setVisible (true);
179             if (dd.getValue () == NotifyDescriptor.OK_OPTION) {
180                 String JavaDoc cp = ((NbClassPath) editor.getValue()).getClassPath();
181                 if (cp.startsWith("\"") && cp.endsWith("\"")) {
182                     // *@%!* NbClassPath.getClassPath semantics.
183
cp = cp.substring(1, cp.length() - 1);
184                 }
185                 classpath = new ArrayList JavaDoc<File JavaDoc>();
186                 for (String JavaDoc f : cp.split(Pattern.quote(File.pathSeparator))) {
187                     classpath.add(new File JavaDoc(f));
188                 }
189                 changed = true;
190             }
191         } else
192         if (o == bProperties) {
193             PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(Properties JavaDoc.class);
194             Properties JavaDoc p = new Properties JavaDoc();
195             p.putAll(properties);
196             editor.setValue(p);
197             Component JavaDoc customEditor = editor.getCustomEditor ();
198             DialogDescriptor dd = new DialogDescriptor (
199                 customEditor,
200                 NbBundle.getMessage(AntCustomizer.class, "Properties_Editor_Title")
201             );
202             Dialog JavaDoc dialog = DialogDisplayer.getDefault ().createDialog (dd);
203             dialog.setVisible (true);
204             if (dd.getValue () == NotifyDescriptor.OK_OPTION) {
205                 properties = NbCollections.checkedMapByCopy((Properties JavaDoc) editor.getValue(), String JavaDoc.class, String JavaDoc.class, true);
206                 changed = true;
207             }
208         }
209     }
210     
211     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
212
private void initComponents() {
213         javax.swing.JLabel JavaDoc antHomeLabel;
214         javax.swing.JLabel JavaDoc classpathLabel;
215         javax.swing.JPanel JavaDoc classpathPanel;
216         javax.swing.JLabel JavaDoc propertiesLabel;
217         javax.swing.JPanel JavaDoc propertiesPanel;
218         javax.swing.JLabel JavaDoc verbosityLabel;
219
220         antHomeLabel = new javax.swing.JLabel JavaDoc();
221         tfAntHome = new javax.swing.JTextField JavaDoc();
222         bAntHome = new javax.swing.JButton JavaDoc();
223         bAntHomeDefault = new javax.swing.JButton JavaDoc();
224         lAntVersion = new javax.swing.JLabel JavaDoc();
225         cbSaveFiles = new javax.swing.JCheckBox JavaDoc();
226         cbReuseOutput = new javax.swing.JCheckBox JavaDoc();
227         cbAlwaysShowOutput = new javax.swing.JCheckBox JavaDoc();
228         cbVerbosity = new javax.swing.JComboBox JavaDoc();
229         verbosityLabel = new javax.swing.JLabel JavaDoc();
230         propertiesPanel = new javax.swing.JPanel JavaDoc();
231         propertiesLabel = new javax.swing.JLabel JavaDoc();
232         bProperties = new javax.swing.JButton JavaDoc();
233         classpathPanel = new javax.swing.JPanel JavaDoc();
234         classpathLabel = new javax.swing.JLabel JavaDoc();
235         bClasspath = new javax.swing.JButton JavaDoc();
236
237         setBackground(java.awt.Color.white);
238         antHomeLabel.setLabelFor(tfAntHome);
239         org.openide.awt.Mnemonics.setLocalizedText(antHomeLabel, NbBundle.getMessage(AntCustomizer.class, "Ant_Home"));
240
241         org.openide.awt.Mnemonics.setLocalizedText(bAntHome, NbBundle.getMessage(AntCustomizer.class, "Ant_Home_Button"));
242
243         org.openide.awt.Mnemonics.setLocalizedText(bAntHomeDefault, NbBundle.getMessage(AntCustomizer.class, "Ant_Home_Default_Button"));
244         bAntHomeDefault.addActionListener(new java.awt.event.ActionListener JavaDoc() {
245             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
246                 bAntHomeDefaultActionPerformed(evt);
247             }
248         });
249
250         lAntVersion.setBackground(java.awt.Color.white);
251         org.openide.awt.Mnemonics.setLocalizedText(lAntVersion, "<Ant version here...>");
252
253         cbSaveFiles.setBackground(java.awt.Color.white);
254         org.openide.awt.Mnemonics.setLocalizedText(cbSaveFiles, NbBundle.getMessage(AntCustomizer.class, "Save_Files"));
255         cbSaveFiles.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
256         cbSaveFiles.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
257
258         cbReuseOutput.setBackground(java.awt.Color.white);
259         org.openide.awt.Mnemonics.setLocalizedText(cbReuseOutput, NbBundle.getMessage(AntCustomizer.class, "Reuse_Output"));
260         cbReuseOutput.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
261         cbReuseOutput.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
262
263         cbAlwaysShowOutput.setBackground(java.awt.Color.white);
264         org.openide.awt.Mnemonics.setLocalizedText(cbAlwaysShowOutput, NbBundle.getMessage(AntCustomizer.class, "Always_Show_Output"));
265         cbAlwaysShowOutput.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
266         cbAlwaysShowOutput.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
267
268         cbVerbosity.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(new String JavaDoc[] { "Normal" }));
269
270         verbosityLabel.setLabelFor(cbVerbosity);
271         org.openide.awt.Mnemonics.setLocalizedText(verbosityLabel, NbBundle.getMessage(AntCustomizer.class, "Verbosity"));
272
273         propertiesPanel.setBackground(java.awt.Color.white);
274         propertiesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(NbBundle.getMessage(AntCustomizer.class, "Properties_Panel")));
275         org.openide.awt.Mnemonics.setLocalizedText(propertiesLabel, NbBundle.getMessage(AntCustomizer.class, "Properties_Text_Area"));
276
277         org.openide.awt.Mnemonics.setLocalizedText(bProperties, NbBundle.getMessage(AntCustomizer.class, "Properties_Button"));
278
279         org.jdesktop.layout.GroupLayout propertiesPanelLayout = new org.jdesktop.layout.GroupLayout(propertiesPanel);
280         propertiesPanel.setLayout(propertiesPanelLayout);
281         propertiesPanelLayout.setHorizontalGroup(
282             propertiesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
283             .add(propertiesPanelLayout.createSequentialGroup()
284                 .addContainerGap()
285                 .add(propertiesLabel)
286                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 25, Short.MAX_VALUE)
287                 .add(bProperties)
288                 .addContainerGap())
289         );
290         propertiesPanelLayout.setVerticalGroup(
291             propertiesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
292             .add(propertiesPanelLayout.createSequentialGroup()
293                 .add(propertiesPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
294                     .add(propertiesLabel)
295                     .add(bProperties))
296                 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
297         );
298
299         classpathPanel.setBackground(java.awt.Color.white);
300         classpathPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(NbBundle.getMessage(AntCustomizer.class, "Classpath_Panel")));
301         org.openide.awt.Mnemonics.setLocalizedText(classpathLabel, NbBundle.getMessage(AntCustomizer.class, "Classpath_Text_Area"));
302
303         org.openide.awt.Mnemonics.setLocalizedText(bClasspath, NbBundle.getMessage(AntCustomizer.class, "Classpath_Button"));
304
305         org.jdesktop.layout.GroupLayout classpathPanelLayout = new org.jdesktop.layout.GroupLayout(classpathPanel);
306         classpathPanel.setLayout(classpathPanelLayout);
307         classpathPanelLayout.setHorizontalGroup(
308             classpathPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
309             .add(classpathPanelLayout.createSequentialGroup()
310                 .addContainerGap()
311                 .add(classpathLabel)
312                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
313                 .add(bClasspath)
314                 .addContainerGap())
315         );
316         classpathPanelLayout.setVerticalGroup(
317             classpathPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
318             .add(classpathLabel)
319             .add(bClasspath)
320         );
321
322         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
323         this.setLayout(layout);
324         layout.setHorizontalGroup(
325             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
326             .add(propertiesPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
327             .add(layout.createSequentialGroup()
328                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
329                     .add(antHomeLabel)
330                     .add(verbosityLabel))
331                 .add(16, 16, 16)
332                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
333                     .add(layout.createSequentialGroup()
334                         .add(cbAlwaysShowOutput)
335                         .addContainerGap())
336                     .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
337                         .add(lAntVersion, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
338                         .add(layout.createSequentialGroup()
339                             .add(cbVerbosity, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
340                             .addContainerGap())
341                         .add(layout.createSequentialGroup()
342                             .add(cbReuseOutput)
343                             .addContainerGap())
344                         .add(layout.createSequentialGroup()
345                             .add(cbSaveFiles)
346                             .addContainerGap())
347                         .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
348                             .add(tfAntHome, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
349                             .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
350                             .add(bAntHome)
351                             .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
352                             .add(bAntHomeDefault)))))
353             .add(classpathPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
354         );
355         layout.setVerticalGroup(
356             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
357             .add(layout.createSequentialGroup()
358                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE, false)
359                     .add(antHomeLabel)
360                     .add(bAntHomeDefault)
361                     .add(bAntHome)
362                     .add(tfAntHome, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
363                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
364                 .add(lAntVersion, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
365                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
366                 .add(cbSaveFiles)
367                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
368                 .add(cbReuseOutput)
369                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
370                 .add(cbAlwaysShowOutput)
371                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
372                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
373                     .add(cbVerbosity, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
374                     .add(verbosityLabel))
375                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
376                 .add(propertiesPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
377                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
378                 .add(classpathPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 59, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
379                 .addContainerGap())
380         );
381     }// </editor-fold>//GEN-END:initComponents
382

383     private void bAntHomeDefaultActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_bAntHomeDefaultActionPerformed
384
AntSettings.setAntHome(null);
385         File JavaDoc antHome = AntSettings.getAntHome();
386         if (antHome != null) {
387             tfAntHome.setText(antHome.getAbsolutePath());
388         } else {
389             tfAntHome.setText(null);
390         }
391         lAntVersion.setText("(" + AntSettings.getAntVersion() + ")");
392         changed = true;
393     }//GEN-LAST:event_bAntHomeDefaultActionPerformed
394

395     // Variables declaration - do not modify//GEN-BEGIN:variables
396
private javax.swing.JButton JavaDoc bAntHome;
397     private javax.swing.JButton JavaDoc bAntHomeDefault;
398     private javax.swing.JButton JavaDoc bClasspath;
399     private javax.swing.JButton JavaDoc bProperties;
400     private javax.swing.JCheckBox JavaDoc cbAlwaysShowOutput;
401     private javax.swing.JCheckBox JavaDoc cbReuseOutput;
402     private javax.swing.JCheckBox JavaDoc cbSaveFiles;
403     private javax.swing.JComboBox JavaDoc cbVerbosity;
404     private javax.swing.JLabel JavaDoc lAntVersion;
405     private javax.swing.JTextField JavaDoc tfAntHome;
406     // End of variables declaration//GEN-END:variables
407

408 }
409
Popular Tags