prevent closing when default file is invalid
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 30 May 2008 17:25:07 +0000 (19:25 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 30 May 2008 17:25:07 +0000 (19:25 +0200)
src/net/pterodactylus/jsite/gui/FileManager.java

index af1637e..c90614a 100644 (file)
@@ -76,7 +76,7 @@ import net.pterodactylus.util.swing.SwingUtils;
 
 /**
  * Manages physical and virtual files in a project.
- *
+ * 
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  */
 public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener, MouseListener {
@@ -155,7 +155,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
 
        /**
         * Creates a new file manager.
-        *
+        * 
         * @param swingInterface
         *            The Swing interface
         * @param project
@@ -203,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);
                        }
                };
@@ -222,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) {
@@ -253,6 +266,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                        }
                };
                overrideAction = new I18nAction("fileManager.checkbox.overrideActive") {
+
                        /**
                         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
                         */
@@ -282,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() {
@@ -361,7 +375,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
 
        /**
         * Creates the button panel.
-        *
+        * 
         * @return The button panel
         */
        private Component createButtonPanel() {
@@ -403,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
         */
@@ -425,7 +439,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
@@ -538,7 +552,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 {
@@ -600,7 +614,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 {
@@ -644,7 +658,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
                 */
@@ -658,7 +672,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);
                        }
                }
@@ -666,12 +680,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 }));
                        }
                }
@@ -682,7 +696,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
                 */
@@ -703,7 +717,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
@@ -711,7 +725,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);
                                }
@@ -804,17 +818,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);
@@ -826,7 +843,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 {
@@ -839,7 +856,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
                 */
@@ -849,7 +866,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
 
                /**
                 * Returns the wrapped project file.
-                *
+                * 
                 * @return The wrapped project file
                 */
                public ProjectFile getProjectFile() {
@@ -859,7 +876,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
                 */