1 19 20 package org.apache.tools.ant.module.nodes; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.text.Collator ; 26 import java.util.Collections ; 27 import java.util.List ; 28 import java.util.Properties ; 29 import java.util.Set ; 30 import java.util.SortedSet ; 31 import java.util.StringTokenizer ; 32 import java.util.TreeSet ; 33 import javax.swing.DefaultComboBoxModel ; 34 import javax.swing.JEditorPane ; 35 import javax.swing.KeyStroke ; 36 import javax.swing.text.EditorKit ; 37 import org.apache.tools.ant.module.AntSettings; 38 import org.apache.tools.ant.module.api.AntProjectCookie; 39 import org.apache.tools.ant.module.api.support.TargetLister; 40 import org.apache.tools.ant.module.run.TargetExecutor; 41 import org.openide.awt.Mnemonics; 42 import org.openide.filesystems.FileObject; 43 import org.openide.util.NbBundle; 44 import org.openide.util.NbCollections; 45 46 50 final class AdvancedActionPanel extends javax.swing.JPanel { 51 52 53 private static final String ATTR_TARGETS = "org.apache.tools.ant.module.preferredTargets"; 55 private static final String ATTR_PROPERTIES = "org.apache.tools.ant.module.preferredProperties"; 57 private static final String ATTR_VERBOSITY = "org.apache.tools.ant.module.preferredVerbosity"; 59 private static final String [] VERBOSITIES = { 60 63 NbBundle.getMessage(AdvancedActionPanel.class, "LBL_verbosity_warn"), 64 NbBundle.getMessage(AdvancedActionPanel.class, "LBL_verbosity_info"), 65 NbBundle.getMessage(AdvancedActionPanel.class, "LBL_verbosity_verbose"), 66 NbBundle.getMessage(AdvancedActionPanel.class, "LBL_verbosity_debug"), 67 }; 68 private static final int[] VERBOSITY_LEVELS = { 69 1 , 71 2 , 72 3 , 73 4 , 74 }; 75 76 private final AntProjectCookie project; 77 private final Set <TargetLister.Target> allTargets; 78 private String defaultTarget = null; 79 80 public AdvancedActionPanel(AntProjectCookie project, Set <TargetLister.Target> allTargets) { 81 this.project = project; 82 this.allTargets = allTargets; 83 initComponents(); 84 Mnemonics.setLocalizedText(targetLabel, NbBundle.getMessage(AdvancedActionPanel.class, "AdvancedActionsPanel.targetLabel.text")); 85 Mnemonics.setLocalizedText(targetDescriptionLabel, NbBundle.getMessage(AdvancedActionPanel.class, "AdvancedActionsPanel.targetDescriptionLabel.text")); 86 Mnemonics.setLocalizedText(propertiesLabel, NbBundle.getMessage(AdvancedActionPanel.class, "AdvancedActionsPanel.propertiesLabel.text")); 87 Mnemonics.setLocalizedText(verbosityLabel, NbBundle.getMessage(AdvancedActionPanel.class, "AdvancedActionsPanel.verbosityLabel.text")); 88 EditorKit kit = propertiesPane.getEditorKit(); 91 String clazz = kit.getClass().getName(); 92 if (clazz.equals("javax.swing.text.DefaultEditorKit") || clazz.equals("javax.swing.JEditorPane$PlainEditorKit")) { propertiesPane.setEditorKit(JEditorPane.createEditorKitForContentType("text/plain")); } 96 targetComboBox.getInputMap().remove(KeyStroke.getKeyStroke("ENTER")); initializeFields(); 99 } 100 101 private void initializeFields() { 102 FileObject script = project.getFileObject(); 103 assert script != null : "No file found for " + project; 104 String initialTargets = (String ) script.getAttribute(ATTR_TARGETS); 105 SortedSet <String > relevantTargets = new TreeSet <String >(Collator.getInstance()); 106 for (TargetLister.Target target : allTargets) { 107 if (!target.isOverridden() && !target.isInternal()) { 108 relevantTargets.add(target.getName()); 109 if (defaultTarget == null && target.isDefault()) { 110 defaultTarget = target.getName(); 111 } 112 } 113 } 114 targetComboBox.setModel(new DefaultComboBoxModel (relevantTargets.toArray())); 115 if (initialTargets != null) { 116 targetComboBox.setSelectedItem(initialTargets); 117 } else { 118 targetComboBox.setSelectedItem(defaultTarget); 119 } 120 targetComboBoxActionPerformed(null); 122 String initialProperties = (String ) script.getAttribute(ATTR_PROPERTIES); 123 if (initialProperties == null) { 124 Properties props = new Properties (); 125 props.putAll(AntSettings.getProperties()); 126 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 127 try { 128 props.store(baos, null); 129 String text = baos.toString("ISO-8859-1"); initialProperties = text.replaceFirst("^#.*\n", ""); } catch (IOException e) { 133 assert false : e; 134 } 135 } 136 propertiesPane.setText(initialProperties); 137 Integer verbosity = (Integer ) script.getAttribute(ATTR_VERBOSITY); 138 if (verbosity == null) { 139 verbosity = AntSettings.getVerbosity(); 140 } 141 verbosityComboBox.setModel(new DefaultComboBoxModel (VERBOSITIES)); 142 for (int i = 0; i < VERBOSITY_LEVELS.length; i++) { 143 if (VERBOSITY_LEVELS[i] == verbosity) { 144 verbosityComboBox.setSelectedItem(VERBOSITIES[i]); 145 break; 146 } 147 } 148 } 149 150 155 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 157 158 targetLabel = new javax.swing.JLabel (); 159 targetComboBox = new javax.swing.JComboBox (); 160 targetDescriptionLabel = new javax.swing.JLabel (); 161 targetDescriptionField = new javax.swing.JTextField (); 162 propertiesLabel = new javax.swing.JLabel (); 163 propertiesScrollPane = new javax.swing.JScrollPane (); 164 propertiesPane = new javax.swing.JEditorPane (); 165 verbosityLabel = new javax.swing.JLabel (); 166 verbosityComboBox = new javax.swing.JComboBox (); 167 168 setLayout(new java.awt.GridBagLayout ()); 169 170 targetLabel.setText("Select target(s) to run:"); 171 targetLabel.setLabelFor(targetComboBox); 172 gridBagConstraints = new java.awt.GridBagConstraints (); 173 gridBagConstraints.insets = new java.awt.Insets (5, 5, 5, 5); 174 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 175 add(targetLabel, gridBagConstraints); 176 177 targetComboBox.setEditable(true); 178 targetComboBox.setModel(new javax.swing.DefaultComboBoxModel (new String [] { "sampleTarget1", "sampleTarget2", "sampleTarget3" })); 179 targetComboBox.addActionListener(new java.awt.event.ActionListener () { 180 public void actionPerformed(java.awt.event.ActionEvent evt) { 181 targetComboBoxActionPerformed(evt); 182 } 183 }); 184 185 gridBagConstraints = new java.awt.GridBagConstraints (); 186 gridBagConstraints.insets = new java.awt.Insets (5, 5, 5, 5); 187 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 188 add(targetComboBox, gridBagConstraints); 189 190 targetDescriptionLabel.setText("Target description:"); 191 gridBagConstraints = new java.awt.GridBagConstraints (); 192 gridBagConstraints.gridx = 0; 193 gridBagConstraints.gridy = 1; 194 gridBagConstraints.insets = new java.awt.Insets (5, 5, 5, 5); 195 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 196 add(targetDescriptionLabel, gridBagConstraints); 197 198 targetDescriptionField.setEditable(false); 199 targetDescriptionField.setText("Sample description here."); 200 gridBagConstraints = new java.awt.GridBagConstraints (); 201 gridBagConstraints.gridx = 1; 202 gridBagConstraints.gridy = 1; 203 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 204 gridBagConstraints.insets = new java.awt.Insets (5, 5, 5, 5); 205 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 206 add(targetDescriptionField, gridBagConstraints); 207 208 propertiesLabel.setText("Special Ant properties:"); 209 propertiesLabel.setLabelFor(propertiesPane); 210 gridBagConstraints = new java.awt.GridBagConstraints (); 211 gridBagConstraints.gridx = 0; 212 gridBagConstraints.gridy = 2; 213 gridBagConstraints.insets = new java.awt.Insets (5, 5, 5, 5); 214 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST; 215 add(propertiesLabel, gridBagConstraints); 216 217 propertiesScrollPane.setPreferredSize(new java.awt.Dimension (400, 150)); 218 propertiesScrollPane.setMinimumSize(new java.awt.Dimension (400, 150)); 219 propertiesPane.setText("# This is sample text for GUI design.\nsomeprop1=someval1\nsomeprop2=someval2\nsomeprop3=someval3\n"); 220 propertiesPane.setContentType("text/x-properties"); 221 propertiesScrollPane.setViewportView(propertiesPane); 222 223 gridBagConstraints = new java.awt.GridBagConstraints (); 224 gridBagConstraints.gridx = 1; 225 gridBagConstraints.gridy = 2; 226 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 227 gridBagConstraints.insets = new java.awt.Insets (5, 5, 5, 5); 228 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 229 add(propertiesScrollPane, gridBagConstraints); 230 231 verbosityLabel.setText("Verbosity level:"); 232 verbosityLabel.setLabelFor(verbosityComboBox); 233 gridBagConstraints = new java.awt.GridBagConstraints (); 234 gridBagConstraints.gridx = 0; 235 gridBagConstraints.gridy = 3; 236 gridBagConstraints.insets = new java.awt.Insets (5, 5, 5, 5); 237 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 238 add(verbosityLabel, gridBagConstraints); 239 240 verbosityComboBox.setModel(new javax.swing.DefaultComboBoxModel (new String [] { "Errors only [SAMPLE]", "Normal [SAMPLE]", "Verbose [SAMPLE]" })); 241 gridBagConstraints = new java.awt.GridBagConstraints (); 242 gridBagConstraints.gridx = 1; 243 gridBagConstraints.gridy = 3; 244 gridBagConstraints.insets = new java.awt.Insets (5, 5, 5, 5); 245 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 246 add(verbosityComboBox, gridBagConstraints); 247 248 } 250 private void targetComboBoxActionPerformed(java.awt.event.ActionEvent evt) { String selection = (String ) targetComboBox.getSelectedItem(); 252 if (selection == null) { 253 selection = ""; 255 } 256 StringTokenizer tok = new StringTokenizer (selection, " ,"); List <String > targetsL = Collections.list(NbCollections.checkedEnumerationByFilter(tok, String .class, true)); 258 String description = ""; 259 if (targetsL.size() == 1) { 260 String targetName = targetsL.get(0); 261 for (TargetLister.Target target : allTargets) { 262 if (!target.isOverridden() && target.getName().equals(targetName)) { 263 description = target.getElement().getAttribute("description"); break; 266 } 267 } 268 } 269 targetDescriptionField.setText(description); 270 } 272 273 private javax.swing.JLabel propertiesLabel; 275 private javax.swing.JEditorPane propertiesPane; 276 private javax.swing.JScrollPane propertiesScrollPane; 277 private javax.swing.JComboBox targetComboBox; 278 private javax.swing.JTextField targetDescriptionField; 279 private javax.swing.JLabel targetDescriptionLabel; 280 private javax.swing.JLabel targetLabel; 281 private javax.swing.JComboBox verbosityComboBox; 282 private javax.swing.JLabel verbosityLabel; 283 285 288 public void run() throws IOException { 289 String selection = (String ) targetComboBox.getSelectedItem(); 291 String [] targets = null; if (selection != null) { 293 StringTokenizer tok = new StringTokenizer (selection, " ,"); List <String > targetsL = Collections.list(NbCollections.checkedEnumerationByFilter(tok, String .class, true)); 295 if (!targetsL.isEmpty()) { 296 targets = targetsL.toArray(new String [targetsL.size()]); 297 } 298 } 299 Properties props = new Properties (); 300 ByteArrayInputStream bais = new ByteArrayInputStream (propertiesPane.getText().getBytes("ISO-8859-1")); 301 props.load(bais); 302 int verbosity = 2; 303 String verbosityString = (String ) verbosityComboBox.getSelectedItem(); 304 for (int i = 0; i < VERBOSITIES.length; i++) { 305 if (VERBOSITIES[i].equals(verbosityString)) { 306 verbosity = VERBOSITY_LEVELS[i]; 307 break; 308 } 309 } 310 FileObject script = project.getFileObject(); 313 assert script != null; 314 if (targets == null || (targets.length == 1 && targets[0].equals(defaultTarget))) { 315 script.setAttribute(ATTR_TARGETS, null); 316 } else { 317 StringBuffer targetsSpaceSep = new StringBuffer (); 318 for (int i = 0; i < targets.length; i++) { 319 if (i > 0) { 320 targetsSpaceSep.append(' '); 321 } 322 targetsSpaceSep.append(targets[i]); 323 } 324 script.setAttribute(ATTR_TARGETS, targetsSpaceSep.toString()); 325 } 326 if (props.equals(AntSettings.getProperties())) { 327 script.setAttribute(ATTR_PROPERTIES, null); 328 } else { 329 script.setAttribute(ATTR_PROPERTIES, propertiesPane.getText()); 330 } 331 if (verbosity == AntSettings.getVerbosity()) { 332 script.setAttribute(ATTR_VERBOSITY, null); 333 } else { 334 script.setAttribute(ATTR_VERBOSITY, verbosity); 335 } 336 TargetExecutor exec = new TargetExecutor(project, targets); 338 exec.setProperties(NbCollections.checkedMapByCopy(props, String .class, String .class, true)); 339 exec.setVerbosity(verbosity); 340 exec.execute(); 341 } 342 343 } 344 | Popular Tags |