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.io.File;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+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.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.DefaultTreeModel;
+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.i18n.gui.I18nAction;
import net.pterodactylus.jsite.i18n.gui.I18nLabel;
+import net.pterodactylus.jsite.project.FileOverride;
import net.pterodactylus.jsite.project.Project;
-import net.pterodactylus.util.io.MimeTypes;
+import net.pterodactylus.jsite.project.ProjectFile;
import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.swing.SortableTreeNode;
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 {
+public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener, MouseListener {
/** Logger. */
private static final Logger logger = Logging.getLogger(FileManager.class.getName());
/** The project whose files to manage. */
private final Project project;
- /** The root of the file tree. */
- private final SortableTreeNode fileTreeRoot;
-
/** The tree model for the project files. */
- private final TreeModel 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 scroll pane that holds the file tree. */
+ private JScrollPane fileScrollPane;
- /** The “insert” checkbox. */
- private JCheckBox insertCheckBox;
+ /** The “file properties” label. */
+ private I18nLabel filePropertiesLabel;
+
+ /** The “file path” label. */
+ private I18nLabel filePathLabel;
- /** The “use custom mime type” action. */
- private I18nAction useCustomMimeTypeAction;
+ /** The “file path” textfield. */
+ private JTextField filePathTextField;
- /** The “use custom mime type” checkbox. */
- private JCheckBox useCustomMimeTypeCheckBox;
+ /** The “file name” label. */
+ private I18nLabel fileNameLabel;
- /** The “mime type” combo box. */
- private JComboBox mimeTypeComboBox;
+ /** 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 swingInterface
* The Swing interface
* @param project
logger.log(Level.FINEST, "project: " + project);
this.swingInterface = swingInterface;
this.project = project;
- fileTreeRoot = new SortableTreeNode(project.getName());
- fileTreeModel = new DefaultTreeModel(fileTreeRoot);
+ fileTreeModel = new ProjectFileTreeModel();
+ project.addPropertyChangeListener(fileTreeModel);
+ fileCellRenderer = new FileCellRenderer();
initActions();
initComponents();
pack();
initiateFileScan();
}
};
- insertAction = new I18nAction("fileManager.checkbox.insertFile") {
+ setDefaultFileAction = new I18nAction("fileManager.menu.item.setDefaultFile") {
/**
* {@inheritDoc}
*/
- @SuppressWarnings("synthetic-access")
public void actionPerformed(ActionEvent actionEvent) {
- /* TODO */
+ 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.setEnabled(false);
- useCustomMimeTypeAction = new I18nAction("fileManager.checkbox.useCustomMimeType") {
+ insertAction = new I18nAction("fileManager.menu.item.insert") {
/**
* {@inheritDoc}
*/
- @SuppressWarnings("synthetic-access")
- public void actionPerformed(ActionEvent actionEvent) {
+ public void actionPerformed(ActionEvent e) {
/* TODO */
}
};
- useCustomMimeTypeAction.setEnabled(false);
}
/**
* 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() {
fileTree = new JTree(fileTreeModel);
fileTree.setShowsRootHandles(false);
fileTree.addTreeSelectionListener(this);
- fileTreePanel.add(new JScrollPane(fileTree), BorderLayout.CENTER);
+ 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());
- fileManagerPanel.add(propertiesPanel, BorderLayout.CENTER);
+ 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));
- 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));
+ 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));
- useCustomMimeTypeCheckBox = new JCheckBox(useCustomMimeTypeAction);
- List<String> allMimeTypes = MimeTypes.getAllMimeTypes();
- mimeTypeComboBox = new JComboBox(allMimeTypes.toArray(new String[0]));
- mimeTypeComboBox.setEnabled(false);
- mimeTypeComboBox.addActionListener(this);
- propertiesPanel.add(useCustomMimeTypeCheckBox, 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));
- actionButtonPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(-12, -12, -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() {
*/
@SuppressWarnings("synthetic-access")
public void run() {
- String basePath = project.getBasePath();
- File basePathDirectory = new File(basePath);
- if (!basePathDirectory.exists() || !basePathDirectory.isDirectory()) {
- /* TODO - i18n */
- JOptionPane.showMessageDialog(FileManager.this, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
- return;
- }
- synchronized (fileTreeRoot) {
- scanDirectory(fileTreeRoot, basePathDirectory);
- }
- fileTree.repaint();
- }
-
- private void scanDirectory(SortableTreeNode rootNode, File directory) {
- System.out.println("scanning " + directory.getAbsolutePath());
- for (File file: directory.listFiles()) {
- SortableTreeNode fileNode = new SortableTreeNode(file.getName());
- rootNode.add(fileNode);
- if (file.isDirectory()) {
- scanDirectory(fileNode, file);
- }
+ fileTree.setEnabled(false);
+ rescanAction.setEnabled(false);
+ ProjectFile baseProjectFile = project.getBaseFile();
+ if (baseProjectFile != null) {
+ fileTreeModel.setBaseProjectFile(baseProjectFile);
}
- rootNode.sort();
+ // 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();
}
//
* {@inheritDoc}
*/
public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
+ 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()));
+ }
+ }
}
//
* {@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 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 = Collections.synchronizedList(new ArrayList<TreeModelListener>());
+
+ /** The base project file. */
+ private ProjectFile baseProjectFile;
+
+ /** 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>());
+
+ /**
+ * Empty constructor.
+ */
+ ProjectFileTreeModel() {
+ /* do nothing. */
+ }
+
+ //
+ // EVENT MANAGEMENT
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ public void addTreeModelListener(TreeModelListener treeModelListener) {
+ treeModelListeners.add(treeModelListener);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void removeTreeModelListener(TreeModelListener treeModelListener) {
+ treeModelListeners.remove(treeModelListener);
+ }
+
+ /**
+ * Notifies all listeners that a node has changed.
+ *
+ * @param changedProjectFileWrapper
+ * The wrapper around the changed project file
+ */
+ 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);
+ }
+ }
+
+ /**
+ * Notifies all listeners that the tree structure has changed
+ * significantly.
+ *
+ * @see TreeModelListener#treeStructureChanged(TreeModelEvent)
+ * @param newRootNode
+ */
+ protected void fireTreeStructureChanged(ProjectFileWrapper newRootNode) {
+ for (TreeModelListener treeModelListener : treeModelListeners) {
+ treeModelListener.treeStructureChanged(new TreeModelEvent(this, new Object[] { newRootNode }));
+ }
+ }
+
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * Sets the new base project file. This causes the model to reload.
+ *
+ * @param baseProjectFile
+ * The new base project file
+ */
+ @SuppressWarnings("synthetic-access")
+ 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
+ //
+
+ /**
+ * 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
+ */
+ 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);
+ }
+ }
+
+ //
+ // INTERFACE TreeModel
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object getRoot() {
+ return projectFileWrappers.get(baseProjectFile);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @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;
+ }
+ ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) parent;
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ return projectFileWrappers.get(projectFile.getFiles().get(index));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("synthetic-access")
+ 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();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @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;
+ }
+ ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) parent;
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ return projectFile.getFiles().indexOf(((ProjectFileWrapper) child).getProjectFile());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @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. */
+ }
+
+ //
+ // INTERFACE PropertyChangeListener
+ //
+
+ /**
+ * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
+ */
+ public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
+ 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);
+ }
+ }
+
+ }
+
+ /**
+ * 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 {
+
+ /** The wrapped project file. */
+ private final ProjectFile projectFile;
+
+ /** The override name. */
+ private String nameOverride;
+
+ /**
+ * Creates a new wrapper around a project file.
+ *
+ * @param projectFile
+ * The project file to wrap
+ */
+ public ProjectFileWrapper(ProjectFile projectFile) {
+ this.projectFile = projectFile;
+ }
+
+ /**
+ * Returns the wrapped project file.
+ *
+ * @return The wrapped project file
+ */
+ public ProjectFile getProjectFile() {
+ return projectFile;
+ }
+
+ /**
+ * 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 (nameOverride != null) ? nameOverride : projectFile.getName();
+ }
+
}
}