/** Constructs new directory chooser panel. */
  public WebDirectoryChooserPanel() {
    super();

    // Panel settings
    setOpaque(true);

    // Controls pane
    final WebToolBar contolsToolbar = new WebToolBar(WebToolBar.HORIZONTAL);
    contolsToolbar.setToolbarStyle(ToolbarStyle.attached);
    contolsToolbar.setFloatable(false);

    folderUp = new WebButton(FOLDER_UP_ICON);
    folderUp.setLanguage("weblaf.ex.dirchooser.folderup");
    folderUp
        .addHotkey(WebDirectoryChooserPanel.this, Hotkey.ALT_UP)
        .setHotkeyDisplayWay(TooltipWay.down);
    folderUp.setRolloverDecoratedOnly(true);
    folderUp.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            if (selectedDirectory != null) {
              updateSelectedDirectory(selectedDirectory.getParentFile(), true, true);
            }
          }
        });
    contolsToolbar.add(folderUp);

    folderHome = new WebButton(FOLDER_HOME_ICON);
    folderHome.setLanguage("weblaf.ex.dirchooser.home");
    folderHome
        .addHotkey(WebDirectoryChooserPanel.this, Hotkey.ALT_H)
        .setHotkeyDisplayWay(TooltipWay.trailing);
    folderHome.setRolloverDecoratedOnly(true);
    folderHome.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            updateSelectedDirectory(FileUtils.getUserHome(), true, true);
          }
        });
    contolsToolbar.add(folderHome);

    contolsToolbar.addSeparator();

    for (final File file : FileTreeRootType.drives.getRoots()) {
      final WebButton root = new WebButton(FileUtils.getFileIcon(file));
      TooltipManager.setTooltip(root, FileUtils.getDisplayFileName(file));
      root.setRolloverDecoratedOnly(true);
      root.addActionListener(
          new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent e) {
              updateSelectedDirectory(file, true, true);
            }
          });
      contolsToolbar.add(root);
    }

    refresh = new WebButton(REFRESH_ICON);
    refresh.setLanguage("weblaf.ex.dirchooser.refresh");
    refresh
        .addHotkey(WebDirectoryChooserPanel.this, Hotkey.F5)
        .setHotkeyDisplayWay(TooltipWay.leading);
    refresh.setRolloverDecoratedOnly(true);
    refresh.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            if (selectedDirectory != null) {
              fileTree.reloadChilds(selectedDirectory);
            } else {
              fileTree.reloadRootNode();
            }
          }
        });
    contolsToolbar.add(refresh, ToolbarLayout.END);

    folderNew = new WebButton(FOLDER_NEW_ICON);
    folderNew.setLanguage("weblaf.ex.dirchooser.newfolder");
    folderNew
        .addHotkey(WebDirectoryChooserPanel.this, Hotkey.CTRL_N)
        .setHotkeyDisplayWay(TooltipWay.down);
    folderNew.setRolloverDecoratedOnly(true);
    folderNew.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            if (selectedDirectory != null) {
              final String defaultName = LanguageManager.get("weblaf.ex.dirchooser.newfolder.name");
              final String freeName = FileUtils.getAvailableName(selectedDirectory, defaultName);
              final File file = new File(selectedDirectory, freeName);
              if (file.mkdir()) {
                // Updating filestree
                fileTree.addFile(selectedDirectory, file);

                // Editing added folder name
                fileTree.startEditingFile(file);
              } else {
                final String message =
                    LanguageManager.get("weblaf.ex.dirchooser.newfolder.error.text");
                final String title =
                    LanguageManager.get("weblaf.ex.dirchooser.newfolder.error.title");
                WebOptionPane.showMessageDialog(
                    WebDirectoryChooserPanel.this, message, title, WebOptionPane.ERROR_MESSAGE);
              }
            }
          }
        });
    contolsToolbar.add(folderNew, ToolbarLayout.END);

    remove = new WebButton(REMOVE_ICON);
    remove.setLanguage("weblaf.ex.dirchooser.delete");
    remove
        .addHotkey(WebDirectoryChooserPanel.this, Hotkey.DELETE)
        .setHotkeyDisplayWay(TooltipWay.down);
    remove.setRolloverDecoratedOnly(true);
    remove.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            final File file = fileTree.getSelectedFile();
            if (file == null) {
              return;
            }

            // Displaying delete confirmation
            final String message = LanguageManager.get("weblaf.ex.dirchooser.delete.confirm.text");
            final String title = LanguageManager.get("weblaf.ex.dirchooser.delete.confirm.title");
            final int confirm =
                WebOptionPane.showConfirmDialog(
                    WebDirectoryChooserPanel.this,
                    message,
                    title,
                    WebOptionPane.YES_NO_OPTION,
                    WebOptionPane.QUESTION_MESSAGE);

            // Proceed if delete was confirmed
            if (confirm == WebOptionPane.YES_OPTION) {
              // Retrieving index of deleted file node in parent node
              final FileTreeNode parentNode = fileTree.getSelectedNode().getParent();
              final int index = parentNode.indexOfFileChild(file);
              final int count = parentNode.getChildCount();

              // Removing file
              FileUtils.deleteFile(file);
              fileTree.removeFile(file);

              // Restoring selection
              fileTree.setSelectedNode(
                  count == 1
                      ? parentNode
                      : (index < count - 1
                          ? parentNode.getChildAt(index)
                          : parentNode.getChildAt(index - 1)));
            }
          }
        });
    contolsToolbar.add(remove, ToolbarLayout.END);

    // Path field
    webPathField = new WebPathField(selectedDirectory);
    webPathField.setFileFilter(filter);
    webPathField.addPathFieldListener(
        new PathFieldListener() {
          @Override
          public void directoryChanged(final File newDirectory) {
            updateSelectedDirectory(webPathField.getSelectedPath(), false, true);
          }
        });

    // Files tree
    fileTree = new WebFileTree(FileTreeRootType.drives);
    fileTree.setVisibleRowCount(15);
    fileTree.setFileFilter(filter);
    fileTree.setSelectedFile(selectedDirectory, true);
    fileTree.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    fileTree.setEditable(true);

    // Selected directory update
    fileTreeListener =
        new TreeSelectionListener() {
          @Override
          public void valueChanged(final TreeSelectionEvent e) {
            updateSelectedDirectory(fileTree.getSelectedFile(), true, false);
          }
        };
    fileTree.addTreeSelectionListener(fileTreeListener);

    // Toolbar update
    fileTree.addTreeSelectionListener(
        new TreeSelectionListener() {
          @Override
          public void valueChanged(final TreeSelectionEvent e) {
            updateToolbarControlsState();
          }
        });

    // Tree scroll
    final WebScrollPane treeScroll = new WebScrollPane(fileTree);
    treeScroll.setPreferredWidth(400);

    // Panel content
    setLayout(new BorderLayout(0, 3));
    add(contolsToolbar, BorderLayout.NORTH);
    final WebPanel panel = new WebPanel(new BorderLayout(0, 1));
    panel.setMargin(0, 3, 2, 3);
    panel.add(webPathField, BorderLayout.NORTH);
    panel.add(treeScroll, BorderLayout.CENTER);
    add(panel, BorderLayout.CENTER);

    updateSelectedDirectory(null, true, true);
    updateToolbarControlsState();
  }
예제 #2
0
  private WebButton createClassSearch() {
    WebButton classSearch = new WebButton(classSearchIcon);
    classSearch.setDrawFocus(false);
    classSearch.setRolloverDecoratedOnly(true);
    classSearch.addHotkey(Hotkey.CTRL_N);
    classSearch.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            showClassSearchPopup();
          }
        });

    classSearchField = new WebTextField(20, false);
    classSearchField.setHideInputPromptOnFocus(false);
    classSearchField.setInputPrompt("Enter class name here...");
    HotkeyManager.registerHotkey(
        classSearchField,
        Hotkey.ESCAPE,
        new HotkeyRunnable() {
          @Override
          public void run(KeyEvent e) {
            hideClassSearchPopup();
          }
        });

    final WebImage leadingComponent = new WebImage(classSearchIcon);
    leadingComponent.setMargin(2);
    classSearchField.setLeadingComponent(leadingComponent);

    classSearchPopup = new WebPopup();
    classSearchPopup.setCloseOnFocusLoss(true);
    classSearchPopup.add(classSearchField);
    classSearchPopup.setDefaultFocusComponent(classSearchField);

    classSearchHintsPopup =
        new WebWindow(classSearchPopup) {
          @Override
          public Dimension getPreferredSize() {
            final Dimension ps = super.getPreferredSize();
            ps.width = Math.max(classSearchField.getWidth(), ps.width);
            return ps;
          }
        };
    classSearchHintsPopup.setFocusable(false);
    classSearchHintsPopup.setAlwaysOnTop(true);
    classSearchPopup.addFocusableChild(classSearchHintsPopup);

    classSearchHintsList = new WebList(new DefaultListModel());
    classSearchHintsList.setFocusable(false);
    classSearchHintsList.setRolloverSelectionEnabled(true);
    classSearchHintsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    classSearchHintsList.setCellRenderer(
        new WebListCellRenderer() {
          @Override
          public Component getListCellRendererComponent(
              JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JarEntry entry = (JarEntry) value;
            WebListElement renderer =
                (WebListElement)
                    super.getListCellRendererComponent(
                        list, value, index, isSelected, cellHasFocus);
            renderer.setIcon(entry.getIcon());
            renderer.setText(entry.getName());
            return renderer;
          }
        });
    classSearchHintsList.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mousePressed(MouseEvent e) {
            if (SwingUtils.isLeftMouseButton(e)) {
              openSelectedHint();
            }
          }
        });

    HotkeyManager.registerHotkey(
        classSearchField,
        Hotkey.HOME,
        new HotkeyRunnable() {
          @Override
          public void run(KeyEvent e) {
            if (classSearchHintsList.getModelSize() > 0) {
              classSearchHintsList.setSelectedIndex(0);
            }
          }
        });
    HotkeyManager.registerHotkey(
        classSearchField,
        Hotkey.UP,
        new HotkeyRunnable() {
          @Override
          public void run(KeyEvent e) {
            if (classSearchHintsList.getModelSize() > 0) {
              int index = classSearchHintsList.getSelectedIndex();
              if (index > 0) {
                classSearchHintsList.setSelectedIndex(index - 1);
              } else {
                classSearchHintsList.setSelectedIndex(classSearchHintsList.getModelSize() - 1);
              }
            }
          }
        });
    HotkeyManager.registerHotkey(
        classSearchField,
        Hotkey.DOWN,
        new HotkeyRunnable() {
          @Override
          public void run(KeyEvent e) {
            if (classSearchHintsList.getModelSize() > 0) {
              int index = classSearchHintsList.getSelectedIndex();
              if (index < classSearchHintsList.getModelSize() - 1) {
                classSearchHintsList.setSelectedIndex(index + 1);
              } else {
                classSearchHintsList.setSelectedIndex(0);
              }
            }
          }
        });
    HotkeyManager.registerHotkey(
        classSearchField,
        Hotkey.END,
        new HotkeyRunnable() {
          @Override
          public void run(KeyEvent e) {
            if (classSearchHintsList.getModelSize() > 0) {
              classSearchHintsList.setSelectedIndex(classSearchHintsList.getModelSize() - 1);
            }
          }
        });
    HotkeyManager.registerHotkey(
        classSearchField,
        Hotkey.ENTER,
        new HotkeyRunnable() {
          @Override
          public void run(KeyEvent e) {
            openSelectedHint();
          }
        });

    WebScrollPane foundClassesScroll = new WebScrollPane(classSearchHintsList);
    foundClassesScroll.setShadeWidth(0);
    foundClassesScroll.setRound(0);
    classSearchHintsPopup.add(foundClassesScroll);

    classSearchPopup.addComponentListener(
        new ComponentAdapter() {
          @Override
          public void componentMoved(ComponentEvent e) {
            updateHintsLocation();
          }

          @Override
          public void componentResized(ComponentEvent e) {
            updateHintsLocation();
          }
        });

    classSearchPopup.addPopupListener(
        new PopupAdapter() {
          @Override
          public void popupWillBeOpened() {
            lastSearchedText = null;
            lastFocusBeforeSearch = FocusManager.getFocusOwner();
          }

          @Override
          public void popupOpened() {
            updateHints();
          }

          @Override
          public void popupClosed() {
            hideHints();
            if (lastFocusBeforeSearch != null) {
              lastFocusBeforeSearch.requestFocusInWindow();
            }
          }
        });

    classSearchField.addCaretListener(
        new CaretListener() {
          @Override
          public void caretUpdate(CaretEvent e) {
            if (classSearchHintsDelay == null) {
              classSearchHintsDelay =
                  new WebTimer(
                      500,
                      new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                          updateHints();
                        }
                      });
              classSearchHintsDelay.setRepeats(false);
            }
            if (classSearchField.getText().trim().length() > 0) {
              classSearchHintsDelay.restart();
            } else {
              classSearchHintsDelay.stop();
              hideHints();
            }
          }
        });

    return classSearch;
  }