next stab at file manager
[jSite2.git] / src / net / pterodactylus / jsite / gui / FileManager.java
index 78c24a7..038eda8 100644 (file)
 
 package net.pterodactylus.jsite.gui;
 
+import java.awt.BorderLayout;
+import java.awt.Component;
+import java.awt.FlowLayout;
+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.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.swing.BorderFactory;
+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.DefaultTreeModel;
+import javax.swing.tree.TreeModel;
 
 import net.pterodactylus.jsite.i18n.I18n;
 import net.pterodactylus.jsite.i18n.I18nable;
+import net.pterodactylus.jsite.i18n.gui.I18nAction;
+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 {
+public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener {
+
+       /** Logger. */
+       private static final Logger logger = Logging.getLogger(FileManager.class.getName());
+
+       /** The Swing interface. */
+       private final SwingInterface swingInterface;
 
        /** 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;
+
+       /** The “rescan” action. */
+       private I18nAction rescanAction;
+
+       /** The “close” action. */
+       private I18nAction closeAction;
+
+       /** The “project files” label. */
+       private I18nLabel projectFilesLabel;
+
+       /** The tree that shows the files. */
+       private JTree fileTree;
+
+       /** The “insert” action. */
+       private I18nAction insertAction;
+
+       /** The “insert” checkbox. */
+       private JCheckBox insertCheckBox;
+
+       /** The “use custom mime type” action. */
+       private I18nAction useCustomMimeTypeAction;
+
+       /** The “use custom mime type” checkbox. */
+       private JCheckBox useCustomMimeTypeCheckBox;
+
+       /** The “mime type” combo box. */
+       private JComboBox mimeTypeComboBox;
+
        /**
         * Creates a new file manager.
         * 
-        * @param parent
-        *            The parent frame
+        * @param swingInterface
+        *            The Swing interface
         * @param project
         *            The project whose files to manage
         */
-       public FileManager(JFrame parent, Project project) {
-               super(parent, I18n.get("fileManager.title", project.getName()));
+       public FileManager(SwingInterface swingInterface, Project project) {
+               super(swingInterface.getMainWindow(), I18n.get("fileManager.title", project.getName()), true);
+               logger.log(Level.FINEST, "project: " + project);
+               this.swingInterface = swingInterface;
                this.project = project;
+               fileTreeRoot = new SortableTreeNode(project.getName());
+               fileTreeModel = new DefaultTreeModel(fileTreeRoot);
+               initActions();
                initComponents();
+               pack();
+               SwingUtils.center(this);
+       }
+
+       //
+       // ACTIONS
+       //
+
+       /**
+        * @see java.awt.Component#setVisible(boolean)
+        */
+       @Override
+       public void setVisible(boolean visible) {
+               if (visible) {
+                       initiateFileScan();
+               }
+               super.setVisible(visible);
        }
 
        //
-       // PRIVATE ACTIONS
+       // PRIVATE METHODS
        //
 
        /**
+        * Initializes all actions.
+        */
+       private void initActions() {
+               closeAction = new I18nAction("fileManager.button.close") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       public void actionPerformed(ActionEvent e) {
+                               setVisible(false);
+                       }
+               };
+               rescanAction = new I18nAction("fileManager.button.rescan") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               initiateFileScan();
+                       }
+               };
+               insertAction = new I18nAction("fileManager.checkbox.insertFile") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               /* TODO */
+                       }
+               };
+               insertAction.setEnabled(false);
+               useCustomMimeTypeAction = new I18nAction("fileManager.checkbox.useCustomMimeType") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               /* TODO */
+                       }
+               };
+               useCustomMimeTypeAction.setEnabled(false);
+       }
+
+       /**
         * Initializes all components.
         */
        private void initComponents() {
-               /* TODO. */
+               JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
+               contentPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
+
+               contentPanel.add(createFileManagerPanel(), BorderLayout.CENTER);
+               contentPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
+
+               setContentPane(contentPanel);
+       }
+
+       /**
+        * 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);
+
+               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);
+
+               /* properties panel */
+               JPanel propertiesPanel = new JPanel(new GridBagLayout());
+               fileManagerPanel.add(propertiesPanel, BorderLayout.CENTER);
+               propertiesPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
+
+               insertCheckBox = new JCheckBox(insertAction);
+               propertiesPanel.add(insertCheckBox, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
+
+               useCustomMimeTypeCheckBox = new JCheckBox(useCustomMimeTypeAction);
+               List<String> allMimeTypes = MimeTypes.getAllMimeTypes();
+               mimeTypeComboBox = new JComboBox(allMimeTypes.toArray(new String[0]));
+               mimeTypeComboBox.setEnabled(false);
+               mimeTypeComboBox.addActionListener(this);
+               propertiesPanel.add(useCustomMimeTypeCheckBox, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 0, 0, 0), 0, 0));
+               propertiesPanel.add(mimeTypeComboBox, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
+
+               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));
+               actionButtonPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(-12, -12, -12, -12)));
+               
+               return fileManagerPanel;
+       }
+
+       /**
+        * Creates the button panel.
+        * 
+        * @return The button panel
+        */
+       private Component createButtonPanel() {
+               JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
+
+               buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
+               JButton closeButton = new JButton(closeAction);
+               buttonPanel.add(closeButton);
+
+               getRootPane().setDefaultButton(closeButton);
+               return buttonPanel;
+       }
+
+       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);
+                               if (!basePathDirectory.exists() || !basePathDirectory.isDirectory()) {
+                                       /* TODO - i18n */
+                                       JOptionPane.showMessageDialog(FileManager.this, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
+                                       return;
+                               }
+                               synchronized (fileTreeRoot) {
+                                       scanDirectory(fileTreeRoot, basePathDirectory);
+                               }
+                               fileTree.repaint();
+                       }
+
+                       private void scanDirectory(SortableTreeNode rootNode, File directory) {
+                               System.out.println("scanning " + directory.getAbsolutePath());
+                               for (File file: directory.listFiles()) {
+                                       SortableTreeNode fileNode = new SortableTreeNode(file.getName());
+                                       rootNode.add(fileNode);
+                                       if (file.isDirectory()) {
+                                               scanDirectory(fileNode, file);
+                                       }
+                               }
+                               rootNode.sort();
+                       }
+
+               });
        }
 
        //
@@ -72,4 +315,24 @@ public class FileManager extends JDialog implements I18nable {
                setTitle(I18n.get("fileManager.title", project.getName()));
        }
 
+       //
+       // INTERFACE TreeSelectionListener
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
+       }
+
+       //
+       // INTERFACE ActionListener
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public void actionPerformed(ActionEvent actionEvent) {
+       }
+
 }