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.JFrame;
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.DefaultMutableTreeNode;
47 import javax.swing.tree.DefaultTreeModel;
48 import javax.swing.tree.TreeModel;
50 import net.pterodactylus.jsite.i18n.I18n;
51 import net.pterodactylus.jsite.i18n.I18nable;
52 import net.pterodactylus.jsite.i18n.gui.I18nAction;
53 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
54 import net.pterodactylus.jsite.project.Project;
55 import net.pterodactylus.util.io.MimeTypes;
56 import net.pterodactylus.util.logging.Logging;
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 tree model for the project files. */
76 private final TreeModel fileTreeModel;
78 /** The “close” action. */
79 private I18nAction closeAction;
81 /** The “project files” label. */
82 private I18nLabel projectFilesLabel;
84 /** The tree that shows the files. */
85 private JTree fileTree;
87 /** The “insert” action. */
88 private I18nAction insertAction;
90 /** The “insert” checkbox. */
91 private JCheckBox insertCheckBox;
93 /** The “use custom mime type” action. */
94 private I18nAction useCustomMimeTypeAction;
96 /** The “use custom mime type” checkbox. */
97 private JCheckBox useCustomMimeTypeCheckBox;
99 /** The “mime type” combo box. */
100 private JComboBox mimeTypeComboBox;
103 * Creates a new file manager.
105 * @param swingInterface
106 * The Swing interface
108 * The project whose files to manage
110 public FileManager(SwingInterface swingInterface, Project project) {
111 super(swingInterface.getMainWindow(), I18n.get("fileManager.title", project.getName()), true);
112 logger.log(Level.FINEST, "project: " + project);
113 this.swingInterface = swingInterface;
114 this.project = project;
115 fileTreeModel = new DefaultTreeModel(new DefaultMutableTreeNode(File.separator));
119 SwingUtils.center(this);
127 * @see java.awt.Component#setVisible(boolean)
130 public void setVisible(boolean visible) {
134 super.setVisible(visible);
142 * Initializes all actions.
144 private void initActions() {
145 closeAction = new I18nAction("fileManager.button.close") {
150 public void actionPerformed(ActionEvent e) {
154 insertAction = new I18nAction("fileManager.checkbox.insertFile") {
159 @SuppressWarnings("synthetic-access")
160 public void actionPerformed(ActionEvent actionEvent) {
164 insertAction.setEnabled(false);
165 useCustomMimeTypeAction = new I18nAction("fileManager.checkbox.useCustomMimeType") {
170 @SuppressWarnings("synthetic-access")
171 public void actionPerformed(ActionEvent actionEvent) {
175 useCustomMimeTypeAction.setEnabled(false);
179 * Initializes all components.
181 private void initComponents() {
182 JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
183 contentPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
185 contentPanel.add(createFileManagerPanel(), BorderLayout.CENTER);
186 contentPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
188 setContentPane(contentPanel);
192 * Creates the main panel with the file tree and the file properties.
194 * @return The mail panel
196 private Component createFileManagerPanel() {
197 JPanel fileManagerPanel = new JPanel(new BorderLayout(12, 12));
199 JPanel fileTreePanel = new JPanel(new BorderLayout(12, 12));
200 fileManagerPanel.add(fileTreePanel, BorderLayout.LINE_START);
202 fileTree = new JTree(fileTreeModel);
203 fileTree.setShowsRootHandles(false);
204 fileTree.addTreeSelectionListener(this);
205 fileTreePanel.add(new JScrollPane(fileTree), BorderLayout.CENTER);
207 projectFilesLabel = new I18nLabel("fileManager.label.projectFiles", fileTree);
208 JPanel projectFilesLabelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
209 fileTreePanel.add(projectFilesLabelPanel, BorderLayout.NORTH);
210 projectFilesLabelPanel.add(projectFilesLabel);
212 JPanel propertiesPanel = new JPanel(new GridBagLayout());
213 fileManagerPanel.add(propertiesPanel, BorderLayout.CENTER);
214 propertiesPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
216 insertCheckBox = new JCheckBox(insertAction);
217 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));
219 useCustomMimeTypeCheckBox = new JCheckBox(useCustomMimeTypeAction);
220 List<String> allMimeTypes = MimeTypes.getAllMimeTypes();
221 mimeTypeComboBox = new JComboBox(allMimeTypes.toArray(new String[0]));
222 mimeTypeComboBox.setEnabled(false);
223 mimeTypeComboBox.addActionListener(this);
224 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));
225 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));
227 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));
229 return fileManagerPanel;
233 * Creates the button panel.
235 * @return The button panel
237 private Component createButtonPanel() {
238 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
240 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
241 JButton closeButton = new JButton(closeAction);
242 buttonPanel.add(closeButton);
244 getRootPane().setDefaultButton(closeButton);
248 private void initiateFileScan() {
249 swingInterface.getThreadPool().execute(new Runnable() {
251 * @see java.lang.Runnable#run()
254 String basePath = project.getBasePath();
255 File basePathDirectory = new File(basePath);
261 // INTERFACE I18nable
267 public void updateI18n() {
268 setTitle(I18n.get("fileManager.title", project.getName()));
272 // INTERFACE TreeSelectionListener
278 public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
282 // INTERFACE ActionListener
288 public void actionPerformed(ActionEvent actionEvent) {