1 19 20 package org.netbeans.modules.ant.freeform.ui; 21 22 import java.awt.Dialog ; 23 import java.awt.Dimension ; 24 import java.awt.event.ItemEvent ; 25 import java.awt.event.ItemListener ; 26 import java.io.IOException ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import java.util.StringTokenizer ; 30 import javax.swing.DefaultComboBoxModel ; 31 import javax.swing.JPanel ; 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 54 public final class UnboundTargetAlert extends JPanel { 55 56 private final FreeformProject project; 57 private final String command; 58 59 private final String label; 60 61 65 public UnboundTargetAlert(FreeformProject project, String command) { 66 this.project = project; 67 this.command = command; 68 label = NbBundle.getMessage(Actions.class, "CMD_" + command); 69 initComponents(); 70 listTargets(); 71 } 72 73 74 public static void main(String [] args) { 75 UnboundTargetAlert alert = new UnboundTargetAlert("Twiddle Project", new String [] {"targ1", "targ2", "targ3"}); boolean accepted = alert.displayAlert("BazzBuilder"); System.out.println("accepted=" + accepted + " value=" + alert.selectCombo.getSelectedItem()); System.exit(0); 79 } 80 private UnboundTargetAlert(String label, String [] targets) { 81 project = null; 82 command = null; 83 this.label = label; 84 initComponents(); 85 selectCombo.setModel(new DefaultComboBoxModel (targets)); 86 selectCombo.setSelectedItem(""); 87 } 88 89 92 private void listTargets() { 93 FileObject script = FreeformProjectGenerator.getAntScript(project.helper(), project.evaluator()); 94 if (script != null) { 95 List <String > targets = Util.getAntScriptTargetNames(script); 96 if (targets != null) { 97 selectCombo.setModel(new DefaultComboBoxModel (targets.toArray(new String [targets.size()]))); 98 selectCombo.setSelectedItem(""); 99 } 100 } 101 } 102 103 106 private boolean displayAlert(String projectDisplayName) { 107 String 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 () { 113 public void itemStateChanged(ItemEvent e) { 114 d.setValid(((String ) selectCombo.getSelectedItem()).trim().length() > 0); 115 } 116 }); 117 Dialog dlg = DialogDisplayer.getDefault().createDialog(d); 118 selectCombo.requestFocusInWindow(); 119 Dimension 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 132 public boolean accepted() throws IOException { 133 String projectDisplayName = ProjectUtils.getInformation(project).getDisplayName(); 134 if (displayAlert(projectDisplayName)) { 135 try { 136 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void >() { 137 public Void run() throws IOException { 138 generateBindingAndAddContextMenuItem(); 139 ProjectManager.getDefault().saveProject(project); 140 return null; 141 } 142 }); 143 } catch (MutexException e) { 144 throw (IOException ) e.getException(); 145 } 146 return true; 147 } else { 148 return false; 149 } 150 } 151 152 156 void generateBindingAndAddContextMenuItem() { 157 List <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 ((String ) selectCombo.getSelectedItem()), String .class, true)); 162 mappings.add(mapping); 163 FreeformProjectGenerator.putTargetMappings(project.helper(), mappings); 164 FreeformProjectGenerator.putContextMenuAction(project.helper(), mappings); 165 } 166 167 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 169 170 introLabel = new javax.swing.JLabel (); 171 explanation = new javax.swing.JTextArea (); 172 selectLabel = new javax.swing.JLabel (); 173 selectCombo = new javax.swing.JComboBox (); 174 175 setLayout(new java.awt.GridBagLayout ()); 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 (); 179 gridBagConstraints.gridwidth = 2; 180 gridBagConstraints.insets = new java.awt.Insets (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 (); 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 (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 (); 199 gridBagConstraints.gridx = 0; 200 gridBagConstraints.gridy = 2; 201 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 6); 202 add(selectLabel, gridBagConstraints); 203 204 selectCombo.setEditable(true); 205 gridBagConstraints = new java.awt.GridBagConstraints (); 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 } 214 215 private javax.swing.JTextArea explanation; 217 private javax.swing.JLabel introLabel; 218 private javax.swing.JComboBox selectCombo; 219 private javax.swing.JLabel selectLabel; 220 222 223 void simulateTargetSelection(String comboText) { 224 selectCombo.setSelectedItem(comboText); 225 } 226 227 } 228 | Popular Tags |