X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fgui%2FFileManager.java;h=a9713429f9538859de250c7a34ae3dfb30ca0db2;hb=060d411e74835c5c2cdf39efde67f23313208e06;hp=6bd83fc1f6c97c662eec38f11587e918796de1ed;hpb=3964c896681793cb27a0c02c6482c5d7704d5d0c;p=jSite2.git
diff --git a/src/net/pterodactylus/jsite/gui/FileManager.java b/src/net/pterodactylus/jsite/gui/FileManager.java
index 6bd83fc..a971342 100644
--- a/src/net/pterodactylus/jsite/gui/FileManager.java
+++ b/src/net/pterodactylus/jsite/gui/FileManager.java
@@ -35,6 +35,7 @@ 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;
@@ -45,7 +46,7 @@ import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
-import javax.swing.JCheckBoxMenuItem;
+import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
@@ -66,6 +67,7 @@ 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.i18n.gui.I18nMenu;
import net.pterodactylus.jsite.project.FileOverride;
import net.pterodactylus.jsite.project.Project;
import net.pterodactylus.jsite.project.ProjectFile;
@@ -74,7 +76,7 @@ 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, MouseListener {
@@ -136,15 +138,30 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/** 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 âinsertâ checkbox. */
- private JCheckBoxMenuItem insertCheckBoxMenuItem;
+ /** 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 swingInterface
* The Swing interface
* @param project
@@ -192,7 +209,19 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* {@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);
}
};
@@ -211,6 +240,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* {@inheritDoc}
*/
+ @SuppressWarnings("synthetic-access")
public void actionPerformed(ActionEvent actionEvent) {
TreePath selectedPath = fileTree.getSelectionPath();
if (selectedPath == null) {
@@ -241,6 +271,35 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/* 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();
+ }
+ };
}
/**
@@ -249,8 +308,12 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
private void initComponents() {
treeContextMenu = new JPopupMenu();
treeContextMenu.add(setDefaultFileAction);
- insertCheckBoxMenuItem = new JCheckBoxMenuItem(insertAction);
- treeContextMenu.add(insertCheckBoxMenuItem);
+
+ 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));
@@ -263,7 +326,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* Creates the main panel with the file tree and the file properties.
- *
+ *
* @return The mail panel
*/
private Component createFileManagerPanel() {
@@ -279,7 +342,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
fileTree.addMouseListener(this);
fileTree.setCellRenderer(fileCellRenderer);
fileTreePanel.add(fileScrollPane = new JScrollPane(fileTree), BorderLayout.CENTER);
- fileScrollPane.setPreferredSize(new Dimension(250, 400));
+ fileScrollPane.setPreferredSize(new Dimension(200, 350));
projectFilesLabel = new I18nLabel("fileManager.label.projectFiles", fileTree);
JPanel projectFilesLabelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
@@ -294,6 +357,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
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));
@@ -317,8 +381,16 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
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, 4, 2, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
+ 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));
@@ -333,7 +405,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* Creates the button panel.
- *
+ *
* @return The button panel
*/
private Component createButtonPanel() {
@@ -375,7 +447,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* 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
*/
@@ -384,20 +456,32 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
return;
}
Point eventLocation = mouseEvent.getPoint();
- TreePath clickedPath = fileTree.getPathForLocation(eventLocation.x, eventLocation.y);
- if (clickedPath == null) {
- return;
+ 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);
}
- 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
* projectFileWrapper
is hidden.
- *
+ *
* @param projectFileWrapper
* The wrapped project file
* @return true
if the file is hidden and should not be
@@ -409,6 +493,44 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
return ((fileOverride == null) && projectFile.isHidden()) || ((fileOverride != null) && (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 */
+ }
+
//
// INTERFACE I18nable
//
@@ -432,6 +554,9 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
*/
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)) {
@@ -441,9 +566,18 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
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 */
}
}
@@ -500,7 +634,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* 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 {
@@ -562,7 +696,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* TreeModel that is based on {@link Project#getBaseFile()}.
- *
+ *
* @author David âBombeâ Roden <bombe@freenetproject.org>
*/
private class ProjectFileTreeModel implements TreeModel, PropertyChangeListener {
@@ -606,7 +740,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* Notifies all listeners that a node has changed.
- *
+ *
* @param changedProjectFileWrapper
* The wrapper around the changed project file
*/
@@ -620,7 +754,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
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) {
+ for (TreeModelListener treeModelListener: treeModelListeners) {
treeModelListener.treeNodesChanged(treeModelEvent);
}
}
@@ -628,12 +762,12 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* 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) {
+ for (TreeModelListener treeModelListener: treeModelListeners) {
treeModelListener.treeStructureChanged(new TreeModelEvent(this, new Object[] { newRootNode }));
}
}
@@ -644,7 +778,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* Sets the new base project file. This causes the model to reload.
- *
+ *
* @param baseProjectFile
* The new base project file
*/
@@ -665,7 +799,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* 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
@@ -673,7 +807,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
private void createWrappers(ProjectFile projectFile) {
projectFileWrappers.put(projectFile, new ProjectFileWrapper(projectFile));
pathProjectFiles.put(projectFile.getCompletePath(), projectFile);
- for (ProjectFile projectFileChild : projectFile.getFiles()) {
+ for (ProjectFile projectFileChild: projectFile.getFiles()) {
if (projectFileChild.isDirectory()) {
createWrappers(projectFileChild);
}
@@ -749,8 +883,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
return true;
}
ProjectFileWrapper projectFileWrapper = (ProjectFileWrapper) node;
- ProjectFile projectFile = projectFileWrapper.getProjectFile();
- return projectFile.getFiles().isEmpty();
+ return projectFileWrapper.getProjectFile().isFile();
}
/**
@@ -767,17 +900,20 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
*/
+ @SuppressWarnings("synthetic-access")
public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
if (Project.PROPERTY_DEFAULT_FILE.equals(propertyChangeEvent.getPropertyName())) {
- String oldCompletePath = (String) propertyChangeEvent.getOldValue();
+ 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 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);
@@ -789,7 +925,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* 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 {
@@ -802,7 +938,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* Creates a new wrapper around a project file.
- *
+ *
* @param projectFile
* The project file to wrap
*/
@@ -812,7 +948,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* Returns the wrapped project file.
- *
+ *
* @return The wrapped project file
*/
public ProjectFile getProjectFile() {
@@ -822,7 +958,7 @@ public class FileManager extends JDialog implements I18nable, ActionListener, Tr
/**
* Sets the name override. If the name override is not null
* it will be shown insted of the project fileâs name.
- *
+ *
* @param nameOverride
* The name override
*/