1 19 20 package org.netbeans.modules.java.freeform.ui; 21 22 import java.awt.Component ; 23 import java.awt.event.ItemEvent ; 24 import java.io.File ; 25 import java.net.MalformedURLException ; 26 import java.text.Collator ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Collection ; 30 import java.util.Comparator ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.SortedSet ; 34 import java.util.TreeSet ; 35 import javax.swing.DefaultComboBoxModel ; 36 import javax.swing.DefaultListCellRenderer ; 37 import javax.swing.DefaultListModel ; 38 import javax.swing.JFileChooser ; 39 import javax.swing.JList ; 40 import javax.swing.filechooser.FileFilter ; 41 import org.netbeans.api.java.platform.JavaPlatform; 42 import org.netbeans.api.java.platform.JavaPlatformManager; 43 import org.netbeans.api.java.platform.PlatformsCustomizer; 44 import org.netbeans.modules.ant.freeform.spi.support.Util; 45 import org.netbeans.modules.java.freeform.JavaProjectGenerator; 46 import org.netbeans.modules.java.freeform.jdkselection.JdkConfiguration; 47 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 48 import org.netbeans.spi.project.support.ant.PropertyUtils; 49 import org.openide.ErrorManager; 50 import org.openide.filesystems.FileUtil; 51 import org.openide.util.HelpCtx; 52 import org.openide.util.NbBundle; 53 54 57 public class ClasspathPanel extends javax.swing.JPanel implements HelpCtx.Provider { 58 59 private DefaultListModel listModel; 60 private File lastChosenFile = null; 61 private boolean isSeparateClasspath = true; 62 private List <ProjectModel.CompilationUnitKey> compUnitsKeys; 63 private boolean ignoreEvent; 64 private ProjectModel model; 65 private final JdkConfiguration jdkConf; 66 67 70 public ClasspathPanel() { 71 jdkConf = null; 72 basicInit(); 73 javaPlatformPanel.setVisible(false); 74 } 75 76 79 ClasspathPanel(JdkConfiguration jdkConf) { 80 this.jdkConf = jdkConf; 81 basicInit(); 82 jTextArea1.setText(NbBundle.getMessage(ClasspathPanel.class, "LBL_ClasspathPanel_Explanation")); 83 javaPlatformIntro.setBackground(getBackground()); 84 javaPlatformIntro.setDisabledTextColor(jLabel2.getForeground()); 85 refreshJavaPlatforms(); 86 javaPlatform.setRenderer(new DefaultListCellRenderer () { 87 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 88 return super.getListCellRendererComponent(list, ((JavaPlatform) value).getDisplayName(), index, isSelected, cellHasFocus); 89 } 90 }); 91 } 92 93 private void basicInit() { 94 initComponents(); 95 jTextArea1.setBackground(getBackground()); 96 jTextArea1.setDisabledTextColor(jLabel2.getForeground()); 97 listModel = new DefaultListModel (); 98 classpath.setModel(listModel); 99 } 100 101 private void refreshJavaPlatforms() { 102 SortedSet <JavaPlatform> platforms = new TreeSet <JavaPlatform>(new Comparator <JavaPlatform>() { 103 Collator COLL = Collator.getInstance(); 104 public int compare(JavaPlatform p1, JavaPlatform p2) { 105 int res = COLL.compare(p1.getDisplayName(), p2.getDisplayName()); 106 if (res != 0) { 107 return res; 108 } else { 109 return System.identityHashCode(p1) - System.identityHashCode(p2); 110 } 111 } 112 }); 113 platforms.addAll(Arrays.asList(JavaPlatformManager.getDefault().getInstalledPlatforms())); 114 javaPlatform.setModel(new DefaultComboBoxModel (platforms.toArray(new JavaPlatform[platforms.size()]))); 115 JavaPlatform pf = jdkConf.getSelectedPlatform(); 116 if (pf == null) { 117 pf = JavaPlatformManager.getDefault().getDefaultPlatform(); 118 } 119 javaPlatform.setSelectedItem(pf); 120 } 121 122 public HelpCtx getHelpCtx() { 123 return new HelpCtx( ClasspathPanel.class ); 124 } 125 126 void updateControls() { 127 sourceFolder.removeAllItems(); 128 compUnitsKeys = model.createCompilationUnitKeys(); 129 isSeparateClasspath = !ProjectModel.isSingleCompilationUnit(compUnitsKeys); 130 List <String > names = createComboContent(compUnitsKeys, model.getEvaluator(), model.getNBProjectFolder()); 131 for (String nm : names) { 132 sourceFolder.addItem(nm); 133 } 134 if (names.size() > 0) { 135 ignoreEvent = true; 136 sourceFolder.setSelectedIndex(0); 137 ignoreEvent = false; 138 } 139 loadClasspath(); 140 141 boolean sepClasspath = model.canHaveSeparateClasspath(); 143 separateClasspath.setEnabled(sepClasspath); 144 if (sepClasspath) { 145 if (isSeparateClasspath && !model.canCreateSingleCompilationUnit()) { 149 separateClasspath.setEnabled(false); 150 } 151 } 152 jLabel2.setEnabled(sepClasspath && isSeparateClasspath); 153 sourceFolder.setEnabled(sepClasspath && isSeparateClasspath); 154 ignoreEvent = true; 156 separateClasspath.setSelected(isSeparateClasspath); 157 ignoreEvent = false; 158 159 addClasspath.setEnabled(compUnitsKeys.size() > 0); 162 classpath.setEnabled(compUnitsKeys.size() > 0); 163 } 164 165 166 static List <String > createComboContent(List <ProjectModel.CompilationUnitKey> compilationUnitKeys, PropertyEvaluator evaluator, File nbProjectFolder) { 167 List <String > l = new ArrayList <String >(); 168 for (ProjectModel.CompilationUnitKey cul : compilationUnitKeys) { 169 String name; 170 if (cul.locations.size() == 1) { 171 if (cul.label != null) { 172 name = cul.label + " [" + SourceFoldersPanel.getLocationDisplayName(evaluator, nbProjectFolder, cul.locations.get(0)) + "]"; } else { 174 name = convertListToString(cul.locations); 175 } 176 } else { 177 name = convertListToString(cul.locations); 178 } 179 l.add(name); 180 } 181 return l; 182 } 183 184 private static String convertListToString(List <String > l) { 185 StringBuffer sb = new StringBuffer (); 186 Iterator <String > it = l.iterator(); 187 while (it.hasNext()) { 188 String s = it.next(); 189 sb.append(s); 190 if (it.hasNext()) { 191 sb.append(File.pathSeparatorChar+" "); } 193 } 194 return sb.toString(); 195 } 196 197 202 private void initComponents() { 204 java.awt.GridBagConstraints gridBagConstraints; 205 206 jLabel2 = new javax.swing.JLabel (); 207 jLabel3 = new javax.swing.JLabel (); 208 addClasspath = new javax.swing.JButton (); 209 removeClasspath = new javax.swing.JButton (); 210 jScrollPane1 = new javax.swing.JScrollPane (); 211 classpath = new javax.swing.JList (); 212 jPanel1 = new javax.swing.JPanel (); 213 sourceFolder = new javax.swing.JComboBox (); 214 moveUp = new javax.swing.JButton (); 215 moveDown = new javax.swing.JButton (); 216 jTextArea1 = new javax.swing.JTextArea (); 217 separateClasspath = new javax.swing.JCheckBox (); 218 javaPlatformPanel = new javax.swing.JPanel (); 219 javaPlatformIntro = new javax.swing.JTextArea (); 220 javaPlatformLabel = new javax.swing.JLabel (); 221 javaPlatform = new javax.swing.JComboBox (); 222 javaPlatformButton = new javax.swing.JButton (); 223 224 setPreferredSize(new java.awt.Dimension (275, 202)); 225 setLayout(new java.awt.GridBagLayout ()); 226 227 jLabel2.setLabelFor(sourceFolder); 228 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "LBL_ClasspathPanel_jLabel2")); gridBagConstraints = new java.awt.GridBagConstraints (); 230 gridBagConstraints.gridx = 0; 231 gridBagConstraints.gridy = 2; 232 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 233 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 6); 234 add(jLabel2, gridBagConstraints); 235 jLabel2.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSD_ClasspathPanel_jLabel2")); 237 jLabel3.setLabelFor(classpath); 238 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "LBL_ClasspathPanel_jLabel3")); gridBagConstraints = new java.awt.GridBagConstraints (); 240 gridBagConstraints.gridx = 0; 241 gridBagConstraints.gridy = 3; 242 gridBagConstraints.gridwidth = 2; 243 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 244 gridBagConstraints.insets = new java.awt.Insets (11, 0, 0, 0); 245 add(jLabel3, gridBagConstraints); 246 jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSD_ClasspathPanel_jLabel3")); 248 org.openide.awt.Mnemonics.setLocalizedText(addClasspath, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "BTN_ClasspathPanel_addClasspath")); addClasspath.addActionListener(new java.awt.event.ActionListener () { 250 public void actionPerformed(java.awt.event.ActionEvent evt) { 251 addClasspathActionPerformed(evt); 252 } 253 }); 254 gridBagConstraints = new java.awt.GridBagConstraints (); 255 gridBagConstraints.gridx = 2; 256 gridBagConstraints.gridy = 4; 257 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 258 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 259 add(addClasspath, gridBagConstraints); 260 addClasspath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSD_ClasspathPanel_addClasspath")); 262 org.openide.awt.Mnemonics.setLocalizedText(removeClasspath, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "BTN_ClasspathPanel_removeClasspath")); removeClasspath.addActionListener(new java.awt.event.ActionListener () { 264 public void actionPerformed(java.awt.event.ActionEvent evt) { 265 removeClasspathActionPerformed(evt); 266 } 267 }); 268 gridBagConstraints = new java.awt.GridBagConstraints (); 269 gridBagConstraints.gridx = 2; 270 gridBagConstraints.gridy = 5; 271 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 272 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 273 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 274 add(removeClasspath, gridBagConstraints); 275 removeClasspath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSD_ClasspathPanel_removeClasspath")); 277 classpath.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 278 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 279 classpathValueChanged(evt); 280 } 281 }); 282 jScrollPane1.setViewportView(classpath); 283 classpath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSD_ClasspathPanel_classpath")); 285 gridBagConstraints = new java.awt.GridBagConstraints (); 286 gridBagConstraints.gridx = 0; 287 gridBagConstraints.gridy = 4; 288 gridBagConstraints.gridwidth = 2; 289 gridBagConstraints.gridheight = 4; 290 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 291 gridBagConstraints.weightx = 1.0; 292 gridBagConstraints.weighty = 1.0; 293 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 294 add(jScrollPane1, gridBagConstraints); 295 296 jPanel1.setLayout(new java.awt.GridBagLayout ()); 297 298 sourceFolder.addItemListener(new java.awt.event.ItemListener () { 299 public void itemStateChanged(java.awt.event.ItemEvent evt) { 300 sourceFolderItemStateChanged(evt); 301 } 302 }); 303 gridBagConstraints = new java.awt.GridBagConstraints (); 304 gridBagConstraints.gridx = 0; 305 gridBagConstraints.gridy = 0; 306 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 307 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 308 gridBagConstraints.weightx = 1.0; 309 jPanel1.add(sourceFolder, gridBagConstraints); 310 311 gridBagConstraints = new java.awt.GridBagConstraints (); 312 gridBagConstraints.gridx = 1; 313 gridBagConstraints.gridy = 2; 314 gridBagConstraints.gridwidth = 2; 315 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 316 add(jPanel1, gridBagConstraints); 317 318 org.openide.awt.Mnemonics.setLocalizedText(moveUp, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "LBL_ClasspathPanel_Move_Up")); moveUp.addActionListener(new java.awt.event.ActionListener () { 320 public void actionPerformed(java.awt.event.ActionEvent evt) { 321 moveUpActionPerformed(evt); 322 } 323 }); 324 gridBagConstraints = new java.awt.GridBagConstraints (); 325 gridBagConstraints.gridx = 2; 326 gridBagConstraints.gridy = 6; 327 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 328 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 329 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 330 add(moveUp, gridBagConstraints); 331 java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/java/freeform/ui/Bundle"); moveUp.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_ClasspathPanel_noveUp")); 334 org.openide.awt.Mnemonics.setLocalizedText(moveDown, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "LBL_ClasspathPanel_Move_Down")); moveDown.addActionListener(new java.awt.event.ActionListener () { 336 public void actionPerformed(java.awt.event.ActionEvent evt) { 337 moveDownActionPerformed(evt); 338 } 339 }); 340 gridBagConstraints = new java.awt.GridBagConstraints (); 341 gridBagConstraints.gridx = 2; 342 gridBagConstraints.gridy = 7; 343 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 344 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 345 add(moveDown, gridBagConstraints); 346 moveDown.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_ClasspathPanel_moveDown")); 348 jTextArea1.setEditable(false); 349 jTextArea1.setLineWrap(true); 350 jTextArea1.setText(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "MSG_ClasspathPanel_jTextArea")); jTextArea1.setWrapStyleWord(true); 352 jTextArea1.setEnabled(false); 353 gridBagConstraints = new java.awt.GridBagConstraints (); 354 gridBagConstraints.gridwidth = 3; 355 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 356 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 357 add(jTextArea1, gridBagConstraints); 358 jTextArea1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSN_ClasspathPanel_jTextArea")); jTextArea1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ACSD_ClasspathPanel_jTextArea")); 361 separateClasspath.setSelected(true); 362 org.openide.awt.Mnemonics.setLocalizedText(separateClasspath, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "LBL_ClasspathPanel_sepatateClasspath")); separateClasspath.setMargin(new java.awt.Insets (0, 0, 0, 0)); 364 separateClasspath.addActionListener(new java.awt.event.ActionListener () { 365 public void actionPerformed(java.awt.event.ActionEvent evt) { 366 separateClasspathActionPerformed(evt); 367 } 368 }); 369 gridBagConstraints = new java.awt.GridBagConstraints (); 370 gridBagConstraints.gridx = 0; 371 gridBagConstraints.gridy = 1; 372 gridBagConstraints.gridwidth = 3; 373 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 374 gridBagConstraints.insets = new java.awt.Insets (11, 0, 0, 0); 375 add(separateClasspath, gridBagConstraints); 376 separateClasspath.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_ClasspathPanel_separateClasspath")); 378 javaPlatformPanel.setLayout(new java.awt.GridBagLayout ()); 379 380 javaPlatformIntro.setEditable(false); 381 javaPlatformIntro.setLineWrap(true); 382 javaPlatformIntro.setText(org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ClasspathPanel.javaPlatformIntro")); javaPlatformIntro.setWrapStyleWord(true); 384 javaPlatformIntro.setEnabled(false); 385 gridBagConstraints = new java.awt.GridBagConstraints (); 386 gridBagConstraints.gridx = 0; 387 gridBagConstraints.gridy = 0; 388 gridBagConstraints.gridwidth = 3; 389 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 390 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 391 javaPlatformPanel.add(javaPlatformIntro, gridBagConstraints); 392 393 javaPlatformLabel.setLabelFor(javaPlatform); 394 org.openide.awt.Mnemonics.setLocalizedText(javaPlatformLabel, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ClasspathPanel.javaPlatformLabel")); gridBagConstraints = new java.awt.GridBagConstraints (); 396 gridBagConstraints.gridx = 0; 397 gridBagConstraints.gridy = 1; 398 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 399 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 6); 400 javaPlatformPanel.add(javaPlatformLabel, gridBagConstraints); 401 gridBagConstraints = new java.awt.GridBagConstraints (); 402 gridBagConstraints.gridx = 1; 403 gridBagConstraints.gridy = 1; 404 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 405 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 406 gridBagConstraints.weightx = 1.0; 407 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 6); 408 javaPlatformPanel.add(javaPlatform, gridBagConstraints); 409 410 org.openide.awt.Mnemonics.setLocalizedText(javaPlatformButton, org.openide.util.NbBundle.getMessage(ClasspathPanel.class, "ClasspathPanel.javaPlatformButton")); javaPlatformButton.addActionListener(new java.awt.event.ActionListener () { 412 public void actionPerformed(java.awt.event.ActionEvent evt) { 413 javaPlatformButtonActionPerformed(evt); 414 } 415 }); 416 gridBagConstraints = new java.awt.GridBagConstraints (); 417 gridBagConstraints.gridx = 2; 418 gridBagConstraints.gridy = 1; 419 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 420 javaPlatformPanel.add(javaPlatformButton, gridBagConstraints); 421 422 gridBagConstraints = new java.awt.GridBagConstraints (); 423 gridBagConstraints.gridx = 0; 424 gridBagConstraints.gridy = 8; 425 gridBagConstraints.gridwidth = 3; 426 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 427 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 428 add(javaPlatformPanel, gridBagConstraints); 429 } 431 private void javaPlatformButtonActionPerformed(java.awt.event.ActionEvent evt) { PlatformsCustomizer.showCustomizer(jdkConf.getSelectedPlatform()); 433 refreshJavaPlatforms(); 434 } 436 private void classpathValueChanged(javax.swing.event.ListSelectionEvent evt) { updateButtons(); 438 } 440 private void separateClasspathActionPerformed(java.awt.event.ActionEvent evt) { if (ignoreEvent) { 442 return; 443 } 444 applyChanges(); 445 isSeparateClasspath = separateClasspath.isSelected(); 446 model.updateCompilationUnits(isSeparateClasspath); 447 updateControls(); 448 } 450 private void moveDownActionPerformed(java.awt.event.ActionEvent evt) { int indices[] = classpath.getSelectedIndices(); 452 if (indices.length == 0 || 453 indices[indices.length - 1] == listModel.getSize() - 1) { 454 return; 455 } 456 for (int i = 0; i < indices.length; i++) { 457 int index = indices[i]; 458 Object o = listModel.remove(index); 459 index++; 460 listModel.add(index, o); 461 indices[i] = index; 462 } 463 classpath.setSelectedIndices(indices); 464 applyChanges(); 465 updateButtons(); 466 } 468 private void moveUpActionPerformed(java.awt.event.ActionEvent evt) { int indices[] = classpath.getSelectedIndices(); 470 if (indices.length == 0 || indices[0] == 0) { 471 return; 472 } 473 for (int i = 0; i < indices.length; i++) { 474 int index = indices[i]; 475 Object o = listModel.remove(index); 476 index--; 477 listModel.add(index, o); 478 indices[i] = index; 479 } 480 classpath.setSelectedIndices(indices); 481 applyChanges(); 482 updateButtons(); 483 } 485 private void sourceFolderItemStateChanged(java.awt.event.ItemEvent evt) { if (ignoreEvent) { 487 return; 488 } 489 if (evt.getStateChange() == ItemEvent.DESELECTED) { 490 int index = findIndex(evt.getItem()); 491 if (index != -1) { 493 saveClasspath(index); 494 } 495 } else { 496 loadClasspath(); 497 } 498 updateButtons(); 499 } 501 private int findIndex(Object o) { 502 for (int i=0; i<sourceFolder.getModel().getSize(); i++) { 503 if (sourceFolder.getModel().getElementAt(i).equals(o)) { 504 return i; 505 } 506 } 507 return -1; 508 } 509 510 512 private void saveClasspath(int index) { 513 ProjectModel.CompilationUnitKey key = compUnitsKeys.get(index); 514 JavaProjectGenerator.JavaCompilationUnit cu = model.getCompilationUnit(key, model.isTestSourceFolder(index)); 515 updateCompilationUnitCompileClasspath(cu); 516 } 517 518 520 private void loadClasspath() { 521 int index; 522 if (isSeparateClasspath) { 523 index = sourceFolder.getSelectedIndex(); 524 if (index == -1) { 525 return; 526 } 527 } else { 528 index = 0; 529 } 530 ProjectModel.CompilationUnitKey key = compUnitsKeys.get(index); 531 JavaProjectGenerator.JavaCompilationUnit cu = model.getCompilationUnit(key, model.isTestSourceFolder(index)); 532 updateJListClassPath(cu.classpath); 533 } 534 535 537 private void updateCompilationUnitCompileClasspath(JavaProjectGenerator.JavaCompilationUnit cu) { 538 List <JavaProjectGenerator.JavaCompilationUnit.CP> cps = cu.classpath; 539 if (cps != null) { 540 Iterator <JavaProjectGenerator.JavaCompilationUnit.CP> it = cps.iterator(); 541 while (it.hasNext()) { 542 JavaProjectGenerator.JavaCompilationUnit.CP cp = it.next(); 543 if (cp.mode.equals(ProjectModel.CLASSPATH_MODE_COMPILE)) { 544 it.remove(); 545 } 548 } 549 } 550 if (classpath.getModel().getSize() == 0) { 551 return; 552 } 553 StringBuffer sb = new StringBuffer (); 554 for (int i=0; i<classpath.getModel().getSize(); i++) { 555 File f = new File ((String )classpath.getModel().getElementAt(i)); 556 String path = Util.relativizeLocation(model.getBaseFolder(), model.getNBProjectFolder(), f); 557 sb.append(path); 558 if (i+1<classpath.getModel().getSize()) { 559 sb.append(File.pathSeparatorChar); 560 } 561 } 562 if (sb.length() > 0) { 563 if (cps == null) { 564 cps = new ArrayList <JavaProjectGenerator.JavaCompilationUnit.CP>(); 565 cu.classpath = cps; 566 } 567 JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 568 cp.mode = ProjectModel.CLASSPATH_MODE_COMPILE; 569 cp.classpath = sb.toString(); 570 cps.add(cp); 571 } 572 } 573 574 575 private void updateJListClassPath(List <JavaProjectGenerator.JavaCompilationUnit.CP> cps) { 576 listModel.removeAllElements(); 577 if (cps == null) { 578 return; 579 } 580 for (JavaProjectGenerator.JavaCompilationUnit.CP cp : cps) { 581 if (cp.mode.equals(ProjectModel.CLASSPATH_MODE_COMPILE)) { 582 for (String path : PropertyUtils.tokenizePath(model.getEvaluator().evaluate(cp.classpath))) { 583 path = PropertyUtils.resolveFile(model.getNBProjectFolder(), path).getAbsolutePath(); 584 if (path != null) { 585 listModel.addElement(path); 586 } 587 } 588 } 589 } 590 updateButtons(); 591 } 592 593 private void updateButtons() { 594 int indices[] = classpath.getSelectedIndices(); 595 removeClasspath.setEnabled(listModel.getSize() > 0 && indices.length != 0); 596 moveUp.setEnabled(indices.length > 0 && indices[0] != 0); 597 moveDown.setEnabled(indices.length > 0 && indices[indices.length - 1] != listModel.getSize() - 1); 598 } 599 600 private void removeClasspathActionPerformed(java.awt.event.ActionEvent evt) { int entries[] = classpath.getSelectedIndices(); 602 for (int i = 0; i < entries.length; i++) { 603 listModel.remove(entries[i] - i); 604 } 605 applyChanges(); 606 updateButtons(); 607 } 609 private void addClasspathActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 611 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 612 chooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES); 613 chooser.setMultiSelectionEnabled(true); 614 if (lastChosenFile != null) { 615 chooser.setSelectedFile(lastChosenFile); 616 } else { 617 File files[] = model.getBaseFolder().listFiles(); 618 if (files != null && files.length > 0) { 619 chooser.setSelectedFile(files[0]); 620 } else { 621 chooser.setSelectedFile(model.getBaseFolder()); 622 } 623 } 624 chooser.setDialogTitle(NbBundle.getMessage(ClasspathPanel.class, "LBL_Browse_Classpath")); 625 626 FileFilter fileFilter = new SimpleFileFilter ( 628 NbBundle.getMessage( ClasspathPanel.class, "LBL_ZipJarFolderFilter" ), new String [] {"ZIP","JAR"} ); chooser.setAcceptAllFileFilterUsed( false ); 632 chooser.setFileFilter(fileFilter); 633 634 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { 635 for (File file : chooser.getSelectedFiles()) { 636 file = FileUtil.normalizeFile(file); 637 638 if (!fileFilter.accept(file)) { 641 continue; 642 } 643 644 listModel.addElement(file.getAbsolutePath()); 645 lastChosenFile = file; 646 } 647 applyChanges(); 648 updateButtons(); 649 } 650 } 652 private void applyChanges() { 653 if (isSeparateClasspath) { 654 if (sourceFolder.getSelectedIndex() != -1) { 655 saveClasspath(sourceFolder.getSelectedIndex()); 656 } 657 } else { 658 saveClasspath(0); 659 } 660 } 661 662 public void setModel(ProjectModel model) { 663 this.model = model; 664 updateControls(); 665 } 666 667 private javax.swing.JButton addClasspath; 669 private javax.swing.JList classpath; 670 private javax.swing.JLabel jLabel2; 671 private javax.swing.JLabel jLabel3; 672 private javax.swing.JPanel jPanel1; 673 private javax.swing.JScrollPane jScrollPane1; 674 private javax.swing.JTextArea jTextArea1; 675 javax.swing.JComboBox javaPlatform; 676 private javax.swing.JButton javaPlatformButton; 677 private javax.swing.JTextArea javaPlatformIntro; 678 private javax.swing.JLabel javaPlatformLabel; 679 private javax.swing.JPanel javaPlatformPanel; 680 private javax.swing.JButton moveDown; 681 private javax.swing.JButton moveUp; 682 private javax.swing.JButton removeClasspath; 683 private javax.swing.JCheckBox separateClasspath; 684 private javax.swing.JComboBox sourceFolder; 685 687 private static class SimpleFileFilter extends FileFilter { 688 689 private String description; 690 private Collection extensions; 691 692 693 public SimpleFileFilter (String description, String [] extensions) { 694 this.description = description; 695 this.extensions = Arrays.asList(extensions); 696 } 697 698 public boolean accept(File f) { 699 if (f.isDirectory()) 700 return true; 701 try { 702 return FileUtil.isArchiveFile(f.toURI().toURL()); 703 } catch (MalformedURLException mue) { 704 ErrorManager.getDefault().notify(mue); 705 return false; 706 } 707 } 708 709 public String getDescription() { 710 return this.description; 711 } 712 } 713 } 714 | Popular Tags |