public Component getPreview(WebLookAndFeelDemo owner) {
    // Simple decorated file breadcrumb (by default file system root is used)
    WebFileBreadcrumb dfb = new WebFileBreadcrumb(FileUtils.getDiskRoots()[0]);

    // Simple decorated file breadcrumb (by default file system root is used)
    WebFileBreadcrumb sfb = new WebFileBreadcrumb();

    // Simple undecorated file breadcrumb (user home used as root
    WebFileBreadcrumb hfb = new WebFileBreadcrumb(FileUtils.getUserHomePath(), false);
    hfb.setEncloseLastElement(true);

    return new GroupPanel(4, false, new GroupPanel(dfb), new GroupPanel(sfb), new GroupPanel(hfb));
  }
Ejemplo n.º 2
0
  /**
   * Returns preview component for this example.
   *
   * @param owner demo application main frame
   * @return preview component
   */
  public Component getPreview(WebLookAndFeelDemo owner) {
    // Files tree with default root
    WebFileTree fileTree = new WebFileTree();
    fileTree.setAutoExpandSelectedNode(false);
    fileTree.setShowsRootHandles(true);
    WebScrollPane fileTreeScroll = new WebScrollPane(fileTree);
    fileTreeScroll.setPreferredSize(new Dimension(200, 150));

    // Files tree with user home directory as root
    WebFileTree homeFileTree = new WebFileTree(FileUtils.getUserHomePath());
    homeFileTree.setAutoExpandSelectedNode(false);
    homeFileTree.setShowsRootHandles(true);
    homeFileTree.setSelectionStyle(TreeSelectionStyle.group);
    WebScrollPane homeFileTreeScroll = new WebScrollPane(homeFileTree);
    homeFileTreeScroll.setPreferredSize(new Dimension(200, 150));

    return new GroupPanel(5, fileTreeScroll, homeFileTreeScroll);
  }
Ejemplo n.º 3
0
 public static String createLongString() {
   return FileUtils.readToString(ExamplesManager.class, "resources/long.txt");
 }
Ejemplo n.º 4
0
 public static String createSmallString() {
   return FileUtils.readToString(ExamplesManager.class, "resources/small.txt");
 }
Ejemplo n.º 5
0
  /** 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();
  }
Ejemplo n.º 6
0
/**
 * This custom component provides a directory chooser functionality. It similar to file chooser
 * component but has its own unique interface and doesn't need a lot of configuring to use.
 *
 * @author Mikle Garin
 */
public class WebDirectoryChooserPanel extends WebPanel {
  /** Used icons. */
  protected static final ImageIcon FOLDER_UP_ICON =
      new ImageIcon(WebDirectoryChooserPanel.class.getResource("icons/folder_up.png"));

  protected static final ImageIcon FOLDER_HOME_ICON =
      new ImageIcon(WebDirectoryChooserPanel.class.getResource("icons/folder_home.png"));
  protected static final ImageIcon FOLDER_NEW_ICON =
      new ImageIcon(WebDirectoryChooserPanel.class.getResource("icons/folder_new.png"));
  protected static final ImageIcon REFRESH_ICON =
      new ImageIcon(WebDirectoryChooserPanel.class.getResource("icons/refresh.png"));
  protected static final ImageIcon REMOVE_ICON =
      new ImageIcon(WebDirectoryChooserPanel.class.getResource("icons/remove.png"));

  /** Directory chooser listeners. */
  protected List<DirectoryChooserListener> listeners = new ArrayList<DirectoryChooserListener>(1);

  /** Currently selected directory. */
  protected File selectedDirectory = FileUtils.getDiskRoots()[0];

  /** File filter. */
  protected AbstractFileFilter filter = GlobalConstants.NON_HIDDEN_DIRECTORIES_FILTER;

  /** Toolbar components. */
  protected WebButton folderUp;

  protected WebButton folderHome;
  protected WebButton folderNew;
  protected WebButton refresh;
  protected WebButton remove;

  /** Path field. */
  protected WebPathField webPathField;

  /** File tree. */
  protected WebFileTree fileTree;

  protected TreeSelectionListener fileTreeListener;

  /** 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();
  }

  /** Updates toolbar controls state. */
  protected void updateToolbarControlsState() {
    final File selectedFile = fileTree.getSelectedFile();

    folderUp.setEnabled(selectedFile != null && selectedFile.getParentFile() != null);

    folderNew.setEnabled(selectedFile != null && selectedFile.canWrite());
    remove.setEnabled(
        selectedFile != null && selectedFile.getParentFile() != null && selectedFile.canWrite());
  }

  /**
   * Updates currently selected directory.
   *
   * @param directory directory to select
   * @param updatePath whether to update path field or not
   * @param updateTree whether to update file tree or not
   */
  protected void updateSelectedDirectory(
      final File directory, final boolean updatePath, final boolean updateTree) {
    this.selectedDirectory = directory;

    if (updatePath) {
      webPathField.setSelectedPath(directory);
    }

    if (updateTree) {
      if (directory != null) {
        fileTree.expandToFile(
            directory,
            false,
            true,
            new Runnable() {
              @Override
              public void run() {
                fileTree.removeTreeSelectionListener(fileTreeListener);
                fileTree.setSelectedNode(fileTree.getNode(directory));
                fileTree.addTreeSelectionListener(fileTreeListener);
              }
            });
      } else {
        fileTree.clearSelection();
        fileTree.scrollToStart();
      }
    }

    fireSelectionChanged(this.selectedDirectory);
  }

  /**
   * Returns directory chooser file filter.
   *
   * @return directory chooser file filter
   */
  public AbstractFileFilter getFilter() {
    return filter;
  }

  /**
   * Sets directory chooser file filter.
   *
   * @param filter directory chooser file filter
   */
  public void setFilter(final AbstractFileFilter filter) {
    this.filter = filter;
    fileTree.setFileFilter(filter);
  }

  /**
   * Returns currently selected directory.
   *
   * @return currently selected directory
   */
  public File getSelectedDirectory() {
    return selectedDirectory;
  }

  /**
   * Sets currently selected directory.
   *
   * @param selectedDirectory currently selected directory
   */
  public void setSelectedDirectory(final File selectedDirectory) {
    updateSelectedDirectory(selectedDirectory, true, true);
  }

  /**
   * Returns whether hotkeys are allowed or not.
   *
   * @return true if hotkeys are allowed, false otherwise
   */
  public boolean allowHotkeys() {
    // todo Remove this when hotkeys system will get updated
    return !fileTree.isEditing() && !webPathField.isEditing() /*&& !fileList.isEditing()*/;
  }

  /**
   * Adds directory chooser listener.
   *
   * @param listener directory chooser listener to add
   */
  public void addDirectoryChooserListener(final DirectoryChooserListener listener) {
    listeners.add(listener);
  }

  /**
   * Removes directory chooser listener.
   *
   * @param listener directory chooser listener to remove
   */
  public void removeDirectoryChooserListener(final DirectoryChooserListener listener) {
    listeners.remove(listener);
  }

  /**
   * Fires when directory selection changed.
   *
   * @param file newly selected directory
   */
  protected void fireSelectionChanged(final File file) {
    for (final DirectoryChooserListener listener : CollectionUtils.copy(listeners)) {
      listener.selectionChanged(file);
    }
  }
}
  /**
   * Returns list cell renderer component.
   *
   * @param list tree
   * @param value cell value
   * @param index cell index
   * @param isSelected whether cell is selected or not
   * @param cellHasFocus whether cell has focus or not
   * @return cell renderer component
   */
  @Override
  public Component getListCellRendererComponent(
      final JList list,
      final Object value,
      final int index,
      final boolean isSelected,
      final boolean cellHasFocus) {
    super.getListCellRendererComponent(list, "", index, isSelected, cellHasFocus);

    final FileElement element = (FileElement) value;
    final File file = element.getFile();

    // Proper margin
    setMargin(isTilesView() ? tileCellMargin : iconCellMargin);

    // Renderer icon
    String imageSize = null;
    if (iconLabel.isEnabled()) {
      // Thumbnail loading
      synchronized (thumbnailsLock) {
        if (!element.isThumbnailQueued() && !element.isDisabledThumbnailQueued()) {
          queueThumbnailLoad(element, false);
        }
      }

      // Image thumbnail
      final ImageIcon thumbnail = element.getEnabledThumbnail();
      iconLabel.setIcon(thumbnail);

      // Image description
      if (thumbnail != null) {
        imageSize = thumbnail.getDescription();
      }
    } else {
      // Disabled thumbnail loading
      synchronized (thumbnailsLock) {
        if (!element.isDisabledThumbnailQueued()) {
          queueThumbnailLoad(element, true);
        }
      }

      // Image disabled thumbnail
      iconLabel.setDisabledIcon(element.getDisabledThumbnail());
    }

    // Updating file description elements
    if (fileList.getEditedCell() != index) {
      // Settings description
      final FileDescription fileDescription = FileUtils.getFileDescription(file, imageSize);
      nameLabel.setText(fileDescription.getName());

      // Updating tile view additional description
      if (isTilesView()) {
        descriptionLabel.setText(fileDescription.getDescription());

        // Updating size label
        if (fileDescription.getSize() != null) {
          sizeLabel.setText(fileDescription.getSize());
        } else {
          sizeLabel.setText(null);
        }
      } else {
        descriptionLabel.setText(null);
        sizeLabel.setText(null);
      }
    } else {
      nameLabel.setText(null);
      descriptionLabel.setText(null);
      sizeLabel.setText(null);
    }

    return this;
  }
Ejemplo n.º 8
0
 private String loadString(JarEntry lastEntry) {
   return FileUtils.readToString(getEntryInputStream(lastEntry));
 }
Ejemplo n.º 9
0
  private Component createEntryViewer(JarEntry entry) {
    if (entry.getType().equals(JarEntryType.classEntry)
        || entry.getType().equals(JarEntryType.javaEntry)
        || entry.getType().equals(JarEntryType.fileEntry)) {
      String ext = FileUtils.getFileExtPart(entry.getName(), false).toLowerCase();
      if (GlobalConstants.IMAGE_FORMATS.contains(ext)) {
        // todo A better image viewer (actually a new component - WebImageViewer)

        // Image file viewer
        WebImage image = new WebImage();
        image.setIcon(ImageUtils.loadImage(getEntryInputStream(entry)));

        // Image scroll
        WebScrollPane imageScroll = new WebScrollPane(image, false);
        imageScroll.setVerticalScrollBarPolicy(WebScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        imageScroll.setHorizontalScrollBarPolicy(WebScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        return imageScroll;
      } else {
        // Source code viewer
        final RSyntaxTextArea source = new RSyntaxTextArea();

        // Syntax style
        boolean libraryCode = false;
        if (ext.equals("java") || ext.equals("class")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
          libraryCode = true;
        } else if (ext.equals("xml")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);
        } else if (ext.equals("html")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_HTML);
        } else if (ext.equals("css")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_CSS);
        } else if (ext.equals("js")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT);
        } else if (ext.equals("php")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PHP);
        } else if (ext.equals("sql")) {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
        } else {
          source.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);
        }

        // Settings
        source.setEditable(false);
        source.setMargin(new Insets(0, 5, 0, 0));
        source.setAntiAliasingEnabled(true);
        source.setUseFocusableTips(true);
        source.setTabSize(4);
        // source.setLineWrap ( true );
        // source.setWrapStyleWord ( true );
        source.setCodeFoldingEnabled(allowCodeFolding.isSelected());
        source.setPaintTabLines(paintTabLines.isSelected());
        source.setWhitespaceVisible(showWhitespaces.isSelected());
        source.setEOLMarkersVisible(showEol.isSelected());
        source.addHyperlinkListener(
            new HyperlinkListener() {
              @Override
              public void hyperlinkUpdate(HyperlinkEvent e) {
                WebUtils.browseSiteSafely(e.getURL().toExternalForm());
              }
            });
        ((RSyntaxTextAreaHighlighter) source.getHighlighter()).setDrawsLayeredHighlights(false);

        // Source code
        source.setText(libraryCode ? loadSource(entry) : loadString(entry));
        source.setCaretPosition(0);

        // "Jump to source"-like action
        source.addMouseListener(
            new MouseAdapter() {
              @Override
              public void mousePressed(MouseEvent e) {
                // todo Fix when clicked in class "MyName" on string "MyName"
                // Additional feature to dive into related classes
                if (SwingUtilities.isMiddleMouseButton(e)
                    || SwingUtils.isCtrl(e) && SwingUtilities.isLeftMouseButton(e)) {
                  int pos = source.getUI().viewToModel(source, e.getPoint());
                  String word = TextUtils.getWord(source.getText(), pos);
                  if (word != null) {
                    JarEntry classByName = jarStructure.findEntryByName(word);
                    if (classByName != null
                        && (classByName.getType().equals(JarEntryType.classEntry)
                            || classByName.getType().equals(JarEntryType.javaEntry))) {
                      updateClassPath(classByName, true);
                    }
                  }
                }
              }
            });

        // Saving opened editor
        synchronized (activeEditorsLock) {
          activeEditors.put(entry, source);
        }

        // Special code viewer scroll pane
        RTextScrollPane sourceScroll = new RTextScrollPane(source);
        sourceScroll.setVerticalScrollBarPolicy(WebScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        ((WebScrollPaneUI) sourceScroll.getUI()).setDrawBorder(false);

        // Source code viewer theme
        loadTheme(theme.getSelectedItem().toString().toLowerCase(), source);

        return sourceScroll;
      }
    }
    return new WebLabel();
  }
Ejemplo n.º 10
0
 private String getDisplayName(File file) {
   String name = FileUtils.getDisplayFileName(file);
   return showFileExtensions || file.isDirectory() ? name : FileUtils.getFileNamePart(name);
 }
Ejemplo n.º 11
0
 private ImageIcon getDisplayIcon(File file) {
   return FileUtils.getFileIcon(file, false);
 }