From: David ‘Bombe’ Roden Date: Thu, 29 May 2008 11:32:59 +0000 (+0200) Subject: work on rescanning and displaying X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;ds=sidebyside;h=4275e16db6748befadcefb1b6e2cc3ecb17faf29;p=jSite2.git work on rescanning and displaying --- diff --git a/src/net/pterodactylus/jsite/gui/FileManager.java b/src/net/pterodactylus/jsite/gui/FileManager.java index 20d98a3..8be6f11 100644 --- a/src/net/pterodactylus/jsite/gui/FileManager.java +++ b/src/net/pterodactylus/jsite/gui/FileManager.java @@ -20,15 +20,18 @@ package net.pterodactylus.jsite.gui; import java.awt.BorderLayout; +import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; +import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; +import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -38,13 +41,16 @@ import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JDialog; +import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; +import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.TreeNode; import net.pterodactylus.jsite.i18n.I18n; import net.pterodactylus.jsite.i18n.I18nable; @@ -58,7 +64,7 @@ import net.pterodactylus.util.swing.SwingUtils; /** * Manages physical and virtual files in a project. - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener { @@ -78,6 +84,12 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr /** The tree model for the project files. */ private final DefaultTreeModel fileTreeModel; + /** Files that are hidden as per {@link File#isHidden()}. */ + private final List hiddenFiles = new ArrayList(); + + /** The tree cell renderer. */ + private final FileCellRenderer fileCellRenderer; + /** The “rescan” action. */ private I18nAction rescanAction; @@ -92,7 +104,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr /** The scroll pane that holds the file tree. */ private JScrollPane fileScrollPane; - + /** The “insert” action. */ private I18nAction insertAction; @@ -110,7 +122,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr /** * Creates a new file manager. - * + * * @param swingInterface * The Swing interface * @param project @@ -123,6 +135,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr this.project = project; fileTreeRoot = new SortableTreeNode(project.getName()); fileTreeModel = new DefaultTreeModel(fileTreeRoot); + fileCellRenderer = new FileCellRenderer(project); initActions(); initComponents(); pack(); @@ -169,7 +182,6 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr @SuppressWarnings("synthetic-access") public void actionPerformed(ActionEvent actionEvent) { initiateFileScan(); - setEnabled(false); } }; insertAction = new I18nAction("fileManager.checkbox.insertFile") { @@ -211,7 +223,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr /** * Creates the main panel with the file tree and the file properties. - * + * * @return The mail panel */ private Component createFileManagerPanel() { @@ -224,6 +236,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr fileTree = new JTree(fileTreeModel); fileTree.setShowsRootHandles(false); fileTree.addTreeSelectionListener(this); + fileTree.setCellRenderer(fileCellRenderer); fileTreePanel.add(fileScrollPane = new JScrollPane(fileTree), BorderLayout.CENTER); fileScrollPane.setPreferredSize(new Dimension(250, 400)); @@ -267,7 +280,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr /** * Creates the button panel. - * + * * @return The button panel */ private Component createButtonPanel() { @@ -292,28 +305,34 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr */ @SuppressWarnings("synthetic-access") public void run() { + fileTree.setEnabled(false); + rescanAction.setEnabled(false); String basePath = project.getBasePath(); File basePathDirectory = new File(basePath); + fileTreeRoot.removeAll(); + hiddenFiles.clear(); if (!basePathDirectory.exists() || !basePathDirectory.isDirectory()) { /* TODO - i18n */ JOptionPane.showMessageDialog(FileManager.this, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE); - return; - } - synchronized (fileTreeRoot) { - fileTreeRoot.removeAll(); - scanDirectory(fileTreeRoot, basePathDirectory); + } else { + scanDirectory(fileTreeRoot, basePathDirectory, "", hiddenFiles); } fileTreeModel.reload(); - fileScrollPane.revalidate(); + // fileScrollPane.revalidate(); rescanAction.setEnabled(true); + fileTree.setEnabled(true); } - private void scanDirectory(SortableTreeNode rootNode, File directory) { - for (File file: directory.listFiles()) { - SortableTreeNode fileNode = new SortableTreeNode(file.getName()); + private void scanDirectory(SortableTreeNode rootNode, File directory, String currentDirectory, List hiddenFiles) { + for (File file : directory.listFiles()) { + String fileName = file.getName(); + SortableTreeNode fileNode = new SortableTreeNode(fileName); rootNode.add(fileNode); + if (file.isFile() && file.isHidden()) { + hiddenFiles.add((currentDirectory + File.separator + fileName).substring(1)); + } if (file.isDirectory()) { - scanDirectory(fileNode, file); + scanDirectory(fileNode, file, currentDirectory + File.separator + fileName, hiddenFiles); } } rootNode.sort(); @@ -355,4 +374,72 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr /* TODO */ } + /** + * Tree cell renderer that takes care of certain display properties for + * project-specific stuff. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ + private class FileCellRenderer extends DefaultTreeCellRenderer { + + /** The project. */ + private final Project project; + + public FileCellRenderer(Project project) { + this.project = project; + } + + /** + * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree, + * java.lang.Object, boolean, boolean, boolean, int, boolean) + */ + public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { + Component superCellRenderer = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); + if (!(superCellRenderer instanceof JLabel)) { + logger.log(Level.WARNING, "superCellRenderer is not a JLabel!"); + return superCellRenderer; + } + if (!(value instanceof SortableTreeNode)) { + logger.log(Level.WARNING, "value is not a SortableTreeNode!"); + return superCellRenderer; + } + SortableTreeNode node = (SortableTreeNode) value; + TreeNode[] pathToRoot = fileTreeModel.getPathToRoot(node); + if (pathToRoot.length > 1) { + StringBuilder completePathBuilder = new StringBuilder(); + boolean first = true; + for (TreeNode rootPathNode : pathToRoot) { + if (first) { + first = false; + continue; + } + if (!(rootPathNode instanceof SortableTreeNode)) { + logger.log(Level.WARNING, "rootPathNode is not a SortableTreeNode!"); + continue; + } + completePathBuilder.append(File.separatorChar).append(((SortableTreeNode) rootPathNode).getUserObject()); + } + String completePath = completePathBuilder.substring(1); + System.out.println("completePath: " + completePath); + if (project.getDefaultFile().equals(completePath)) { + superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.BOLD)); + } else { + if (hiddenFiles.contains(completePath)) { + /* fade hidden files’ font */ + Color foreground = superCellRenderer.getForeground(); + Color background = selected ? getBackgroundSelectionColor() : getBackgroundNonSelectionColor(); + Color averageColor = new Color((foreground.getRed() + background.getRed()) / 2, (foreground.getGreen() + background.getGreen()) / 2, (foreground.getBlue() + background.getBlue()) / 2); + superCellRenderer.setForeground(averageColor); + } else { + superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.PLAIN)); + } + } + } else { + superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.BOLD)); + } + return superCellRenderer; + } + + } + }