KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > ui > TargetMappingPanel


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.freeform.ui;
21
22 import java.awt.Frame JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.regex.Matcher JavaDoc;
32 import java.util.regex.Pattern JavaDoc;
33 import javax.swing.ComboBoxModel JavaDoc;
34 import javax.swing.DefaultCellEditor JavaDoc;
35 import javax.swing.JComboBox JavaDoc;
36 import javax.swing.JDialog JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.WindowConstants JavaDoc;
40 import javax.swing.table.AbstractTableModel JavaDoc;
41 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
42 import org.netbeans.modules.ant.freeform.Util;
43 import org.netbeans.modules.ant.freeform.spi.ProjectConstants;
44 import org.netbeans.modules.ant.freeform.spi.TargetDescriptor;
45 import org.netbeans.spi.project.support.ant.AntProjectHelper;
46 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
47 import org.openide.awt.Mnemonics;
48 import org.openide.filesystems.FileObject;
49 import org.openide.util.HelpCtx;
50 import org.openide.util.NbBundle;
51
52 /**
53  * @author David Konecny
54  */

55 public class TargetMappingPanel extends JPanel JavaDoc implements ActionListener JavaDoc, HelpCtx.Provider {
56
57     public static final String JavaDoc BUILD_ACTION = "build"; // NOI18N
58
public static final String JavaDoc CLEAN_ACTION = "clean"; // NOI18N
59
public static final String JavaDoc REBUILD_ACTION = "rebuild"; // NOI18N
60
public static final String JavaDoc JAVADOC_ACTION = "javadoc"; // NOI18N
61
public static final String JavaDoc RUN_ACTION = "run"; // NOI18N
62
public static final String JavaDoc TEST_ACTION = "test"; // NOI18N
63

64     private static final List JavaDoc<String JavaDoc> DEFAULT_BUILD_TARGETS = Arrays.asList("build", "compile", "jar", "dist", "all", ".*jar.*"); // NOI18N
65
private static final List JavaDoc<String JavaDoc> DEFAULT_CLEAN_TARGETS = Arrays.asList("clean", ".*clean.*"); // NOI18N
66
//private static final List<String> DEFAULT_REBUILD_TARGETS = Arrays.asList("rebuild", ".*rebuild.*"); // NOI18N
67
private static final List JavaDoc<String JavaDoc> DEFAULT_JAVADOC_TARGETS = Arrays.asList("javadoc", "javadocs", "docs", "doc", ".*javadoc.*", ".*doc.*"); // NOI18N
68
private static final List JavaDoc<String JavaDoc> DEFAULT_RUN_TARGETS = Arrays.asList("run", "start", ".*run.*", ".*start.*"); // NOI18N
69
private static final List JavaDoc<String JavaDoc> DEFAULT_TEST_TARGETS = Arrays.asList("test", ".*test.*"); // NOI18N
70

71     private List JavaDoc<String JavaDoc> targetNames;
72     private List JavaDoc<FreeformProjectGenerator.TargetMapping> targetMappings;
73     private List JavaDoc<FreeformProjectGenerator.CustomTarget> custTargets;
74     private CustomTargetsModel customTargetsModel;
75     private String JavaDoc antScript;
76     
77     private List JavaDoc<JComboBox JavaDoc> combos = new ArrayList JavaDoc<JComboBox JavaDoc>();
78     private List JavaDoc<TargetDescriptor> targetDescs = new ArrayList JavaDoc<TargetDescriptor>();
79     
80     /** Any change in standard tasks which needs to be persisted? */
81     private boolean dirtyRegular;
82     
83     /** Any change in custom tasks which needs to be persisted? */
84     private boolean dirtyCustom;
85     
86     private AntProjectHelper helper;
87     
88     public TargetMappingPanel(boolean advancedPart) {
89         this(new ArrayList JavaDoc<TargetDescriptor>(), advancedPart);
90     }
91     
92     public TargetMappingPanel(List JavaDoc<TargetDescriptor> extraTargets, boolean advancedPart) {
93         initComponents();
94         targetMappings = new ArrayList JavaDoc<FreeformProjectGenerator.TargetMapping>();
95
96         custTargets = new ArrayList JavaDoc<FreeformProjectGenerator.CustomTarget>();
97         customTargetsModel = new CustomTargetsModel();
98         customTargets.setModel(customTargetsModel);
99         customTargets.getTableHeader().setReorderingAllowed(false);
100         
101         addTargets(extraTargets);
102         showAdvancedPart(advancedPart);
103     }
104
105     public TargetMappingPanel(List JavaDoc<TargetDescriptor> extraTargets, PropertyEvaluator evaluator, AntProjectHelper helper) {
106         this(extraTargets, true);
107         this.helper = helper;
108         FileObject as = FreeformProjectGenerator.getAntScript(helper, evaluator);
109         List JavaDoc<String JavaDoc> l = null;
110         // #50933 - script can be null
111
if (as != null) {
112             l = Util.getAntScriptTargetNames(as);
113         }
114         if (l != null) {
115             setTargetNames(l, false);
116             initAntTargetEditor(l);
117         }
118         antScript = defaultAntScript(evaluator);
119         initMappings(FreeformProjectGenerator.getTargetMappings(helper), antScript);
120
121         custTargets = FreeformProjectGenerator.getCustomContextMenuActions(helper);
122         customTargetsModel.fireTableDataChanged();
123
124         updateButtons();
125     }
126     
127     /**
128      * Get the default name of a project's Ant script, as for project.xml usage.
129      * @param evaluator a property evaluator for the project
130      * @return the script name to use, or null for the default
131      * @see FreeformProjectGenerator.TargetMapping#script
132      */

133     static String JavaDoc defaultAntScript(PropertyEvaluator evaluator) {
134         String JavaDoc antScript = evaluator.getProperty(ProjectConstants.PROP_ANT_SCRIPT);
135         if (antScript == null) {
136             // Default, i.e. build.xml.
137
return null;
138         } else {
139             // Set to something specific; refer to it symbolically.
140
return "${" + ProjectConstants.PROP_ANT_SCRIPT + "}"; // NOI18N
141
}
142     }
143
144     
145     private void addTargets(List JavaDoc<TargetDescriptor> extraTargets) {
146         combos.add(buildCombo);
147         targetDescs.add(new TargetDescriptor(BUILD_ACTION, DEFAULT_BUILD_TARGETS, null, null));
148         combos.add(cleanCombo);
149         targetDescs.add(new TargetDescriptor(CLEAN_ACTION, DEFAULT_CLEAN_TARGETS, null, null));
150         combos.add(javadocCombo);
151         targetDescs.add(new TargetDescriptor(JAVADOC_ACTION, DEFAULT_JAVADOC_TARGETS, null, null));
152         combos.add(runCombo);
153         targetDescs.add(new TargetDescriptor(RUN_ACTION, DEFAULT_RUN_TARGETS, null, null));
154         combos.add(testCombo);
155         targetDescs.add(new TargetDescriptor(TEST_ACTION, DEFAULT_TEST_TARGETS, null, null));
156         int y = 5;
157         for (TargetDescriptor desc : extraTargets) {
158             targetDescs.add(desc);
159
160             JComboBox JavaDoc combo = new JComboBox JavaDoc();
161             combo.setEditable(true);
162             java.awt.GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
163             gridBagConstraints.gridx = 1;
164             gridBagConstraints.gridy = y;
165             gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
166             gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
167             gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
168             targetsPanel.add(combo, gridBagConstraints);
169
170             JLabel JavaDoc label = new JLabel JavaDoc();
171             label.setLabelFor(combo);
172             Mnemonics.setLocalizedText(label, desc.getIDEActionLabel());
173             gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
174             gridBagConstraints.gridx = 0;
175             gridBagConstraints.gridy = y;
176             gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
177             gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 6);
178             targetsPanel.add(label, gridBagConstraints);
179             label.getAccessibleContext().setAccessibleDescription(desc.getAccessibleLabel());
180             
181             combos.add(combo);
182             y++;
183         }
184     }
185     
186     public HelpCtx getHelpCtx() {
187         return new HelpCtx( TargetMappingPanel.class );
188     }
189     
190     private void showAdvancedPart(boolean show) {
191         additionalTargetsLabel.setVisible(show);
192         jScrollPane1.setVisible(show);
193         customTargets.setVisible(show);
194         add.setVisible(show);
195         remove.setVisible(show);
196         // handle panel resizing:
197
remainder.setVisible(show);
198         specialRemainder.setVisible(!show);
199     }
200     
201     private void initAntTargetEditor(List JavaDoc<String JavaDoc> targets) {
202         JComboBox JavaDoc combo = new JComboBox JavaDoc();
203         combo.setEditable(true);
204         for (String JavaDoc target : targets) {
205             combo.addItem(target);
206         }
207         customTargets.setDefaultEditor(JComboBox JavaDoc.class, new DefaultCellEditor JavaDoc(combo));
208     }
209     
210     private FreeformProjectGenerator.CustomTarget getItem(int index) {
211         return custTargets.get(index);
212     }
213
214     public void setTargetNames(List JavaDoc<String JavaDoc> list, boolean selectDefaults) {
215         targetNames = list;
216         targetNames.add(0, ""); // NOI18N
217
updateCombos(selectDefaults);
218     }
219
220     public void setScript(String JavaDoc script) {
221         this.antScript = script;
222         for (FreeformProjectGenerator.TargetMapping tm : targetMappings) {
223             tm.script = script;
224         }
225     }
226
227     private void updateCombos(boolean selectDefaults) {
228         // In case you go back and choose a different script:
229
Iterator JavaDoc<TargetDescriptor> descriptors = targetDescs.iterator();
230         for (JComboBox JavaDoc combo : combos) {
231             TargetDescriptor desc = descriptors.next();
232             combo.removeAllItems();
233             for (String JavaDoc name : targetNames) {
234                 combo.addItem(name);
235             }
236             if (selectDefaults) {
237                 selectItem(combo, desc.getDefaultTargets(), false); // NOI18N
238
}
239         }
240     }
241
242     /**
243      * @param items concrete item to be selected (or added) or list of
244      * regular expressions to match
245      */

246     private void selectItem(JComboBox JavaDoc combo, List JavaDoc<String JavaDoc> items, boolean add) {
247         ComboBoxModel JavaDoc model = combo.getModel();
248         for (String JavaDoc item : items) {
249             Pattern JavaDoc pattern = Pattern.compile(item);
250             for (int i=0; i<model.getSize(); i++) {
251                 String JavaDoc target = (String JavaDoc)model.getElementAt(i);
252                 Matcher JavaDoc matcher = pattern.matcher(target);
253                 if (matcher.matches()) {
254                     model.setSelectedItem(target);
255                     return;
256                 }
257             }
258         }
259         if (add) {
260             assert items.size() == 1 : "There should be only one item in this case"; // NOI18N
261
combo.addItem(items.get(0));
262             model.setSelectedItem(items.get(0));
263         } else {
264             model.setSelectedItem(""); // NOI18N
265
}
266     }
267
268     private void initMappings(List JavaDoc<FreeformProjectGenerator.TargetMapping> list, String JavaDoc antScript) {
269         for (FreeformProjectGenerator.TargetMapping tm : list) {
270             Iterator JavaDoc<JComboBox JavaDoc> combosIt = combos.iterator();
271             for (TargetDescriptor desc : targetDescs) {
272                 JComboBox JavaDoc combo = combosIt.next();
273                 if (tm.name.equals(desc.getIDEActionName())) {
274                     selectItem(combo, Collections.singletonList(getListAsString(tm.targets)), true);
275                     checkAntScript(combo, antScript, tm.script);
276                 }
277             }
278         }
279         targetMappings = list;
280     }
281     
282     private void checkAntScript(JComboBox JavaDoc combo, String JavaDoc antScript, String JavaDoc targetScript) {
283         if ((antScript == null && targetScript == null) ||
284             (antScript != null && antScript.equals(targetScript))) {
285             combo.setEnabled(true);
286         } else {
287             combo.setEnabled(false);
288         }
289     }
290
291     private static String JavaDoc getListAsString(List JavaDoc<String JavaDoc> list) {
292         assert list != null;
293         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
294         Iterator JavaDoc<String JavaDoc> it = list.iterator();
295         while (it.hasNext()) {
296             sb.append(it.next());
297             if (it.hasNext()) {
298                 sb.append(" "); // NOI18N
299
}
300         }
301         return sb.toString();
302     }
303
304     static List JavaDoc<String JavaDoc> getStringAsList(String JavaDoc str) {
305         List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>(2);
306         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(str, " "); // NOI18N
307
while (tok.hasMoreTokens()) {
308             String JavaDoc target = tok.nextToken().trim();
309             if (target.length() == 0) {
310                 continue;
311             }
312             l.add(target);
313         }
314         return l;
315     }
316
317     private void storeTarget(String JavaDoc key, JComboBox JavaDoc combo) {
318         if (combo.getModel().getSelectedItem() == null || ((String JavaDoc)combo.getModel().getSelectedItem()).length() == 0) {
319             removeTargetMapping(key);
320             return;
321         }
322         FreeformProjectGenerator.TargetMapping tm = getTargetMapping(key);
323         String JavaDoc value = (String JavaDoc)combo.getModel().getSelectedItem();
324         List JavaDoc<String JavaDoc> l = getStringAsList(value);
325         if (!l.equals(tm.targets)) {
326             dirtyRegular = true;
327         }
328         tm.targets = l;
329         return;
330     }
331
332     private FreeformProjectGenerator.TargetMapping getTargetMapping(String JavaDoc key) {
333         for (FreeformProjectGenerator.TargetMapping tm : targetMappings) {
334             if (tm.name.equals(key)) {
335                 return tm;
336             }
337         }
338         FreeformProjectGenerator.TargetMapping tm = new FreeformProjectGenerator.TargetMapping();
339         tm.name = key;
340         tm.script = antScript;
341         targetMappings.add(tm);
342         dirtyRegular = true;
343         return tm;
344     }
345
346     private void removeTargetMapping(String JavaDoc key) {
347         Iterator JavaDoc<FreeformProjectGenerator.TargetMapping> it = targetMappings.iterator();
348         while (it.hasNext()) {
349             if (it.next().name.equals(key)) {
350                 it.remove();
351                 dirtyRegular = true;
352                 return;
353             }
354         }
355     }
356
357     public List JavaDoc<FreeformProjectGenerator.TargetMapping> getMapping() {
358         Iterator JavaDoc<JComboBox JavaDoc> combosIt = combos.iterator();
359         for (TargetDescriptor desc : targetDescs) {
360             JComboBox JavaDoc combo = combosIt.next();
361             storeTarget(desc.getIDEActionName(), combo);
362         }
363         // update rebuilt:
364
if (cleanCombo.getModel().getSelectedItem() != null &&
365                 ((String JavaDoc)cleanCombo.getModel().getSelectedItem()).length() > 0 &&
366                 buildCombo.getModel().getSelectedItem() != null &&
367                 ((String JavaDoc)buildCombo.getModel().getSelectedItem()).length() > 0) {
368             FreeformProjectGenerator.TargetMapping tm = getTargetMapping(REBUILD_ACTION);
369             String JavaDoc val = (String JavaDoc)cleanCombo.getModel().getSelectedItem() + " " + (String JavaDoc)buildCombo.getModel().getSelectedItem(); // NOI18N
370
tm.targets = getStringAsList(val);
371         } else {
372             removeTargetMapping(REBUILD_ACTION);
373         }
374         return targetMappings;
375     }
376
377     /** This method is called from within the constructor to
378      * initialize the form.
379      * WARNING: Do NOT modify this code. The content of this method is
380      * always regenerated by the Form Editor.
381      */

382     private void initComponents() {//GEN-BEGIN:initComponents
383
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
384
385         mainLabel = new javax.swing.JLabel JavaDoc();
386         additionalTargetsLabel = new javax.swing.JLabel JavaDoc();
387         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
388         customTargets = new javax.swing.JTable JavaDoc();
389         add = new javax.swing.JButton JavaDoc();
390         remove = new javax.swing.JButton JavaDoc();
391         remainder = new javax.swing.JPanel JavaDoc();
392         targetsPanel = new javax.swing.JPanel JavaDoc();
393         buildLabel = new javax.swing.JLabel JavaDoc();
394         cleanLabel = new javax.swing.JLabel JavaDoc();
395         runLabel = new javax.swing.JLabel JavaDoc();
396         javadocLabel = new javax.swing.JLabel JavaDoc();
397         testLabel = new javax.swing.JLabel JavaDoc();
398         buildCombo = new javax.swing.JComboBox JavaDoc();
399         cleanCombo = new javax.swing.JComboBox JavaDoc();
400         javadocCombo = new javax.swing.JComboBox JavaDoc();
401         runCombo = new javax.swing.JComboBox JavaDoc();
402         testCombo = new javax.swing.JComboBox JavaDoc();
403         specialRemainder = new javax.swing.JPanel JavaDoc();
404
405         setLayout(new java.awt.GridBagLayout JavaDoc());
406
407         setPreferredSize(new java.awt.Dimension JavaDoc(300, 280));
408         mainLabel.setLabelFor(this);
409         org.openide.awt.Mnemonics.setLocalizedText(mainLabel, org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "LBL_TargetMappingPanel_jLabel1"));
410         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
411         gridBagConstraints.gridx = 0;
412         gridBagConstraints.gridy = 0;
413         gridBagConstraints.gridwidth = 2;
414         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
415         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
416         add(mainLabel, gridBagConstraints);
417         mainLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "ACSD_TargetMappingPanel_jLabel1"));
418
419         additionalTargetsLabel.setLabelFor(customTargets);
420         org.openide.awt.Mnemonics.setLocalizedText(additionalTargetsLabel, org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "LBL_TargetMappingPanel_jLabel10"));
421         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
422         gridBagConstraints.gridx = 0;
423         gridBagConstraints.gridy = 3;
424         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
425         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
426         add(additionalTargetsLabel, gridBagConstraints);
427         additionalTargetsLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "ACSD_TargetMappingPanel_jLabel10"));
428
429         jScrollPane1.setViewportView(customTargets);
430
431         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
432         gridBagConstraints.gridx = 0;
433         gridBagConstraints.gridy = 4;
434         gridBagConstraints.gridheight = 2;
435         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
436         gridBagConstraints.weightx = 1.0;
437         gridBagConstraints.weighty = 1.0;
438         add(jScrollPane1, gridBagConstraints);
439
440         org.openide.awt.Mnemonics.setLocalizedText(add, org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "BTN_TargetMappingPanel_add"));
441         add.addActionListener(this);
442
443         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
444         gridBagConstraints.gridx = 1;
445         gridBagConstraints.gridy = 4;
446         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
447         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
448         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 0);
449         add(add, gridBagConstraints);
450         add.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "ACSD_TargetMappingPanel_add"));
451
452         org.openide.awt.Mnemonics.setLocalizedText(remove, org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "BTN_TargetMappingPanel_remove"));
453         remove.addActionListener(this);
454
455         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
456         gridBagConstraints.gridx = 1;
457         gridBagConstraints.gridy = 5;
458         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
459         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
460         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 12, 0, 0);
461         add(remove, gridBagConstraints);
462         remove.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "ACSD_TargetMappingPanel_remove"));
463
464         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
465         gridBagConstraints.gridx = 0;
466         gridBagConstraints.gridy = 6;
467         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
468         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
469         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
470         add(remainder, gridBagConstraints);
471
472         targetsPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
473
474         buildLabel.setLabelFor(buildCombo);
475         org.openide.awt.Mnemonics.setLocalizedText(buildLabel, org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "LBL_TargetMappingPanel_jLabel2"));
476         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
477         gridBagConstraints.gridx = 0;
478         gridBagConstraints.gridy = 0;
479         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
480         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 6);
481         targetsPanel.add(buildLabel, gridBagConstraints);
482         buildLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "ACSD_TargetMappingPanel_jLabel2"));
483
484         cleanLabel.setLabelFor(cleanCombo);
485         org.openide.awt.Mnemonics.setLocalizedText(cleanLabel, org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "LBL_TargetMappingPanel_jLabel4"));
486         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
487         gridBagConstraints.gridx = 0;
488         gridBagConstraints.gridy = 1;
489         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
490         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 6);
491         targetsPanel.add(cleanLabel, gridBagConstraints);
492         cleanLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "ACSD_TargetMappingPanel_jLabel4"));
493
494         runLabel.setLabelFor(runCombo);
495         org.openide.awt.Mnemonics.setLocalizedText(runLabel, org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "LBL_TargetMappingPanel_jLabel5"));
496         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
497         gridBagConstraints.gridx = 0;
498         gridBagConstraints.gridy = 3;
499         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
500         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 6);
501         targetsPanel.add(runLabel, gridBagConstraints);
502         runLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "ACSD_TargetMappingPanel_jLabel5"));
503
504         javadocLabel.setLabelFor(javadocCombo);
505         org.openide.awt.Mnemonics.setLocalizedText(javadocLabel, org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "LBL_TargetMappingPanel_jLabel6"));
506         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
507         gridBagConstraints.gridx = 0;
508         gridBagConstraints.gridy = 2;
509         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
510         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 6);
511         targetsPanel.add(javadocLabel, gridBagConstraints);
512         javadocLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "ACSD_TargetMappingPanel_jLabel6"));
513
514         testLabel.setLabelFor(testCombo);
515         org.openide.awt.Mnemonics.setLocalizedText(testLabel, org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "LBL_TargetMappingPanel_jLabel7"));
516         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
517         gridBagConstraints.gridx = 0;
518         gridBagConstraints.gridy = 4;
519         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
520         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 6);
521         targetsPanel.add(testLabel, gridBagConstraints);
522         testLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TargetMappingPanel.class, "ACSD_TargetMappingPanel_jLabel7"));
523
524         buildCombo.setEditable(true);
525         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
526         gridBagConstraints.gridx = 1;
527         gridBagConstraints.gridy = 0;
528         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
529         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
530         gridBagConstraints.weightx = 1.0;
531         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
532         targetsPanel.add(buildCombo, gridBagConstraints);
533
534         cleanCombo.setEditable(true);
535         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
536         gridBagConstraints.gridx = 1;
537         gridBagConstraints.gridy = 1;
538         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
539         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
540         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
541         targetsPanel.add(cleanCombo, gridBagConstraints);
542
543         javadocCombo.setEditable(true);
544         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
545         gridBagConstraints.gridx = 1;
546         gridBagConstraints.gridy = 2;
547         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
548         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
549         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
550         targetsPanel.add(javadocCombo, gridBagConstraints);
551
552         runCombo.setEditable(true);
553         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
554         gridBagConstraints.gridx = 1;
555         gridBagConstraints.gridy = 3;
556         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
557         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
558         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
559         targetsPanel.add(runCombo, gridBagConstraints);
560
561         testCombo.setEditable(true);
562         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
563         gridBagConstraints.gridx = 1;
564         gridBagConstraints.gridy = 4;
565         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
566         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
567         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
568         targetsPanel.add(testCombo, gridBagConstraints);
569
570         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
571         gridBagConstraints.gridx = 0;
572         gridBagConstraints.gridy = 1;
573         gridBagConstraints.gridwidth = 2;
574         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
575         add(targetsPanel, gridBagConstraints);
576
577         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
578         gridBagConstraints.gridx = 0;
579         gridBagConstraints.gridy = 6;
580         gridBagConstraints.gridwidth = 2;
581         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
582         gridBagConstraints.weightx = 1.0;
583         gridBagConstraints.weighty = 1.0;
584         add(specialRemainder, gridBagConstraints);
585
586     }
587
588     // Code for dispatching events from components to event handlers.
589

590     public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
591         if (evt.getSource() == add) {
592             TargetMappingPanel.this.addActionPerformed(evt);
593         }
594         else if (evt.getSource() == remove) {
595             TargetMappingPanel.this.removeActionPerformed(evt);
596         }
597     }//GEN-END:initComponents
598

599     private void removeActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeActionPerformed
600
int index = customTargets.getSelectedRow();
601         if (index == -1) {
602             return;
603         }
604         custTargets.remove(index);
605         customTargetsModel.fireTableDataChanged();
606         dirtyCustom = true;
607         updateButtons();
608     }//GEN-LAST:event_removeActionPerformed
609

610     private void addActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addActionPerformed
611
FreeformProjectGenerator.CustomTarget ct = new FreeformProjectGenerator.CustomTarget();
612         ct.targets = new ArrayList JavaDoc<String JavaDoc>();
613         ct.script = antScript;
614         custTargets.add(ct);
615         customTargetsModel.fireTableDataChanged();
616         dirtyCustom = true;
617         updateButtons();
618     }//GEN-LAST:event_addActionPerformed
619

620     private void updateButtons() {
621         remove.setEnabled(custTargets.size() > 0);
622     }
623     
624     
625     ActionListener JavaDoc getCustomizerOkListener() {
626         return new ActionListener JavaDoc() {
627             public void actionPerformed(ActionEvent JavaDoc arg0) {
628                 List JavaDoc<FreeformProjectGenerator.TargetMapping> mapping = getMapping();
629                 if (dirtyRegular) {
630                     FreeformProjectGenerator.putTargetMappings(helper, mapping);
631                     FreeformProjectGenerator.putContextMenuAction(helper, mapping);
632                 }
633                 
634                 if (dirtyCustom) {
635                     List JavaDoc<FreeformProjectGenerator.CustomTarget> l = new ArrayList JavaDoc<FreeformProjectGenerator.CustomTarget>(custTargets);
636                     Iterator JavaDoc<FreeformProjectGenerator.CustomTarget> it = l.iterator();
637                     while (it.hasNext()) {
638                         FreeformProjectGenerator.CustomTarget ct = it.next();
639                         // ignore row if target was not set
640
if (ct.targets == null || ct.targets.size() == 0) {
641                             it.remove();
642                             continue;
643                         }
644                         if (ct.label == null || ct.label.length() == 0) {
645                             ct.label = ct.targets.get(0);
646                         }
647                     }
648                     FreeformProjectGenerator.putCustomContextMenuActions(helper, l);
649                 }
650             }
651         };
652     }
653     
654     
655     // Variables declaration - do not modify//GEN-BEGIN:variables
656
private javax.swing.JButton JavaDoc add;
657     private javax.swing.JLabel JavaDoc additionalTargetsLabel;
658     private javax.swing.JComboBox JavaDoc buildCombo;
659     private javax.swing.JLabel JavaDoc buildLabel;
660     private javax.swing.JComboBox JavaDoc cleanCombo;
661     private javax.swing.JLabel JavaDoc cleanLabel;
662     private javax.swing.JTable JavaDoc customTargets;
663     private javax.swing.JScrollPane JavaDoc jScrollPane1;
664     private javax.swing.JComboBox JavaDoc javadocCombo;
665     private javax.swing.JLabel JavaDoc javadocLabel;
666     private javax.swing.JLabel JavaDoc mainLabel;
667     private javax.swing.JPanel JavaDoc remainder;
668     private javax.swing.JButton JavaDoc remove;
669     private javax.swing.JComboBox JavaDoc runCombo;
670     private javax.swing.JLabel JavaDoc runLabel;
671     private javax.swing.JPanel JavaDoc specialRemainder;
672     private javax.swing.JPanel JavaDoc targetsPanel;
673     private javax.swing.JComboBox JavaDoc testCombo;
674     private javax.swing.JLabel JavaDoc testLabel;
675     // End of variables declaration//GEN-END:variables
676

677     private class CustomTargetsModel extends AbstractTableModel JavaDoc {
678         
679         public CustomTargetsModel() {
680         }
681         
682         public int getColumnCount() {
683             return 2;
684         }
685         
686         public String JavaDoc getColumnName(int column) {
687             switch (column) {
688                 case 0: return NbBundle.getMessage(TargetMappingPanel.class, "LBL_TargetMappingPanel_Target");
689                 default: return NbBundle.getMessage(TargetMappingPanel.class, "LBL_TargetMappingPanel_Label");
690             }
691         }
692         
693         public int getRowCount() {
694             return custTargets.size();
695         }
696         
697         public boolean isCellEditable(int row, int column) {
698             if (column == 0) {
699                 FreeformProjectGenerator.CustomTarget ct = getItem(row);
700                 if (ct.targets != null && ct.targets.size() > 1) {
701                     return false;
702                 }
703                 if ((antScript == null && ct.script == null) ||
704                         (antScript != null && antScript.equals(ct.script))) {
705                     return true;
706                 } else {
707                     return false;
708                 }
709             }
710             return true;
711         }
712         
713         public Class JavaDoc<?> getColumnClass(int column) {
714             switch (column) {
715                 case 0: return JComboBox JavaDoc.class;
716                 default: return String JavaDoc.class;
717             }
718         }
719         
720         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
721             if (columnIndex == 0) {
722                 if ((getItem(rowIndex) == null) || (getItem(rowIndex).targets == null)) {
723                     return ""; // NOI18N
724
} else {
725                     return TargetMappingPanel.getListAsString(getItem(rowIndex).targets);
726                 }
727             } else {
728                 return getItem(rowIndex).label;
729             }
730         }
731         
732         public void setValueAt(Object JavaDoc val, int rowIndex, int columnIndex) {
733             if (rowIndex >= custTargets.size ()) {
734                 return ;
735             }
736             FreeformProjectGenerator.CustomTarget ct = getItem(rowIndex);
737             String JavaDoc v = (String JavaDoc) val;
738             if (columnIndex == 0) {
739                 if (v.length() > 0) {
740                     ct.targets = Collections.singletonList(v);
741                 } else {
742                     ct.targets = null;
743                 }
744             } else {
745                 ct.label = v;
746             }
747             dirtyCustom = true;
748         }
749         
750     }
751     
752     // For UI testing purposes.
753
public static void main(String JavaDoc[] ignore) {
754         boolean[] adv = {false, true};
755         for (int j = 0; j < adv.length; j++) {
756             JDialog JavaDoc dlg = new JDialog JavaDoc((Frame JavaDoc) null, "advancedMode=" + adv[j], false); // NOI18N
757
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
758             TargetMappingPanel panel = new TargetMappingPanel(adv[j]);
759             panel.setTargetNames(new ArrayList JavaDoc<String JavaDoc>(Arrays.asList("build", "clean", "test")), true); // NOI18N
760
dlg.getContentPane().add(panel);
761             dlg.pack();
762             dlg.setSize(700, 500);
763             dlg.setVisible(true);
764         }
765     }
766
767 }
768
Popular Tags