add context menu
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 30 May 2008 06:45:36 +0000 (08:45 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 30 May 2008 06:45:36 +0000 (08:45 +0200)
show project name

src/net/pterodactylus/jsite/gui/FileManager.java
src/net/pterodactylus/jsite/i18n/jSite.properties
src/net/pterodactylus/jsite/i18n/jSite_de.properties

index b3f772e..0c714c5 100644 (file)
@@ -28,8 +28,11 @@ import java.awt.Font;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.awt.Insets;
+import java.awt.Point;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -40,9 +43,11 @@ import java.util.logging.Logger;
 
 import javax.swing.BorderFactory;
 import javax.swing.JButton;
+import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JDialog;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
+import javax.swing.JPopupMenu;
 import javax.swing.JScrollPane;
 import javax.swing.JTextField;
 import javax.swing.JTree;
@@ -68,7 +73,7 @@ import net.pterodactylus.util.swing.SwingUtils;
  * 
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  */
-public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener {
+public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener, MouseListener {
 
        /** Logger. */
        private static final Logger logger = Logging.getLogger(FileManager.class.getName());
@@ -91,6 +96,12 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
        /** The “close” action. */
        private I18nAction closeAction;
 
+       /** The “set default file” action. */
+       private I18nAction setDefaultFileAction;
+
+       /** The “insert” action. */
+       private I18nAction insertAction;
+
        /** The “project files” label. */
        private I18nLabel projectFilesLabel;
 
@@ -121,6 +132,12 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
        /** The “file size” text field. */
        private JTextField fileSizeTextField;
 
+       /** The context menu for the tree. */
+       private JPopupMenu treeContextMenu;
+
+       /** The “insert” checkbox. */
+       private JCheckBoxMenuItem insertCheckBoxMenuItem;
+
        /**
         * Creates a new file manager.
         * 
@@ -184,12 +201,35 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                                initiateFileScan();
                        }
                };
+               setDefaultFileAction = new I18nAction("fileManager.menu.item.setDefaultFile") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       public void actionPerformed(ActionEvent e) {
+                               /* TODO */
+                       }
+               };
+               insertAction = new I18nAction("fileManager.menu.item.insert") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       public void actionPerformed(ActionEvent e) {
+                               /* TODO */
+                       }
+               };
        }
 
        /**
         * Initializes all components.
         */
        private void initComponents() {
+               treeContextMenu = new JPopupMenu();
+               treeContextMenu.add(setDefaultFileAction);
+               insertCheckBoxMenuItem = new JCheckBoxMenuItem(insertAction);
+               treeContextMenu.add(insertCheckBoxMenuItem);
+
                JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
                contentPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
 
@@ -214,6 +254,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                fileTree = new JTree(fileTreeModel);
                fileTree.setShowsRootHandles(false);
                fileTree.addTreeSelectionListener(this);
+               fileTree.addMouseListener(this);
                fileTree.setCellRenderer(fileCellRenderer);
                fileTreePanel.add(fileScrollPane = new JScrollPane(fileTree), BorderLayout.CENTER);
                fileScrollPane.setPreferredSize(new Dimension(250, 400));
@@ -309,6 +350,26 @@ 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
+        */
+       private void maybeShowContextMenu(MouseEvent mouseEvent) {
+               if (!mouseEvent.isPopupTrigger()) {
+                       return;
+               }
+               Point eventLocation = mouseEvent.getPoint();
+               TreePath clickedPath = fileTree.getPathForLocation(eventLocation.x, eventLocation.y);
+               if (clickedPath == null) {
+                       return;
+               }
+               fileTree.setSelectionPath(clickedPath);
+               treeContextMenu.show(fileTree, eventLocation.x, eventLocation.y);
+       }
+
        //
        // INTERFACE I18nable
        //
@@ -331,7 +392,20 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
         * {@inheritDoc}
         */
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
-               /* TODO */
+               TreePath[] selectedPaths = fileTree.getSelectionPaths();
+               if (selectedPaths.length == 1) {
+                       Object lastPathComponent = selectedPaths[0].getLastPathComponent();
+                       if (!(lastPathComponent instanceof ProjectFileWrapper)) {
+                               logger.log(Level.SEVERE, "lastPathComponent is not a ProjectFileWrapper!");
+                               return;
+                       }
+                       ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) lastPathComponent;
+                       ProjectFile projectFile = projectFileWrapper.getProjectFile();
+                       if (projectFile.isFile()) {
+                               fileNameTextField.setText(projectFile.getName());
+                               fileSizeTextField.setText(String.valueOf(projectFile.getSize()));
+                       }
+               }
        }
 
        //
@@ -345,6 +419,45 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                /* TODO */
        }
 
+       //
+       // INTERFACE MouseListener
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public void mouseClicked(MouseEvent mouseEvent) {
+               maybeShowContextMenu(mouseEvent);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void mouseEntered(MouseEvent mouseEvent) {
+               /* ignore. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void mouseExited(MouseEvent mouseEvent) {
+               /* ignore. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void mousePressed(MouseEvent mouseEvent) {
+               maybeShowContextMenu(mouseEvent);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void mouseReleased(MouseEvent mouseEvent) {
+               maybeShowContextMenu(mouseEvent);
+       }
+
        /**
         * Tree cell renderer that takes care of certain display properties for
         * project-specific stuff.
@@ -387,6 +500,8 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                                superCellRenderer.setForeground(averageColor);
                        } else if (completePath.equals(project.getDefaultFile())) {
                                superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.BOLD));
+                       } else if (projectFile.getParents().size() == 1) {
+                               superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.BOLD));
                        } else {
                                superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.PLAIN));
                        }
@@ -459,10 +574,12 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                 * @param baseProjectFile
                 *            The new base project file
                 */
+               @SuppressWarnings("synthetic-access")
                public synchronized void setBaseProjectFile(ProjectFile baseProjectFile) {
                        this.baseProjectFile = baseProjectFile;
                        projectFileWrappers.clear();
                        createWrappers(baseProjectFile);
+                       projectFileWrappers.get(baseProjectFile).setNameOverride(project.getName());
                        fireTreeStructureChanged(projectFileWrappers.get(baseProjectFile));
                }
 
@@ -579,6 +696,9 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
                /** The wrapped project file. */
                private final ProjectFile projectFile;
 
+               /** The override name. */
+               private String nameOverride;
+
                /**
                 * Creates a new wrapper around a project file.
                 * 
@@ -599,11 +719,22 @@ 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
+                */
+               void setNameOverride(String nameOverride) {
+                       this.nameOverride = nameOverride;
+               }
+
+               /**
                 * {@inheritDoc}
                 */
                @Override
                public String toString() {
-                       return projectFile.getName();
+                       return (nameOverride != null) ? nameOverride : projectFile.getName();
                }
 
        }
index e7c10e6..6e3e3cc 100644 (file)
@@ -366,3 +366,15 @@ fileManager.checkbox.useCustomMimeType.mnemonic: VK_M
 fileManager.checkbox.useCustomMimeType.accelerator: Ctrl-VK_M
 fileManager.checkbox.useCustomMimeType.shortDescription: Use a custom MIME type for the file
 fileManager.checkbox.useCustomMimeType.longDescription: Use a cusstom MIME type for the file
+
+fileManager.menu.item.setDefaultFile.name: Set as default file
+fileManager.menu.item.setDefaultFile.mnemonic: VK_D
+fileManager.menu.item.setDefaultFile.accelerator: VK_UNDEFINED
+fileManager.menu.item.setDefaultFile.shortDescription: Sets this file as default file
+fileManager.menu.item.setDefaultFile.longDescription: Sets this file as default file
+
+fileManager.menu.item.insert.name: Insert
+fileManager.menu.item.insert.mnemonic: VK_I
+fileManager.menu.item.insert.accelerator: VK_UNDEFINED
+fileManager.menu.item.insert.shortDescription: Toggles the insert state of this file
+fileManager.menu.item.insert.longDescription Toggles the insert state of this file
index 0ebec9d..c3c4a94 100644 (file)
@@ -366,3 +366,15 @@ fileManager.checkbox.useCustomMimeType.mnemonic: VK_M
 fileManager.checkbox.useCustomMimeType.accelerator: Ctrl-VK_M
 fileManager.checkbox.useCustomMimeType.shortDescription: Einen anderen MIME-Typ f\u00fcr die Datei benutzen
 fileManager.checkbox.useCustomMimeType.longDescription: Einen anderen MIME-Typ f\u00fcr die Datei benutzen
+
+fileManager.menu.item.setDefaultFile.name: Als Indexdatei benutzen
+fileManager.menu.item.setDefaultFile.mnemonic: VK_I
+fileManager.menu.item.setDefaultFile.accelerator: VK_UNDEFINED
+fileManager.menu.item.setDefaultFile.shortDescription: Macht diese Datei zur Indexdatei
+fileManager.menu.item.setDefaultFile.longDescription: Macht diese Datei zur Indexdatei
+
+fileManager.menu.item.insert.name: Datei einfügen
+fileManager.menu.item.insert.mnemonic: VK_E
+fileManager.menu.item.insert.accelerator: VK_UNDEFINED
+fileManager.menu.item.insert.shortDescription: Wechselt den Einfügestatus dieser Datei
+fileManager.menu.item.insert.longDescription Wechselt den Einfügestatus dieser Datei