KickJava   Java API By Example, From Geeks To Geeks.

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


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.Dialog JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.event.ItemEvent JavaDoc;
25 import java.awt.event.ItemListener JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import javax.swing.DefaultComboBoxModel JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import org.netbeans.api.project.ProjectManager;
33 import org.netbeans.api.project.ProjectUtils;
34 import org.netbeans.modules.ant.freeform.Actions;
35 import org.netbeans.modules.ant.freeform.FreeformProject;
36 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
37 import org.netbeans.modules.ant.freeform.Util;
38 import org.openide.DialogDescriptor;
39 import org.openide.DialogDisplayer;
40 import org.openide.NotifyDescriptor;
41 import org.openide.filesystems.FileObject;
42 import org.openide.util.Mutex;
43 import org.openide.util.MutexException;
44 import org.openide.util.NbBundle;
45 import org.openide.util.NbCollections;
46
47 /**
48  * Alert in case a common global action is invoked by the user but there is no binding.
49  * The user is prompted to pick one.
50  * @see "#46886"
51  * @see <a HREF="http://projects.netbeans.org/buildsys/freeform-project-ui-spec-promoe.html#Alert_Run_Project">UI Spec</a>
52  * @author Jesse Glick
53  */

54 public final class UnboundTargetAlert extends JPanel JavaDoc {
55     
56     private final FreeformProject project;
57     private final String JavaDoc command;
58     /** display label of the command */
59     private final String JavaDoc label;
60     
61     /**
62      * Create an alert.
63      * @param command an action as in {@link ActionProvider}
64      */

65     public UnboundTargetAlert(FreeformProject project, String JavaDoc command) {
66         this.project = project;
67         this.command = command;
68         label = NbBundle.getMessage(Actions.class, "CMD_" + command);
69         initComponents();
70         listTargets();
71     }
72     
73     /** Just for testing. */
74     public static void main(String JavaDoc[] args) {
75         UnboundTargetAlert alert = new UnboundTargetAlert("Twiddle Project", new String JavaDoc[] {"targ1", "targ2", "targ3"}); // NOI18N
76
boolean accepted = alert.displayAlert("BazzBuilder"); // NOI18N
77
System.out.println("accepted=" + accepted + " value=" + alert.selectCombo.getSelectedItem()); // NOI18N
78
System.exit(0);
79     }
80     private UnboundTargetAlert(String JavaDoc label, String JavaDoc[] targets) {
81         project = null;
82         command = null;
83         this.label = label;
84         initComponents();
85         selectCombo.setModel(new DefaultComboBoxModel JavaDoc(targets));
86         selectCombo.setSelectedItem("");
87     }
88     
89     /**
90      * Populate the combo box with (eligible) build targets.
91      */

92     private void listTargets() {
93         FileObject script = FreeformProjectGenerator.getAntScript(project.helper(), project.evaluator());
94         if (script != null) {
95             List JavaDoc<String JavaDoc> targets = Util.getAntScriptTargetNames(script);
96             if (targets != null) {
97                 selectCombo.setModel(new DefaultComboBoxModel JavaDoc(targets.toArray(new String JavaDoc[targets.size()])));
98                 selectCombo.setSelectedItem("");
99             }
100         }
101     }
102
103     /**
104      * Just show the dialog but do not do anything about it.
105      */

106     private boolean displayAlert(String JavaDoc projectDisplayName) {
107         String JavaDoc title = NbBundle.getMessage(UnboundTargetAlert.class, "UTA_TITLE", label, projectDisplayName);
108         final DialogDescriptor d = new DialogDescriptor(this, title);
109         d.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION);
110         d.setMessageType(NotifyDescriptor.ERROR_MESSAGE);
111         d.setValid(false);
112         selectCombo.addItemListener(new ItemListener JavaDoc() {
113             public void itemStateChanged(ItemEvent JavaDoc e) {
114                 d.setValid(((String JavaDoc) selectCombo.getSelectedItem()).trim().length() > 0);
115             }
116         });
117         Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(d);
118         selectCombo.requestFocusInWindow();
119         // XXX combo box gets cut off at the bottom unless you do something - why??
120
Dimension JavaDoc sz = dlg.getSize();
121         dlg.setSize(sz.width, sz.height + 30);
122         dlg.setVisible(true);
123         return d.getValue() == NotifyDescriptor.OK_OPTION;
124     }
125     
126     /**
127      * Show the alert.
128      * If accepted, generate a binding for the command (and add a context menu item for the project).
129      * @return true if the alert was accepted and there is now a binding, false if cancelled
130      * @throws IOException if there is a problem writing bindings
131      */

132     public boolean accepted() throws IOException JavaDoc {
133         String JavaDoc projectDisplayName = ProjectUtils.getInformation(project).getDisplayName();
134         if (displayAlert(projectDisplayName)) {
135             try {
136                 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void JavaDoc>() {
137                     public Void JavaDoc run() throws IOException JavaDoc {
138                         generateBindingAndAddContextMenuItem();
139                         ProjectManager.getDefault().saveProject(project);
140                         return null;
141                     }
142                 });
143             } catch (MutexException e) {
144                 throw (IOException JavaDoc) e.getException();
145             }
146             return true;
147         } else {
148             return false;
149         }
150     }
151     
152     /**
153      * Generate the action binding itself.
154      * Also add the context menu item for the new command.
155      */

156     void generateBindingAndAddContextMenuItem() {
157         List JavaDoc<FreeformProjectGenerator.TargetMapping> mappings = FreeformProjectGenerator.getTargetMappings(project.helper());
158         FreeformProjectGenerator.TargetMapping mapping = new FreeformProjectGenerator.TargetMapping();
159         mapping.name = command;
160         mapping.script = TargetMappingPanel.defaultAntScript(project.evaluator());
161         mapping.targets = Collections.list(NbCollections.checkedEnumerationByFilter(new StringTokenizer JavaDoc((String JavaDoc) selectCombo.getSelectedItem()), String JavaDoc.class, true));
162         mappings.add(mapping);
163         FreeformProjectGenerator.putTargetMappings(project.helper(), mappings);
164         FreeformProjectGenerator.putContextMenuAction(project.helper(), mappings);
165     }
166     
167     private void initComponents() {//GEN-BEGIN:initComponents
168
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
169
170         introLabel = new javax.swing.JLabel JavaDoc();
171         explanation = new javax.swing.JTextArea JavaDoc();
172         selectLabel = new javax.swing.JLabel JavaDoc();
173         selectCombo = new javax.swing.JComboBox JavaDoc();
174
175         setLayout(new java.awt.GridBagLayout JavaDoc());
176
177         org.openide.awt.Mnemonics.setLocalizedText(introLabel, org.openide.util.NbBundle.getMessage(UnboundTargetAlert.class, "UTA_LBL_intro", label));
178         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
179         gridBagConstraints.gridwidth = 2;
180         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
181         add(introLabel, gridBagConstraints);
182
183         explanation.setBackground(javax.swing.UIManager.getDefaults().getColor("Label.background"));
184         explanation.setEditable(false);
185         explanation.setLineWrap(true);
186         explanation.setText(org.openide.util.NbBundle.getMessage(UnboundTargetAlert.class, "UTA_TEXT_explanation", label));
187         explanation.setWrapStyleWord(true);
188         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
189         gridBagConstraints.gridx = 0;
190         gridBagConstraints.gridy = 1;
191         gridBagConstraints.gridwidth = 2;
192         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
193         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
194         add(explanation, gridBagConstraints);
195
196         selectLabel.setLabelFor(selectCombo);
197         org.openide.awt.Mnemonics.setLocalizedText(selectLabel, org.openide.util.NbBundle.getMessage(UnboundTargetAlert.class, "UTA_LBL_select", label));
198         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
199         gridBagConstraints.gridx = 0;
200         gridBagConstraints.gridy = 2;
201         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 6);
202         add(selectLabel, gridBagConstraints);
203
204         selectCombo.setEditable(true);
205         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
206         gridBagConstraints.gridx = 1;
207         gridBagConstraints.gridy = 2;
208         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
209         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
210         add(selectCombo, gridBagConstraints);
211
212     }//GEN-END:initComponents
213

214     
215     // Variables declaration - do not modify//GEN-BEGIN:variables
216
private javax.swing.JTextArea JavaDoc explanation;
217     private javax.swing.JLabel JavaDoc introLabel;
218     private javax.swing.JComboBox JavaDoc selectCombo;
219     private javax.swing.JLabel JavaDoc selectLabel;
220     // End of variables declaration//GEN-END:variables
221

222     /** For unit testing only. */
223     void simulateTargetSelection(String JavaDoc comboText) {
224         selectCombo.setSelectedItem(comboText);
225     }
226     
227 }
228
Popular Tags