1 19 20 package org.netbeans.modules.form.editors2; 21 22 import java.awt.*; 23 import java.awt.event.*; 24 import java.beans.*; 25 import java.io.*; 26 import java.net.MalformedURLException ; 27 import java.util.*; 28 import java.net.URL ; 29 30 import javax.swing.*; 31 import javax.swing.filechooser.FileFilter ; 32 import javax.swing.border.*; 33 34 import org.openide.*; 35 import org.openide.awt.Mnemonics; 36 import org.openide.loaders.*; 37 import org.openide.nodes.*; 38 import org.openide.explorer.ExplorerManager; 39 import org.openide.explorer.view.BeanTreeView; 40 import org.openide.explorer.propertysheet.*; 41 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor; 42 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor; 43 import org.openide.filesystems.FileUtil; 44 import org.openide.filesystems.FileObject; 45 import org.openide.filesystems.FileStateInvalidException; 46 import org.openide.util.NbBundle; 47 48 import org.netbeans.api.project.*; 49 import org.netbeans.api.java.classpath.ClassPath; 50 import org.netbeans.api.java.queries.SourceForBinaryQuery; 51 import org.netbeans.modules.form.FormModel; 52 import org.netbeans.modules.form.FormAwareEditor; 53 import org.netbeans.modules.form.FormDesignValue; 54 import org.netbeans.modules.form.FormEditor; 55 import org.netbeans.modules.form.FormProperty; 56 import org.netbeans.modules.form.NamedPropertyEditor; 57 58 66 public class IconEditor extends PropertyEditorSupport implements 67 XMLPropertyEditor, FormAwareEditor, NamedPropertyEditor { 68 69 public static final int TYPE_URL = 1; 70 71 public static final int TYPE_FILE = 2; 72 73 public static final int TYPE_CLASSPATH = 3; 74 75 private static final String URL_PREFIX = "URL"; 77 private static final String FILE_PREFIX = "File"; 79 private static final String CLASSPATH_PREFIX = "Classpath"; 81 87 private static String getString(String key) { 88 return NbBundle.getBundle(IconEditor.class).getString(key); 89 } 90 91 99 public static boolean isImage(String s) { 100 if (s == null) { 101 return false; 102 } 103 s = s.toLowerCase(); 104 return s.endsWith(".jpg") || s.endsWith(".gif") || s.endsWith(".jpeg") || s.endsWith(".jpe") || s.endsWith(".png"); } 107 108 114 private static String convert(String s) { 115 StringTokenizer st = new StringTokenizer(s, "\\"); StringBuffer sb = new StringBuffer (); 117 if (st.hasMoreElements()) { 118 sb.append(st.nextElement()); 119 while (st.hasMoreElements()) 120 sb.append("\\\\").append(st.nextElement()); } 122 return sb.toString(); 123 } 124 125 127 private FormModel formModel; 128 129 131 137 public int getSourceType() { 138 if (getValue() instanceof NbImageIcon) 139 return ((NbImageIcon)getValue()).type; 140 else 141 return TYPE_FILE; 142 } 143 144 150 public String getSourceName() { 151 if (getValue() instanceof NbImageIcon) 152 return ((NbImageIcon)getValue()).name; 153 else 154 return null; 155 } 156 157 159 public Object getValue() { 160 return super.getValue(); 161 } 162 163 public String getAsText() { 164 Object val = getValue(); 165 166 if (val == null) return "null"; 168 if (val instanceof NbImageIcon) { 169 NbImageIcon ii = (NbImageIcon)val; 170 switch (ii.type) { 171 case TYPE_URL: 172 return URL_PREFIX + ": " + ii.name; case TYPE_FILE: 174 return FILE_PREFIX + ": " + ii.name; case TYPE_CLASSPATH: 176 return CLASSPATH_PREFIX + ": " + ii.name; } 178 } 179 return null; 180 } 181 182 public void setAsText(String string) throws IllegalArgumentException { 183 setValue(iconFromText(string)); 184 } 185 186 public String getJavaInitializationString() { 187 if (getValue() instanceof NbImageIcon) { 188 NbImageIcon ii = (NbImageIcon)getValue(); 189 switch (ii.type) { 190 case TYPE_URL: return 191 "new javax.swing.JLabel() {\n" + " public javax.swing.Icon getIcon() {\n" + " try {\n" + " return new javax.swing.ImageIcon(\n" + " new java.net.URL(\"" + convert(ii.name) + "\")\n" + " );\n" + " } catch (java.net.MalformedURLException e) {\n" + " }\n" + " return null;\n" + " }\n" + "}.getIcon()"; case TYPE_FILE: return 203 "new javax.swing.ImageIcon(\"" + convert(ii.name) + "\")"; case TYPE_CLASSPATH: return 205 "new javax.swing.ImageIcon(getClass().getResource(\"" + convert(ii.name) + "\"))"; } 207 } 208 return "null"; } 210 211 public String [] getTags() { 212 return null; 213 } 214 215 public boolean isPaintable() { 216 return false; 217 } 218 219 public void paintValue(Graphics g, Rectangle rectangle) { 220 } 221 222 public boolean supportsCustomEditor() { 223 return true; 224 } 225 226 public Component getCustomEditor() { 227 return new IconPanel(); 228 } 229 230 236 private URL findResource(String resource) { 237 if (resource.startsWith("/")) { resource = resource.substring(1); 239 } 240 FileObject formFile = FormEditor.getFormDataObject(formModel).getFormFile(); 241 ClassPath classPath = ClassPath.getClassPath(formFile, ClassPath.SOURCE); 242 FileObject resourceObject = classPath.findResource(resource); 243 if (resourceObject == null) { 244 classPath = ClassPath.getClassPath(formFile, ClassPath.EXECUTE); 245 resourceObject = classPath.findResource(resource); 246 } 247 if (resourceObject == null) { 248 return null; 249 } else { 250 try { 251 return resourceObject.getURL(); 252 } catch (FileStateInvalidException fsie) { 253 ErrorManager.getDefault().notify(fsie); 254 return null; 255 } 256 } 257 } 258 259 public void setFormModel(FormModel model) { 260 this.formModel = model; 261 } 262 263 271 private NbImageIcon iconFromText(String string) throws IllegalArgumentException { 272 NbImageIcon ii; 273 try { 274 if (string.startsWith(FILE_PREFIX)) { 275 String s = string.substring(FILE_PREFIX.length() + 1).trim(); 276 ii = new NbImageIcon(s); 277 ii.type = TYPE_FILE; 278 ii.name = s; 279 } else 280 if (string.startsWith(CLASSPATH_PREFIX)) { 281 String s = string.substring(CLASSPATH_PREFIX.length() + 1).trim(); 282 283 if(("".equals(s)) || ("/".equals(s)) || ("///".equals(s)) || (s.endsWith("#"))) { return null; 291 } 292 293 URL u = findResource(s); 294 295 if (u == null) { 296 ii = new NbImageIcon(); 297 } else { 298 ii = new NbImageIcon(u); 299 } 300 301 ii.type = TYPE_CLASSPATH; 302 ii.name = s; 303 } else { 304 if (string.startsWith(URL_PREFIX)) { 305 String s = string.substring(URL_PREFIX.length() + 1).trim(); 306 try { 307 URL url = new URL (s); 308 ii = new NbImageIcon(url); 309 } catch (MalformedURLException muex) { 310 ii = new NbImageIcon(); 311 } 312 ii.type = TYPE_URL; 313 ii.name = s; 314 } else { 315 if (string.equals("null")) { ii = null; 317 } else { 318 ii = new NbImageIcon(string.trim()); 319 ii.type = TYPE_FILE; 320 ii.name = string; 321 } 322 } 323 } 324 } catch (Exception e) { 325 IllegalArgumentException iae = new IllegalArgumentException (); 326 throw iae; 327 } 328 return ii; 329 } 330 331 public String getDisplayName() { 333 return NbBundle.getBundle(getClass()).getString("CTL_IconEditor_DisplayName"); } 335 336 338 public static class NbImageIcon implements FormDesignValue, Serializable { 339 340 static final long serialVersionUID = 7018807466471349466L; 341 342 private ImageIcon icon = new ImageIcon(""); 343 344 private int type; 345 346 private String name; 347 348 public NbImageIcon() { 349 } 350 351 NbImageIcon(URL url) { 352 type = TYPE_URL; 353 try { 354 icon = new ImageIcon(url); 355 } catch (Exception ex) {} 356 } 357 358 NbImageIcon(String file) { 359 type = TYPE_FILE; 360 try { 361 icon = new ImageIcon(file); 362 } catch (Exception ex) {} 363 } 364 365 public NbImageIcon(NbImageIcon nbIcon) { 366 icon = nbIcon.icon; 367 type = nbIcon.type; 368 name = nbIcon.name; 369 } 370 371 String getName() { 372 return name; 373 } 374 375 public Object getDesignValue() { 376 return icon; 377 } 378 379 public String getDescription() { 380 return name; 381 } 382 383 public FormDesignValue copy(FormProperty formProperty) { 384 return new IconEditor.NbImageIcon(this); 385 } 386 } 387 388 389 private class IconPanel extends JPanel implements EnhancedCustomPropertyEditor, ActionListener { 390 391 static final long serialVersionUID = -6904264999063788703L; 392 393 private JPanel jPanel1; 394 private JLabel jLabel1; 395 private JRadioButton rbUrl; 396 private JRadioButton rbFile; 397 private JRadioButton rbClasspath; 398 private JRadioButton rbNoPicture; 399 private JLabel jLabel2; 400 private JLabel jLabel3; 401 private JLabel jLabel4; 402 private JLabel jLabel5; 403 private JPanel jPanel2; 404 private JLabel lName; 405 private JTextField tfName; 406 private JButton bSelect; 407 private JPanel jPanel3; 408 private JLabel jLabel7; 409 private JScrollPane spImage; 410 private JLabel iconLabel; 411 412 414 private NbImageIcon localIcon; 415 416 419 public IconPanel() { 420 iconLabel = new JLabel() { 421 public boolean isFocusTraversable() { 422 return true; 423 } 424 }; 425 iconLabel.setPreferredSize(new Dimension(32, 32)); 426 iconLabel.setHorizontalAlignment(SwingConstants.CENTER); 427 iconLabel.setVerticalAlignment(SwingConstants.CENTER); 428 429 initComponents(); 430 spImage.setViewportView(iconLabel); 431 432 jLabel1.setText(getString("CTL_ImageSourceType")); jLabel2.setText(getString("CTL_URLExample")); jLabel3.setText(getString("CTL_FileExample")); jLabel4.setText(getString("CTL_ClasspathExample")); jLabel5.setText(getString("CTL_Null")); lName.setLabelFor(tfName); 438 439 jLabel1.setLabelFor(jPanel1); 440 jLabel2.setLabelFor(rbUrl); 441 jLabel3.setLabelFor(rbFile); 442 jLabel4.setLabelFor(rbClasspath); 443 jLabel5.setLabelFor(rbNoPicture); 444 jLabel7.setLabelFor(iconLabel); 445 446 Mnemonics.setLocalizedText(rbUrl, getString("CTL_URL")); Mnemonics.setLocalizedText(rbFile, getString("CTL_File")); Mnemonics.setLocalizedText(rbClasspath, getString("CTL_Classpath")); Mnemonics.setLocalizedText(rbNoPicture, getString("CTL_NoPicture")); Mnemonics.setLocalizedText(lName, getString("CTL_ImageSourceName")); Mnemonics.setLocalizedText(jLabel7, getString("CTL_Preview")); Mnemonics.setLocalizedText(bSelect, getString("CTL_ButtonSelect")); 454 tfName.getAccessibleContext().setAccessibleDescription(getString("ACSD_CTL_ImageSourceName")); bSelect.getAccessibleContext().setAccessibleDescription(getString("ACSD_CTL_ButtonSelect")); iconLabel.getAccessibleContext().setAccessibleDescription(getString("ACSD_CTL_Preview")); rbUrl.getAccessibleContext().setAccessibleDescription(jLabel2.getText()); 458 rbFile.getAccessibleContext().setAccessibleDescription(jLabel3.getText()); 459 rbClasspath.getAccessibleContext().setAccessibleDescription(jLabel4.getText()); 460 rbNoPicture.getAccessibleContext().setAccessibleDescription(jLabel5.getText()); 461 462 getAccessibleContext().setAccessibleDescription(getString("ACSD_IconCustomEditor")); 464 ButtonGroup bg = new ButtonGroup(); 465 bg.add(rbUrl); 466 bg.add(rbFile); 467 bg.add(rbClasspath); 468 bg.add(rbNoPicture); 469 470 Object value = getValue(); 471 472 if (!(value instanceof NbImageIcon)) { 473 rbNoPicture.setSelected(true); 474 bSelect.setEnabled(false); 475 tfName.setEnabled(false); 476 return; 477 } else { 478 localIcon = (NbImageIcon)value; 479 } 480 481 switch (((NbImageIcon)localIcon).type) { 482 case TYPE_URL: 483 rbUrl.setSelected(true); 484 bSelect.setEnabled(false); 485 break; 486 case TYPE_FILE: 487 rbFile.setSelected(true); 488 bSelect.setEnabled(true); 489 break; 490 case TYPE_CLASSPATH: 491 rbClasspath.setSelected(true); 492 bSelect.setEnabled(true); 493 break; 494 } 495 tfName.setText(((NbImageIcon)localIcon).name); 496 497 updateIcon(); 498 } 499 500 503 private void initComponents() { 504 jPanel1 = new JPanel(); 505 jLabel1 = new JLabel(); 506 rbUrl = new JRadioButton(); 507 rbFile = new JRadioButton(); 508 rbClasspath = new JRadioButton(); 509 rbNoPicture = new JRadioButton(); 510 jLabel2 = new JLabel(); 511 jLabel3 = new JLabel(); 512 jLabel4 = new JLabel(); 513 jLabel5 = new JLabel(); 514 jPanel2 = new JPanel(); 515 lName = new JLabel(); 516 tfName = new JTextField(); 517 bSelect = new JButton(); 518 jPanel3 = new JPanel(); 519 jLabel7 = new JLabel(); 520 spImage = new JScrollPane(); 521 522 setLayout(new GridBagLayout()); 523 GridBagConstraints gridBagConstraints2; 524 525 jPanel1.setLayout(new GridBagLayout()); 526 GridBagConstraints gridBagConstraints1; 527 528 gridBagConstraints1 = new GridBagConstraints(); 529 gridBagConstraints1.insets = new Insets(12, 12, 0, 0); 530 gridBagConstraints1.anchor = GridBagConstraints.WEST; 531 jPanel1.add(jLabel1, gridBagConstraints1); 532 533 gridBagConstraints1 = new GridBagConstraints(); 534 gridBagConstraints1.gridx = 0; 535 gridBagConstraints1.gridy = 1; 536 gridBagConstraints1.insets = new Insets(12, 24, 0, 0); 537 gridBagConstraints1.anchor = GridBagConstraints.WEST; 538 jPanel1.add(rbUrl, gridBagConstraints1); 539 540 gridBagConstraints1 = new GridBagConstraints(); 541 gridBagConstraints1.gridx = 0; 542 gridBagConstraints1.gridy = 2; 543 gridBagConstraints1.insets = new Insets(0, 24, 0, 0); 544 gridBagConstraints1.anchor = GridBagConstraints.WEST; 545 jPanel1.add(rbFile, gridBagConstraints1); 546 547 gridBagConstraints1 = new GridBagConstraints(); 548 gridBagConstraints1.gridx = 0; 549 gridBagConstraints1.gridy = 3; 550 gridBagConstraints1.insets = new Insets(0, 24, 0, 0); 551 gridBagConstraints1.anchor = GridBagConstraints.WEST; 552 jPanel1.add(rbClasspath, gridBagConstraints1); 553 554 gridBagConstraints1 = new GridBagConstraints(); 555 gridBagConstraints1.gridx = 0; 556 gridBagConstraints1.gridy = 4; 557 gridBagConstraints1.insets = new Insets(0, 24, 0, 0); 558 gridBagConstraints1.anchor = GridBagConstraints.WEST; 559 jPanel1.add(rbNoPicture, gridBagConstraints1); 560 561 gridBagConstraints1 = new GridBagConstraints(); 562 gridBagConstraints1.gridx = 1; 563 gridBagConstraints1.gridy = 1; 564 gridBagConstraints1.insets = new Insets(12, 5, 0, 12); 565 gridBagConstraints1.anchor = GridBagConstraints.WEST; 566 jPanel1.add(jLabel2, gridBagConstraints1); 567 568 gridBagConstraints1 = new GridBagConstraints(); 569 gridBagConstraints1.gridx = 1; 570 gridBagConstraints1.gridy = 2; 571 gridBagConstraints1.insets = new Insets(5, 5, 0, 12); 572 gridBagConstraints1.anchor = GridBagConstraints.WEST; 573 jPanel1.add(jLabel3, gridBagConstraints1); 574 575 gridBagConstraints1 = new GridBagConstraints(); 576 gridBagConstraints1.gridx = 1; 577 gridBagConstraints1.gridy = 3; 578 gridBagConstraints1.insets = new Insets(5, 5, 0, 12); 579 gridBagConstraints1.anchor = GridBagConstraints.WEST; 580 jPanel1.add(jLabel4, gridBagConstraints1); 581 582 gridBagConstraints1 = new GridBagConstraints(); 583 gridBagConstraints1.gridx = 1; 584 gridBagConstraints1.gridy = 4; 585 gridBagConstraints1.insets = new Insets(5, 5, 0, 12); 586 gridBagConstraints1.anchor = GridBagConstraints.WEST; 587 gridBagConstraints1.weightx = 1.0; 588 gridBagConstraints1.weighty = 1.0; 589 jPanel1.add(jLabel5, gridBagConstraints1); 590 591 gridBagConstraints2 = new GridBagConstraints(); 592 gridBagConstraints2.fill = GridBagConstraints.BOTH; 593 add(jPanel1, gridBagConstraints2); 594 595 jPanel2.setLayout(new GridBagLayout()); 596 GridBagConstraints gridBagConstraints3; 597 598 gridBagConstraints3 = new GridBagConstraints(); 599 gridBagConstraints3.insets = new Insets(12, 12, 0, 0); 600 gridBagConstraints3.anchor = GridBagConstraints.WEST; 601 jPanel2.add(lName, gridBagConstraints3); 602 603 gridBagConstraints3 = new GridBagConstraints(); 604 gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL; 605 gridBagConstraints3.insets = new Insets(12, 5, 0, 0); 606 gridBagConstraints3.anchor = GridBagConstraints.WEST; 607 gridBagConstraints3.weightx = 1.0; 608 jPanel2.add(tfName, gridBagConstraints3); 609 610 gridBagConstraints3 = new GridBagConstraints(); 611 gridBagConstraints3.insets = new Insets(12, 5, 0, 17); 612 gridBagConstraints3.anchor = GridBagConstraints.WEST; 613 jPanel2.add(bSelect, gridBagConstraints3); 614 615 gridBagConstraints2 = new GridBagConstraints(); 616 gridBagConstraints2.gridx = 0; 617 gridBagConstraints2.gridy = 1; 618 gridBagConstraints2.fill = GridBagConstraints.BOTH; 619 add(jPanel2, gridBagConstraints2); 620 621 jPanel3.setLayout(new GridBagLayout()); 622 GridBagConstraints gridBagConstraints4; 623 624 gridBagConstraints4 = new GridBagConstraints(); 625 gridBagConstraints4.insets = new Insets(12, 12, 0, 0); 626 gridBagConstraints4.anchor = GridBagConstraints.WEST; 627 jPanel3.add(jLabel7, gridBagConstraints4); 628 629 gridBagConstraints4 = new GridBagConstraints(); 630 gridBagConstraints4.gridx = 0; 631 gridBagConstraints4.gridy = 1; 632 gridBagConstraints4.fill = GridBagConstraints.BOTH; 633 gridBagConstraints4.insets = new Insets(5, 12, 0, 12); 634 gridBagConstraints4.weightx = 1.0; 635 gridBagConstraints4.weighty = 1.0; 636 jPanel3.add(spImage, gridBagConstraints4); 637 638 gridBagConstraints2 = new GridBagConstraints(); 639 gridBagConstraints2.gridx = 0; 640 gridBagConstraints2.gridy = 2; 641 gridBagConstraints2.fill = GridBagConstraints.BOTH; 642 gridBagConstraints2.weightx = 1.0; 643 gridBagConstraints2.weighty = 1.0; 644 add(jPanel3, gridBagConstraints2); 645 646 648 tfName.addActionListener(this); 649 rbFile.addActionListener(this); 650 rbUrl.addActionListener(this); 651 rbClasspath.addActionListener(this); 652 rbNoPicture.addActionListener(this); 653 bSelect.addActionListener(this); 654 } 655 656 public void actionPerformed(ActionEvent e) { 657 Object source = e.getSource(); 658 if (source == tfName) { 659 setValue(); 660 } else if (source == rbFile) { 661 bSelect.setEnabled(true); 662 tfName.setEnabled(true); 663 setValue(); 664 updateIcon(); 665 } else if (source == rbUrl) { 666 bSelect.setEnabled(false); 667 tfName.setEnabled(true); 668 setValue(); 669 } else if (source == rbClasspath) { 670 bSelect.setEnabled(true); 671 tfName.setEnabled(true); 672 setValue(); 673 } else if (source == rbNoPicture) { 674 bSelect.setEnabled(false); 675 tfName.setEnabled(false); 676 localIcon = null; 677 updateIcon(); 678 } else if (source == bSelect) { 679 if(rbFile.isSelected()) { 680 File f = selectFile(); 681 if (f != null) { 682 tfName.setText(f.getAbsolutePath()); 683 setValue(); 684 } 685 } else { 686 if (rbClasspath.isSelected()) { 687 String name = selectResource(); 688 if (name != null) { 689 tfName.setText("/" + name); setValue(); 691 } 692 } 693 } 694 695 } 696 } 697 698 699 704 private File selectFile() { 705 final File[] ff = new File[1]; 706 final FeatureDescriptor fd = new FeatureDescriptor(); 707 ExPropertyModel epm = new ExPropertyModel() { 708 public void setValue(Object val) { 709 ff[0] = (File)val; 710 } 711 public Object getValue() { 712 return ff[0]; 713 } 714 public Class getPropertyType() { 715 return File.class; 716 } 717 public Class getPropertyEditorClass() { 718 return null; 719 } 720 public void addPropertyChangeListener(PropertyChangeListener l) { 721 } 722 public void removePropertyChangeListener(PropertyChangeListener l) { 723 } 724 public Object [] getBeans() { 725 return new Object [0]; 726 } 727 public FeatureDescriptor getFeatureDescriptor() { 728 return fd; 729 } 730 }; 731 FileFilter filter = new FileFilter () { 732 public boolean accept(java.io.File f) { 733 return isImage(f.getName()) || f.isDirectory(); 734 } 735 public String getDescription() { 736 return getString("CTL_ImagesExtensionName"); } 738 }; 739 fd.setValue("directories", Boolean.FALSE); fd.setValue("files", Boolean.TRUE); fd.setValue("filter", filter); PropertyPanel panel = new PropertyPanel(epm, PropertyPanel.PREF_CUSTOM_EDITOR); 743 DialogDescriptor dd = new DialogDescriptor(panel, getString("CTL_OpenDialogName"), true, null); Object res = DialogDisplayer.getDefault().notify(dd); 745 if (res == DialogDescriptor.OK_OPTION) { 746 return ff[0]; 747 } else { 748 return null; 749 } 750 } 751 752 private java.util.List getRoots(ClassPath cp) { 753 ArrayList l = new ArrayList(cp.entries().size()); 754 Iterator eit = cp.entries().iterator(); 755 while(eit.hasNext()) { 756 ClassPath.Entry e = (ClassPath.Entry)eit.next(); 757 758 URL url = e.getURL(); 760 SourceForBinaryQuery.Result r= SourceForBinaryQuery.findSourceRoots(url); 761 FileObject [] fos = r.getRoots(); 762 if (fos.length > 0) { 763 for (int i = 0 ; i < fos.length; i++) l.add(fos[i]); 764 } else { 765 if (e.getRoot()!=null) 766 l.add(e.getRoot()); } 769 } 770 771 return l; 772 } 773 774 779 private String selectResource() { 780 FileObject formFile = FormEditor.getFormDataObject(formModel).getFormFile(); 781 ClassPath executeClassPath = ClassPath.getClassPath(formFile, ClassPath.EXECUTE); 782 java.util.List roots = (executeClassPath == null) ? Collections.EMPTY_LIST : getRoots(executeClassPath); 783 Project project = FileOwnerQuery.getOwner(formFile); 784 Node nodes[] = new Node[roots.size()]; 785 int selRoot = -1; 786 try { 787 ListIterator iter = roots.listIterator(); 788 while (iter.hasNext()) { 789 FileObject root = (FileObject)iter.next(); 790 DataObject dob = DataObject.find(root); 791 Project owner = FileOwnerQuery.getOwner(root); 792 final String displayName = rootDisplayName(root, owner, owner != project); 793 nodes[iter.previousIndex()] = new RootNode(dob.getNodeDelegate(), displayName); 794 } 795 } catch (DataObjectNotFoundException donfex) { 796 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, donfex); 797 return null; 798 } 799 Children children = new Children.Array(); 800 children.add(nodes); 801 final AbstractNode root = new AbstractNode(children); 802 root.setIconBase("org/netbeans/modules/form/editors2/iconResourceRoot"); root.setDisplayName(getString("CTL_ClassPathName")); 805 ResourceSelector selector = new ResourceSelector(root); 806 DialogDescriptor dd = new DialogDescriptor(selector, getString("CTL_OpenDialogName")); Object res = DialogDisplayer.getDefault().notify(dd); 808 nodes = (res == DialogDescriptor.OK_OPTION) ? selector.getNodes() : null; 809 String name = null; 810 if ((nodes != null) && (nodes.length == 1)) { 811 DataObject dob = (DataObject)nodes[0].getCookie(DataObject.class); 812 if (dob != null) { 813 FileObject fob = dob.getPrimaryFile(); 814 if (fob != null) { 815 if (executeClassPath.contains(fob)) { 816 name = executeClassPath.getResourceName(fob); 817 } else { 818 ClassPath sourceClassPath = ClassPath.getClassPath(fob, ClassPath.SOURCE); 819 name = sourceClassPath.getResourceName(fob); 820 } 821 } 822 } 823 } 824 return name; 825 } 826 827 private String rootDisplayName(FileObject fo, Project owner, boolean withProjectName) { 828 if (owner != null) { 829 SourceGroup grp = sourceGroup(fo, owner); 830 String name = (grp!=null) ? grp.getDisplayName() : FileUtil.getFileDisplayName(fo); 831 if (withProjectName) { 832 ProjectInformation pi = ProjectUtils.getInformation(owner); 833 name += " [" + pi.getDisplayName() + "]"; } 835 return name; 836 } else 837 return FileUtil.getFileDisplayName(fo); 838 } 839 840 private SourceGroup sourceGroup(FileObject file, Project prj) { 841 Sources src = ProjectUtils.getSources(prj); 842 SourceGroup[] srcgrps = src.getSourceGroups("java"); for (int i = 0 ; i < srcgrps.length; i++) { 844 if (file == srcgrps[i].getRootFolder()) 845 return srcgrps[i]; 846 } 847 return null; 848 } 849 850 873 874 881 public Object getPropertyValue() throws IllegalStateException { 882 NbImageIcon ii = null; 883 String s = tfName.getText().trim(); 884 885 if (("".equals(s)) || ("/".equals(s)) || ("///".equals(s)) || (s.endsWith("#"))) { return null; 893 } 894 895 if (rbFile.isSelected()) { 896 ii = new NbImageIcon(s); 897 ii.type = TYPE_FILE; 898 ii.name = s; 899 } else if (rbClasspath.isSelected()) { 900 URL url = findResource(s); 901 ii = new NbImageIcon(url); 902 ii.type = TYPE_CLASSPATH; 903 ii.name = s; 904 } else if (rbUrl.isSelected()) { 905 URL url = null; 906 try { 907 url = new URL (s); 908 } catch (MalformedURLException ex) {} 909 ii = new NbImageIcon(url); 910 ii.type = TYPE_URL; 911 ii.name = s; 912 } 913 return ii; 914 } 915 916 void updateIcon() { 917 IconEditor.this.setValue(localIcon); 918 iconLabel.setIcon((localIcon == null) ? null : localIcon.icon); 919 iconLabel.setEnabled(localIcon != null); 920 validate(); 921 } 922 923 void setValue() { 924 String val = tfName.getText(); 925 if ("".equals(val)) { localIcon = null; 927 updateIcon(); 928 return; 929 } 930 931 String pref = ""; if (rbUrl.isSelected()) pref = URL_PREFIX + ": "; else 934 if (rbFile.isSelected()) pref = FILE_PREFIX + ": "; else 936 if (rbClasspath.isSelected()) pref = CLASSPATH_PREFIX + ": "; try { 938 localIcon = iconFromText(pref + val); 939 } catch (IllegalArgumentException ee) { 940 localIcon = null; 941 } 942 updateIcon(); 943 } 944 945 } 947 private static class RootNode extends FilterNode { 948 949 RootNode(Node node, String displayName) { 950 super(node); 951 if (displayName != null) { 952 disableDelegation(DELEGATE_GET_DISPLAY_NAME | DELEGATE_SET_DISPLAY_NAME); 953 setDisplayName(displayName); 954 } 955 } 956 957 } 958 959 private static class ResourceSelector extends JPanel implements ExplorerManager.Provider { 960 961 private ExplorerManager manager = new ExplorerManager(); 962 963 public ResourceSelector(Node root) { 964 ResourceBundle bundle = NbBundle.getBundle(ResourceSelector.class); 965 966 setLayout(new BorderLayout(0, 5)); 967 setBorder(new EmptyBorder(12, 12, 0, 11)); 968 getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ResourceSelector")); getAccessibleContext().setAccessibleName(bundle.getString("ACSN_ResourceSelector")); manager.setRootContext(root); 971 972 BeanTreeView tree = new BeanTreeView(); 973 tree.setPopupAllowed(false); 974 tree.setDefaultActionAllowed(false); 975 tree.setBorder((Border)UIManager.get("Nb.ScrollPane.border")); tree.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_ResourceSelectorView")); tree.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_ResourceSelectorView")); add(tree, BorderLayout.CENTER); 980 } 981 982 986 public Dimension getPreferredSize() { 987 Dimension dim = super.getPreferredSize(); 988 dim.height = Math.max(dim.height, org.openide.util.Utilities.getUsableScreenBounds().height / 2); 989 return dim; 990 } 991 992 995 public Node[] getNodes() { 996 return manager.getSelectedNodes(); 997 } 998 999 public ExplorerManager getExplorerManager() { 1000 return manager; 1001 } 1002 1003 } 1004 1005 1007 1008 public static final String XML_IMAGE = "Image"; 1010 1011 public static final String ATTR_TYPE = "iconType"; 1013 public static final String ATTR_NAME = "name"; 1015 public void readFromXML(org.w3c.dom.Node element) throws java.io.IOException { 1016 if (!XML_IMAGE.equals(element.getNodeName())) { 1017 throw new java.io.IOException (); 1018 } 1019 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 1020 try { 1021 int type = Integer.parseInt(attributes.getNamedItem(ATTR_TYPE).getNodeValue()); 1022 String name = attributes.getNamedItem(ATTR_NAME).getNodeValue(); 1023 switch (type) { 1024 case 0: setValue(null); break; 1025 case TYPE_URL: setAsText(URL_PREFIX + ": " + name); break; case TYPE_FILE: setAsText(FILE_PREFIX + ": " + name); break; case TYPE_CLASSPATH: setAsText(CLASSPATH_PREFIX + ": " + name); break; } 1029 } catch (NullPointerException e) { 1030 java.io.IOException ioe = new java.io.IOException (); 1031 ErrorManager.getDefault().annotate(ioe, e); 1032 throw ioe; 1033 } 1034 } 1035 1036 public org.w3c.dom.Node storeToXML(org.w3c.dom.Document doc) { 1037 org.w3c.dom.Element el = doc.createElement(XML_IMAGE); 1038 if (getValue() instanceof NbImageIcon) { 1039 NbImageIcon ii = (NbImageIcon)getValue(); 1040 el.setAttribute(ATTR_TYPE, Integer.toString(ii.type)); 1041 el.setAttribute(ATTR_NAME, ii.name); 1042 } else { 1043 el.setAttribute(ATTR_TYPE, "0"); el.setAttribute(ATTR_NAME, "null"); } 1046 return el; 1047 } 1048 1049} 1050 | Popular Tags |