X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fgui%2FFileManager.java;h=b0713a32a20788a8af199679372fe6b65338711e;hb=6846df4a60a659b23346881b3c860e40002930bf;hp=faec4ae72fea862b441c590b1257c0469539eca8;hpb=8c8b64909ad70184080464d940aef7d86e04c408;p=jSite2.git
diff --git a/src/net/pterodactylus/jsite/gui/FileManager.java b/src/net/pterodactylus/jsite/gui/FileManager.java
index faec4ae..b0713a3 100644
--- a/src/net/pterodactylus/jsite/gui/FileManager.java
+++ b/src/net/pterodactylus/jsite/gui/FileManager.java
@@ -20,29 +20,57 @@
package net.pterodactylus.jsite.gui;
import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.FlowLayout;
+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.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.swing.BorderFactory;
+import javax.swing.JButton;
+import javax.swing.JCheckBox;
import javax.swing.JDialog;
-import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
import javax.swing.JPanel;
+import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
+import javax.swing.JTextField;
import javax.swing.JTree;
+import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
+import javax.swing.event.TreeSelectionEvent;
+import javax.swing.event.TreeSelectionListener;
+import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import net.pterodactylus.jsite.i18n.I18n;
import net.pterodactylus.jsite.i18n.I18nable;
-import net.pterodactylus.jsite.project.Entry;
+import net.pterodactylus.jsite.i18n.gui.I18nAction;
+import net.pterodactylus.jsite.i18n.gui.I18nLabel;
+import net.pterodactylus.jsite.i18n.gui.I18nMenu;
+import net.pterodactylus.jsite.project.FileOverride;
import net.pterodactylus.jsite.project.Project;
-import net.pterodactylus.util.data.Node;
-import net.pterodactylus.util.data.Tree;
+import net.pterodactylus.jsite.project.ProjectFile;
import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.swing.SwingUtils;
@@ -51,52 +79,457 @@ import net.pterodactylus.util.swing.SwingUtils;
*
* @author David âBombeâ Roden <bombe@freenetproject.org>
*/
-public class FileManager extends JDialog implements I18nable {
+public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener, MouseListener {
/** Logger. */
private static final Logger logger = Logging.getLogger(FileManager.class.getName());
+ /** The Swing interface. */
+ private final SwingInterface swingInterface;
+
/** The project whose files to manage. */
private final Project project;
/** The tree model for the project files. */
- private final FileTreeModel fileTreeModel;
+ private final ProjectFileTreeModel fileTreeModel;
+
+ /** The tree cell renderer. */
+ private final FileCellRenderer fileCellRenderer;
+
+ /** The ârescanâ action. */
+ private I18nAction rescanAction;
+
+ /** 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 tree that shows the files. */
private JTree fileTree;
+ /** The scroll pane that holds the file tree. */
+ private JScrollPane fileScrollPane;
+
+ /** The âfile propertiesâ label. */
+ private I18nLabel filePropertiesLabel;
+
+ /** The âfile pathâ label. */
+ private I18nLabel filePathLabel;
+
+ /** The âfile pathâ textfield. */
+ private JTextField filePathTextField;
+
+ /** The âfile nameâ label. */
+ private I18nLabel fileNameLabel;
+
+ /** The âfile nameâ textfield. */
+ private JTextField fileNameTextField;
+
+ /** The âfile sizeâ label. */
+ private I18nLabel fileSizeLabel;
+
+ /** 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;
+
+ /** The âapply overrideâ menu. */
+ private I18nMenu overrideMenu;
+
+ /** The âapply insert overrideâ action. */
+ private I18nAction applyInsertOverrideAction;
+
+ /** The âapply mime type overrideâ action. */
+ private I18nAction applyMimeTypeOverrideAction;
+
/**
* Creates a new file manager.
*
- * @param parent
- * The parent frame
+ * @param swingInterface
+ * The Swing interface
* @param project
* The project whose files to manage
*/
- public FileManager(JFrame parent, Project project) {
- super(parent, I18n.get("fileManager.title", project.getName()), true);
+ public FileManager(SwingInterface swingInterface, Project project) {
+ super(swingInterface.getMainWindow(), I18n.get("fileManager.title", project.getName()), true);
logger.log(Level.FINEST, "project: " + project);
+ this.swingInterface = swingInterface;
this.project = project;
- fileTreeModel = new FileTreeModel();
+ fileTreeModel = new ProjectFileTreeModel();
+ project.addPropertyChangeListener(fileTreeModel);
+ fileCellRenderer = new FileCellRenderer();
+ initActions();
initComponents();
- SwingUtils.repackCentered(this);
+ pack();
+ SwingUtils.center(this);
}
//
- // PRIVATE ACTIONS
+ // ACTIONS
//
/**
+ * @see java.awt.Component#setVisible(boolean)
+ */
+ @Override
+ public void setVisible(boolean visible) {
+ if (visible) {
+ initiateFileScan();
+ }
+ super.setVisible(visible);
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Initializes all actions.
+ */
+ private void initActions() {
+ closeAction = new I18nAction("fileManager.button.close") {
+
+ /**
+ * {@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);
+ }
+ };
+ rescanAction = new I18nAction("fileManager.button.rescan") {
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("synthetic-access")
+ public void actionPerformed(ActionEvent actionEvent) {
+ initiateFileScan();
+ }
+ };
+ setDefaultFileAction = new I18nAction("fileManager.menu.item.setDefaultFile") {
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("synthetic-access")
+ public void actionPerformed(ActionEvent actionEvent) {
+ TreePath selectedPath = fileTree.getSelectionPath();
+ if (selectedPath == null) {
+ logger.log(Level.WARNING, "nothing selected!");
+ return;
+ }
+ ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) selectedPath.getLastPathComponent();
+ if (isHidden(projectFileWrapper)) {
+ /* TODO - i18n */
+ JOptionPane.showMessageDialog(FileManager.this, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ if (projectFileWrapper.getProjectFile().isDirectory()) {
+ /* TODO - i18n */
+ JOptionPane.showMessageDialog(FileManager.this, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ String completePath = projectFileWrapper.getProjectFile().getCompletePath();
+ project.setDefaultFile(completePath);
+ }
+ };
+ insertAction = new I18nAction("fileManager.menu.item.insert") {
+
+ /**
+ * {@inheritDoc}
+ */
+ public void actionPerformed(ActionEvent e) {
+ /* TODO */
+ }
+ };
+ overrideAction = new I18nAction("fileManager.checkbox.overrideActive") {
+
+ /**
+ * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
+ */
+ public void actionPerformed(ActionEvent actionEvent) {
+ /* TODO */
+ }
+ };
+ applyInsertOverrideAction = new I18nAction("fileManager.menu.item.applyInsertOverride") {
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("synthetic-access")
+ public void actionPerformed(ActionEvent actionEvent) {
+ applyInsertOverride();
+ }
+ };
+ applyMimeTypeOverrideAction = new I18nAction("fileManager.menu.item.applyMimeTypeOverride") {
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("synthetic-access")
+ public void actionPerformed(ActionEvent actionEvent) {
+ applyMimeTypeOverride();
+ }
+ };
+ }
+
+ /**
* Initializes all components.
*/
private void initComponents() {
+ treeContextMenu = new JPopupMenu();
+ treeContextMenu.add(setDefaultFileAction);
+
+ overrideMenu = new I18nMenu("fileManager.menu.override");
+ treeContextMenu.add(overrideMenu);
+
+ overrideMenu.add(applyInsertOverrideAction);
+ overrideMenu.add(applyMimeTypeOverrideAction);
+
JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
+ contentPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
+
+ contentPanel.add(createFileManagerPanel(), BorderLayout.CENTER);
+ contentPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
+
+ setContentPane(contentPanel);
+ }
+
+ /**
+ * Creates the main panel with the file tree and the file properties.
+ *
+ * @return The mail panel
+ */
+ private Component createFileManagerPanel() {
+ JPanel fileManagerPanel = new JPanel(new BorderLayout(12, 12));
+
+ /* file tree panel */
+ JPanel fileTreePanel = new JPanel(new BorderLayout(12, 12));
+ fileManagerPanel.add(fileTreePanel, BorderLayout.LINE_START);
fileTree = new JTree(fileTreeModel);
fileTree.setShowsRootHandles(false);
- contentPanel.add(new JScrollPane(fileTree), BorderLayout.CENTER);
+ fileTree.addTreeSelectionListener(this);
+ fileTree.addMouseListener(this);
+ fileTree.setCellRenderer(fileCellRenderer);
+ fileTreePanel.add(fileScrollPane = new JScrollPane(fileTree), BorderLayout.CENTER);
+ fileScrollPane.setPreferredSize(new Dimension(200, 350));
+
+ projectFilesLabel = new I18nLabel("fileManager.label.projectFiles", fileTree);
+ JPanel projectFilesLabelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
+ fileTreePanel.add(projectFilesLabelPanel, BorderLayout.NORTH);
+ projectFilesLabelPanel.add(projectFilesLabel);
+
+ /* the right panel */
+ JPanel rightPanel = new JPanel(new BorderLayout(12, 12));
+ fileManagerPanel.add(rightPanel, BorderLayout.CENTER);
+
+ /* properties panel */
+ JPanel propertiesPanel = new JPanel(new GridBagLayout());
+ rightPanel.add(propertiesPanel, BorderLayout.CENTER);
+ propertiesPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
+ propertiesPanel.setPreferredSize(new Dimension(400, 350));
+
+ filePropertiesLabel = new I18nLabel("fileManager.label.fileProperties");
+ filePropertiesLabel.setFont(filePropertiesLabel.getFont().deriveFont(Font.BOLD));
+ propertiesPanel.add(filePropertiesLabel, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
+
+ filePathLabel = new I18nLabel("fileManager.label.filePath");
+ filePathTextField = new JTextField();
+ filePathTextField.setEditable(false);
+ propertiesPanel.add(filePathLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 24, 0, 0), 0, 0));
+ propertiesPanel.add(filePathTextField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
+
+ fileNameLabel = new I18nLabel("fileManager.label.fileName");
+ fileNameTextField = new JTextField();
+ fileNameTextField.setEditable(false);
+ propertiesPanel.add(fileNameLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 24, 0, 0), 0, 0));
+ propertiesPanel.add(fileNameTextField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
+
+ fileSizeLabel = new I18nLabel("fileManager.label.fileSize");
+ fileSizeTextField = new JTextField();
+ fileSizeTextField.setEditable(false);
+ 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, 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));
+ rightPanel.add(actionButtonPanel, BorderLayout.PAGE_END);
+ actionButtonPanel.setBorder(BorderFactory.createEtchedBorder());
+
+ JButton rescanButton = new JButton(rescanAction);
+ actionButtonPanel.add(rescanButton);
+
+ return fileManagerPanel;
+ }
- setContentPane(contentPanel);
+ /**
+ * Creates the button panel.
+ *
+ * @return The button panel
+ */
+ private Component createButtonPanel() {
+ JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
+
+ buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
+ JButton closeButton = new JButton(closeAction);
+ buttonPanel.add(closeButton);
+
+ getRootPane().setDefaultButton(closeButton);
+ return buttonPanel;
+ }
+
+ /**
+ * Initiates a file scan.
+ */
+ private void initiateFileScan() {
+ swingInterface.getThreadPool().execute(new Runnable() {
+
+ /**
+ * @see java.lang.Runnable#run()
+ */
+ @SuppressWarnings("synthetic-access")
+ public void run() {
+ fileTree.setEnabled(false);
+ rescanAction.setEnabled(false);
+ ProjectFile baseProjectFile = project.getBaseFile();
+ if (baseProjectFile != null) {
+ fileTreeModel.setBaseProjectFile(baseProjectFile);
+ }
+ // fileScrollPane.revalidate();
+ rescanAction.setEnabled(true);
+ fileTree.setEnabled(true);
+ }
+
+ });
+ }
+
+ /**
+ * 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[] selectedPaths = fileTree.getSelectionPaths();
+ if ((selectedPaths == null) || (selectedPaths.length == 0)) {
+ /* try to find item under click. */
+ TreePath clickedPath = fileTree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
+ if (clickedPath != null) {
+ fileTree.setSelectionPath(clickedPath);
+ selectedPaths = new TreePath[] { clickedPath };
+ } else {
+ logger.log(Level.FINER, "nothing selected for context menu");
+ return;
+ }
+ }
+ if (selectedPaths.length == 1) {
+ ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) selectedPaths[0].getLastPathComponent();
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ setDefaultFileAction.setEnabled(!isHidden(projectFileWrapper) && projectFile.isFile() && !projectFile.getCompletePath().equals(project.getDefaultFile()));
+ } else {
+ setDefaultFileAction.setEnabled(false);
+ }
+ treeContextMenu.show(fileTree, eventLocation.x, eventLocation.y);
+ }
+
+ /**
+ * Finds whether the {@link ProjectFile} given by
+ * projectFileWrapper
is hidden.
+ *
+ * @param projectFileWrapper
+ * The wrapped project file
+ * @return true
if the file is hidden and should not be
+ * inserted, false
otherwise
+ */
+ private boolean isHidden(ProjectFileWrapper projectFileWrapper) {
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ FileOverride fileOverride = project.getFileOverrides().get(projectFile.getCompletePath());
+ logger.log(Level.FINEST, "fileOverride: " + fileOverride);
+ return ((fileOverride == null) && projectFile.isHidden()) || ((fileOverride != null) && (Boolean.TRUE.equals(fileOverride.isInsert())));
+ }
+
+ /**
+ * Opens the âapply insert overrideâ dialog and lets the user apply an
+ * override for the âinsertâ setting for multiple files.
+ */
+ private void applyInsertOverride() {
+ JCheckBox insertCheckBox = new JCheckBox(I18n.get("fileManager.checkbox.insertFile.name"));
+ String okString = I18n.get("general.button.okay.name");
+ String cancelString = I18n.get("general.button.cancel.name");
+ int choice = JOptionPane.showOptionDialog(this, new Object[] { I18n.get("fileManager.dialog.insertOverride.message"), insertCheckBox }, I18n.get("fileManager.dialog.insertOverride.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] { okString, cancelString }, okString);
+ logger.log(Level.FINEST, "choice: " + choice);
+ if ((choice == JOptionPane.CLOSED_OPTION) || (choice == 1)) {
+ return;
+ }
+ logger.log(Level.INFO, "selected insert override: " + insertCheckBox.isSelected());
+ TreePath[] selectedPaths = fileTree.getSelectionPaths();
+ for (TreePath selectedPath: selectedPaths) {
+ ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) selectedPath.getLastPathComponent();
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ if (!projectFile.isFile()) {
+ continue;
+ }
+ FileOverride fileOverride = project.getFileOverride(projectFile);
+ if (fileOverride == null) {
+ fileOverride = new FileOverride();
+ project.addFileOverride(projectFile, fileOverride);
+ }
+ fileOverride.setInsert(insertCheckBox.isSelected());
+ }
+ }
+
+ /**
+ * Opens the âapply mime type overrideâ dialog and lets the user apply an
+ * override for the âmime typeâ setting for multiple files.
+ */
+ private void applyMimeTypeOverride() {
+ /* TODO */
}
//
@@ -108,26 +541,184 @@ public class FileManager extends JDialog implements I18nable {
*/
public void updateI18n() {
setTitle(I18n.get("fileManager.title", project.getName()));
+ projectFilesLabel.updateI18n();
+ filePropertiesLabel.updateI18n();
+ filePathLabel.updateI18n();
+ }
+
+ //
+ // INTERFACE TreeSelectionListener
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ 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)) {
+ logger.log(Level.SEVERE, "lastPathComponent is not a ProjectFileWrapper!");
+ return;
+ }
+ 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()));
+ }
+ } else if ((selectedPaths != null) && (selectedPaths.length > 1)) {
+ /* TODO */
+ }
+ }
+
+ //
+ // INTERFACE ActionListener
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ public void actionPerformed(ActionEvent actionEvent) {
+ /* 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. */
}
/**
- * Model for the tree of files.
+ * {@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.
*
* @author David âBombeâ Roden <bombe@freenetproject.org>
*/
- private class FileTreeModel implements TreeModel, PropertyChangeListener {
+ private class FileCellRenderer extends DefaultTreeCellRenderer {
+
+ /**
+ * Empty constructor.
+ */
+ FileCellRenderer() {
+ /* do nothing. */
+ }
+
+ /**
+ * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree,
+ * java.lang.Object, boolean, boolean, boolean, int, boolean)
+ */
+ @SuppressWarnings("synthetic-access")
+ @Override
+ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
+ Component superCellRenderer = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
+ if (!(superCellRenderer instanceof JLabel)) {
+ logger.log(Level.SEVERE, "superCellRenderer is not a JLabel!");
+ return superCellRenderer;
+ }
+ if (!(value instanceof ProjectFileWrapper)) {
+ logger.log(Level.SEVERE, "value is not a ProjectFileWrapper!");
+ return superCellRenderer;
+ }
+ ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) value;
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ String completePath = projectFile.getCompletePath();
+ boolean paintBold = false;
+ boolean paintHalfColor = false;
+ if (projectFile.isFile() && projectFile.isHidden()) {
+ /* TODO - check override */
+ paintHalfColor = true;
+ } else if (completePath.equals(project.getDefaultFile())) {
+ paintBold = true;
+ } else if (projectFile.getParents().size() == 1) {
+ paintBold = true;
+ }
+ if (paintHalfColor) {
+ /* TODO - cache colors */
+ Color foreground = superCellRenderer.getForeground();
+ Color background = selected ? getBackgroundSelectionColor() : getBackgroundNonSelectionColor();
+ Color averageColor = new Color((foreground.getRed() + background.getRed()) / 2, (foreground.getGreen() + background.getGreen()) / 2, (foreground.getBlue() + background.getBlue()) / 2);
+ superCellRenderer.setForeground(averageColor);
+ } else {
+ superCellRenderer.setForeground(selected ? getTextSelectionColor() : getTextNonSelectionColor());
+ }
+ if (paintBold) {
+ superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.BOLD));
+ } else {
+ superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.PLAIN));
+ }
+ return superCellRenderer;
+ }
+
+ }
+
+ /**
+ * TreeModel that is based on {@link Project#getBaseFile()}.
+ *
+ * @author David âBombeâ Roden <bombe@freenetproject.org>
+ */
+ private class ProjectFileTreeModel implements TreeModel, PropertyChangeListener {
/** Tree model listeners. */
- private final List treeModelListeners = new ArrayList();
+ private final List treeModelListeners = Collections.synchronizedList(new ArrayList());
+
+ /** The base project file. */
+ private ProjectFile baseProjectFile;
- /** The tree of files. */
- private final Tree fileTreePathTree = new Tree();
+ /** Maps path names to project files. */
+ private final Map pathProjectFiles = Collections.synchronizedMap(new HashMap());
+
+ /** Maps project files to wrappers. */
+ private final Map projectFileWrappers = Collections.synchronizedMap(new HashMap());
/**
- * Creates a new file tree model.
+ * Empty constructor.
*/
- FileTreeModel() {
- buildTree();
+ ProjectFileTreeModel() {
+ /* do nothing. */
}
//
@@ -148,161 +739,159 @@ public class FileManager extends JDialog implements I18nable {
treeModelListeners.remove(treeModelListener);
}
- //
- // ACCESSORS
- //
-
/**
- * {@inheritDoc}
+ * Notifies all listeners that a node has changed.
+ *
+ * @param changedProjectFileWrapper
+ * The wrapper around the changed project file
*/
- @SuppressWarnings("synthetic-access")
- public Object getChild(Object parent, int index) {
- logger.log(Level.FINEST, "getChild(" + parent + ", " + index + ")");
- Node parentNode = findNode(parent);
- if (parentNode != null) {
- return parentNode.getChild(index).getElement();
+ protected void fireTreeNodesChanged(ProjectFileWrapper changedProjectFileWrapper) {
+ ProjectFile changedProjectFile = changedProjectFileWrapper.getProjectFile();
+ ProjectFile changedProjectFileParent = changedProjectFile.getParent();
+ ProjectFile currentProjectFileParent = changedProjectFile;
+ List parentProjectFileWrappers = new ArrayList();
+ do {
+ parentProjectFileWrappers.add(0, projectFileWrappers.get(currentProjectFileParent));
+ 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) {
+ treeModelListener.treeNodesChanged(treeModelEvent);
}
- return null;
}
/**
- * {@inheritDoc}
+ * Notifies all listeners that the tree structure has changed
+ * significantly.
+ *
+ * @see TreeModelListener#treeStructureChanged(TreeModelEvent)
+ * @param newRootNode
*/
- @SuppressWarnings("synthetic-access")
- public int getChildCount(Object parent) {
- logger.log(Level.FINEST, "getChildCount(" + parent + ")");
- Node parentNode = findNode(parent);
- if (parentNode != null) {
- logger.log(Level.FINEST, "getChildCount(" + parent + "): " + parentNode.size());
- return parentNode.size();
+ protected void fireTreeStructureChanged(ProjectFileWrapper newRootNode) {
+ for (TreeModelListener treeModelListener: treeModelListeners) {
+ treeModelListener.treeStructureChanged(new TreeModelEvent(this, new Object[] { newRootNode }));
}
- return -1;
}
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("synthetic-access")
- public int getIndexOfChild(Object parent, Object child) {
- logger.log(Level.FINEST, "getIndexOfChild(" + parent + ", " + child + ")");
- Node parentNode = findNode(parent);
- if (parentNode != null) {
- return parentNode.getIndexOfChild((FileTreePath) child);
- }
- return -1;
- }
+ //
+ // ACCESSORS
+ //
/**
- * {@inheritDoc}
+ * Sets the new base project file. This causes the model to reload.
+ *
+ * @param baseProjectFile
+ * The new base project file
*/
@SuppressWarnings("synthetic-access")
- public Object getRoot() {
- logger.log(Level.FINEST, "getRoot()");
- return fileTreePathTree.getRootNode().getChild(0).getElement();
+ public synchronized void setBaseProjectFile(ProjectFile baseProjectFile) {
+ this.baseProjectFile = baseProjectFile;
+ projectFileWrappers.clear();
+ pathProjectFiles.clear();
+ createWrappers(baseProjectFile);
+ projectFileWrappers.get(baseProjectFile).setNameOverride(project.getName());
+ fireTreeStructureChanged(projectFileWrappers.get(baseProjectFile));
}
+ //
+ // PRIVATE METHODS
+ //
+
/**
- * {@inheritDoc}
+ * 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
*/
- @SuppressWarnings("synthetic-access")
- public boolean isLeaf(Object node) {
- logger.log(Level.FINEST, "isLeaf(" + node + ")");
- Node parentNode = findNode(node);
- if (parentNode != null) {
- return parentNode.size() == 0;
+ private void createWrappers(ProjectFile projectFile) {
+ projectFileWrappers.put(projectFile, new ProjectFileWrapper(projectFile));
+ pathProjectFiles.put(projectFile.getCompletePath(), projectFile);
+ for (ProjectFile projectFileChild: projectFile.getFiles()) {
+ if (projectFileChild.isDirectory()) {
+ createWrappers(projectFileChild);
+ }
+ projectFileWrappers.put(projectFileChild, new ProjectFileWrapper(projectFileChild));
+ pathProjectFiles.put(projectFileChild.getCompletePath(), projectFileChild);
}
- return true;
}
//
- // ACTIONS
+ // INTERFACE TreeModel
//
/**
* {@inheritDoc}
*/
- public void valueForPathChanged(TreePath path, Object newValue) {
- /* TODO - implement */
+ public Object getRoot() {
+ return projectFileWrappers.get(baseProjectFile);
}
- //
- // PRIVATE METHODS
- //
-
/**
- * Finds the node for the given object. This method is quite necessary
- * because the element for the root node of the JTree is
- * null
- *
- * @param node
- * The element whose node to return
- * @return The node, or null
if no node could be found
+ * {@inheritDoc}
*/
- private Node findNode(Object node) {
- if (node == null) {
- return fileTreePathTree.getRootNode().getChild(0);
+ @SuppressWarnings("synthetic-access")
+ public Object getChild(Object parent, int index) {
+ if (!(parent instanceof ProjectFileWrapper)) {
+ logger.log(Level.SEVERE, "parent is not a ProjectFileWrapper!");
+ return null;
}
- return fileTreePathTree.getRootNode().getChild(0).findChild((FileTreePath) node);
+ ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) parent;
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ return projectFileWrappers.get(projectFile.getFiles().get(index));
}
/**
- * Builds the tree from the projectâs file entries.
+ * {@inheritDoc}
*/
@SuppressWarnings("synthetic-access")
- private void buildTree() {
- Tree pathTree = new Tree();
- Node pathRootNode = pathTree.getRootNode().addChild(File.separator);
- logger.log(Level.FINEST, "project: " + project);
- buildTree(pathRootNode, project.getBasePathEntries());
- buildTree(pathRootNode, project.getVirtualEntries());
- /* now convert to a tree suitable for the JTree. */
- Node fileTreePathRootNode = fileTreePathTree.getRootNode();
- 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. */
+ public int getChildCount(Object parent) {
+ if (!(parent instanceof ProjectFileWrapper)) {
+ logger.log(Level.SEVERE, "parent is not a ProjectFileWrapper!");
+ return -1;
+ }
+ ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) parent;
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ return projectFile.getFiles().size();
}
/**
- * Traverses the tree of path nodes and converts all paths to
- * {@link FileTreePath} objects, suitable for the JTree.
- *
- * @param completePath
- * The base path of the current root node
- * @param pathRootNode
- * The root node of the path tree
- * @param fileTreePathRootNode
- * The root node of the file tree path tree.
+ * {@inheritDoc}
*/
- private void convertTree(String completePath, Node pathRootNode, Node fileTreePathRootNode) {
- for (Node pathChild: pathRootNode) {
- String currentFilePath = completePath + pathChild.getElement();
- Node newNode = fileTreePathRootNode.addChild(new FileTreePath(currentFilePath));
- convertTree(currentFilePath, pathChild, newNode);
+ @SuppressWarnings("synthetic-access")
+ public int getIndexOfChild(Object parent, Object child) {
+ if (!(parent instanceof ProjectFileWrapper)) {
+ logger.log(Level.SEVERE, "parent is not a ProjectFileWrapper!");
+ return -1;
+ }
+ if (!(child instanceof ProjectFileWrapper)) {
+ logger.log(Level.SEVERE, "child is not a ProjectFileWrapper!");
+ return -1;
}
- fileTreePathRootNode.sortChildren();
+ ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) parent;
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ return projectFile.getFiles().indexOf(((ProjectFileWrapper) child).getProjectFile());
}
/**
- * Builds a tree matching the directory structure of the given entries.
- *
- * @param pathRootNode
- * The root node of the tree
- * @param entries
- * The entries
+ * {@inheritDoc}
*/
- private void buildTree(Node pathRootNode, List entries) {
- for (Entry basePathEntry: entries) {
- String entryName = basePathEntry.getName();
- String[] directories = entryName.split("\\" + File.separator);
- Node currentPathNode = pathRootNode;
- for (String directory: directories) {
- if (!currentPathNode.hasChild(directory)) {
- currentPathNode = currentPathNode.addChild(directory);
- } else {
- currentPathNode = currentPathNode.getChild(directory);
- }
- }
+ @SuppressWarnings("synthetic-access")
+ public boolean isLeaf(Object node) {
+ if (!(node instanceof ProjectFileWrapper)) {
+ logger.log(Level.SEVERE, "node is not a ProjectFileWrapper!");
+ return true;
}
+ ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) node;
+ return projectFileWrapper.getProjectFile().isFile();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void valueForPathChanged(TreePath path, Object newValue) {
+ /* ignore, items will not be modified in tree. */
}
//
@@ -310,98 +899,72 @@ public class FileManager extends JDialog implements I18nable {
//
/**
- * {@inheritDoc}
+ * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
*/
+ @SuppressWarnings("synthetic-access")
public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
- if (propertyChangeEvent.getSource() instanceof Project) {
- if (propertyChangeEvent.getPropertyName().equals(Project.PROPERTY_BASE_PATH_ENTRIES)) {
- buildTree();
+ if (Project.PROPERTY_DEFAULT_FILE.equals(propertyChangeEvent.getPropertyName())) {
+ 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 newProjectFile = pathProjectFiles.get(newCompletePath);
+ ProjectFileWrapper newProjectFileWrapper = projectFileWrappers.get(newProjectFile);
+ System.out.println("newProjectFileWrapper: " + newProjectFileWrapper);
+ fireTreeNodesChanged(newProjectFileWrapper);
+ /* HACK - swing sucks a bit. */
+ fileTree.setShowsRootHandles(false);
}
}
}
- private static class FileTreePath implements Comparable {
-
- private final String filePath;
- private final String fileName;
- private Entry fileEntry;
-
- public FileTreePath(String filePath) {
- this(filePath, null);
- }
+ /**
+ * Wrapper around a {@link ProjectFile} that overwrites
+ * {@link Object#toString()} to return the project fileâs name.
+ *
+ * @author David âBombeâ Roden <bombe@freenetproject.org>
+ */
+ private static class ProjectFileWrapper {
- public FileTreePath(String filePath, String fileName) {
- this.filePath = filePath;
- if (fileName == null) {
- if (filePath.indexOf(File.separatorChar) != -1) {
- this.fileName = filePath.substring(filePath.lastIndexOf(File.separatorChar) + 1);
- } else {
- this.fileName = filePath;
- }
- } else {
- this.fileName = fileName;
- }
- }
+ /** The wrapped project file. */
+ private final ProjectFile projectFile;
- /**
- * Returns the complete file path.
- *
- * @return The file path
- */
- public String getFilePath() {
- return filePath;
- }
+ /** The override name. */
+ private String nameOverride;
/**
- * Returns the file name, i.e. everything after the last
- * {@link File#separatorChar}.
+ * Creates a new wrapper around a project file.
*
- * @return The file name
+ * @param projectFile
+ * The project file to wrap
*/
- public String getFileName() {
- return fileName;
+ public ProjectFileWrapper(ProjectFile projectFile) {
+ this.projectFile = projectFile;
}
/**
- * Returns the file entry associated with this path, if any.
+ * Returns the wrapped project file.
*
- * @return The file entry associated with this path, or
- * null
if this path denotes a directory
+ * @return The wrapped project file
*/
- public Entry getFileEntry() {
- return fileEntry;
+ public ProjectFile getProjectFile() {
+ return projectFile;
}
/**
- * Sets the entry associated with this path.
+ * Sets the name override. If the name override is not null
+ * it will be shown insted of the project fileâs name.
*
- * @param fileEntry
- * The entry
- */
- public void setFileEntry(Entry fileEntry) {
- this.fileEntry = fileEntry;
- }
-
- /**
- * {@inheritDoc}
+ * @param nameOverride
+ * The name override
*/
- @Override
- public boolean equals(Object object) {
- if ((object == null) || !(object instanceof FileTreePath)) {
- return false;
- }
- FileTreePath fileTreePath = (FileTreePath) object;
- return fileTreePath.filePath.equals(filePath);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int hashCode() {
- return filePath.hashCode();
+ void setNameOverride(String nameOverride) {
+ this.nameOverride = nameOverride;
}
/**
@@ -409,18 +972,7 @@ public class FileManager extends JDialog implements I18nable {
*/
@Override
public String toString() {
- return fileName;
- }
-
- //
- // INTERFACE Comparable
- //
-
- /**
- * {@inheritDoc}
- */
- public int compareTo(FileTreePath otherFileTreePath) {
- return filePath.compareTo(otherFileTreePath.filePath);
+ return (nameOverride != null) ? nameOverride : projectFile.getName();
}
}