1 19 20 package org.netbeans.modules.java.freeform.ui; 21 22 import java.awt.event.ItemEvent ; 23 import java.io.File ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import javax.swing.DefaultListModel ; 27 import javax.swing.JFileChooser ; 28 import javax.swing.ListSelectionModel ; 29 import javax.swing.event.ChangeEvent ; 30 import javax.swing.event.ChangeListener ; 31 import javax.swing.event.DocumentEvent ; 32 import org.netbeans.modules.ant.freeform.spi.support.Util; 33 import org.netbeans.modules.java.freeform.JavaProjectGenerator; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.util.HelpCtx; 36 import org.openide.util.NbBundle; 37 38 42 public class OutputPanel extends javax.swing.JPanel implements HelpCtx.Provider { 43 44 private DefaultListModel listModel; 45 private File lastChosenFile = null; 46 private boolean isSeparateClasspath = true; 47 private List <ProjectModel.CompilationUnitKey> compUnitsKeys; 48 private boolean ignoreEvent; 49 private ProjectModel model; 50 51 public OutputPanel() { 52 initComponents(); 53 jTextArea1.setBackground(getBackground()); 54 listModel = new DefaultListModel (); 55 output.setModel(listModel); 56 output.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 58 javadoc.getDocument().addDocumentListener(new javax.swing.event.DocumentListener () { 59 public void insertUpdate(DocumentEvent e) { 60 update (); 61 } 62 63 public void removeUpdate(DocumentEvent e) { 64 update (); 65 } 66 67 public void changedUpdate(DocumentEvent e) { 68 update (); 69 } 70 }); 71 jTextArea1.setDisabledTextColor(jLabel2.getForeground()); 72 } 73 74 private void update() { 75 int index = sourceFolder.getSelectedIndex(); 76 assert index >= 0; 77 ProjectModel.CompilationUnitKey key = compUnitsKeys.get(index); 78 JavaProjectGenerator.JavaCompilationUnit cu = model.getCompilationUnit(key, model.isTestSourceFolder(index)); 79 updateCompilationUnitJavadoc(cu); 80 } 81 82 83 public HelpCtx getHelpCtx() { 84 return new HelpCtx( OutputPanel.class ); 85 } 86 87 private void updateControls() { 88 sourceFolder.removeAllItems(); 89 compUnitsKeys = model.createCompilationUnitKeys(); 90 isSeparateClasspath = !ProjectModel.isSingleCompilationUnit(compUnitsKeys); 91 List <String > names = ClasspathPanel.createComboContent(compUnitsKeys, model.getEvaluator(), model.getNBProjectFolder()); 92 for (String nm : names) { 93 sourceFolder.addItem(nm); 94 } 95 if (names.size() > 0) { 96 ignoreEvent = true; 97 sourceFolder.setSelectedIndex(0); 98 ignoreEvent = false; 99 } 100 101 loadOutput(); 102 103 boolean sepClasspath = model.canHaveSeparateClasspath(); 105 jLabel2.setEnabled(sepClasspath && isSeparateClasspath); 106 sourceFolder.setEnabled(sepClasspath && isSeparateClasspath); 107 108 addOutput.setEnabled(compUnitsKeys.size() > 0); 111 output.setEnabled(compUnitsKeys.size() > 0); 112 javadoc.setEnabled(compUnitsKeys.size() > 0); 113 javadocBrowse.setEnabled(compUnitsKeys.size() > 0); 114 updateButtons(); 115 } 116 117 122 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 124 125 jLabel2 = new javax.swing.JLabel (); 126 jLabel3 = new javax.swing.JLabel (); 127 addOutput = new javax.swing.JButton (); 128 removeOutput = new javax.swing.JButton (); 129 jScrollPane1 = new javax.swing.JScrollPane (); 130 output = new javax.swing.JList (); 131 jPanel1 = new javax.swing.JPanel (); 132 sourceFolder = new javax.swing.JComboBox (); 133 jTextArea1 = new javax.swing.JTextArea (); 134 jPanel2 = new javax.swing.JPanel (); 135 javadoc = new javax.swing.JTextField (); 136 javadocBrowse = new javax.swing.JButton (); 137 javadocLabel = new javax.swing.JLabel (); 138 139 setLayout(new java.awt.GridBagLayout ()); 140 141 setPreferredSize(new java.awt.Dimension (275, 202)); 142 jLabel2.setLabelFor(sourceFolder); 143 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(OutputPanel.class, "LBL_OutputPanel_jLabel1")); 144 gridBagConstraints = new java.awt.GridBagConstraints (); 145 gridBagConstraints.gridx = 0; 146 gridBagConstraints.gridy = 2; 147 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 148 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 6); 149 add(jLabel2, gridBagConstraints); 150 151 jLabel3.setLabelFor(output); 152 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(OutputPanel.class, "LBL_OutputPanel_jLabel3")); 153 gridBagConstraints = new java.awt.GridBagConstraints (); 154 gridBagConstraints.gridx = 0; 155 gridBagConstraints.gridy = 3; 156 gridBagConstraints.gridwidth = 2; 157 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 158 gridBagConstraints.insets = new java.awt.Insets (11, 0, 0, 0); 159 add(jLabel3, gridBagConstraints); 160 jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSD_OutputPanel_jLabel3")); 161 162 org.openide.awt.Mnemonics.setLocalizedText(addOutput, org.openide.util.NbBundle.getMessage(OutputPanel.class, "BTN_OutputPanel_addOutput")); 163 addOutput.addActionListener(new java.awt.event.ActionListener () { 164 public void actionPerformed(java.awt.event.ActionEvent evt) { 165 addOutputActionPerformed(evt); 166 } 167 }); 168 169 gridBagConstraints = new java.awt.GridBagConstraints (); 170 gridBagConstraints.gridx = 2; 171 gridBagConstraints.gridy = 4; 172 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 173 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 174 add(addOutput, gridBagConstraints); 175 addOutput.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSD_OutputPanel_addOutput")); 176 177 org.openide.awt.Mnemonics.setLocalizedText(removeOutput, org.openide.util.NbBundle.getMessage(OutputPanel.class, "BTN_OutputPanel_removeOutput")); 178 removeOutput.addActionListener(new java.awt.event.ActionListener () { 179 public void actionPerformed(java.awt.event.ActionEvent evt) { 180 removeOutputActionPerformed(evt); 181 } 182 }); 183 184 gridBagConstraints = new java.awt.GridBagConstraints (); 185 gridBagConstraints.gridx = 2; 186 gridBagConstraints.gridy = 5; 187 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 188 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 189 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 190 add(removeOutput, gridBagConstraints); 191 removeOutput.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSD_OutputPanel_removeOutput")); 192 193 output.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 194 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 195 outputValueChanged(evt); 196 } 197 }); 198 199 jScrollPane1.setViewportView(output); 200 output.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSD_OutputPanel_output")); 201 202 gridBagConstraints = new java.awt.GridBagConstraints (); 203 gridBagConstraints.gridx = 0; 204 gridBagConstraints.gridy = 4; 205 gridBagConstraints.gridwidth = 2; 206 gridBagConstraints.gridheight = 4; 207 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 208 gridBagConstraints.weightx = 1.0; 209 gridBagConstraints.weighty = 1.0; 210 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 211 add(jScrollPane1, gridBagConstraints); 212 jScrollPane1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSD_OutputPanel_jScrollPanel1")); 213 214 jPanel1.setLayout(new java.awt.GridBagLayout ()); 215 216 sourceFolder.addItemListener(new java.awt.event.ItemListener () { 217 public void itemStateChanged(java.awt.event.ItemEvent evt) { 218 sourceFolderItemStateChanged(evt); 219 } 220 }); 221 222 gridBagConstraints = new java.awt.GridBagConstraints (); 223 gridBagConstraints.gridx = 0; 224 gridBagConstraints.gridy = 0; 225 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 226 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 227 gridBagConstraints.weightx = 1.0; 228 jPanel1.add(sourceFolder, gridBagConstraints); 229 sourceFolder.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/java/freeform/ui/Bundle").getString("AD_OutputPanel_sourceFolder")); 230 231 gridBagConstraints = new java.awt.GridBagConstraints (); 232 gridBagConstraints.gridx = 1; 233 gridBagConstraints.gridy = 2; 234 gridBagConstraints.gridwidth = 2; 235 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 236 add(jPanel1, gridBagConstraints); 237 238 jTextArea1.setEditable(false); 239 jTextArea1.setLineWrap(true); 240 jTextArea1.setText(org.openide.util.NbBundle.getMessage(OutputPanel.class, "MSG_OutputPanel_jTextArea1")); 241 jTextArea1.setWrapStyleWord(true); 242 jTextArea1.setEnabled(false); 243 gridBagConstraints = new java.awt.GridBagConstraints (); 244 gridBagConstraints.gridwidth = 3; 245 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 246 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 0); 247 add(jTextArea1, gridBagConstraints); 248 jTextArea1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSN_OutputPanel_jTextArea1")); 249 jTextArea1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(OutputPanel.class, "ACSD_OutputPanel_jTextArea1")); 250 251 jPanel2.setLayout(new java.awt.GridBagLayout ()); 252 253 gridBagConstraints = new java.awt.GridBagConstraints (); 254 gridBagConstraints.gridx = 1; 255 gridBagConstraints.gridy = 0; 256 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 257 gridBagConstraints.weightx = 1.0; 258 gridBagConstraints.weighty = 1.0; 259 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 260 jPanel2.add(javadoc, gridBagConstraints); 261 javadoc.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/java/freeform/ui/Bundle").getString("AD_OutputPanel_javadoc")); 262 263 org.openide.awt.Mnemonics.setLocalizedText(javadocBrowse, org.openide.util.NbBundle.getMessage(OutputPanel.class, "BTN_OutputPanel_browseJavadoc")); 264 javadocBrowse.addActionListener(new java.awt.event.ActionListener () { 265 public void actionPerformed(java.awt.event.ActionEvent evt) { 266 javadocBrowseActionPerformed(evt); 267 } 268 }); 269 270 gridBagConstraints = new java.awt.GridBagConstraints (); 271 gridBagConstraints.gridx = 2; 272 gridBagConstraints.gridy = 0; 273 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 274 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 275 jPanel2.add(javadocBrowse, gridBagConstraints); 276 javadocBrowse.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/java/freeform/ui/Bundle").getString("AD_OutputPanel_javadocBrowse")); 277 278 javadocLabel.setLabelFor(javadoc); 279 org.openide.awt.Mnemonics.setLocalizedText(javadocLabel, org.openide.util.NbBundle.getMessage(OutputPanel.class, "LBL_OutputPanel_JavadocLabel")); 280 gridBagConstraints = new java.awt.GridBagConstraints (); 281 gridBagConstraints.gridx = 0; 282 gridBagConstraints.gridy = 0; 283 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 284 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 6); 285 jPanel2.add(javadocLabel, gridBagConstraints); 286 287 gridBagConstraints = new java.awt.GridBagConstraints (); 288 gridBagConstraints.gridx = 0; 289 gridBagConstraints.gridy = 8; 290 gridBagConstraints.gridwidth = 3; 291 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 292 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 293 gridBagConstraints.insets = new java.awt.Insets (11, 0, 0, 0); 294 add(jPanel2, gridBagConstraints); 295 296 } 298 private void javadocBrowseActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 300 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 301 chooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES); 302 chooser.setMultiSelectionEnabled(false); 303 if (lastChosenFile != null) { 304 chooser.setSelectedFile(lastChosenFile); 305 } else if (javadoc.getText().length() > 0) { 306 chooser.setSelectedFile(new File (javadoc.getText())); 307 } else { 308 File files[] = model.getBaseFolder().listFiles(); 309 if (files != null && files.length > 0) { 310 chooser.setSelectedFile(files[0]); 311 } else { 312 chooser.setSelectedFile(model.getBaseFolder()); 313 } 314 } 315 chooser.setDialogTitle(NbBundle.getMessage(OutputPanel.class, "LBL_Browse_Javadoc")); if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 317 File file = chooser.getSelectedFile(); 318 file = FileUtil.normalizeFile(file); 319 javadoc.setText(file.getAbsolutePath()); 320 lastChosenFile = file; 321 } 322 } 324 private void outputValueChanged(javax.swing.event.ListSelectionEvent evt) { updateButtons(); 326 } 328 private void sourceFolderItemStateChanged(java.awt.event.ItemEvent evt) { if (ignoreEvent) { 330 return; 331 } 332 if (evt.getStateChange() == ItemEvent.DESELECTED) { 333 int index = findIndex(evt.getItem()); 334 if (index != -1) { 336 saveOutput(index); 337 } 338 } else { 339 loadOutput(); 340 } 341 } 343 private int findIndex(Object o) { 344 for (int i=0; i<sourceFolder.getModel().getSize(); i++) { 345 if (sourceFolder.getModel().getElementAt(i).equals(o)) { 346 return i; 347 } 348 } 349 return -1; 350 } 351 352 354 private void saveOutput(int index) { 355 ProjectModel.CompilationUnitKey key = compUnitsKeys.get(index); 356 JavaProjectGenerator.JavaCompilationUnit cu = model.getCompilationUnit(key, model.isTestSourceFolder(index)); 357 updateCompilationUnitOutput(cu); 358 updateCompilationUnitJavadoc(cu); 359 } 360 361 363 private void loadOutput() { 364 int index; 365 if (isSeparateClasspath) { 366 index = sourceFolder.getSelectedIndex(); 367 if (index == -1) { 368 return; 369 } 370 } else { 371 index = 0; 372 } 373 ProjectModel.CompilationUnitKey key = compUnitsKeys.get(index); 374 JavaProjectGenerator.JavaCompilationUnit cu = model.getCompilationUnit(key, model.isTestSourceFolder(index)); 375 updateJListOutput(cu.output); 376 updateJavadocField(cu.javadoc); 377 } 378 379 private void updateJavadocField(List <String > jd) { 380 String value = ""; 381 boolean enabled = true; 382 if (jd != null) { 383 if (jd.size() > 1) { 384 value = getListAsString(jd); 385 enabled = false; 386 } else if (jd.size() == 1) { 387 File f = Util.resolveFile(model.getEvaluator(), model.getNBProjectFolder(), jd.get(0)); 388 if (f != null) { 389 value = f.getAbsolutePath(); 390 } 391 } 392 } 393 javadoc.setEnabled(enabled); 394 javadocBrowse.setEnabled(enabled); 395 javadoc.setText(value); 396 } 397 398 private String getListAsString(List <String > list) { 399 assert list != null; 400 StringBuffer sb = new StringBuffer (); 401 for (String s : list) { 402 File f = Util.resolveFile(model.getEvaluator(), model.getNBProjectFolder(), s); 403 if (f != null) { 404 if (sb.length()>0) { 405 sb.append(", "); } 407 sb.append(f.getAbsolutePath()); 408 } 409 } 410 return sb.toString(); 411 } 412 413 private void updateCompilationUnitJavadoc(JavaProjectGenerator.JavaCompilationUnit cu) { 414 if (javadoc.isEnabled()) { 415 if (javadoc.getText().length() > 0) { 416 cu.javadoc = new ArrayList <String >(); 417 for (String part : javadoc.getText().split(",")) { 418 File f = FileUtil.normalizeFile(new File (part.trim())); 419 String path = Util.relativizeLocation(model.getBaseFolder(), model.getNBProjectFolder(), f); 420 cu.javadoc.add (path); 421 } 422 } else { 423 cu.javadoc = null; 424 } 425 } 426 } 427 428 429 private void updateCompilationUnitOutput(JavaProjectGenerator.JavaCompilationUnit cu) { 430 if (output.getModel().getSize() == 0) { 431 cu.output = null; 432 return; 433 } 434 List <String > l = new ArrayList <String >(); 435 for (int i=0; i<output.getModel().getSize(); i++) { 436 File f = new File ((String )output.getModel().getElementAt(i)); 437 String path = Util.relativizeLocation(model.getBaseFolder(), model.getNBProjectFolder(), f); 438 l.add(path); 439 } 440 cu.output = l; 441 } 442 443 444 private void updateJListOutput(List <String > l) { 445 listModel.removeAllElements(); 446 if (l != null) { 447 for (String out : l) { 448 File f = Util.resolveFile(model.getEvaluator(), model.getNBProjectFolder(), out); 449 if (f != null) { 450 listModel.addElement(f.getAbsolutePath()); 451 } 452 } 453 } 454 updateButtons(); 455 } 456 457 private void updateButtons() { 458 removeOutput.setEnabled(output.isEnabled() && listModel.getSize() > 0 && output.getSelectedIndex() != -1); 459 } 460 461 private void removeOutputActionPerformed(java.awt.event.ActionEvent evt) { int i = output.getSelectedIndex(); 463 if (i != -1) { 464 listModel.remove(i); 465 } 466 applyChanges(); 467 updateButtons(); 468 } 470 private void addOutputActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 472 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 473 chooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES); 474 chooser.setMultiSelectionEnabled(true); 475 if (lastChosenFile != null) { 476 chooser.setSelectedFile(lastChosenFile); 477 } else { 478 File files[] = model.getBaseFolder().listFiles(); 479 if (files != null && files.length > 0) { 480 chooser.setSelectedFile(files[0]); 481 } else { 482 chooser.setSelectedFile(model.getBaseFolder()); 483 } 484 } 485 chooser.setDialogTitle(NbBundle.getMessage(OutputPanel.class, "LBL_Browse_Output")); if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 487 for (File file : chooser.getSelectedFiles()) { 488 file = FileUtil.normalizeFile(file); 489 listModel.addElement(file.getAbsolutePath()); 490 lastChosenFile = file; 491 } 492 applyChanges(); 493 updateButtons(); 494 } 495 } 497 private void applyChanges() { 498 if (isSeparateClasspath) { 499 if (sourceFolder.getSelectedIndex() != -1) { 500 saveOutput(sourceFolder.getSelectedIndex()); 501 } 502 } else { 503 saveOutput(0); 504 } 505 } 506 507 void setModel(ProjectModel model) { 508 this.model = model; 509 updateControls(); 510 model.addChangeListener(new ChangeListener () { 511 public void stateChanged(ChangeEvent arg0) { 512 updateControls(); 513 } 514 }); 515 } 516 517 518 private javax.swing.JButton addOutput; 520 private javax.swing.JLabel jLabel2; 521 private javax.swing.JLabel jLabel3; 522 private javax.swing.JPanel jPanel1; 523 private javax.swing.JPanel jPanel2; 524 private javax.swing.JScrollPane jScrollPane1; 525 private javax.swing.JTextArea jTextArea1; 526 private javax.swing.JTextField javadoc; 527 private javax.swing.JButton javadocBrowse; 528 private javax.swing.JLabel javadocLabel; 529 private javax.swing.JList output; 530 private javax.swing.JButton removeOutput; 531 private javax.swing.JComboBox sourceFolder; 532 534 } 535 | Popular Tags |