show file properties on selection
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 29 May 2008 13:04:02 +0000 (15:04 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 29 May 2008 13:04:02 +0000 (15:04 +0200)
src/net/pterodactylus/jsite/gui/FileManager.java

index b3d3755..cb33c11 100644 (file)
@@ -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 {