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.JComboBox;
+import javax.swing.JCheckBoxMenuItem;
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.I18nable;
import net.pterodactylus.jsite.i18n.gui.I18nAction;
import net.pterodactylus.jsite.i18n.gui.I18nLabel;
-import net.pterodactylus.jsite.project.Entry;
+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.util.io.MimeTypes;
+import net.pterodactylus.jsite.project.ProjectFile;
import net.pterodactylus.util.logging.Logging;
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, TreeSelectionListener {
+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 “insert” action. */
- private I18nAction insertAction;
-
- /** The “insert” checkbox. */
- private JCheckBox insertCheckBox;
-
- /** The “mime type” label. */
- private I18nLabel mimeTypeLabel;
+ /** 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 “mime type” combo box. */
- private JComboBox mimeTypeComboBox;
+ /** 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 context menu for the tree. */
+ private JPopupMenu treeContextMenu;
+
+ /** The “insert” checkbox. */
+ private JCheckBoxMenuItem insertCheckBoxMenuItem;
/**
* 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();
pack();
}
//
+ // ACTIONS
+ //
+
+ /**
+ * @see java.awt.Component#setVisible(boolean)
+ */
+ @Override
+ public void setVisible(boolean visible) {
+ if (visible) {
+ initiateFileScan();
+ }
+ super.setVisible(visible);
+ }
+
+ //
// PRIVATE METHODS
//
setVisible(false);
}
};
- insertAction = new I18nAction("fileManager.checkbox.insertFile") {
+ rescanAction = new I18nAction("fileManager.button.rescan") {
+
/**
* {@inheritDoc}
*/
+ @SuppressWarnings("synthetic-access")
public void actionPerformed(ActionEvent actionEvent) {
- /* TODO - implements. */
+ initiateFileScan();
+ }
+ };
+ setDefaultFileAction = new I18nAction("fileManager.menu.item.setDefaultFile") {
+
+ /**
+ * {@inheritDoc}
+ */
+ 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 */
}
};
}
* 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));
/**
* 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);
- fileTreePanel.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(250, 400));
+
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());
- fileManagerPanel.add(propertiesPanel, BorderLayout.CENTER);
+ rightPanel.add(propertiesPanel, BorderLayout.CENTER);
propertiesPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
- insertCheckBox = new JCheckBox(insertAction);
- propertiesPanel.add(insertCheckBox, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
+ 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));
- List<String> allMimeTypes = MimeTypes.getAllMimeTypes();
- allMimeTypes.add(0, "auto-detect");
- mimeTypeComboBox = new JComboBox(allMimeTypes.toArray(new String[0]));
- mimeTypeLabel = new I18nLabel("fileManager.label.mimeType", mimeTypeComboBox);
- propertiesPanel.add(mimeTypeLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 0, 0, 0), 0, 0));
- propertiesPanel.add(mimeTypeComboBox, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 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));
- propertiesPanel.add(new JPanel(), new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 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));
+
+ /* 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));
+
+ /* 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;
}
/**
* Creates the button panel.
- *
+ *
* @return The button panel
*/
private Component createButtonPanel() {
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 clickedPath = fileTree.getPathForLocation(eventLocation.x, eventLocation.y);
+ if (clickedPath == null) {
+ return;
+ }
+ fileTree.setSelectionPath(clickedPath);
+ ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) clickedPath.getLastPathComponent();
+ insertCheckBoxMenuItem.setSelected(!isHidden(projectFileWrapper));
+ treeContextMenu.show(fileTree, eventLocation.x, eventLocation.y);
+ }
+
+ /**
+ * 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
+ * inserted, <code>false</code> otherwise
+ */
+ private boolean isHidden(ProjectFileWrapper projectFileWrapper) {
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ FileOverride fileOverride = project.getFileOverrides().get(projectFile.getCompletePath());
+ return ((fileOverride == null) && projectFile.isHidden()) || ((fileOverride != null) && (fileOverride.isInsert()));
+ }
+
//
// INTERFACE 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) {
- /* TODO - implement */
+ TreePath[] selectedPaths = fileTree.getSelectionPaths();
+ 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()) {
+ fileNameTextField.setText(projectFile.getName());
+ fileSizeTextField.setText(String.valueOf(projectFile.getSize()));
+ }
+ }
}
+ //
+ // INTERFACE ActionListener
+ //
+
/**
- * Model for the tree of files.
- *
+ * {@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. */
+ }
+
+ /**
+ * {@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<TreeModelListener> treeModelListeners = new ArrayList<TreeModelListener>();
+ private final List<TreeModelListener> treeModelListeners = Collections.synchronizedList(new ArrayList<TreeModelListener>());
+
+ /** The base project file. */
+ private ProjectFile baseProjectFile;
- /** The tree of files. */
- private final Tree<FileTreePath> fileTreePathTree = new Tree<FileTreePath>();
+ /** Maps path names to project files. */
+ private final Map<String, ProjectFile> pathProjectFiles = Collections.synchronizedMap(new HashMap<String, ProjectFile>());
+
+ /** Maps project files to wrappers. */
+ private final Map<ProjectFile, ProjectFileWrapper> projectFileWrappers = Collections.synchronizedMap(new HashMap<ProjectFile, ProjectFileWrapper>());
/**
- * Creates a new file tree model.
+ * Empty constructor.
*/
- FileTreeModel() {
- buildTree();
+ ProjectFileTreeModel() {
+ /* do nothing. */
}
//
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<FileTreePath> 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<ProjectFileWrapper> parentProjectFileWrappers = new ArrayList<ProjectFileWrapper>();
+ 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<FileTreePath> 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<FileTreePath> 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<FileTreePath> 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
- * <code>null</code>
- *
- * @param node
- * The element whose node to return
- * @return The node, or <code>null</code> if no node could be found
+ * {@inheritDoc}
*/
- private Node<FileTreePath> 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<String> pathTree = new Tree<String>();
- Node<String> 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<FileTreePath> 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<String> pathRootNode, Node<FileTreePath> fileTreePathRootNode) {
- for (Node<String> pathChild: pathRootNode) {
- String currentFilePath = completePath + pathChild.getElement();
- Node<FileTreePath> 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;
}
- fileTreePathRootNode.sortChildren();
+ if (!(child instanceof ProjectFileWrapper)) {
+ logger.log(Level.SEVERE, "child is not a ProjectFileWrapper!");
+ return -1;
+ }
+ 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<String> pathRootNode, List<Entry> entries) {
- for (Entry basePathEntry: entries) {
- String entryName = basePathEntry.getName();
- String[] directories = entryName.split("\\" + File.separator);
- Node<String> 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;
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ return projectFile.getFiles().isEmpty();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void valueForPathChanged(TreePath path, Object newValue) {
+ /* ignore, items will not be modified in tree. */
}
//
//
/**
- * {@inheritDoc}
+ * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
*/
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())) {
+ String oldCompletePath = (String) propertyChangeEvent.getOldValue();
+ 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);
}
}
}
/**
- * Container that is used to back the {@link FileTreeModel}. Each
- * FileTreePath contains a complete path name, a filename, and the
- * associated {@link Entry}, if any.
- *
+ * 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 FileTreePath implements Comparable<FileTreePath> {
-
- /** The complete file path. */
- private final String filePath;
-
- /** The file name. */
- private final String fileName;
-
- /** The file entry, if any. */
- private Entry fileEntry;
-
- /**
- * Creates a new file tree path with an auto-detected file name. The
- * file name is everything after the last separator in the complete
- * path, or the complete path itself if it does not contain any
- * separators.
- *
- * @param filePath
- * The complete file path
- */
- public FileTreePath(String filePath) {
- this(filePath, null);
- }
-
- /**
- * Creates a new file tree path with the given file path and file name.
- *
- * @param filePath
- * The complete file path
- * @param fileName
- * The file name
- */
- 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;
- }
- }
+ private static class ProjectFileWrapper {
- /**
- * Returns the complete file path.
- *
- * @return The file path
- */
- public String getFilePath() {
- return filePath;
- }
+ /** The wrapped project file. */
+ private final ProjectFile projectFile;
- /**
- * Returns the file name, i.e. everything after the last
- * {@link File#separatorChar}.
- *
- * @return The file name
- */
- public String getFileName() {
- return fileName;
- }
+ /** The override name. */
+ private String nameOverride;
/**
- * Returns the file entry associated with this path, if any.
- *
- * @return The file entry associated with this path, or
- * <code>null</code> if this path denotes a directory
+ * Creates a new wrapper around a project file.
+ *
+ * @param projectFile
+ * The project file to wrap
*/
- public Entry getFileEntry() {
- return fileEntry;
+ public ProjectFileWrapper(ProjectFile projectFile) {
+ this.projectFile = projectFile;
}
/**
- * Sets the entry associated with this path.
- *
- * @param fileEntry
- * The entry
+ * Returns the wrapped project file.
+ *
+ * @return The wrapped project file
*/
- public void setFileEntry(Entry fileEntry) {
- this.fileEntry = fileEntry;
+ public ProjectFile getProjectFile() {
+ return projectFile;
}
/**
- * {@inheritDoc}
+ * 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
*/
- @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;
}
/**
*/
@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();
}
}