2 * jSite2 - FileManager.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.jsite.gui;
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.awt.FlowLayout;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
31 import java.util.List;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
35 import javax.swing.BorderFactory;
36 import javax.swing.JButton;
37 import javax.swing.JCheckBox;
38 import javax.swing.JComboBox;
39 import javax.swing.JDialog;
40 import javax.swing.JOptionPane;
41 import javax.swing.JPanel;
42 import javax.swing.JScrollPane;
43 import javax.swing.JTree;
44 import javax.swing.event.TreeSelectionEvent;
45 import javax.swing.event.TreeSelectionListener;
46 import javax.swing.tree.DefaultTreeModel;
47 import javax.swing.tree.TreeModel;
49 import net.pterodactylus.jsite.i18n.I18n;
50 import net.pterodactylus.jsite.i18n.I18nable;
51 import net.pterodactylus.jsite.i18n.gui.I18nAction;
52 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
53 import net.pterodactylus.jsite.project.Project;
54 import net.pterodactylus.util.io.MimeTypes;
55 import net.pterodactylus.util.logging.Logging;
56 import net.pterodactylus.util.swing.SortableTreeNode;
57 import net.pterodactylus.util.swing.SwingUtils;
60 * Manages physical and virtual files in a project.
62 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
64 public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener {
67 private static final Logger logger = Logging.getLogger(FileManager.class.getName());
69 /** The Swing interface. */
70 private final SwingInterface swingInterface;
72 /** The project whose files to manage. */
73 private final Project project;
75 /** The root of the file tree. */
76 private final SortableTreeNode fileTreeRoot;
78 /** The tree model for the project files. */
79 private final TreeModel fileTreeModel;
81 /** The “rescan” action. */
82 private I18nAction rescanAction;
84 /** The “close” action. */
85 private I18nAction closeAction;
87 /** The “project files” label. */
88 private I18nLabel projectFilesLabel;
90 /** The tree that shows the files. */
91 private JTree fileTree;
93 /** The “insert” action. */
94 private I18nAction insertAction;
96 /** The “insert” checkbox. */
97 private JCheckBox insertCheckBox;
99 /** The “use custom mime type” action. */
100 private I18nAction useCustomMimeTypeAction;
102 /** The “use custom mime type” checkbox. */
103 private JCheckBox useCustomMimeTypeCheckBox;
105 /** The “mime type” combo box. */
106 private JComboBox mimeTypeComboBox;
109 * Creates a new file manager.
111 * @param swingInterface
112 * The Swing interface
114 * The project whose files to manage
116 public FileManager(SwingInterface swingInterface, Project project) {
117 super(swingInterface.getMainWindow(), I18n.get("fileManager.title", project.getName()), true);
118 logger.log(Level.FINEST, "project: " + project);
119 this.swingInterface = swingInterface;
120 this.project = project;
121 fileTreeRoot = new SortableTreeNode(project.getName());
122 fileTreeModel = new DefaultTreeModel(fileTreeRoot);
126 SwingUtils.center(this);
134 * @see java.awt.Component#setVisible(boolean)
137 public void setVisible(boolean visible) {
141 super.setVisible(visible);
149 * Initializes all actions.
151 private void initActions() {
152 closeAction = new I18nAction("fileManager.button.close") {
157 public void actionPerformed(ActionEvent e) {
161 rescanAction = new I18nAction("fileManager.button.rescan") {
166 @SuppressWarnings("synthetic-access")
167 public void actionPerformed(ActionEvent actionEvent) {
171 insertAction = new I18nAction("fileManager.checkbox.insertFile") {
176 @SuppressWarnings("synthetic-access")
177 public void actionPerformed(ActionEvent actionEvent) {
181 insertAction.setEnabled(false);
182 useCustomMimeTypeAction = new I18nAction("fileManager.checkbox.useCustomMimeType") {
187 @SuppressWarnings("synthetic-access")
188 public void actionPerformed(ActionEvent actionEvent) {
192 useCustomMimeTypeAction.setEnabled(false);
196 * Initializes all components.
198 private void initComponents() {
199 JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
200 contentPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
202 contentPanel.add(createFileManagerPanel(), BorderLayout.CENTER);
203 contentPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
205 setContentPane(contentPanel);
209 * Creates the main panel with the file tree and the file properties.
211 * @return The mail panel
213 private Component createFileManagerPanel() {
214 JPanel fileManagerPanel = new JPanel(new BorderLayout(12, 12));
216 /* file tree panel */
217 JPanel fileTreePanel = new JPanel(new BorderLayout(12, 12));
218 fileManagerPanel.add(fileTreePanel, BorderLayout.LINE_START);
220 fileTree = new JTree(fileTreeModel);
221 fileTree.setShowsRootHandles(false);
222 fileTree.addTreeSelectionListener(this);
223 fileTreePanel.add(new JScrollPane(fileTree), BorderLayout.CENTER);
225 projectFilesLabel = new I18nLabel("fileManager.label.projectFiles", fileTree);
226 JPanel projectFilesLabelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
227 fileTreePanel.add(projectFilesLabelPanel, BorderLayout.NORTH);
228 projectFilesLabelPanel.add(projectFilesLabel);
230 /* properties panel */
231 JPanel propertiesPanel = new JPanel(new GridBagLayout());
232 fileManagerPanel.add(propertiesPanel, BorderLayout.CENTER);
233 propertiesPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
235 insertCheckBox = new JCheckBox(insertAction);
236 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));
238 useCustomMimeTypeCheckBox = new JCheckBox(useCustomMimeTypeAction);
239 List<String> allMimeTypes = MimeTypes.getAllMimeTypes();
240 mimeTypeComboBox = new JComboBox(allMimeTypes.toArray(new String[0]));
241 mimeTypeComboBox.setEnabled(false);
242 mimeTypeComboBox.addActionListener(this);
243 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));
244 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));
246 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));
248 /* action button panel */
249 JPanel actionButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
250 actionButtonPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(-12, -12, -12, -12)));
252 return fileManagerPanel;
256 * Creates the button panel.
258 * @return The button panel
260 private Component createButtonPanel() {
261 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
263 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
264 JButton closeButton = new JButton(closeAction);
265 buttonPanel.add(closeButton);
267 getRootPane().setDefaultButton(closeButton);
271 private void initiateFileScan() {
272 swingInterface.getThreadPool().execute(new Runnable() {
275 * @see java.lang.Runnable#run()
277 @SuppressWarnings("synthetic-access")
279 String basePath = project.getBasePath();
280 File basePathDirectory = new File(basePath);
281 if (!basePathDirectory.exists() || !basePathDirectory.isDirectory()) {
283 JOptionPane.showMessageDialog(FileManager.this, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
286 synchronized (fileTreeRoot) {
287 scanDirectory(fileTreeRoot, basePathDirectory);
292 private void scanDirectory(SortableTreeNode rootNode, File directory) {
293 System.out.println("scanning " + directory.getAbsolutePath());
294 for (File file: directory.listFiles()) {
295 SortableTreeNode fileNode = new SortableTreeNode(file.getName());
296 rootNode.add(fileNode);
297 if (file.isDirectory()) {
298 scanDirectory(fileNode, file);
308 // INTERFACE I18nable
314 public void updateI18n() {
315 setTitle(I18n.get("fileManager.title", project.getName()));
319 // INTERFACE TreeSelectionListener
325 public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
329 // INTERFACE ActionListener
335 public void actionPerformed(ActionEvent actionEvent) {