import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.BorderFactory;
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
+import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
+import javax.swing.JSeparator;
+import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JTree;
+import javax.swing.SwingConstants;
+import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
+import net.pterodactylus.jsite.core.FileOverride;
+import net.pterodactylus.jsite.core.Project;
+import net.pterodactylus.jsite.core.ProjectFile;
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;
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, TreeSelectionListener, MouseListener {
/** The “file size” text field. */
private JTextField fileSizeTextField;
- /** The “override” label. */
- private I18nLabel fileSettingsLabel;
+ /** The “insert settings” label. */
+ private I18nLabel insertSettingsLabel;
/** The “insert default” label. */
private I18nLabel insertDefaultLabel;
/** The “override insert” checkbox. */
private JCheckBox insertOverrideCheckBox;
+ /** The “content type settings” label. */
+ private I18nLabel contentTypeSettingsLabel;
+
+ /** The “content type default” label. */
+ private I18nLabel contentTypeDefaultLabel;
+
+ /** The “content type default” text field. */
+ private JTextField contentTypeDefaultTextField;
+
+ /** The “override content type default” action. */
+ private I18nAction overrideContentTypeDefaultAction;
+
+ /** The “override content type default” checkbox. */
+ private JCheckBox overrideContentTypeDefaultCheckBox;
+
+ /** The “content type override” action. */
+ private Action contentTypeOverrideAction;
+
+ /** The “content type override” combo box. */
+ private JComboBox contentTypeOverrideComboBox;
+
/** The context menu for the tree. */
private JPopupMenu treeContextMenu;
/**
* Creates a new file manager.
- *
+ *
* @param swingInterface
* The Swing interface
* @param project
project.removeFileOverride(projectFile);
}
}
+ fileTree.setShowsRootHandles(false);
+ fileTree.repaint();
}
};
overrideInsertDefaultAction.setEnabled(false);
/**
* {@inheritDoc}
*/
+ @SuppressWarnings("synthetic-access")
public void actionPerformed(ActionEvent actionEvent) {
boolean insertOverride = insertOverrideCheckBox.isSelected();
List<ProjectFileWrapper> selectedProjectFileWrappers = getSelectedProjectFileWrappers(true);
}
};
insertOverrideAction.setEnabled(false);
+ overrideContentTypeDefaultAction = new I18nAction("fileManager.checkbox.overrideContentTypeDefault") {
+
+ /**
+ * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
+ */
+ @SuppressWarnings("synthetic-access")
+ public void actionPerformed(ActionEvent actionEvent) {
+ boolean contentTypeOverrideEnabled = overrideContentTypeDefaultCheckBox.isSelected();
+ contentTypeOverrideComboBox.setEnabled(contentTypeOverrideEnabled);
+ List<ProjectFileWrapper> selectedProjectFileWrappers = getSelectedProjectFileWrappers(true);
+ ProjectFileWrapper projectFileWrapper = selectedProjectFileWrappers.get(0);
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ FileOverride fileOverride = project.getFileOverride(projectFile);
+ if (contentTypeOverrideEnabled) {
+ if (fileOverride == null) {
+ fileOverride = new FileOverride();
+ project.addFileOverride(projectFile, fileOverride);
+ }
+ String projectFileName = projectFile.getName();
+ String mimeType = MimeTypes.getMimeType(getFileExtension(projectFileName));
+ fileOverride.setContentType(mimeType);
+ fillComboBox(projectFileName);
+ contentTypeOverrideComboBox.setSelectedItem(mimeType);
+ } else {
+ fileOverride.setContentType(null);
+ if (fileOverride.isEmpty()) {
+ project.removeFileOverride(projectFile);
+ }
+ }
+ }
+ };
+ overrideContentTypeDefaultAction.setEnabled(false);
+ contentTypeOverrideAction = new AbstractAction() {
+
+ /**
+ * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
+ */
+ @SuppressWarnings("synthetic-access")
+ public void actionPerformed(ActionEvent actionEvent) {
+ if (!contentTypeOverrideComboBox.isEnabled()) {
+ return;
+ }
+ String selectedContentType = (String) contentTypeOverrideComboBox.getSelectedItem();
+ logger.log(Level.FINEST, "selectedContentType: " + selectedContentType);
+ if ("--".equals(selectedContentType)) {
+ return;
+ }
+ List<ProjectFileWrapper> selectedProjectFileWrappers = getSelectedProjectFileWrappers(true);
+ for (ProjectFileWrapper projectFileWrapper : selectedProjectFileWrappers) {
+ ProjectFile projectFile = projectFileWrapper.getProjectFile();
+ FileOverride fileOverride = project.getFileOverride(projectFile);
+ if (fileOverride == null) {
+ fileOverride = new FileOverride();
+ project.addFileOverride(projectFile, fileOverride);
+ }
+ fileOverride.setContentType(selectedContentType);
+ }
+ }
+ };
+ contentTypeOverrideAction.setEnabled(false);
}
/**
/**
* 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));
+ JSplitPane fileManagerPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
+ fileManagerPanel.setBorder(null);
/* file tree panel */
JPanel fileTreePanel = new JPanel(new BorderLayout(12, 12));
- fileManagerPanel.add(fileTreePanel, BorderLayout.LINE_START);
+ fileTreePanel.setBorder(new EmptyBorder(0, 0, 0, 12));
+ fileManagerPanel.setLeftComponent(fileTreePanel);
fileTree = new JTree(fileTreeModel);
fileTree.setShowsRootHandles(false);
/* the right panel */
JPanel rightPanel = new JPanel(new BorderLayout(12, 12));
- fileManagerPanel.add(rightPanel, BorderLayout.CENTER);
+ rightPanel.setBorder(new EmptyBorder(0, 12, 0, 0));
+ fileManagerPanel.setRightComponent(rightPanel);
/* properties panel */
JPanel propertiesPanel = new JPanel(new GridBagLayout());
rightPanel.add(propertiesPanel, BorderLayout.CENTER);
propertiesPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
- propertiesPanel.setPreferredSize(new Dimension(400, 350));
+ propertiesPanel.setPreferredSize(new Dimension(500, 350));
filePropertiesLabel = new I18nLabel("fileManager.label.fileProperties");
filePropertiesLabel.setFont(filePropertiesLabel.getFont().deriveFont(Font.BOLD));
- propertiesPanel.add(filePropertiesLabel, new GridBagConstraints(0, 0, 4, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
+ 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();
propertiesPanel.add(fileSizeTextField, new GridBagConstraints(1, 3, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
/* file settings. */
- fileSettingsLabel = new I18nLabel("fileManager.label.fileSettings");
- fileSettingsLabel.setFont(fileSettingsLabel.getFont().deriveFont(Font.BOLD));
- propertiesPanel.add(fileSettingsLabel, new GridBagConstraints(0, 4, 4, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(24, 0, 0, 0), 0, 0));
+ insertSettingsLabel = new I18nLabel("fileManager.label.insertSetting");
+ insertSettingsLabel.setFont(insertSettingsLabel.getFont().deriveFont(Font.BOLD));
+ propertiesPanel.add(insertSettingsLabel, new GridBagConstraints(0, 4, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(24, 0, 0, 0), 0, 0));
insertDefaultLabel = new I18nLabel("fileManager.label.insertDefault");
propertiesPanel.add(insertDefaultLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 24, 0, 0), 0, 0));
insertDefaultCheckBox.setEnabled(false);
propertiesPanel.add(insertDefaultCheckBox, new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
overrideInsertDefaultCheckBox = new JCheckBox(overrideInsertDefaultAction);
- propertiesPanel.add(overrideInsertDefaultCheckBox, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
+ propertiesPanel.add(overrideInsertDefaultCheckBox, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 24, 0, 0), 0, 0));
insertOverrideCheckBox = new JCheckBox(insertOverrideAction);
- propertiesPanel.add(insertOverrideCheckBox, new GridBagConstraints(3, 5, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
+ propertiesPanel.add(insertOverrideCheckBox, new GridBagConstraints(1, 6, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
+
+ /* content type settings. */
+ contentTypeSettingsLabel = new I18nLabel("fileManager.label.contentTypeSetting");
+ contentTypeSettingsLabel.setFont(contentTypeSettingsLabel.getFont().deriveFont(Font.BOLD));
+ propertiesPanel.add(contentTypeSettingsLabel, new GridBagConstraints(0, 7, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(24, 0, 0, 0), 0, 0));
+
+ contentTypeDefaultLabel = new I18nLabel("fileManager.label.contentTypeDefault");
+ propertiesPanel.add(contentTypeDefaultLabel, new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 24, 0, 0), 0, 0));
+ contentTypeDefaultTextField = new JTextField();
+ contentTypeDefaultTextField.setEditable(false);
+ propertiesPanel.add(contentTypeDefaultTextField, new GridBagConstraints(1, 8, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
+ overrideContentTypeDefaultCheckBox = new JCheckBox(overrideContentTypeDefaultAction);
+ propertiesPanel.add(overrideContentTypeDefaultCheckBox, new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 24, 0, 0), 0, 0));
+ contentTypeOverrideComboBox = new JComboBox(new DefaultComboBoxModel());
+ contentTypeOverrideComboBox.setRenderer(new MimeTypeComboBoxRenderer());
+ contentTypeOverrideComboBox.addActionListener(contentTypeOverrideAction);
+ contentTypeOverrideComboBox.setEnabled(false);
+ propertiesPanel.add(contentTypeOverrideComboBox, new GridBagConstraints(1, 9, 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, 6, 4, 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, 10, 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));
/**
* Creates the button panel.
- *
+ *
* @return The button panel
*/
private Component createButtonPanel() {
/**
* 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
*/
/**
* 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
/**
* Returns all currently selected {@link ProjectFileWrapper}s.
- *
+ *
* @param filesOnly
* <code>true</code> to return only selected files,
* <code>false</code> to include directories
/**
* Sets the given action’s enabled state to the given enabled state if the
* action’s current enabled state is not the given enabled state.
- *
+ *
* @param action
* The action to set the enabled state on
* @param enabled
/**
* Sets the given button’s selected state to the given selected state if the
* button’s current selected state is not the given selected state.
- *
+ *
* @param button
* The button to set the selected state on
* @param selected
}
}
+ /**
+ * Returns the extension of the given filename. If the file name does not
+ * have an extension, the name of the file (without any path components) is
+ * returned.
+ *
+ * @param fileName
+ * The name of the file
+ * @return The extension of the file
+ */
+ private String getFileExtension(String fileName) {
+ String lastComponent = fileName.substring(fileName.lastIndexOf(File.separator) + 1);
+ return lastComponent.substring(lastComponent.lastIndexOf('.') + 1);
+ }
+
+ /**
+ * Repopulates the content type combo box with all content types, putting
+ * the most probably ones to the front of the list.
+ *
+ * @param fileName
+ * The name of the file
+ */
+ private void fillComboBox(String fileName) {
+ String fileExtension = getFileExtension(fileName);
+ List<String> allMimeTypes = MimeTypes.getAllMimeTypes();
+ List<String> eligibleMimeTypes = MimeTypes.getMimeTypes(fileExtension);
+ for (String mimeType : eligibleMimeTypes) {
+ allMimeTypes.remove(mimeType);
+ allMimeTypes.add(eligibleMimeTypes.indexOf(mimeType), mimeType);
+ }
+ allMimeTypes.add(eligibleMimeTypes.size(), "--");
+ DefaultComboBoxModel contentTypeOverrideComboBoxModel = (DefaultComboBoxModel) contentTypeOverrideComboBox.getModel();
+ contentTypeOverrideComboBoxModel.removeAllElements();
+ for (String mimeType : allMimeTypes) {
+ contentTypeOverrideComboBoxModel.addElement(mimeType);
+ }
+ }
+
//
// INTERFACE I18nable
//
boolean overrideInsertSelected = false;
boolean insertOverrideEnabled = false;
boolean insertOverrideSelected = false;
+ String defaultContentType = "";
+ boolean overrideContentTypeEnabled = false;
+ boolean overrideContentTypeSelected = false;
+ boolean contentTypeOverrideEnabled = false;
+ String contentTypeOverride = "--";
if (selectedProjectFileWrappers.size() == 1) {
ProjectFileWrapper projectFileWrapper = selectedProjectFileWrappers.get(0);
ProjectFile projectFile = projectFileWrapper.getProjectFile();
fileSizeText = String.valueOf(projectFile.getSize());
insertDefaultSelected = !projectFile.isHidden();
overrideInsertEnabled = true;
+ overrideContentTypeEnabled = true;
+ defaultContentType = MimeTypes.getMimeType(getFileExtension(projectFile.getName()));
FileOverride fileOverride = project.getFileOverride(projectFile);
if (fileOverride != null) {
Boolean overrideInsert = fileOverride.isInsert();
overrideInsertSelected = overrideInsert != null;
insertOverrideEnabled = overrideInsertSelected;
insertOverrideSelected = overrideInsertSelected ? overrideInsert : !projectFile.isHidden();
+ String contentType = fileOverride.getContentType();
+ if (contentType != null) {
+ contentTypeOverride = contentType;
+ overrideContentTypeSelected = true;
+ contentTypeOverrideEnabled = true;
+ }
}
+ fillComboBox(projectFile.getName());
}
} else if (selectedProjectFileWrappers.size() > 1) {
/* TODO */
setSelected(overrideInsertDefaultCheckBox, overrideInsertSelected);
setEnabled(insertOverrideAction, insertOverrideEnabled);
setSelected(insertOverrideCheckBox, insertOverrideSelected);
+ contentTypeDefaultTextField.setText(defaultContentType);
+ overrideContentTypeDefaultAction.setEnabled(overrideContentTypeEnabled);
+ overrideContentTypeDefaultCheckBox.setSelected(overrideContentTypeSelected);
+ contentTypeOverrideComboBox.setEnabled(contentTypeOverrideEnabled);
+ contentTypeOverrideComboBox.setSelectedItem(contentTypeOverride);
}
//
/**
* 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 {
/**
* TreeModel that is based on {@link Project#getBaseFile()}.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
private class ProjectFileTreeModel implements TreeModel, PropertyChangeListener {
/**
* Notifies all listeners that a node has changed.
- *
+ *
* @param changedProjectFileWrapper
* The wrapper around the changed project file
*/
/**
* Notifies all listeners that the tree structure has changed
* significantly.
- *
+ *
* @see TreeModelListener#treeStructureChanged(TreeModelEvent)
* @param newRootNode
*/
/**
* Sets the new base project file. This causes the model to reload.
- *
+ *
* @param baseProjectFile
* The new base project file
*/
/**
* 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
/**
* 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 {
/**
* Creates a new wrapper around a project file.
- *
+ *
* @param projectFile
* The project file to wrap
*/
/**
* Returns the wrapped project file.
- *
+ *
* @return The wrapped project file
*/
public ProjectFile getProjectFile() {
/**
* 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
*/
}
+ /**
+ * A cell renderer for combo boxes that converts the string “--” to a
+ * separator.
+ *
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+ */
+ private class MimeTypeComboBoxRenderer extends DefaultListCellRenderer {
+
+ /** The separator component. */
+ private final JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
+
+ /**
+ * Empty constructor.
+ */
+ MimeTypeComboBoxRenderer() {
+ /* do nothing. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
+ if ("--".equals(value)) {
+ return separator;
+ }
+ if (value == null) {
+ return super.getListCellRendererComponent(list, "\u00a0", index, isSelected, cellHasFocus);
+ }
+ return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
+ }
+
+ }
+
}