import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
-import java.beans.PropertyChangeEvent;
-import java.beans.PropertyChangeListener;
import java.io.File;
-import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
-import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
+import javax.swing.tree.DefaultMutableTreeNode;
+import javax.swing.tree.DefaultTreeModel;
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.i18n.gui.I18nAction;
import net.pterodactylus.jsite.i18n.gui.I18nLabel;
-import net.pterodactylus.jsite.project.Entry;
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.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, ActionListener, TreeSelectionListener {
/** 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 TreeModel fileTreeModel;
/** The “close” action. */
private I18nAction closeAction;
/**
* 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 DefaultTreeModel(new DefaultMutableTreeNode(File.separator));
initActions();
initComponents();
pack();
}
//
+ // ACTIONS
+ //
+
+ /**
+ * @see java.awt.Component#setVisible(boolean)
+ */
+ @Override
+ public void setVisible(boolean visible) {
+ if (visible) {
+ initiateFileScan();
+ }
+ super.setVisible(visible);
+ }
+
+ //
// PRIVATE METHODS
//
*/
@SuppressWarnings("synthetic-access")
public void actionPerformed(ActionEvent actionEvent) {
- useCustomMimeTypeAction.setEnabled(insertCheckBox.isSelected());
- mimeTypeComboBox.setEnabled(insertCheckBox.isSelected());
- for (Entry entry: getSelectedEntries()) {
- entry.setInsert(insertCheckBox.isSelected());
- }
+ /* TODO */
}
};
insertAction.setEnabled(false);
*/
@SuppressWarnings("synthetic-access")
public void actionPerformed(ActionEvent actionEvent) {
- mimeTypeComboBox.setEnabled(useCustomMimeTypeCheckBox.isSelected());
- if (!useCustomMimeTypeCheckBox.isSelected()) {
- for (Entry entry: getSelectedEntries()) {
- entry.restoreDefaultContentType();
- }
- }
+ /* TODO */
}
};
useCustomMimeTypeAction.setEnabled(false);
/**
* Creates the main panel with the file tree and the file properties.
- *
+ *
* @return The mail panel
*/
private Component createFileManagerPanel() {
/**
* Creates the button panel.
- *
+ *
* @return The button panel
*/
private Component createButtonPanel() {
return buttonPanel;
}
- /**
- * Returns a list of all selected entries.
- *
- * @return The selected entries
- */
- private List<Entry> getSelectedEntries() {
- TreePath[] selectedPaths = fileTree.getSelectionPaths();
- List<Entry> entries = new ArrayList<Entry>();
- for (TreePath selectedPath: selectedPaths) {
- Entry entry = ((FileTreePath) selectedPath.getLastPathComponent()).getFileEntry();
- if (entry != null) {
- entries.add(entry);
+ private void initiateFileScan() {
+ swingInterface.getThreadPool().execute(new Runnable() {
+ /**
+ * @see java.lang.Runnable#run()
+ */
+ public void run() {
+ String basePath = project.getBasePath();
+ File basePathDirectory = new File(basePath);
}
- }
- return entries;
+ });
}
//
* {@inheritDoc}
*/
public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
- TreePath[] selectedPaths = fileTree.getSelectionPaths();
- if (selectedPaths.length == 1) {
- Entry fileEntry = ((FileTreePath) selectedPaths[0].getLastPathComponent()).getFileEntry();
- if (fileEntry == null) {
- /* some directory node selected. */
- insertAction.setEnabled(false);
- insertCheckBox.setSelected(false);
- useCustomMimeTypeAction.setEnabled(false);
- useCustomMimeTypeCheckBox.setSelected(false);
- mimeTypeComboBox.setEnabled(false);
- } else {
- String contentType = fileEntry.getContentType();
- insertAction.setEnabled(true);
- insertCheckBox.setSelected(fileEntry.isInsert());
- useCustomMimeTypeAction.setEnabled(fileEntry.isInsert());
- useCustomMimeTypeCheckBox.setSelected(!fileEntry.isDefaultContentType());
- mimeTypeComboBox.setEnabled(fileEntry.isDefaultContentType());
- mimeTypeComboBox.setSelectedItem(contentType);
- }
- }
}
//
* {@inheritDoc}
*/
public void actionPerformed(ActionEvent actionEvent) {
- if (actionEvent.getSource() == mimeTypeComboBox) {
- String contentType = (String) mimeTypeComboBox.getSelectedItem();
- for (Entry entry: getSelectedEntries()) {
- entry.setContentType(contentType);
- }
- useCustomMimeTypeCheckBox.setSelected(!getSelectedEntries().get(0).isDefaultContentType());
- }
- }
-
- /**
- * Model for the tree of files.
- *
- * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- */
- private class FileTreeModel implements TreeModel, PropertyChangeListener {
-
- /** Tree model listeners. */
- private final List<TreeModelListener> treeModelListeners = new ArrayList<TreeModelListener>();
-
- /** The tree of files. */
- private final Tree<FileTreePath> fileTreePathTree = new Tree<FileTreePath>();
-
- /**
- * Creates a new file tree model.
- */
- FileTreeModel() {
- buildTree();
- }
-
- //
- // EVENT MANAGEMENT
- //
-
- /**
- * {@inheritDoc}
- */
- public void addTreeModelListener(TreeModelListener treeModelListener) {
- treeModelListeners.add(treeModelListener);
- }
-
- /**
- * {@inheritDoc}
- */
- public void removeTreeModelListener(TreeModelListener treeModelListener) {
- treeModelListeners.remove(treeModelListener);
- }
-
- //
- // ACCESSORS
- //
-
- /**
- * {@inheritDoc}
- */
- @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();
- }
- return null;
- }
-
- /**
- * {@inheritDoc}
- */
- @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();
- }
- 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;
- }
-
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("synthetic-access")
- public Object getRoot() {
- logger.log(Level.FINEST, "getRoot()");
- return fileTreePathTree.getRootNode().getChild(0).getElement();
- }
-
- /**
- * {@inheritDoc}
- */
- @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;
- }
- return true;
- }
-
- //
- // ACTIONS
- //
-
- /**
- * {@inheritDoc}
- */
- public void valueForPathChanged(TreePath path, Object newValue) {
- /* TODO - implement */
- }
-
- //
- // 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
- */
- private Node<FileTreePath> findNode(Object node) {
- if (node == null) {
- return fileTreePathTree.getRootNode().getChild(0);
- }
- return fileTreePathTree.getRootNode().getChild(0).findChild((FileTreePath) node);
- }
-
- /**
- * Builds the tree from the project’s file entries.
- */
- @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. */
- addEntries(fileTreePathRootNode.getChild(0), project.getVirtualEntries());
- addEntries(fileTreePathRootNode.getChild(0), project.getBasePathEntries());
- }
-
- /**
- * Traverses the tree and assigned {@link Entry}s to every file tree
- * path whose name matchtes the name of an Entry.
- *
- * @param fileTreePathRootNode
- * The root node of the tree to walk
- * @param entries
- * The list of entries
- */
- private void addEntries(Node<FileTreePath> fileTreePathRootNode, List<Entry> entries) {
- for (Entry entry: entries) {
- String completeEntryName = File.separatorChar + entry.getName();
- FileTreePath fileTreePath = getFileTreePath(fileTreePathRootNode, completeEntryName);
- if (fileTreePath != null) {
- fileTreePath.setFileEntry(entry);
- }
- }
- }
-
- /**
- * Find the {@link FileTreePath} below the given node that has the given
- * file path.
- *
- * @param fileTreePathNode
- * The node to start searching at
- * @param filePath
- * The path to search
- * @return The file tree path with the matching file path, or
- * <code>null</code> if these is no such file tree path
- */
- private FileTreePath getFileTreePath(Node<FileTreePath> fileTreePathNode, String filePath) {
- for (Node<FileTreePath> child: fileTreePathNode) {
- if (child.getElement().getFilePath().equals(filePath)) {
- return child.getElement();
- }
- FileTreePath fileTreePath = getFileTreePath(child, filePath);
- if (fileTreePath != null) {
- return fileTreePath;
- }
- }
- return null;
- }
-
- /**
- * 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.
- */
- 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);
- }
- fileTreePathRootNode.sortChildren();
- }
-
- /**
- * Builds a tree matching the directory structure of the given entries.
- *
- * @param pathRootNode
- * The root node of the tree
- * @param entries
- * The entries
- */
- 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);
- }
- }
- }
- }
-
- //
- // INTERFACE PropertyChangeListener
- //
-
- /**
- * {@inheritDoc}
- */
- public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
- if (propertyChangeEvent.getSource() instanceof Project) {
- if (propertyChangeEvent.getPropertyName().equals(Project.PROPERTY_BASE_PATH_ENTRIES)) {
- buildTree();
- }
- }
- }
-
- }
-
- /**
- * 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.
- *
- * @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;
- }
- }
-
- /**
- * Returns the complete file path.
- *
- * @return The file path
- */
- public String getFilePath() {
- return filePath;
- }
-
- /**
- * Returns the file name, i.e. everything after the last
- * {@link File#separatorChar}.
- *
- * @return The file name
- */
- public String getFileName() {
- return fileName;
- }
-
- /**
- * 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
- */
- public Entry getFileEntry() {
- return fileEntry;
- }
-
- /**
- * Sets the entry associated with this path.
- *
- * @param fileEntry
- * The entry
- */
- public void setFileEntry(Entry fileEntry) {
- this.fileEntry = fileEntry;
- }
-
- /**
- * {@inheritDoc}
- */
- @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();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String toString() {
- return fileName;
- }
-
- //
- // INTERFACE Comparable
- //
-
- /**
- * {@inheritDoc}
- */
- public int compareTo(FileTreePath otherFileTreePath) {
- return filePath.compareTo(otherFileTreePath.filePath);
- }
-
}
}