001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.ant.freeform.ui;
043:
044: import java.awt.Frame;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.util.ArrayList;
048: import java.util.Arrays;
049: import java.util.Collections;
050: import java.util.Iterator;
051: import java.util.List;
052: import java.util.StringTokenizer;
053: import java.util.regex.Matcher;
054: import java.util.regex.Pattern;
055: import javax.swing.ComboBoxModel;
056: import javax.swing.DefaultCellEditor;
057: import javax.swing.JComboBox;
058: import javax.swing.JDialog;
059: import javax.swing.JLabel;
060: import javax.swing.JPanel;
061: import javax.swing.WindowConstants;
062: import javax.swing.table.AbstractTableModel;
063: import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
064: import org.netbeans.modules.ant.freeform.Util;
065: import org.netbeans.modules.ant.freeform.spi.ProjectConstants;
066: import org.netbeans.modules.ant.freeform.spi.TargetDescriptor;
067: import org.netbeans.spi.project.support.ant.AntProjectHelper;
068: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
069: import org.openide.awt.Mnemonics;
070: import org.openide.filesystems.FileObject;
071: import org.openide.util.HelpCtx;
072: import org.openide.util.NbBundle;
073:
074: /**
075: * @author David Konecny
076: */
077: public class TargetMappingPanel extends JPanel implements
078: ActionListener, HelpCtx.Provider {
079:
080: public static final String BUILD_ACTION = "build"; // NOI18N
081: public static final String CLEAN_ACTION = "clean"; // NOI18N
082: public static final String REBUILD_ACTION = "rebuild"; // NOI18N
083: public static final String JAVADOC_ACTION = "javadoc"; // NOI18N
084: public static final String RUN_ACTION = "run"; // NOI18N
085: public static final String TEST_ACTION = "test"; // NOI18N
086:
087: private static final List<String> DEFAULT_BUILD_TARGETS = Arrays
088: .asList("build", "compile", "jar", "dist", "all", ".*jar.*"); // NOI18N
089: private static final List<String> DEFAULT_CLEAN_TARGETS = Arrays
090: .asList("clean", ".*clean.*"); // NOI18N
091: //private static final List<String> DEFAULT_REBUILD_TARGETS = Arrays.asList("rebuild", ".*rebuild.*"); // NOI18N
092: private static final List<String> DEFAULT_JAVADOC_TARGETS = Arrays
093: .asList("javadoc", "javadocs", "docs", "doc",
094: ".*javadoc.*", ".*doc.*"); // NOI18N
095: private static final List<String> DEFAULT_RUN_TARGETS = Arrays
096: .asList("run", "start", ".*run.*", ".*start.*"); // NOI18N
097: private static final List<String> DEFAULT_TEST_TARGETS = Arrays
098: .asList("test", ".*test.*"); // NOI18N
099:
100: private List<String> targetNames;
101: private List<FreeformProjectGenerator.TargetMapping> targetMappings;
102: private List<FreeformProjectGenerator.CustomTarget> custTargets;
103: private CustomTargetsModel customTargetsModel;
104: private String antScript;
105:
106: private List<JComboBox> combos = new ArrayList<JComboBox>();
107: private List<TargetDescriptor> targetDescs = new ArrayList<TargetDescriptor>();
108:
109: /** Any change in standard tasks which needs to be persisted? */
110: private boolean dirtyRegular;
111:
112: /** Any change in custom tasks which needs to be persisted? */
113: private boolean dirtyCustom;
114:
115: private AntProjectHelper helper;
116:
117: public TargetMappingPanel(boolean advancedPart) {
118: this (new ArrayList<TargetDescriptor>(), advancedPart);
119: }
120:
121: public TargetMappingPanel(List<TargetDescriptor> extraTargets,
122: boolean advancedPart) {
123: initComponents();
124: targetMappings = new ArrayList<FreeformProjectGenerator.TargetMapping>();
125:
126: custTargets = new ArrayList<FreeformProjectGenerator.CustomTarget>();
127: customTargetsModel = new CustomTargetsModel();
128: customTargets.setModel(customTargetsModel);
129: customTargets.getTableHeader().setReorderingAllowed(false);
130:
131: addTargets(extraTargets);
132: showAdvancedPart(advancedPart);
133: }
134:
135: public TargetMappingPanel(List<TargetDescriptor> extraTargets,
136: PropertyEvaluator evaluator, AntProjectHelper helper) {
137: this (extraTargets, true);
138: this .helper = helper;
139: FileObject as = FreeformProjectGenerator.getAntScript(helper,
140: evaluator);
141: List<String> l = null;
142: // #50933 - script can be null
143: if (as != null) {
144: l = Util.getAntScriptTargetNames(as);
145: }
146: if (l != null) {
147: setTargetNames(l, false);
148: initAntTargetEditor(l);
149: }
150: antScript = defaultAntScript(evaluator);
151: initMappings(
152: FreeformProjectGenerator.getTargetMappings(helper),
153: antScript);
154:
155: custTargets = FreeformProjectGenerator
156: .getCustomContextMenuActions(helper);
157: customTargetsModel.fireTableDataChanged();
158:
159: updateButtons();
160: }
161:
162: /**
163: * Get the default name of a project's Ant script, as for project.xml usage.
164: * @param evaluator a property evaluator for the project
165: * @return the script name to use, or null for the default
166: * @see FreeformProjectGenerator.TargetMapping#script
167: */
168: static String defaultAntScript(PropertyEvaluator evaluator) {
169: String antScript = evaluator
170: .getProperty(ProjectConstants.PROP_ANT_SCRIPT);
171: if (antScript == null) {
172: // Default, i.e. build.xml.
173: return null;
174: } else {
175: // Set to something specific; refer to it symbolically.
176: return "${" + ProjectConstants.PROP_ANT_SCRIPT + "}"; // NOI18N
177: }
178: }
179:
180: private void addTargets(List<TargetDescriptor> extraTargets) {
181: combos.add(buildCombo);
182: targetDescs.add(new TargetDescriptor(BUILD_ACTION,
183: DEFAULT_BUILD_TARGETS, null, null));
184: combos.add(cleanCombo);
185: targetDescs.add(new TargetDescriptor(CLEAN_ACTION,
186: DEFAULT_CLEAN_TARGETS, null, null));
187: combos.add(javadocCombo);
188: targetDescs.add(new TargetDescriptor(JAVADOC_ACTION,
189: DEFAULT_JAVADOC_TARGETS, null, null));
190: combos.add(runCombo);
191: targetDescs.add(new TargetDescriptor(RUN_ACTION,
192: DEFAULT_RUN_TARGETS, null, null));
193: combos.add(testCombo);
194: targetDescs.add(new TargetDescriptor(TEST_ACTION,
195: DEFAULT_TEST_TARGETS, null, null));
196: int y = 5;
197: for (TargetDescriptor desc : extraTargets) {
198: targetDescs.add(desc);
199:
200: JComboBox combo = new JComboBox();
201: combo.setEditable(true);
202: java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
203: gridBagConstraints.gridx = 1;
204: gridBagConstraints.gridy = y;
205: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
206: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
207: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
208: targetsPanel.add(combo, gridBagConstraints);
209:
210: JLabel label = new JLabel();
211: label.setLabelFor(combo);
212: Mnemonics.setLocalizedText(label, desc.getIDEActionLabel());
213: gridBagConstraints = new java.awt.GridBagConstraints();
214: gridBagConstraints.gridx = 0;
215: gridBagConstraints.gridy = y;
216: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
217: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 6);
218: targetsPanel.add(label, gridBagConstraints);
219: label.getAccessibleContext().setAccessibleDescription(
220: desc.getAccessibleLabel());
221:
222: combos.add(combo);
223: y++;
224: }
225: }
226:
227: public HelpCtx getHelpCtx() {
228: return new HelpCtx(TargetMappingPanel.class);
229: }
230:
231: private void showAdvancedPart(boolean show) {
232: additionalTargetsLabel.setVisible(show);
233: jScrollPane1.setVisible(show);
234: customTargets.setVisible(show);
235: add.setVisible(show);
236: remove.setVisible(show);
237: // handle panel resizing:
238: remainder.setVisible(show);
239: specialRemainder.setVisible(!show);
240: }
241:
242: private void initAntTargetEditor(List<String> targets) {
243: JComboBox combo = new JComboBox();
244: combo.setEditable(true);
245: for (String target : targets) {
246: combo.addItem(target);
247: }
248: customTargets.setDefaultEditor(JComboBox.class,
249: new DefaultCellEditor(combo));
250: }
251:
252: private FreeformProjectGenerator.CustomTarget getItem(int index) {
253: return custTargets.get(index);
254: }
255:
256: public void setTargetNames(List<String> list, boolean selectDefaults) {
257: targetNames = list;
258: targetNames.add(0, ""); // NOI18N
259: updateCombos(selectDefaults);
260: }
261:
262: public void setScript(String script) {
263: this .antScript = script;
264: for (FreeformProjectGenerator.TargetMapping tm : targetMappings) {
265: tm.script = script;
266: }
267: }
268:
269: private void updateCombos(boolean selectDefaults) {
270: // In case you go back and choose a different script:
271: Iterator<TargetDescriptor> descriptors = targetDescs.iterator();
272: for (JComboBox combo : combos) {
273: TargetDescriptor desc = descriptors.next();
274: combo.removeAllItems();
275: for (String name : targetNames) {
276: combo.addItem(name);
277: }
278: if (selectDefaults) {
279: selectItem(combo, desc.getDefaultTargets(), false); // NOI18N
280: }
281: }
282: }
283:
284: /**
285: * @param items concrete item to be selected (or added) or list of
286: * regular expressions to match
287: */
288: private void selectItem(JComboBox combo, List<String> items,
289: boolean add) {
290: ComboBoxModel model = combo.getModel();
291: for (String item : items) {
292: Pattern pattern = Pattern.compile(item);
293: for (int i = 0; i < model.getSize(); i++) {
294: String target = (String) model.getElementAt(i);
295: Matcher matcher = pattern.matcher(target);
296: if (matcher.matches()) {
297: model.setSelectedItem(target);
298: return;
299: }
300: }
301: }
302: if (add) {
303: assert items.size() == 1 : "There should be only one item in this case"; // NOI18N
304: combo.addItem(items.get(0));
305: model.setSelectedItem(items.get(0));
306: } else {
307: model.setSelectedItem(""); // NOI18N
308: }
309: }
310:
311: private void initMappings(
312: List<FreeformProjectGenerator.TargetMapping> list,
313: String antScript) {
314: for (FreeformProjectGenerator.TargetMapping tm : list) {
315: Iterator<JComboBox> combosIt = combos.iterator();
316: for (TargetDescriptor desc : targetDescs) {
317: JComboBox combo = combosIt.next();
318: if (tm.name.equals(desc.getIDEActionName())) {
319: selectItem(
320: combo,
321: Collections
322: .singletonList(getListAsString(tm.targets)),
323: true);
324: checkAntScript(combo, antScript, tm.script);
325: }
326: }
327: }
328: targetMappings = list;
329: }
330:
331: private void checkAntScript(JComboBox combo, String antScript,
332: String targetScript) {
333: if ((antScript == null && targetScript == null)
334: || (antScript != null && antScript.equals(targetScript))) {
335: combo.setEnabled(true);
336: } else {
337: combo.setEnabled(false);
338: }
339: }
340:
341: private static String getListAsString(List<String> list) {
342: assert list != null;
343: StringBuffer sb = new StringBuffer();
344: Iterator<String> it = list.iterator();
345: while (it.hasNext()) {
346: sb.append(it.next());
347: if (it.hasNext()) {
348: sb.append(" "); // NOI18N
349: }
350: }
351: return sb.toString();
352: }
353:
354: static List<String> getStringAsList(String str) {
355: List<String> l = new ArrayList<String>(2);
356: StringTokenizer tok = new StringTokenizer(str, " "); // NOI18N
357: while (tok.hasMoreTokens()) {
358: String target = tok.nextToken().trim();
359: if (target.length() == 0) {
360: continue;
361: }
362: l.add(target);
363: }
364: return l;
365: }
366:
367: private void storeTarget(String key, JComboBox combo) {
368: if (combo.getModel().getSelectedItem() == null
369: || ((String) combo.getModel().getSelectedItem())
370: .length() == 0) {
371: removeTargetMapping(key);
372: return;
373: }
374: FreeformProjectGenerator.TargetMapping tm = getTargetMapping(key);
375: String value = (String) combo.getModel().getSelectedItem();
376: List<String> l = getStringAsList(value);
377: if (!l.equals(tm.targets)) {
378: dirtyRegular = true;
379: }
380: tm.targets = l;
381: return;
382: }
383:
384: private FreeformProjectGenerator.TargetMapping getTargetMapping(
385: String key) {
386: for (FreeformProjectGenerator.TargetMapping tm : targetMappings) {
387: if (tm.name.equals(key)) {
388: return tm;
389: }
390: }
391: FreeformProjectGenerator.TargetMapping tm = new FreeformProjectGenerator.TargetMapping();
392: tm.name = key;
393: tm.script = antScript;
394: targetMappings.add(tm);
395: dirtyRegular = true;
396: return tm;
397: }
398:
399: private void removeTargetMapping(String key) {
400: Iterator<FreeformProjectGenerator.TargetMapping> it = targetMappings
401: .iterator();
402: while (it.hasNext()) {
403: if (it.next().name.equals(key)) {
404: it.remove();
405: dirtyRegular = true;
406: return;
407: }
408: }
409: }
410:
411: public List<FreeformProjectGenerator.TargetMapping> getMapping() {
412: Iterator<JComboBox> combosIt = combos.iterator();
413: for (TargetDescriptor desc : targetDescs) {
414: JComboBox combo = combosIt.next();
415: storeTarget(desc.getIDEActionName(), combo);
416: }
417: // update rebuilt:
418: if (cleanCombo.getModel().getSelectedItem() != null
419: && ((String) cleanCombo.getModel().getSelectedItem())
420: .length() > 0
421: && buildCombo.getModel().getSelectedItem() != null
422: && ((String) buildCombo.getModel().getSelectedItem())
423: .length() > 0) {
424: FreeformProjectGenerator.TargetMapping tm = getTargetMapping(REBUILD_ACTION);
425: String val = (String) cleanCombo.getModel()
426: .getSelectedItem()
427: + " "
428: + (String) buildCombo.getModel().getSelectedItem(); // NOI18N
429: tm.targets = getStringAsList(val);
430: } else {
431: removeTargetMapping(REBUILD_ACTION);
432: }
433: return targetMappings;
434: }
435:
436: /** This method is called from within the constructor to
437: * initialize the form.
438: * WARNING: Do NOT modify this code. The content of this method is
439: * always regenerated by the Form Editor.
440: */
441: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
442: private void initComponents() {
443: java.awt.GridBagConstraints gridBagConstraints;
444:
445: additionalTargetsLabel = new javax.swing.JLabel();
446: jScrollPane1 = new javax.swing.JScrollPane();
447: customTargets = new javax.swing.JTable();
448: add = new javax.swing.JButton();
449: remove = new javax.swing.JButton();
450: jLabel1 = new javax.swing.JLabel();
451: remainder = new javax.swing.JPanel();
452: targetsPanel = new javax.swing.JPanel();
453: buildLabel = new javax.swing.JLabel();
454: cleanLabel = new javax.swing.JLabel();
455: runLabel = new javax.swing.JLabel();
456: javadocLabel = new javax.swing.JLabel();
457: testLabel = new javax.swing.JLabel();
458: buildCombo = new javax.swing.JComboBox();
459: cleanCombo = new javax.swing.JComboBox();
460: javadocCombo = new javax.swing.JComboBox();
461: runCombo = new javax.swing.JComboBox();
462: testCombo = new javax.swing.JComboBox();
463: specialRemainder = new javax.swing.JPanel();
464:
465: setPreferredSize(new java.awt.Dimension(300, 280));
466: setLayout(new java.awt.GridBagLayout());
467:
468: additionalTargetsLabel.setLabelFor(customTargets);
469: org.openide.awt.Mnemonics.setLocalizedText(
470: additionalTargetsLabel, org.openide.util.NbBundle
471: .getMessage(TargetMappingPanel.class,
472: "LBL_TargetMappingPanel_jLabel10")); // NOI18N
473: gridBagConstraints = new java.awt.GridBagConstraints();
474: gridBagConstraints.gridx = 0;
475: gridBagConstraints.gridy = 3;
476: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
477: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
478: gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
479: add(additionalTargetsLabel, gridBagConstraints);
480: additionalTargetsLabel.getAccessibleContext()
481: .setAccessibleDescription(
482: org.openide.util.NbBundle.getMessage(
483: TargetMappingPanel.class,
484: "ACSD_TargetMappingPanel_jLabel10")); // NOI18N
485:
486: jScrollPane1.setViewportView(customTargets);
487:
488: gridBagConstraints = new java.awt.GridBagConstraints();
489: gridBagConstraints.gridx = 0;
490: gridBagConstraints.gridy = 4;
491: gridBagConstraints.gridheight = 2;
492: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
493: gridBagConstraints.weightx = 1.0;
494: gridBagConstraints.weighty = 1.0;
495: add(jScrollPane1, gridBagConstraints);
496:
497: org.openide.awt.Mnemonics.setLocalizedText(add,
498: org.openide.util.NbBundle.getMessage(
499: TargetMappingPanel.class,
500: "BTN_TargetMappingPanel_add")); // NOI18N
501: add.addActionListener(this );
502: gridBagConstraints = new java.awt.GridBagConstraints();
503: gridBagConstraints.gridx = 1;
504: gridBagConstraints.gridy = 4;
505: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
506: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
507: gridBagConstraints.insets = new java.awt.Insets(0, 8, 0, 0);
508: add(add, gridBagConstraints);
509: add.getAccessibleContext().setAccessibleDescription(
510: org.openide.util.NbBundle.getMessage(
511: TargetMappingPanel.class,
512: "ACSD_TargetMappingPanel_add")); // NOI18N
513:
514: org.openide.awt.Mnemonics.setLocalizedText(remove,
515: org.openide.util.NbBundle.getMessage(
516: TargetMappingPanel.class,
517: "BTN_TargetMappingPanel_remove")); // NOI18N
518: remove.addActionListener(this );
519: gridBagConstraints = new java.awt.GridBagConstraints();
520: gridBagConstraints.gridx = 1;
521: gridBagConstraints.gridy = 5;
522: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
523: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
524: gridBagConstraints.insets = new java.awt.Insets(6, 8, 0, 0);
525: add(remove, gridBagConstraints);
526: remove.getAccessibleContext().setAccessibleDescription(
527: org.openide.util.NbBundle.getMessage(
528: TargetMappingPanel.class,
529: "ACSD_TargetMappingPanel_remove")); // NOI18N
530:
531: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
532: org.openide.util.NbBundle.getMessage(
533: TargetMappingPanel.class,
534: "LBL_Mapping_Of_General_Actions")); // NOI18N
535: gridBagConstraints = new java.awt.GridBagConstraints();
536: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
537: add(jLabel1, gridBagConstraints);
538: gridBagConstraints = new java.awt.GridBagConstraints();
539: gridBagConstraints.gridx = 0;
540: gridBagConstraints.gridy = 6;
541: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
542: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
543: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
544: add(remainder, gridBagConstraints);
545:
546: targetsPanel.setLayout(new java.awt.GridBagLayout());
547:
548: buildLabel.setLabelFor(buildCombo);
549: org.openide.awt.Mnemonics.setLocalizedText(buildLabel,
550: org.openide.util.NbBundle.getMessage(
551: TargetMappingPanel.class,
552: "LBL_TargetMappingPanel_jLabel2")); // NOI18N
553: gridBagConstraints = new java.awt.GridBagConstraints();
554: gridBagConstraints.gridx = 0;
555: gridBagConstraints.gridy = 0;
556: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
557: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 6);
558: targetsPanel.add(buildLabel, gridBagConstraints);
559: buildLabel.getAccessibleContext().setAccessibleDescription(
560: org.openide.util.NbBundle.getMessage(
561: TargetMappingPanel.class,
562: "ACSD_TargetMappingPanel_jLabel2")); // NOI18N
563:
564: cleanLabel.setLabelFor(cleanCombo);
565: org.openide.awt.Mnemonics.setLocalizedText(cleanLabel,
566: org.openide.util.NbBundle.getMessage(
567: TargetMappingPanel.class,
568: "LBL_TargetMappingPanel_jLabel4")); // NOI18N
569: gridBagConstraints = new java.awt.GridBagConstraints();
570: gridBagConstraints.gridx = 0;
571: gridBagConstraints.gridy = 1;
572: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
573: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 6);
574: targetsPanel.add(cleanLabel, gridBagConstraints);
575: cleanLabel.getAccessibleContext().setAccessibleDescription(
576: org.openide.util.NbBundle.getMessage(
577: TargetMappingPanel.class,
578: "ACSD_TargetMappingPanel_jLabel4")); // NOI18N
579:
580: runLabel.setLabelFor(runCombo);
581: org.openide.awt.Mnemonics.setLocalizedText(runLabel,
582: org.openide.util.NbBundle.getMessage(
583: TargetMappingPanel.class,
584: "LBL_TargetMappingPanel_jLabel5")); // NOI18N
585: gridBagConstraints = new java.awt.GridBagConstraints();
586: gridBagConstraints.gridx = 0;
587: gridBagConstraints.gridy = 3;
588: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
589: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 6);
590: targetsPanel.add(runLabel, gridBagConstraints);
591: runLabel.getAccessibleContext().setAccessibleDescription(
592: org.openide.util.NbBundle.getMessage(
593: TargetMappingPanel.class,
594: "ACSD_TargetMappingPanel_jLabel5")); // NOI18N
595:
596: javadocLabel.setLabelFor(javadocCombo);
597: org.openide.awt.Mnemonics.setLocalizedText(javadocLabel,
598: org.openide.util.NbBundle.getMessage(
599: TargetMappingPanel.class,
600: "LBL_TargetMappingPanel_jLabel6")); // NOI18N
601: gridBagConstraints = new java.awt.GridBagConstraints();
602: gridBagConstraints.gridx = 0;
603: gridBagConstraints.gridy = 2;
604: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
605: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 6);
606: targetsPanel.add(javadocLabel, gridBagConstraints);
607: javadocLabel.getAccessibleContext().setAccessibleDescription(
608: org.openide.util.NbBundle.getMessage(
609: TargetMappingPanel.class,
610: "ACSD_TargetMappingPanel_jLabel6")); // NOI18N
611:
612: testLabel.setLabelFor(testCombo);
613: org.openide.awt.Mnemonics.setLocalizedText(testLabel,
614: org.openide.util.NbBundle.getMessage(
615: TargetMappingPanel.class,
616: "LBL_TargetMappingPanel_jLabel7")); // NOI18N
617: gridBagConstraints = new java.awt.GridBagConstraints();
618: gridBagConstraints.gridx = 0;
619: gridBagConstraints.gridy = 4;
620: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
621: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 6);
622: targetsPanel.add(testLabel, gridBagConstraints);
623: testLabel.getAccessibleContext().setAccessibleDescription(
624: org.openide.util.NbBundle.getMessage(
625: TargetMappingPanel.class,
626: "ACSD_TargetMappingPanel_jLabel7")); // NOI18N
627:
628: buildCombo.setEditable(true);
629: gridBagConstraints = new java.awt.GridBagConstraints();
630: gridBagConstraints.gridx = 1;
631: gridBagConstraints.gridy = 0;
632: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
633: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
634: gridBagConstraints.weightx = 1.0;
635: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
636: targetsPanel.add(buildCombo, gridBagConstraints);
637:
638: cleanCombo.setEditable(true);
639: gridBagConstraints = new java.awt.GridBagConstraints();
640: gridBagConstraints.gridx = 1;
641: gridBagConstraints.gridy = 1;
642: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
643: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
644: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
645: targetsPanel.add(cleanCombo, gridBagConstraints);
646:
647: javadocCombo.setEditable(true);
648: gridBagConstraints = new java.awt.GridBagConstraints();
649: gridBagConstraints.gridx = 1;
650: gridBagConstraints.gridy = 2;
651: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
652: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
653: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
654: targetsPanel.add(javadocCombo, gridBagConstraints);
655:
656: runCombo.setEditable(true);
657: gridBagConstraints = new java.awt.GridBagConstraints();
658: gridBagConstraints.gridx = 1;
659: gridBagConstraints.gridy = 3;
660: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
661: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
662: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
663: targetsPanel.add(runCombo, gridBagConstraints);
664:
665: testCombo.setEditable(true);
666: gridBagConstraints = new java.awt.GridBagConstraints();
667: gridBagConstraints.gridx = 1;
668: gridBagConstraints.gridy = 4;
669: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
670: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
671: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
672: targetsPanel.add(testCombo, gridBagConstraints);
673:
674: gridBagConstraints = new java.awt.GridBagConstraints();
675: gridBagConstraints.gridx = 0;
676: gridBagConstraints.gridy = 1;
677: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
678: gridBagConstraints.weightx = 1.0;
679: gridBagConstraints.insets = new java.awt.Insets(8, 0, 0, 0);
680: add(targetsPanel, gridBagConstraints);
681: gridBagConstraints = new java.awt.GridBagConstraints();
682: gridBagConstraints.gridx = 0;
683: gridBagConstraints.gridy = 6;
684: gridBagConstraints.gridwidth = 2;
685: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
686: gridBagConstraints.weightx = 1.0;
687: gridBagConstraints.weighty = 1.0;
688: add(specialRemainder, gridBagConstraints);
689: }
690:
691: // Code for dispatching events from components to event handlers.
692:
693: public void actionPerformed(java.awt.event.ActionEvent evt) {
694: if (evt.getSource() == add) {
695: TargetMappingPanel.this .addActionPerformed(evt);
696: } else if (evt.getSource() == remove) {
697: TargetMappingPanel.this .removeActionPerformed(evt);
698: }
699: }// </editor-fold>//GEN-END:initComponents
700:
701: private void removeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeActionPerformed
702: int index = customTargets.getSelectedRow();
703: if (index == -1) {
704: return;
705: }
706: custTargets.remove(index);
707: customTargetsModel.fireTableDataChanged();
708: dirtyCustom = true;
709: updateButtons();
710: }//GEN-LAST:event_removeActionPerformed
711:
712: private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
713: FreeformProjectGenerator.CustomTarget ct = new FreeformProjectGenerator.CustomTarget();
714: ct.targets = new ArrayList<String>();
715: ct.script = antScript;
716: custTargets.add(ct);
717: customTargetsModel.fireTableDataChanged();
718: dirtyCustom = true;
719: updateButtons();
720: }//GEN-LAST:event_addActionPerformed
721:
722: private void updateButtons() {
723: remove.setEnabled(custTargets.size() > 0);
724: }
725:
726: ActionListener getCustomizerOkListener() {
727: return new ActionListener() {
728: public void actionPerformed(ActionEvent arg0) {
729: List<FreeformProjectGenerator.TargetMapping> mapping = getMapping();
730: if (dirtyRegular) {
731: FreeformProjectGenerator.putTargetMappings(helper,
732: mapping);
733: FreeformProjectGenerator.putContextMenuAction(
734: helper, mapping);
735: }
736:
737: if (dirtyCustom) {
738: List<FreeformProjectGenerator.CustomTarget> l = new ArrayList<FreeformProjectGenerator.CustomTarget>(
739: custTargets);
740: Iterator<FreeformProjectGenerator.CustomTarget> it = l
741: .iterator();
742: while (it.hasNext()) {
743: FreeformProjectGenerator.CustomTarget ct = it
744: .next();
745: // ignore row if target was not set
746: if (ct.targets == null
747: || ct.targets.size() == 0) {
748: it.remove();
749: continue;
750: }
751: if (ct.label == null || ct.label.length() == 0) {
752: ct.label = ct.targets.get(0);
753: }
754: }
755: FreeformProjectGenerator
756: .putCustomContextMenuActions(helper, l);
757: }
758: }
759: };
760: }
761:
762: // Variables declaration - do not modify//GEN-BEGIN:variables
763: private javax.swing.JButton add;
764: private javax.swing.JLabel additionalTargetsLabel;
765: private javax.swing.JComboBox buildCombo;
766: private javax.swing.JLabel buildLabel;
767: private javax.swing.JComboBox cleanCombo;
768: private javax.swing.JLabel cleanLabel;
769: private javax.swing.JTable customTargets;
770: private javax.swing.JLabel jLabel1;
771: private javax.swing.JScrollPane jScrollPane1;
772: private javax.swing.JComboBox javadocCombo;
773: private javax.swing.JLabel javadocLabel;
774: private javax.swing.JPanel remainder;
775: private javax.swing.JButton remove;
776: private javax.swing.JComboBox runCombo;
777: private javax.swing.JLabel runLabel;
778: private javax.swing.JPanel specialRemainder;
779: private javax.swing.JPanel targetsPanel;
780: private javax.swing.JComboBox testCombo;
781: private javax.swing.JLabel testLabel;
782:
783: // End of variables declaration//GEN-END:variables
784:
785: private class CustomTargetsModel extends AbstractTableModel {
786:
787: public CustomTargetsModel() {
788: }
789:
790: public int getColumnCount() {
791: return 2;
792: }
793:
794: public String getColumnName(int column) {
795: switch (column) {
796: case 0:
797: return NbBundle.getMessage(TargetMappingPanel.class,
798: "LBL_TargetMappingPanel_Label");
799: default:
800: return NbBundle.getMessage(TargetMappingPanel.class,
801: "LBL_TargetMappingPanel_Target");
802: }
803: }
804:
805: public int getRowCount() {
806: return custTargets.size();
807: }
808:
809: public boolean isCellEditable(int row, int column) {
810: if (column == 1) {
811: FreeformProjectGenerator.CustomTarget ct = getItem(row);
812: if (ct.targets != null && ct.targets.size() > 1) {
813: return false;
814: }
815: if ((antScript == null && ct.script == null)
816: || (antScript != null && antScript
817: .equals(ct.script))) {
818: return true;
819: } else {
820: return false;
821: }
822: }
823: return true;
824: }
825:
826: public Class<?> getColumnClass(int column) {
827: switch (column) {
828: case 0:
829: return String.class;
830: default:
831: return JComboBox.class;
832: }
833: }
834:
835: public Object getValueAt(int rowIndex, int columnIndex) {
836: if (columnIndex == 1) {
837: if ((getItem(rowIndex) == null)
838: || (getItem(rowIndex).targets == null)) {
839: return ""; // NOI18N
840: } else {
841: return TargetMappingPanel
842: .getListAsString(getItem(rowIndex).targets);
843: }
844: } else {
845: return getItem(rowIndex).label;
846: }
847: }
848:
849: public void setValueAt(Object val, int rowIndex, int columnIndex) {
850: if (rowIndex >= custTargets.size()) {
851: return;
852: }
853: FreeformProjectGenerator.CustomTarget ct = getItem(rowIndex);
854: String v = (String) val;
855: if (columnIndex == 1) {
856: if (v.length() > 0) {
857: ct.targets = Collections.singletonList(v);
858: } else {
859: ct.targets = null;
860: }
861: } else {
862: ct.label = v;
863: }
864: dirtyCustom = true;
865: }
866:
867: }
868:
869: // For UI testing purposes.
870: public static void main(String[] ignore) {
871: boolean[] adv = { false, true };
872: for (int j = 0; j < adv.length; j++) {
873: JDialog dlg = new JDialog((Frame) null, "advancedMode="
874: + adv[j], false); // NOI18N
875: dlg
876: .setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
877: TargetMappingPanel panel = new TargetMappingPanel(adv[j]);
878: panel.setTargetNames(new ArrayList<String>(Arrays.asList(
879: "build", "clean", "test")), true); // NOI18N
880: dlg.getContentPane().add(panel);
881: dlg.pack();
882: dlg.setSize(700, 500);
883: dlg.setVisible(true);
884: }
885: }
886:
887: }
|