1 19 20 package org.netbeans.core.execution.beaninfo.editors; 21 22 import java.beans.PropertyEditor ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.Collections ; 26 import java.util.List ; 27 import java.util.StringTokenizer ; 28 import javax.swing.DefaultListModel ; 29 import javax.swing.JFileChooser ; 30 import javax.swing.filechooser.FileFilter ; 31 import org.netbeans.beaninfo.editors.FileEditor; 32 import org.openide.execution.NbClassPath; 33 import org.openide.util.HelpCtx; 34 import org.openide.util.NbBundle; 35 import org.openide.util.NbCollections; 36 import org.openide.windows.WindowManager; 37 38 42 class NbClassPathCustomEditor extends javax.swing.JPanel { 43 44 45 private static File lastDirFolder = null; 46 47 private static File lastJarFolder = null; 48 49 private PropertyEditor editor; 50 51 private DefaultListModel listModel = new DefaultListModel (); 52 private boolean editable = true; 53 54 55 56 public NbClassPathCustomEditor() { 57 initComponents (); 58 pathList.setModel(listModel); 59 pathScrollPane.setViewportView(pathList); 60 61 setMinimumSize (new java.awt.Dimension (400, 200)); 62 setPreferredSize(new java.awt.Dimension (400, 200)); 63 64 pathList.getAccessibleContext().setAccessibleDescription(getString("ACSD_PathList")); 65 addDirButton.getAccessibleContext().setAccessibleDescription(getString("ACSD_AddDirectory")); 66 addJarButton.getAccessibleContext().setAccessibleDescription(getString("ACSD_AddJAR")); 67 upButton.getAccessibleContext().setAccessibleDescription(getString("ACSD_MoveUp")); 68 downButton.getAccessibleContext().setAccessibleDescription(getString("ACSD_MoveDown")); 69 removeButton.getAccessibleContext().setAccessibleDescription(getString("ACSD_Remove")); 70 71 getAccessibleContext().setAccessibleDescription(getString("ACSD_CustomNbClassPathEditor")); 72 } 73 74 NbClassPathCustomEditor(PropertyEditor propEd) { 75 this(); 76 editor = propEd; 77 Object value = propEd.getValue(); 78 if (value instanceof NbClassPath) { 79 setClassPath(((NbClassPath)value).getClassPath()); 80 } 81 if ( editor instanceof NbClassPathEditor ) 82 if ( ! ((NbClassPathEditor)editor).isEditable() ) { 83 editable = false; 84 addDirButton.setEnabled( false ); 85 addJarButton.setEnabled( false ); 86 } 87 } 88 89 94 private void initComponents() { 96 java.awt.GridBagConstraints gridBagConstraints; 97 98 innerPanel = new javax.swing.JPanel (); 99 addDirButton = new javax.swing.JButton (); 100 addJarButton = new javax.swing.JButton (); 101 upButton = new javax.swing.JButton (); 102 downButton = new javax.swing.JButton (); 103 removeButton = new javax.swing.JButton (); 104 pathScrollPane = new javax.swing.JScrollPane (); 105 pathList = new javax.swing.JList (); 106 pathLabel = new javax.swing.JLabel (); 107 108 setLayout(new java.awt.BorderLayout ()); 109 110 innerPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 0, 11)); 111 innerPanel.setLayout(new java.awt.GridBagLayout ()); 112 113 org.openide.awt.Mnemonics.setLocalizedText(addDirButton, getString("CTL_AddDirectory")); addDirButton.addActionListener(new java.awt.event.ActionListener () { 115 public void actionPerformed(java.awt.event.ActionEvent evt) { 116 addDirButtonActionPerformed(evt); 117 } 118 }); 119 gridBagConstraints = new java.awt.GridBagConstraints (); 120 gridBagConstraints.gridx = 1; 121 gridBagConstraints.gridy = 1; 122 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 123 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 124 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 125 innerPanel.add(addDirButton, gridBagConstraints); 126 127 org.openide.awt.Mnemonics.setLocalizedText(addJarButton, getString("CTL_AddJAR")); addJarButton.addActionListener(new java.awt.event.ActionListener () { 129 public void actionPerformed(java.awt.event.ActionEvent evt) { 130 addJarButtonActionPerformed(evt); 131 } 132 }); 133 gridBagConstraints = new java.awt.GridBagConstraints (); 134 gridBagConstraints.gridx = 1; 135 gridBagConstraints.gridy = 2; 136 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 137 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 138 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 139 innerPanel.add(addJarButton, gridBagConstraints); 140 141 org.openide.awt.Mnemonics.setLocalizedText(upButton, getString("CTL_MoveUp")); upButton.setEnabled(false); 143 upButton.addActionListener(new java.awt.event.ActionListener () { 144 public void actionPerformed(java.awt.event.ActionEvent evt) { 145 upButtonActionPerformed(evt); 146 } 147 }); 148 gridBagConstraints = new java.awt.GridBagConstraints (); 149 gridBagConstraints.gridx = 1; 150 gridBagConstraints.gridy = 4; 151 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 152 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 153 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 154 innerPanel.add(upButton, gridBagConstraints); 155 156 org.openide.awt.Mnemonics.setLocalizedText(downButton, getString("CTL_MoveDown")); downButton.setEnabled(false); 158 downButton.addActionListener(new java.awt.event.ActionListener () { 159 public void actionPerformed(java.awt.event.ActionEvent evt) { 160 downButtonActionPerformed(evt); 161 } 162 }); 163 gridBagConstraints = new java.awt.GridBagConstraints (); 164 gridBagConstraints.gridx = 1; 165 gridBagConstraints.gridy = 5; 166 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 167 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 168 innerPanel.add(downButton, gridBagConstraints); 169 170 org.openide.awt.Mnemonics.setLocalizedText(removeButton, getString("CTL_Remove")); removeButton.setEnabled(false); 172 removeButton.addActionListener(new java.awt.event.ActionListener () { 173 public void actionPerformed(java.awt.event.ActionEvent evt) { 174 removeButtonActionPerformed(evt); 175 } 176 }); 177 gridBagConstraints = new java.awt.GridBagConstraints (); 178 gridBagConstraints.gridx = 1; 179 gridBagConstraints.gridy = 3; 180 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 181 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 182 gridBagConstraints.insets = new java.awt.Insets (0, 0, 11, 0); 183 innerPanel.add(removeButton, gridBagConstraints); 184 185 pathList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 186 public void valueChanged(javax.swing.event.ListSelectionEvent evt) { 187 pathListValueChanged(evt); 188 } 189 }); 190 pathList.addMouseListener(new java.awt.event.MouseAdapter () { 191 public void mouseClicked(java.awt.event.MouseEvent evt) { 192 pathListMouseClicked(evt); 193 } 194 }); 195 pathScrollPane.setViewportView(pathList); 196 197 gridBagConstraints = new java.awt.GridBagConstraints (); 198 gridBagConstraints.gridx = 0; 199 gridBagConstraints.gridy = 1; 200 gridBagConstraints.gridheight = 5; 201 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 202 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 203 gridBagConstraints.weightx = 1.0; 204 gridBagConstraints.weighty = 1.0; 205 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 11); 206 innerPanel.add(pathScrollPane, gridBagConstraints); 207 208 pathLabel.setLabelFor(pathList); 209 org.openide.awt.Mnemonics.setLocalizedText(pathLabel, getString("CTL_Classpath.Border_Title")); gridBagConstraints = new java.awt.GridBagConstraints (); 211 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 212 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 213 innerPanel.add(pathLabel, gridBagConstraints); 214 215 add(innerPanel, java.awt.BorderLayout.CENTER); 216 } 218 private void pathListMouseClicked(java.awt.event.MouseEvent evt) { if (evt.getClickCount() != 2) { 220 return; 222 } 223 triggerEdit(pathList.getSelectedIndex()); 224 } 226 private void pathListValueChanged(javax.swing.event.ListSelectionEvent evt) { enableButtons(); 228 } 230 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { int index = pathList.getSelectedIndex(); 232 233 Object [] selectedValues = pathList.getSelectedValues(); 234 for (int i = 0; i < selectedValues.length; i++) { 235 listModel.removeElement(selectedValues[i]); 236 fireValueChanged(); 237 } 238 239 int size = listModel.getSize(); 241 242 if(index >= 0 && size > 0) { 243 if(size == index) { 244 pathList.setSelectedIndex(index - 1); 245 } else if(size > index) { 246 pathList.setSelectedIndex(index); 247 } else { 248 pathList.setSelectedIndex(0); 249 } 250 } 251 252 enableButtons(); 254 } 256 private void downButtonActionPerformed(java.awt.event.ActionEvent evt) { int i = pathList.getSelectedIndex(); 258 swap(i); 259 pathList.setSelectedIndex(i+1); 260 } 262 private void upButtonActionPerformed(java.awt.event.ActionEvent evt) { int i = pathList.getSelectedIndex(); 264 swap(i-1); 265 pathList.setSelectedIndex(i - 1); 266 } 268 private void addJarButtonActionPerformed(java.awt.event.ActionEvent evt) { 270 JFileChooser chooser = FileEditor.createHackedFileChooser(); 271 setHelpToChooser( chooser ); 272 273 chooser.setFileFilter(new FileFilter () { 274 public boolean accept(File f) { 275 return (f.isDirectory() || f.getName().endsWith(".jar") || f.getName().endsWith(".zip")); } 277 public String getDescription() { 278 return getString("CTL_JarArchivesMask"); 279 } 280 }); 281 282 if (lastJarFolder != null) { 283 chooser.setCurrentDirectory(lastJarFolder); 284 } 285 286 chooser.setDialogTitle(getString("CTL_FileSystemPanel.Jar_Dialog_Title")); 287 chooser.setMultiSelectionEnabled( true ); 288 if (chooser.showDialog(WindowManager.getDefault ().getMainWindow (), 289 getString("CTL_Approve_Button_Title")) 290 == JFileChooser.APPROVE_OPTION) { 291 File [] files = chooser.getSelectedFiles(); 292 boolean found = false; 293 for (int i=0; i<files.length; i++) { 294 if ((files[i] != null) && (files[i].isFile())) { 295 found = true; 296 String path = files[i].getAbsolutePath(); 297 if (! listModel.contains (path)) { 298 listModel.addElement (path); 299 } 300 } 301 } 302 if ( found ) { 303 lastJarFolder = chooser.getCurrentDirectory(); 304 fireValueChanged(); 305 } 306 pathList.setSelectedIndex(listModel.size() - 1); 307 } 308 } 310 private void addDirButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = FileEditor.createHackedFileChooser(); 312 setHelpToChooser( chooser ); 313 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 314 chooser.setDialogTitle(getString("CTL_FileSystemPanel.Local_Dialog_Title")); 315 316 if (lastDirFolder != null) { 317 chooser.setCurrentDirectory(lastDirFolder); 318 } 319 320 if (chooser.showDialog(WindowManager.getDefault ().getMainWindow (), 321 getString("CTL_Approve_Button_Title")) 322 == JFileChooser.APPROVE_OPTION) { 323 File f = chooser.getSelectedFile(); 324 if ((f != null) && (f.isDirectory())) { 325 lastDirFolder = f.getParentFile(); 326 327 String path = f.getAbsolutePath(); 328 if (! listModel.contains (path)) { 329 listModel.addElement (path); 330 } 331 fireValueChanged(); 332 333 pathList.setSelectedIndex(listModel.size() - 1); 334 } 335 } 336 } 338 339 private javax.swing.JButton addDirButton; 341 private javax.swing.JButton addJarButton; 342 private javax.swing.JButton downButton; 343 private javax.swing.JPanel innerPanel; 344 private javax.swing.JLabel pathLabel; 345 private javax.swing.JList pathList; 346 private javax.swing.JScrollPane pathScrollPane; 347 private javax.swing.JButton removeButton; 348 private javax.swing.JButton upButton; 349 351 352 354 private void fireValueChanged() { 355 if (editor != null) { 356 editor.setValue(getPropertyValue()); 357 } 358 } 359 360 361 private void triggerEdit(int index) { 362 if (index < 0) { 363 return; 364 } 365 String selectedItem = (String )listModel.elementAt(index); 366 File selectedF = new File (selectedItem); 367 if (selectedF.isDirectory()) { 368 369 JFileChooser chooser = FileEditor.createHackedFileChooser(); 370 setHelpToChooser( chooser ); 371 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 372 chooser.setDialogTitle(getString("CTL_Edit_Local_Dialog_Title")); 373 374 if (selectedF.getParentFile() != null) { 375 chooser.setCurrentDirectory(selectedF.getParentFile()); 376 chooser.setSelectedFile(selectedF); 377 } 378 379 if (chooser.showDialog(WindowManager.getDefault ().getMainWindow (), 380 getString("CTL_Approve_Button_Title")) 381 == JFileChooser.APPROVE_OPTION) { 382 File f = chooser.getSelectedFile(); 383 if ((f != null) && (f.isDirectory())) { 384 lastDirFolder = chooser.getCurrentDirectory(); 385 try { 386 f = f.getCanonicalFile (); 387 } catch(IOException ioe) { 388 } 390 391 listModel.set(index, f.getAbsolutePath()); 392 fireValueChanged(); 393 } 394 } 395 } else if (selectedF.isFile()) { 396 JFileChooser chooser = FileEditor.createHackedFileChooser(); 397 setHelpToChooser( chooser ); 398 399 chooser.setFileFilter(new FileFilter () { 400 public boolean accept(File f) { 401 return (f.isDirectory() || f.getName().endsWith(".jar") || f.getName().endsWith(".zip")); } 403 public String getDescription() { 404 return getString("CTL_JarArchivesMask"); 405 } 406 }); 407 408 chooser.setCurrentDirectory(selectedF.getParentFile()); 409 chooser.setSelectedFile(selectedF); 410 411 chooser.setDialogTitle(getString("CTL_Edit_Jar_Dialog_Title")); 412 if (chooser.showDialog(WindowManager.getDefault ().getMainWindow (), 413 getString("CTL_Approve_Button_Title")) 414 == JFileChooser.APPROVE_OPTION) { 415 File f = chooser.getSelectedFile(); 416 if ((f != null) && (f.isFile())) { 417 lastJarFolder = chooser.getCurrentDirectory(); 418 listModel.set(index, f.getAbsolutePath()); 419 fireValueChanged(); 420 } 421 } 422 } 423 } 424 425 426 private void swap(int index) { 427 if ((index < 0)||(index >= listModel.size() -1 )) { 428 return; 429 } 430 Object value = listModel.elementAt(index); 431 listModel.removeElement(value); 432 listModel.add(index + 1, value); 433 fireValueChanged(); 434 } 435 436 437 private void enableButtons() { 438 if ( ! editable ) 439 return; 440 removeButton.setEnabled(pathList.getSelectedIndices().length > 0); 441 if (pathList.getSelectedIndices().length == 1) { 442 downButton.setEnabled(pathList.getSelectedIndices()[0] < pathList.getModel().getSize() - 1); 443 upButton.setEnabled(pathList.getSelectedIndices()[0] > 0); 444 } else { 445 downButton.setEnabled(false); 446 upButton.setEnabled(false); 447 } 448 } 449 450 453 private void setClassPath(String classPath) { 454 StringTokenizer tok = new StringTokenizer (classPath, File.pathSeparator); 455 while (tok.hasMoreTokens()) { 456 String s = tok.nextToken(); 457 if (s.startsWith("\"")) { s = s.substring(1); 459 } 460 if (s.endsWith("\"")) { s = s.substring(0, s.length() -1 ); 462 } 463 if (! listModel.contains (s)) { 464 listModel.addElement(s); 465 } 466 } 467 468 } 469 470 475 public Object getPropertyValue() throws IllegalStateException { 476 List <String > list = Collections.list(NbCollections.checkedEnumerationByFilter(listModel.elements(), String .class, true)); 477 String []arr = list.toArray(new String [list.size()]); 478 return new NbClassPath(arr); 479 } 480 481 482 private static final String getString(String s) { 483 return NbBundle.getMessage(NbClassPathCustomEditor.class, s); 484 } 485 486 private void setHelpToChooser( JFileChooser chooser ) { 487 HelpCtx help = HelpCtx.findHelp( this ); 488 if ( help != null ) 489 HelpCtx.setHelpIDString(chooser, help.getHelpID()); 490 } 491 } 492 | Popular Tags |