From 20ba46930e708b0c3f3c529cd5331a8362cea81a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 29 May 2008 15:04:02 +0200 Subject: [PATCH] show file properties on selection --- src/net/pterodactylus/jsite/gui/FileManager.java | 68 ++++++++++++++++++------ 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/src/net/pterodactylus/jsite/gui/FileManager.java b/src/net/pterodactylus/jsite/gui/FileManager.java index b3d3755..cb33c11 100644 --- a/src/net/pterodactylus/jsite/gui/FileManager.java +++ b/src/net/pterodactylus/jsite/gui/FileManager.java @@ -50,6 +50,7 @@ import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; +import javax.swing.tree.TreePath; import net.pterodactylus.jsite.i18n.I18n; import net.pterodactylus.jsite.i18n.I18nable; @@ -323,6 +324,33 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr }); } + /** + * Returns the complete path for the given node. + * + * @param treeNode + * The node + * @return The complete path for the node, or the empty string (“”) for the + * root node + */ + private String getPathForNode(TreeNode treeNode) { + TreeNode[] pathToNode = fileTreeModel.getPathToRoot(treeNode); + StringBuilder completePathBuilder = new StringBuilder(); + boolean first = true; + for (TreeNode nodePathNode : pathToNode) { + if (first) { + first = false; + continue; + } + if (!(nodePathNode instanceof SortableTreeNode)) { + logger.log(Level.WARNING, "nodePathNode is not a SortableTreeNode!"); + continue; + } + completePathBuilder.append(File.separatorChar).append(((SortableTreeNode) nodePathNode).getUserObject()); + } + String completePath = (completePathBuilder.length() == 0) ? "" : completePathBuilder.substring(1); + return completePath; + } + // // INTERFACE I18nable // @@ -345,7 +373,30 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr * {@inheritDoc} */ public void valueChanged(TreeSelectionEvent treeSelectionEvent) { - /* TODO */ + TreePath[] selectedPaths = fileTree.getSelectionPaths(); + if ((selectedPaths != null) && (selectedPaths.length == 1)) { + Object lastPathComponent = selectedPaths[0].getLastPathComponent(); + if (!(lastPathComponent instanceof SortableTreeNode)) { + logger.log(Level.WARNING, "lastPathComponent is not a SortableTreeNode!"); + return; + } + SortableTreeNode node = (SortableTreeNode) lastPathComponent; + String completePath = getPathForNode(node); + int lastSeparator = completePath.lastIndexOf(File.separatorChar); + String filePath = ""; + String fileName; + if (lastSeparator == -1) { + fileName = completePath; + } else { + filePath = completePath.substring(0, lastSeparator); + fileName = completePath.substring(lastSeparator + 1); + } + filePathTextField.setText(filePath); + fileNameTextField.setText(fileName); + return; + } + filePathTextField.setText(""); + fileNameTextField.setText(""); } // @@ -385,20 +436,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr 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); + String completePath = getPathForNode(node); if (project.getDefaultFile().equals(completePath)) { superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.BOLD)); } else { -- 2.7.4