improve file manager
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 28 May 2008 06:40:52 +0000 (08:40 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 28 May 2008 06:40:52 +0000 (08:40 +0200)
src/net/pterodactylus/jsite/gui/FileManager.java

index d84b6d2..68dfb2c 100644 (file)
@@ -26,6 +26,7 @@ import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.awt.Insets;
 import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.io.File;
@@ -66,7 +67,7 @@ import net.pterodactylus.util.swing.SwingUtils;
  * 
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  */
-public class FileManager extends JDialog implements I18nable, TreeSelectionListener {
+public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener {
 
        /** Logger. */
        private static final Logger logger = Logging.getLogger(FileManager.class.getName());
@@ -82,18 +83,21 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe
 
        /** 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 “mime type” label. */
-       private I18nLabel mimeTypeLabel;
+
+       /** 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;
@@ -135,13 +139,36 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe
                        }
                };
                insertAction = new I18nAction("fileManager.checkbox.insertFile") {
+
                        /**
                         * {@inheritDoc}
                         */
+                       @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent actionEvent) {
-                               /* TODO - implements. */
+                               useCustomMimeTypeAction.setEnabled(insertCheckBox.isSelected());
+                               mimeTypeComboBox.setEnabled(insertCheckBox.isSelected());
+                               for (Entry entry: getSelectedEntries()) {
+                                       entry.setInsert(insertCheckBox.isSelected());
+                               }
+                       }
+               };
+               insertAction.setEnabled(false);
+               useCustomMimeTypeAction = new I18nAction("fileManager.checkbox.useCustomMimeType") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               mimeTypeComboBox.setEnabled(useCustomMimeTypeCheckBox.isSelected());
+                               if (!useCustomMimeTypeCheckBox.isSelected()) {
+                                       for (Entry entry: getSelectedEntries()) {
+                                               entry.setContentType(null);
+                                       }
+                               }
                        }
                };
+               useCustomMimeTypeAction.setEnabled(false);
        }
 
        /**
@@ -167,11 +194,12 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe
 
                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);
@@ -184,11 +212,12 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe
                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();
-               allMimeTypes.add(0, "auto-detect");
                mimeTypeComboBox = new JComboBox(allMimeTypes.toArray(new String[0]));
-               mimeTypeLabel = new I18nLabel("fileManager.label.mimeType", mimeTypeComboBox);
-               propertiesPanel.add(mimeTypeLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 0, 0, 0), 0, 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));
@@ -212,6 +241,23 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe
                return buttonPanel;
        }
 
+       /**
+        * Returns a list of all selected entries.
+        * 
+        * @return The selected entries
+        */
+       private List<Entry> getSelectedEntries() {
+               TreePath[] selectedPaths = fileTree.getSelectionPaths();
+               List<Entry> entries = new ArrayList<Entry>();
+               for (TreePath selectedPath: selectedPaths) {
+                       Entry entry = ((FileTreePath) selectedPath.getLastPathComponent()).getFileEntry();
+                       if (entry != null) {
+                               entries.add(entry);
+                       }
+               }
+               return entries;
+       }
+
        //
        // INTERFACE I18nable
        //
@@ -226,12 +272,48 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe
        //
        // INTERFACE TreeSelectionListener
        //
-       
+
        /**
         * {@inheritDoc}
         */
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
-               /* TODO - implement */
+               TreePath[] selectedPaths = fileTree.getSelectionPaths();
+               if (selectedPaths.length == 1) {
+                       Entry fileEntry = ((FileTreePath) selectedPaths[0].getLastPathComponent()).getFileEntry();
+                       if (fileEntry == null) {
+                               /* some directory node selected. */
+                               insertAction.setEnabled(false);
+                               insertCheckBox.setSelected(false);
+                               useCustomMimeTypeAction.setEnabled(false);
+                               useCustomMimeTypeCheckBox.setSelected(false);
+                               mimeTypeComboBox.setEnabled(false);
+                       } else {
+                               String contentType = fileEntry.getContentType();
+                               insertAction.setEnabled(true);
+                               insertCheckBox.setSelected(fileEntry.isInsert());
+                               useCustomMimeTypeAction.setEnabled(fileEntry.isInsert());
+                               useCustomMimeTypeCheckBox.setSelected(contentType != null);
+                               mimeTypeComboBox.setEnabled(contentType != null);
+                               mimeTypeComboBox.setSelectedItem(contentType);
+                       }
+               }
+       }
+
+       //
+       // INTERFACE ActionListener
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public void actionPerformed(ActionEvent actionEvent) {
+               if (actionEvent.getSource() == mimeTypeComboBox) {
+                       String contentType = (String) mimeTypeComboBox.getSelectedItem();
+                       for (Entry entry: getSelectedEntries()) {
+                               entry.setContentType(contentType);
+                       }
+                       useCustomMimeTypeCheckBox.setSelected(contentType != null);
+               }
        }
 
        /**
@@ -384,6 +466,51 @@ public class FileManager extends JDialog implements I18nable, TreeSelectionListe
                        fileTreePathRootNode.removeAllChildren();
                        convertTree(File.separator, pathRootNode, fileTreePathRootNode.addChild(new FileTreePath(File.separator, project.getName())));
                        /* TODO - now add entries to all file tree path tree nodes. */
+                       addEntries(fileTreePathRootNode.getChild(0), project.getVirtualEntries());
+                       addEntries(fileTreePathRootNode.getChild(0), project.getBasePathEntries());
+               }
+
+               /**
+                * Traverses the tree and assigned {@link Entry}s to every file tree
+                * path whose name matchtes the name of an Entry.
+                * 
+                * @param fileTreePathRootNode
+                *            The root node of the tree to walk
+                * @param entries
+                *            The list of entries
+                */
+               private void addEntries(Node<FileTreePath> fileTreePathRootNode, List<Entry> entries) {
+                       for (Entry entry: entries) {
+                               String completeEntryName = File.separatorChar + entry.getName();
+                               FileTreePath fileTreePath = getFileTreePath(fileTreePathRootNode, completeEntryName);
+                               if (fileTreePath != null) {
+                                       fileTreePath.setFileEntry(entry);
+                               }
+                       }
+               }
+
+               /**
+                * Find the {@link FileTreePath} below the given node that has the given
+                * file path.
+                * 
+                * @param fileTreePathNode
+                *            The node to start searching at
+                * @param filePath
+                *            The path to search
+                * @return The file tree path with the matching file path, or
+                *         <code>null</code> if these is no such file tree path
+                */
+               private FileTreePath getFileTreePath(Node<FileTreePath> fileTreePathNode, String filePath) {
+                       for (Node<FileTreePath> child: fileTreePathNode) {
+                               if (child.getElement().getFilePath().equals(filePath)) {
+                                       return child.getElement();
+                               }
+                               FileTreePath fileTreePath = getFileTreePath(child, filePath);
+                               if (fileTreePath != null) {
+                                       return fileTreePath;
+                               }
+                       }
+                       return null;
                }
 
                /**