disable set default on directories
[jSite2.git] / src / net / pterodactylus / jsite / gui / FileManager.java
index f4ea43c..77df538 100644 (file)
@@ -35,6 +35,7 @@ import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -45,6 +46,7 @@ import java.util.logging.Logger;
 
 import javax.swing.BorderFactory;
 import javax.swing.JButton;
+import javax.swing.JCheckBox;
 import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JDialog;
 import javax.swing.JLabel;
@@ -74,7 +76,7 @@ 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, MouseListener {
@@ -136,6 +138,15 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
        /** The “file size” text field. */
        private JTextField fileSizeTextField;
 
+       /** The “override” label. */
+       private I18nLabel overrideLabel;
+
+       /** The “override active” action. */
+       private I18nAction overrideAction;
+
+       /** The “override action” checkbox. */
+       private JCheckBox overrideCheckBox;
+
        /** The context menu for the tree. */
        private JPopupMenu treeContextMenu;
 
@@ -144,7 +155,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
 
        /**
         * Creates a new file manager.
-        *
+        * 
         * @param swingInterface
         *            The Swing interface
         * @param project
@@ -192,7 +203,19 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                        /**
                         * {@inheritDoc}
                         */
+                       @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent e) {
+                               String defaultFile = project.getDefaultFile();
+                               if ((defaultFile == null) || (defaultFile.length() == 0)) {
+                                       /* TODO - i18n */
+                                       JOptionPane.showMessageDialog(FileManager.this, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
+                                       return;
+                               }
+                               ProjectFile projectFile = project.getFile(defaultFile);
+                               if (projectFile == null) {
+                                       JOptionPane.showMessageDialog(FileManager.this, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
+                                       return;
+                               }
                                setVisible(false);
                        }
                };
@@ -211,6 +234,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                        /**
                         * {@inheritDoc}
                         */
+                       @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent actionEvent) {
                                TreePath selectedPath = fileTree.getSelectionPath();
                                if (selectedPath == null) {
@@ -241,6 +265,15 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                                /* TODO */
                        }
                };
+               overrideAction = new I18nAction("fileManager.checkbox.overrideActive") {
+
+                       /**
+                        * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
+                        */
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               /* TODO */
+                       }
+               };
        }
 
        /**
@@ -263,7 +296,7 @@ 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() {
@@ -318,8 +351,16 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                propertiesPanel.add(fileSizeLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 24, 0, 0), 0, 0));
                propertiesPanel.add(fileSizeTextField, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
 
+               /* override settings. */
+               overrideLabel = new I18nLabel("fileManager.label.override");
+               overrideLabel.setFont(overrideLabel.getFont().deriveFont(Font.BOLD));
+               propertiesPanel.add(overrideLabel, new GridBagConstraints(0, 4, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(24, 0, 0, 0), 0, 0));
+
+               overrideCheckBox = new JCheckBox(overrideAction);
+               propertiesPanel.add(overrideCheckBox, new GridBagConstraints(0, 5, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 24, 0, 0), 0, 0));
+
                /* glue panel. */
-               propertiesPanel.add(new JPanel(), new GridBagConstraints(0, 4, 2, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
+               propertiesPanel.add(new JPanel(), new GridBagConstraints(0, 6, 2, 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));
@@ -334,7 +375,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
 
        /**
         * Creates the button panel.
-        *
+        * 
         * @return The button panel
         */
        private Component createButtonPanel() {
@@ -376,7 +417,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
        /**
         * Checks whether the given mouse event is a popup trigger and occured over
         * a file. If so, the context menu is shown.
-        *
+        * 
         * @param mouseEvent
         *            The mouse event to check
         */
@@ -391,6 +432,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                }
                fileTree.setSelectionPath(clickedPath);
                ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) clickedPath.getLastPathComponent();
+               setDefaultFileAction.setEnabled(projectFileWrapper.getProjectFile().isFile() && !isHidden(projectFileWrapper));
                insertCheckBoxMenuItem.setSelected(!isHidden(projectFileWrapper));
                treeContextMenu.show(fileTree, eventLocation.x, eventLocation.y);
        }
@@ -398,7 +440,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
        /**
         * Finds whether the {@link ProjectFile} given by
         * <code>projectFileWrapper</code> is hidden.
-        *
+        * 
         * @param projectFileWrapper
         *            The wrapped project file
         * @return <code>true</code> if the file is hidden and should not be
@@ -433,6 +475,9 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
         */
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
                TreePath[] selectedPaths = fileTree.getSelectionPaths();
+               filePathTextField.setText("");
+               fileNameTextField.setText("");
+               fileSizeTextField.setText("");
                if ((selectedPaths != null) && (selectedPaths.length == 1)) {
                        Object lastPathComponent = selectedPaths[0].getLastPathComponent();
                        if (!(lastPathComponent instanceof ProjectFileWrapper)) {
@@ -442,6 +487,13 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                        ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) lastPathComponent;
                        ProjectFile projectFile = projectFileWrapper.getProjectFile();
                        if (projectFile.isFile()) {
+                               String completePath = projectFile.getCompletePath();
+                               int lastSeparator = completePath.lastIndexOf(File.separatorChar);
+                               if (lastSeparator == -1) {
+                                       filePathTextField.setText("");
+                               } else {
+                                       filePathTextField.setText(completePath.substring(0, lastSeparator));
+                               }
                                fileNameTextField.setText(projectFile.getName());
                                fileSizeTextField.setText(String.valueOf(projectFile.getSize()));
                        }
@@ -501,7 +553,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
        /**
         * Tree cell renderer that takes care of certain display properties for
         * project-specific stuff.
-        *
+        * 
         * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
         */
        private class FileCellRenderer extends DefaultTreeCellRenderer {
@@ -563,7 +615,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
 
        /**
         * TreeModel that is based on {@link Project#getBaseFile()}.
-        *
+        * 
         * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
         */
        private class ProjectFileTreeModel implements TreeModel, PropertyChangeListener {
@@ -607,7 +659,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
 
                /**
                 * Notifies all listeners that a node has changed.
-                *
+                * 
                 * @param changedProjectFileWrapper
                 *            The wrapper around the changed project file
                 */
@@ -621,7 +673,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                                currentProjectFileParent = currentProjectFileParent.getParent();
                        } while (currentProjectFileParent != null);
                        TreeModelEvent treeModelEvent = new TreeModelEvent(this, parentProjectFileWrappers.toArray(), new int[] { getIndexOfChild(projectFileWrappers.get(changedProjectFileParent), changedProjectFileWrapper) }, new Object[] { changedProjectFileWrapper });
-                       for (TreeModelListener treeModelListener : treeModelListeners) {
+                       for (TreeModelListener treeModelListener: treeModelListeners) {
                                treeModelListener.treeNodesChanged(treeModelEvent);
                        }
                }
@@ -629,12 +681,12 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                /**
                 * Notifies all listeners that the tree structure has changed
                 * significantly.
-                *
+                * 
                 * @see TreeModelListener#treeStructureChanged(TreeModelEvent)
                 * @param newRootNode
                 */
                protected void fireTreeStructureChanged(ProjectFileWrapper newRootNode) {
-                       for (TreeModelListener treeModelListener : treeModelListeners) {
+                       for (TreeModelListener treeModelListener: treeModelListeners) {
                                treeModelListener.treeStructureChanged(new TreeModelEvent(this, new Object[] { newRootNode }));
                        }
                }
@@ -645,7 +697,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
 
                /**
                 * Sets the new base project file. This causes the model to reload.
-                *
+                * 
                 * @param baseProjectFile
                 *            The new base project file
                 */
@@ -666,7 +718,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                /**
                 * Creates {@link ProjectFileWrapper}s for all files below the given
                 * project file.
-                *
+                * 
                 * @param projectFile
                 *            The base project file for all project files to create
                 *            wrappers for
@@ -674,7 +726,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                private void createWrappers(ProjectFile projectFile) {
                        projectFileWrappers.put(projectFile, new ProjectFileWrapper(projectFile));
                        pathProjectFiles.put(projectFile.getCompletePath(), projectFile);
-                       for (ProjectFile projectFileChild : projectFile.getFiles()) {
+                       for (ProjectFile projectFileChild: projectFile.getFiles()) {
                                if (projectFileChild.isDirectory()) {
                                        createWrappers(projectFileChild);
                                }
@@ -749,12 +801,8 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                                logger.log(Level.SEVERE, "node is not a ProjectFileWrapper!");
                                return true;
                        }
-                       if (node == projectFileWrappers.get(baseProjectFile)) {
-                               return false;
-                       }
                        ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) node;
-                       ProjectFile projectFile = projectFileWrapper.getProjectFile();
-                       return projectFile.getFiles().isEmpty();
+                       return projectFileWrapper.getProjectFile().isFile();
                }
 
                /**
@@ -771,17 +819,20 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                /**
                 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
                 */
+               @SuppressWarnings("synthetic-access")
                public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
                        if (Project.PROPERTY_DEFAULT_FILE.equals(propertyChangeEvent.getPropertyName())) {
-                               String oldCompletePath = (String) propertyChangeEvent.getOldValue();
+                               if (propertyChangeEvent.getOldValue() != null) {
+                                       String oldCompletePath = (String) propertyChangeEvent.getOldValue();
+                                       ProjectFile oldProjectFile = pathProjectFiles.get(oldCompletePath);
+                                       ProjectFileWrapper oldProjectFileWrapper = projectFileWrappers.get(oldProjectFile);
+                                       System.out.println("oldProjectFileWrapper: " + oldProjectFileWrapper);
+                                       fireTreeNodesChanged(oldProjectFileWrapper);
+                               }
                                String newCompletePath = (String) propertyChangeEvent.getNewValue();
-                               ProjectFile oldProjectFile = pathProjectFiles.get(oldCompletePath);
                                ProjectFile newProjectFile = pathProjectFiles.get(newCompletePath);
-                               ProjectFileWrapper oldProjectFileWrapper = projectFileWrappers.get(oldProjectFile);
                                ProjectFileWrapper newProjectFileWrapper = projectFileWrappers.get(newProjectFile);
-                               System.out.println("oldProjectFileWrapper: " + oldProjectFileWrapper);
                                System.out.println("newProjectFileWrapper: " + newProjectFileWrapper);
-                               fireTreeNodesChanged(oldProjectFileWrapper);
                                fireTreeNodesChanged(newProjectFileWrapper);
                                /* HACK - swing sucks a bit. */
                                fileTree.setShowsRootHandles(false);
@@ -793,7 +844,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
        /**
         * Wrapper around a {@link ProjectFile} that overwrites
         * {@link Object#toString()} to return the project file’s name.
-        *
+        * 
         * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
         */
        private static class ProjectFileWrapper {
@@ -806,7 +857,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
 
                /**
                 * Creates a new wrapper around a project file.
-                *
+                * 
                 * @param projectFile
                 *            The project file to wrap
                 */
@@ -816,7 +867,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
 
                /**
                 * Returns the wrapped project file.
-                *
+                * 
                 * @return The wrapped project file
                 */
                public ProjectFile getProjectFile() {
@@ -826,7 +877,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                /**
                 * Sets the name override. If the name override is not <code>null</code>
                 * it will be shown insted of the project file’s name.
-                *
+                * 
                 * @param nameOverride
                 *            The name override
                 */