1 19 20 package org.netbeans.modules.apisupport.project.ui; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.awt.Dimension ; 25 import java.awt.FlowLayout ; 26 import java.awt.GridLayout ; 27 import java.awt.Image ; 28 import java.awt.event.InputEvent ; 29 import java.awt.event.KeyEvent ; 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.lang.ref.Reference ; 33 import java.lang.ref.WeakReference ; 34 import java.net.MalformedURLException ; 35 import java.text.Collator ; 36 import java.text.MessageFormat ; 37 import java.util.ArrayList ; 38 import java.util.Collection ; 39 import java.util.Collections ; 40 import java.util.HashSet ; 41 import java.util.List ; 42 import java.util.Locale ; 43 import java.util.Map ; 44 import java.util.SortedSet ; 45 import java.util.Stack ; 46 import java.util.StringTokenizer ; 47 import java.util.TreeSet ; 48 import javax.swing.ComboBoxModel ; 49 import javax.swing.DefaultComboBoxModel ; 50 import javax.swing.DefaultListCellRenderer ; 51 import javax.swing.Icon ; 52 import javax.swing.ImageIcon ; 53 import javax.swing.JButton ; 54 import javax.swing.JComboBox ; 55 import javax.swing.JFileChooser ; 56 import javax.swing.JLabel ; 57 import javax.swing.JList ; 58 import javax.swing.JPanel ; 59 import javax.swing.JTextField ; 60 import javax.swing.KeyStroke ; 61 import javax.swing.ListCellRenderer ; 62 import javax.swing.UIManager ; 63 import javax.swing.event.DocumentEvent ; 64 import javax.swing.event.DocumentListener ; 65 import javax.swing.filechooser.FileFilter ; 66 import javax.swing.filechooser.FileView ; 67 import javax.swing.plaf.UIResource ; 68 import javax.swing.text.JTextComponent ; 69 import org.netbeans.api.project.Project; 70 import org.netbeans.api.project.ProjectInformation; 71 import org.netbeans.api.project.ProjectManager; 72 import org.netbeans.api.project.ProjectUtils; 73 import org.netbeans.api.project.SourceGroup; 74 import org.netbeans.api.project.ui.OpenProjects; 75 import org.netbeans.modules.apisupport.project.NbModuleProject; 76 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider; 77 import org.netbeans.modules.apisupport.project.Util; 78 import org.netbeans.modules.apisupport.project.layers.LayerUtils; 79 import org.netbeans.modules.apisupport.project.suite.SuiteProject; 80 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteUtils; 81 import org.netbeans.modules.apisupport.project.ui.wizard.NewNbModuleWizardIterator; 82 import org.netbeans.spi.java.project.support.ui.PackageView; 83 import org.netbeans.spi.project.ui.support.ProjectChooser; 84 import org.openide.DialogDescriptor; 85 import org.openide.DialogDisplayer; 86 import org.openide.ErrorManager; 87 import org.openide.NotifyDescriptor; 88 import org.openide.WizardDescriptor; 89 import org.openide.awt.Mnemonics; 90 import org.openide.filesystems.FileObject; 91 import org.openide.filesystems.FileStateInvalidException; 92 import org.openide.filesystems.FileSystem; 93 import org.openide.filesystems.FileUtil; 94 import org.openide.util.NbBundle; 95 import org.openide.util.NbCollections; 96 import org.openide.util.Utilities; 97 98 103 public final class UIUtil { 104 105 private static final String ICON_KEY_UIMANAGER = "Tree.closedIcon"; private static final String OPENED_ICON_KEY_UIMANAGER = "Tree.openIcon"; private static final String ICON_KEY_UIMANAGER_NB = "Nb.Explorer.Folder.icon"; private static final String OPENED_ICON_KEY_UIMANAGER_NB = "Nb.Explorer.Folder.openedIcon"; private static final String ICON_PATH = "org/netbeans/modules/apisupport/project/resources/defaultFolder.gif"; private static final String OPENED_ICON_PATH = "org/netbeans/modules/apisupport/project/resources/defaultFolderOpen.gif"; 112 private UIUtil() {} 113 114 public static String keyToLogicalString(KeyStroke keyStroke) { 115 String keyDesc = Utilities.keyToString(keyStroke); 116 int dash = keyDesc.indexOf('-'); 117 return dash == -1 ? keyDesc : 118 keyDesc.substring(0, dash).replace('C', 'D').replace('A', 'O') + keyDesc.substring(dash); 119 } 120 121 public static String keyStrokeToString(KeyStroke keyStroke) { 122 int modifiers = keyStroke.getModifiers(); 123 StringBuffer sb = new StringBuffer (); 124 if ((modifiers & InputEvent.CTRL_DOWN_MASK) > 0) { 125 sb.append("Ctrl+"); } 127 if ((modifiers & InputEvent.ALT_DOWN_MASK) > 0) { 128 sb.append("Alt+"); } 130 if ((modifiers & InputEvent.SHIFT_DOWN_MASK) > 0) { 131 sb.append("Shift+"); } 133 if ((modifiers & InputEvent.META_DOWN_MASK) > 0) { 134 sb.append("Meta+"); } 136 if (keyStroke.getKeyCode() != KeyEvent.VK_SHIFT && 137 keyStroke.getKeyCode() != KeyEvent.VK_CONTROL && 138 keyStroke.getKeyCode() != KeyEvent.VK_META && 139 keyStroke.getKeyCode() != KeyEvent.VK_ALT && 140 keyStroke.getKeyCode() != KeyEvent.VK_ALT_GRAPH) { 141 sb.append(Utilities.keyToString( 142 KeyStroke.getKeyStroke(keyStroke.getKeyCode(), 0))); 143 } 144 return sb.toString(); 145 } 146 147 public static KeyStroke stringToKeyStroke(String keyStroke) { 148 int modifiers = 0; 149 if (keyStroke.startsWith("Ctrl+")) { modifiers |= InputEvent.CTRL_DOWN_MASK; 151 keyStroke = keyStroke.substring(5); 152 } 153 if (keyStroke.startsWith("Alt+")) { modifiers |= InputEvent.ALT_DOWN_MASK; 155 keyStroke = keyStroke.substring(4); 156 } 157 if (keyStroke.startsWith("Shift+")) { modifiers |= InputEvent.SHIFT_DOWN_MASK; 159 keyStroke = keyStroke.substring(6); 160 } 161 if (keyStroke.startsWith("Meta+")) { modifiers |= InputEvent.META_DOWN_MASK; 163 keyStroke = keyStroke.substring(5); 164 } 165 KeyStroke ks = Utilities.stringToKey(keyStroke); 166 if (ks == null) { 167 return null; 168 } 169 KeyStroke result = KeyStroke.getKeyStroke(ks.getKeyCode(), modifiers); 170 return result; 171 } 172 173 178 public static KeyStroke [] stringToKeyStrokes(String keyStrokes) { 179 String delim = " "; if (keyStrokes.length() == 0) { 181 return new KeyStroke [0]; 182 } 183 StringTokenizer st = new StringTokenizer (keyStrokes, delim); 184 List <KeyStroke > result = new ArrayList <KeyStroke >(); 185 while (st.hasMoreTokens()) { 186 String ks = st.nextToken().trim(); 187 KeyStroke keyStroke = stringToKeyStroke(ks); 188 if (keyStroke == null) { return null; 190 } 191 result.add(keyStroke); 192 } 193 return result.toArray(new KeyStroke [result.size()]); 194 } 195 196 public static String keyStrokesToString(final KeyStroke [] keyStrokes) { 197 StringBuffer sb = new StringBuffer (UIUtil.keyStrokeToString(keyStrokes [0])); 198 int i, k = keyStrokes.length; 199 for (i = 1; i < k; i++) { 200 sb.append(' ').append(UIUtil.keyStrokeToString(keyStrokes [i])); 201 } 202 String newShortcut = sb.toString(); 203 return newShortcut; 204 } 205 206 public static String keyStrokesToLogicalString(final KeyStroke [] keyStrokes) { 207 StringBuffer sb = new StringBuffer (UIUtil.keyToLogicalString(keyStrokes [0])); 208 int i, k = keyStrokes.length; 209 for (i = 1; i < k; i++) { 210 sb.append(' ').append(UIUtil.keyToLogicalString((keyStrokes [i]))); 211 } 212 String newShortcut = sb.toString(); 213 return newShortcut; 214 } 215 216 220 public static void setProjectChooserDir(File folder) { 221 if (folder == null || !folder.isDirectory()) { 222 return; 223 } 224 ProjectChooser.setProjectsFolder(folder); 225 } 226 227 232 public static void setProjectChooserDirParent(File fileOrFolder) { 233 if (fileOrFolder == null) { 234 return; 235 } 236 File parent = fileOrFolder.getParentFile(); 237 setProjectChooserDir(parent != null ? parent : 238 (fileOrFolder.isDirectory() ? fileOrFolder : null)); 239 } 240 241 245 public static void setText(JTextComponent textComp, String text) { 246 textComp.setText(text); 247 textComp.setCaretPosition(text == null ? 0 : text.length()); 248 } 249 250 257 public abstract static class DocumentAdapter implements DocumentListener { 258 public void removeUpdate(DocumentEvent e) { insertUpdate(null); } 259 public void changedUpdate(DocumentEvent e) { insertUpdate(null); } 260 } 261 262 private static Reference <JFileChooser > iconChooser; 263 264 270 public static String getIconDimensionWarning(File icon, int expectedWidth, int expectedHeight) { 271 Dimension real = new Dimension (UIUtil.getIconDimension(icon)); 272 if (real.height == expectedHeight && real.width == expectedWidth) { 273 return ""; 274 } 275 return NbBundle.getMessage(UIUtil.class, "MSG_WrongIconSize",new Object [] { 276 real.width, 277 real.height, 278 expectedWidth, 279 expectedHeight 280 }); 281 } 282 283 288 public static String getNoIconSelectedWarning(int expectedWidth, int expectedHeight) { 289 return NbBundle.getMessage(UIUtil.class, "MSG_NoIconSelected", expectedWidth, expectedHeight); 290 } 291 292 298 public static boolean isValidIcon(final File icon, int expectedWidth, int expectedHeight) { 299 Dimension iconDimension = UIUtil.getIconDimension(icon); 300 return (expectedWidth == iconDimension.getWidth() && 301 expectedHeight == iconDimension.getHeight()); 302 } 303 304 308 public static Dimension getIconDimension(final File icon) { 309 try { 310 ImageIcon imc = new ImageIcon (icon.toURI().toURL()); 311 return new Dimension (imc.getIconWidth(), imc.getIconHeight()); 312 } catch (MalformedURLException ex) { 313 ErrorManager.getDefault().notify(ex); 314 } 315 return new Dimension (-1, -1); 316 } 317 318 322 public static JFileChooser getIconFileChooser() { 323 if (iconChooser != null) { 324 JFileChooser choose = iconChooser.get(); 325 if (choose != null) { 326 return choose; 327 } 328 } 329 final JFileChooser chooser = new IconFileChooser(); 330 iconChooser = new WeakReference <JFileChooser >(chooser); 331 return chooser; 332 } 333 334 335 339 public static JFileChooser getIconFileChooser(String oldValue) { 340 JFileChooser chooser = getIconFileChooser(); 341 String iconText = oldValue.trim(); 342 if ( iconText.length() > 0) { 343 File fil = new File (iconText); 344 if (fil.exists()) { 345 chooser.setSelectedFile(fil); 346 } 347 } 348 return chooser; 349 } 350 351 354 public static JComboBox createPackageComboBox(SourceGroup srcRoot) { 355 JComboBox packagesComboBox = new JComboBox (PackageView.createListView(srcRoot)); 356 packagesComboBox.setRenderer(PackageView.listRenderer()); 357 return packagesComboBox; 358 } 359 360 363 public static boolean isValidPackageName(String str) { 364 if (str.length() > 0 && str.charAt(0) == '.') { 365 return false; 366 } 367 StringTokenizer tukac = new StringTokenizer (str, "."); while (tukac.hasMoreTokens()) { 369 String token = tukac.nextToken(); 370 if ("".equals(token)) { 371 return false; 372 } 373 if (!Utilities.isJavaIdentifier(token)) { 374 return false; 375 } 376 } 377 return true; 378 } 379 380 388 public static String generateTextAreaContent(String [] relPaths) { 389 StringBuffer sb = new StringBuffer (); 390 if (relPaths.length > 0) { 391 for (int i = 0; i < relPaths.length; i++) { 392 if (i > 0) { 393 sb.append('\n'); 394 } 395 sb.append(relPaths[i]); 396 } 397 } 398 return sb.toString(); 399 } 400 401 405 public static ComboBoxModel createLayerPresenterComboModel( 406 final Project project, final String sfsRoot) { 407 return createLayerPresenterComboModel(project, sfsRoot, Collections.<String ,Object >emptyMap()); 408 } 409 410 418 public static ComboBoxModel createLayerPresenterComboModel( 419 final Project project, final String sfsRoot, final Map <String ,Object > excludeAttrs) { 420 DefaultComboBoxModel model = new DefaultComboBoxModel (); 421 try { 422 FileSystem sfs = LayerUtils.getEffectiveSystemFilesystem(project); 423 FileObject root = sfs.getRoot().getFileObject(sfsRoot); 424 if (root != null) { 425 SortedSet <LayerItemPresenter> presenters = new TreeSet <LayerItemPresenter>(); 426 for (FileObject subFolder : getFolders(root, excludeAttrs)) { 427 presenters.add(new LayerItemPresenter(subFolder, root)); 428 } 429 for (LayerItemPresenter presenter : presenters) { 430 model.addElement(presenter); 431 } 432 } 433 } catch (IOException exc) { 434 Util.err.notify(exc); 435 } 436 return model; 437 } 438 439 public static class LayerItemPresenter implements Comparable <LayerItemPresenter> { 440 441 private String displayName; 442 private final FileObject item; 443 private final FileObject root; 444 private final boolean contentType; 445 446 public LayerItemPresenter(final FileObject item, 447 final FileObject root, 448 final boolean contentType) { 449 this.item = item; 450 this.root = root; 451 this.contentType = contentType; 452 } 453 454 public LayerItemPresenter(final FileObject item, final FileObject root) { 455 this(item, root, false); 456 } 457 458 public FileObject getFileObject() { 459 return item; 460 } 461 462 public String getFullPath() { 463 return item.getPath(); 464 } 465 466 public String getDisplayName() { 467 if (displayName == null) { 468 displayName = computeDisplayName(); 469 } 470 return displayName; 471 } 472 473 public String toString() { 474 return getDisplayName(); 475 } 476 477 public int compareTo(LayerItemPresenter o) { 478 int res = Collator.getInstance().compare(getDisplayName(), o.getDisplayName()); 479 if (res != 0) { 480 return res; 481 } else { 482 return getFullPath().compareTo(o.getFullPath()); 483 } 484 } 485 486 private static String getFileObjectName(FileObject fo) { 487 String name = null; 488 try { 489 name = fo.getFileSystem().getStatus().annotateName( 490 fo.getNameExt(), Collections.singleton(fo)); 491 } catch (FileStateInvalidException ex) { 492 name = fo.getName(); 493 } 494 return name; 495 } 496 497 private String computeDisplayName() { 498 FileObject displayItem = contentType ? item.getParent() : item; 499 String displaySeparator = contentType ? "/" : " | "; Stack <String > s = new Stack <String >(); 501 s.push(getFileObjectName(displayItem)); 502 FileObject parent = displayItem.getParent(); 503 while (!root.getPath().equals(parent.getPath())) { 504 s.push(getFileObjectName(parent)); 505 parent = parent.getParent(); 506 } 507 StringBuffer sb = new StringBuffer (); 508 sb.append(s.pop()); 509 while (!s.empty()) { 510 sb.append(displaySeparator).append(s.pop()); 511 } 512 return sb.toString(); 513 } 514 515 } 516 517 522 public static String getSFSPath(final JComboBox lpCombo, final String supposedRoot) { 523 Object editorItem = lpCombo.getEditor().getItem(); 524 String path = null; 525 if (editorItem instanceof LayerItemPresenter) { 526 path = ((LayerItemPresenter) editorItem).getFullPath(); 527 } else if (editorItem instanceof String ) { 528 String editorItemS = ((String ) editorItem).trim(); 529 if (editorItemS.length() > 0) { 530 path = searchLIPCategoryCombo(lpCombo, editorItemS); 531 if (path == null) { 532 path = editorItemS.startsWith(supposedRoot) ? editorItemS : 534 supposedRoot + '/' + editorItemS; 535 } 536 } 537 } 538 return path; 539 } 540 541 public static NbModuleProject chooseSuiteComponent(Component parent, SuiteProject suite) { 542 NbModuleProject suiteComponent = null; 543 Project project = chooseProject(parent); 544 if (project != null) { 545 if (SuiteUtils.getSubProjects(suite).contains(project)) { 546 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 547 NbBundle.getMessage(UIUtil.class, "MSG_SuiteAlreadyContainsProject", 548 ProjectUtils.getInformation(suite).getDisplayName(), 549 ProjectUtils.getInformation(project).getDisplayName()))); 550 return null; 551 } 552 NbModuleProvider nmtp = project.getLookup().lookup(NbModuleProvider.class); 553 if (nmtp == null) { DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 555 NbBundle.getMessage(UIUtil.class, "MSG_TryingToAddNonNBModule", 556 ProjectUtils.getInformation(project).getDisplayName()))); 557 } else if (nmtp.getModuleType() == NbModuleProvider.SUITE_COMPONENT) { 558 Object [] params = new Object [] { 559 ProjectUtils.getInformation(project).getDisplayName(), 560 getSuiteProjectName(project), 561 getSuiteProjectDirectory(project), 562 ProjectUtils.getInformation(suite).getDisplayName(), 563 }; 564 NotifyDescriptor.Confirmation confirmation = new NotifyDescriptor.Confirmation( 565 NbBundle.getMessage(UIUtil.class, "MSG_MoveFromSuiteToSuite", params), 566 NotifyDescriptor.OK_CANCEL_OPTION); 567 DialogDisplayer.getDefault().notify(confirmation); 568 if (confirmation.getValue() == NotifyDescriptor.OK_OPTION) { 569 suiteComponent = (NbModuleProject) project; 570 } 571 } else if (nmtp.getModuleType() == NbModuleProvider.STANDALONE) { 572 suiteComponent = (NbModuleProject) project; 573 } else if (nmtp.getModuleType() == NbModuleProvider.NETBEANS_ORG) { 574 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 575 NbBundle.getMessage(UIUtil.class, "MSG_TryingToAddNBORGModule", 576 ProjectUtils.getInformation(project).getDisplayName()))); 577 } 578 } 579 return suiteComponent; 580 } 581 582 586 public static ListCellRenderer createProjectRenderer() { 587 return new ProjectRenderer(); 588 } 589 590 private static class ProjectRenderer extends JLabel implements ListCellRenderer , UIResource { 591 592 public ProjectRenderer () { 593 setOpaque(true); 594 } 595 596 public Component getListCellRendererComponent( 597 JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 598 setName("ComboBox.listRenderer"); 601 String text = null; 602 if (!(value instanceof Project)) { 603 text = value.toString(); 604 } else { 605 ProjectInformation pi = ProjectUtils.getInformation((Project) value); 606 text = pi.getDisplayName(); 607 setIcon(pi.getIcon()); 608 } 609 setText(text); 610 611 if ( isSelected ) { 612 setBackground(list.getSelectionBackground()); 613 setForeground(list.getSelectionForeground()); 614 } 615 else { 616 setBackground(list.getBackground()); 617 setForeground(list.getForeground()); 618 } 619 620 return this; 621 } 622 623 public String getName() { 625 String name = super.getName(); 626 return name == null ? "ComboBox.renderer" : name; } 628 629 } 630 631 637 public static Image getTreeFolderIcon(boolean opened) { 638 Image base = null; 639 Icon baseIcon = UIManager.getIcon(opened ? OPENED_ICON_KEY_UIMANAGER : ICON_KEY_UIMANAGER); if (baseIcon != null) { 641 base = Utilities.icon2Image(baseIcon); 642 } else { 643 base = (Image ) UIManager.get(opened ? OPENED_ICON_KEY_UIMANAGER_NB : ICON_KEY_UIMANAGER_NB); if (base == null) { base = Utilities.loadImage(opened ? OPENED_ICON_PATH : ICON_PATH, true); 646 } 647 } 648 assert base != null; 649 return base; 650 } 651 652 public static NbModuleProject runLibraryWrapperWizard(final Project suiteProvider) { 653 NewNbModuleWizardIterator iterator = NewNbModuleWizardIterator.createLibraryModuleIterator(suiteProvider); 654 return UIUtil.runProjectWizard(iterator, "CTL_NewLibraryWrapperProject"); } 656 657 public static NbModuleProject runProjectWizard( 658 final NewNbModuleWizardIterator iterator, final String titleBundleKey) { 659 WizardDescriptor wd = new WizardDescriptor(iterator); 660 wd.setTitleFormat(new MessageFormat ("{0}")); wd.setTitle(NbBundle.getMessage(UIUtil.class, titleBundleKey)); 662 Dialog dialog = DialogDisplayer.getDefault().createDialog(wd); 663 dialog.setVisible(true); 664 dialog.toFront(); 665 NbModuleProject project = null; 666 boolean cancelled = wd.getValue() != WizardDescriptor.FINISH_OPTION; 667 if (!cancelled) { 668 FileObject folder = iterator.getCreateProjectFolder(); 669 try { 670 project = (NbModuleProject) ProjectManager.getDefault().findProject(folder); 671 OpenProjects.getDefault().open(new Project[] { project }, false); 672 if (wd.getProperty("setAsMain") == Boolean.TRUE) { OpenProjects.getDefault().setMainProject(project); 674 } 675 } catch (IOException e) { 676 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 677 } 678 } 679 return project; 680 } 681 682 685 private static String searchLIPCategoryCombo(final JComboBox lpCombo, final String displayName) { 686 String path = null; 687 for (int i = 0; i < lpCombo.getItemCount(); i++) { 688 Object item = lpCombo.getItemAt(i); 689 if (!(item instanceof LayerItemPresenter)) { 690 continue; 691 } 692 LayerItemPresenter presenter = (LayerItemPresenter) lpCombo.getItemAt(i); 693 if (displayName.equals(presenter.getDisplayName())) { 694 path = presenter.getFullPath(); 695 break; 696 } 697 } 698 return path; 699 } 700 701 private static Project chooseProject(Component parent) { 702 JFileChooser chooser = ProjectChooser.projectChooser(); 703 int option = chooser.showOpenDialog(parent); 704 Project project = null; 705 if (option == JFileChooser.APPROVE_OPTION) { 706 File projectDir = chooser.getSelectedFile(); 707 UIUtil.setProjectChooserDirParent(projectDir); 708 try { 709 project = ProjectManager.getDefault().findProject( 710 FileUtil.toFileObject(projectDir)); 711 } catch (IOException e) { 712 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 713 } 714 } 715 return project; 716 } 717 718 private static File getSuiteDirectory(Project suiteComp) { 719 File suiteDir = SuiteUtils.getSuiteDirectory(suiteComp); 720 assert suiteDir != null : "Invalid suite provider for: " 721 + suiteComp.getProjectDirectory(); 722 return suiteDir; 723 } 724 725 private static String getSuiteProjectDirectory(Project suiteComp) { 726 return getSuiteDirectory(suiteComp).getAbsolutePath(); 727 } 728 729 private static String getSuiteProjectName(Project suiteComp) { 730 FileObject suiteDir = FileUtil.toFileObject(getSuiteDirectory(suiteComp)); 731 if (suiteDir == null) { 732 return "???"; } 735 return Util.getDisplayName(suiteDir); 736 } 737 738 private static Collection <FileObject> getFolders(final FileObject root, final Map <String ,Object > excludeAttrs) { 739 Collection <FileObject> folders = new HashSet <FileObject>(); 740 SUBFOLDERS: for (FileObject subFolder : NbCollections.iterable(root.getFolders(false))) { 741 for (Map.Entry <String ,Object > entry : excludeAttrs.entrySet()) { 742 if (entry.getValue().equals(subFolder.getAttribute(entry.getKey()))) { 743 continue SUBFOLDERS; 744 } 745 } 746 folders.add(subFolder); 747 folders.addAll(getFolders(subFolder, excludeAttrs)); 748 } 749 return folders; 750 } 751 752 private static final class IconFilter extends FileFilter { 753 public boolean accept(File pathname) { 754 return pathname.isDirectory() || 755 pathname.getName().toLowerCase(Locale.ENGLISH).endsWith("gif") || pathname.getName().toLowerCase(Locale.ENGLISH).endsWith("png"); } 758 public String getDescription() { 759 return "*.gif, *.png"; } 761 } 762 763 774 public static boolean showAcceptCancelDialog(String title, String message, String acceptButton, String cancelButton, int messageType) { 775 DialogDescriptor d = new DialogDescriptor(message, title); 776 d.setModal(true); 777 JButton accept = new JButton (acceptButton); 778 accept.setDefaultCapable(true); 779 d.setOptions(new Object [] { 780 accept, 781 cancelButton != null ? new JButton (cancelButton) : NotifyDescriptor.CANCEL_OPTION, 782 }); 783 d.setMessageType(messageType); 784 return DialogDisplayer.getDefault().notify(d).equals(accept); 785 } 786 787 private static class IconFileChooser extends JFileChooser { 788 private final JTextField iconInfo = new JTextField (); 789 private IconFileChooser() { 790 JPanel accessoryPanel = getAccesoryPanel(iconInfo); 791 setDialogTitle(NbBundle.getMessage(UIUtil.class, "TITLE_IconDialog")); setAccessory(accessoryPanel); 793 setAcceptAllFileFilterUsed(false); 794 setFileSelectionMode(JFileChooser.FILES_ONLY); 795 setMultiSelectionEnabled(false); 796 addChoosableFileFilter(new IconFilter()); 797 setFileView(new FileView () { 798 public Icon getIcon(File f) { 799 if (f.getName().endsWith(".gif") || f.getName().endsWith(".png")) { Icon icon = new ImageIcon (f.getAbsolutePath()); 803 if (icon.getIconWidth() == 16 && icon.getIconHeight() == 16) { 804 return icon; 805 } 806 } 807 return null; 808 } 809 public String getName(File f) { 810 File f2 = getSelectedFile(); 811 if (f2 != null && (f2.getName().endsWith(".gif") || f2.getName().endsWith(".png"))) { Icon icon = new ImageIcon (f2.getAbsolutePath()); 813 StringBuffer sb = new StringBuffer (); 814 sb.append(f2.getName()).append(" ["); sb.append(icon.getIconWidth()).append('x').append(icon.getIconHeight()); 816 sb.append(']'); 817 setApproveButtonToolTipText(sb.toString()); 818 iconInfo.setText(sb.toString()); 819 } else { 820 iconInfo.setText(""); 821 } 822 return super.getName(f); 823 } 824 825 }); 826 } 827 828 private static JPanel getAccesoryPanel(final JTextField iconInfo) { 829 iconInfo.setColumns(15); 830 iconInfo.setEditable(false); 831 832 JPanel accessoryPanel = new JPanel (); 833 JPanel inner = new JPanel (); 834 JLabel iconInfoLabel = new JLabel (); 835 accessoryPanel.setLayout(new FlowLayout (FlowLayout.LEFT, 6, 0)); 836 837 inner.setLayout(new GridLayout (2, 1, 0, 6)); 838 839 iconInfoLabel.setLabelFor(iconInfo); 840 Mnemonics.setLocalizedText(iconInfoLabel, NbBundle.getMessage(UIUtil.class, "LBL_IconInfo")); 841 inner.add(iconInfoLabel); 842 843 inner.add(iconInfo); 844 845 accessoryPanel.add(inner); 846 return accessoryPanel; 847 } 848 } 849 } 850 | Popular Tags |