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;
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;
*
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
-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());
/** 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;
/** 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.
*
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));
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));
});
}
+ /**
+ * 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
//
* {@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()));
+ }
+ }
}
//
/* 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.
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));
}
* @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));
}
/** The wrapped project file. */
private final ProjectFile projectFile;
+ /** The override name. */
+ private String nameOverride;
+
/**
* Creates a new wrapper around a project file.
*
}
/**
+ * 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();
}
}