fix scroll pane size
[jSite2.git] / src / net / pterodactylus / jsite / gui / FileManager.java
index 8f10ebb..20d98a3 100644 (file)
@@ -21,6 +21,7 @@ package net.pterodactylus.jsite.gui;
 
 import java.awt.BorderLayout;
 import java.awt.Component;
+import java.awt.Dimension;
 import java.awt.FlowLayout;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
@@ -37,15 +38,13 @@ import javax.swing.JButton;
 import javax.swing.JCheckBox;
 import javax.swing.JComboBox;
 import javax.swing.JDialog;
-import javax.swing.JFrame;
+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.DefaultMutableTreeNode;
 import javax.swing.tree.DefaultTreeModel;
-import javax.swing.tree.TreeModel;
 
 import net.pterodactylus.jsite.i18n.I18n;
 import net.pterodactylus.jsite.i18n.I18nable;
@@ -54,11 +53,12 @@ import net.pterodactylus.jsite.i18n.gui.I18nLabel;
 import net.pterodactylus.jsite.project.Project;
 import net.pterodactylus.util.io.MimeTypes;
 import net.pterodactylus.util.logging.Logging;
+import net.pterodactylus.util.swing.SortableTreeNode;
 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 {
@@ -72,8 +72,14 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
        /** The project whose files to manage. */
        private final Project project;
 
+       /** The root of the file tree. */
+       private final SortableTreeNode fileTreeRoot;
+
        /** The tree model for the project files. */
-       private final TreeModel fileTreeModel;
+       private final DefaultTreeModel fileTreeModel;
+
+       /** The “rescan” action. */
+       private I18nAction rescanAction;
 
        /** The “close” action. */
        private I18nAction closeAction;
@@ -84,6 +90,9 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
        /** The tree that shows the files. */
        private JTree fileTree;
 
+       /** The scroll pane that holds the file tree. */
+       private JScrollPane fileScrollPane;
+       
        /** The “insert” action. */
        private I18nAction insertAction;
 
@@ -101,7 +110,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
 
        /**
         * Creates a new file manager.
-        *
+        * 
         * @param swingInterface
         *            The Swing interface
         * @param project
@@ -112,7 +121,8 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                logger.log(Level.FINEST, "project: " + project);
                this.swingInterface = swingInterface;
                this.project = project;
-               fileTreeModel = new DefaultTreeModel(new DefaultMutableTreeNode(File.separator));
+               fileTreeRoot = new SortableTreeNode(project.getName());
+               fileTreeModel = new DefaultTreeModel(fileTreeRoot);
                initActions();
                initComponents();
                pack();
@@ -131,7 +141,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                if (visible) {
                        initiateFileScan();
                }
-           super.setVisible(visible);
+               super.setVisible(visible);
        }
 
        //
@@ -151,6 +161,17 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                                setVisible(false);
                        }
                };
+               rescanAction = new I18nAction("fileManager.button.rescan") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               initiateFileScan();
+                               setEnabled(false);
+                       }
+               };
                insertAction = new I18nAction("fileManager.checkbox.insertFile") {
 
                        /**
@@ -190,27 +211,34 @@ 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() {
                JPanel fileManagerPanel = new JPanel(new BorderLayout(12, 12));
 
+               /* file tree panel */
                JPanel fileTreePanel = new JPanel(new BorderLayout(12, 12));
                fileManagerPanel.add(fileTreePanel, BorderLayout.LINE_START);
 
                fileTree = new JTree(fileTreeModel);
                fileTree.setShowsRootHandles(false);
                fileTree.addTreeSelectionListener(this);
-               fileTreePanel.add(new JScrollPane(fileTree), BorderLayout.CENTER);
+               fileTreePanel.add(fileScrollPane = new JScrollPane(fileTree), BorderLayout.CENTER);
+               fileScrollPane.setPreferredSize(new Dimension(250, 400));
 
                projectFilesLabel = new I18nLabel("fileManager.label.projectFiles", fileTree);
                JPanel projectFilesLabelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
                fileTreePanel.add(projectFilesLabelPanel, BorderLayout.NORTH);
                projectFilesLabelPanel.add(projectFilesLabel);
 
+               /* the right panel */
+               JPanel rightPanel = new JPanel(new BorderLayout(12, 12));
+               fileManagerPanel.add(rightPanel, BorderLayout.CENTER);
+
+               /* properties panel */
                JPanel propertiesPanel = new JPanel(new GridBagLayout());
-               fileManagerPanel.add(propertiesPanel, BorderLayout.CENTER);
+               rightPanel.add(propertiesPanel, BorderLayout.CENTER);
                propertiesPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
 
                insertCheckBox = new JCheckBox(insertAction);
@@ -226,12 +254,20 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
 
                propertiesPanel.add(new JPanel(), new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
 
+               /* action button panel */
+               JPanel actionButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
+               rightPanel.add(actionButtonPanel, BorderLayout.PAGE_END);
+               actionButtonPanel.setBorder(BorderFactory.createEtchedBorder());
+
+               JButton rescanButton = new JButton(rescanAction);
+               actionButtonPanel.add(rescanButton);
+
                return fileManagerPanel;
        }
 
        /**
         * Creates the button panel.
-        *
+        * 
         * @return The button panel
         */
        private Component createButtonPanel() {
@@ -245,15 +281,44 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                return buttonPanel;
        }
 
+       /**
+        * Initiates a file scan.
+        */
        private void initiateFileScan() {
                swingInterface.getThreadPool().execute(new Runnable() {
+
                        /**
                         * @see java.lang.Runnable#run()
                         */
+                       @SuppressWarnings("synthetic-access")
                        public void run() {
-                String basePath = project.getBasePath();
-                File basePathDirectory = new File(basePath);
+                               String basePath = project.getBasePath();
+                               File basePathDirectory = new File(basePath);
+                               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);
+                               }
+                               fileTreeModel.reload();
+                               fileScrollPane.revalidate();
+                               rescanAction.setEnabled(true);
                        }
+
+                       private void scanDirectory(SortableTreeNode rootNode, File directory) {
+                               for (File file: directory.listFiles()) {
+                                       SortableTreeNode fileNode = new SortableTreeNode(file.getName());
+                                       rootNode.add(fileNode);
+                                       if (file.isDirectory()) {
+                                               scanDirectory(fileNode, file);
+                                       }
+                               }
+                               rootNode.sort();
+                       }
+
                });
        }
 
@@ -276,6 +341,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
         * {@inheritDoc}
         */
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
+               /* TODO */
        }
 
        //
@@ -286,6 +352,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
         * {@inheritDoc}
         */
        public void actionPerformed(ActionEvent actionEvent) {
+               /* TODO */
        }
 
 }