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