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.Color;
24 import java.awt.Component;
25 import java.awt.Dimension;
26 import java.awt.FlowLayout;
28 import java.awt.GridBagConstraints;
29 import java.awt.GridBagLayout;
30 import java.awt.Insets;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
39 import javax.swing.BorderFactory;
40 import javax.swing.JButton;
41 import javax.swing.JDialog;
42 import javax.swing.JLabel;
43 import javax.swing.JOptionPane;
44 import javax.swing.JPanel;
45 import javax.swing.JScrollPane;
46 import javax.swing.JTextField;
47 import javax.swing.JTree;
48 import javax.swing.event.TreeSelectionEvent;
49 import javax.swing.event.TreeSelectionListener;
50 import javax.swing.tree.DefaultTreeCellRenderer;
51 import javax.swing.tree.DefaultTreeModel;
52 import javax.swing.tree.TreeNode;
54 import net.pterodactylus.jsite.i18n.I18n;
55 import net.pterodactylus.jsite.i18n.I18nable;
56 import net.pterodactylus.jsite.i18n.gui.I18nAction;
57 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
58 import net.pterodactylus.jsite.project.Project;
59 import net.pterodactylus.util.logging.Logging;
60 import net.pterodactylus.util.swing.SortableTreeNode;
61 import net.pterodactylus.util.swing.SwingUtils;
64 * Manages physical and virtual files in a project.
66 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
68 public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener {
71 private static final Logger logger = Logging.getLogger(FileManager.class.getName());
73 /** The Swing interface. */
74 private final SwingInterface swingInterface;
76 /** The project whose files to manage. */
77 private final Project project;
79 /** The root of the file tree. */
80 private final SortableTreeNode fileTreeRoot;
82 /** The tree model for the project files. */
83 private final DefaultTreeModel fileTreeModel;
85 /** Files that are hidden as per {@link File#isHidden()}. */
86 private final List<String> hiddenFiles = new ArrayList<String>();
88 /** The tree cell renderer. */
89 private final FileCellRenderer fileCellRenderer;
91 /** The “rescan” action. */
92 private I18nAction rescanAction;
94 /** The “close” action. */
95 private I18nAction closeAction;
97 /** The “project files” label. */
98 private I18nLabel projectFilesLabel;
100 /** The tree that shows the files. */
101 private JTree fileTree;
103 /** The scroll pane that holds the file tree. */
104 private JScrollPane fileScrollPane;
106 /** The “file properties” label. */
107 private I18nLabel filePropertiesLabel;
109 /** The “file path” label. */
110 private I18nLabel filePathLabel;
112 /** The “file path” textfield. */
113 private JTextField filePathTextField;
115 /** The “file name” label. */
116 private I18nLabel fileNameLabel;
118 /** The “file name” textfield. */
119 private JTextField fileNameTextField;
122 * Creates a new file manager.
124 * @param swingInterface
125 * The Swing interface
127 * The project whose files to manage
129 public FileManager(SwingInterface swingInterface, Project project) {
130 super(swingInterface.getMainWindow(), I18n.get("fileManager.title", project.getName()), true);
131 logger.log(Level.FINEST, "project: " + project);
132 this.swingInterface = swingInterface;
133 this.project = project;
134 fileTreeRoot = new SortableTreeNode(project.getName());
135 fileTreeModel = new DefaultTreeModel(fileTreeRoot);
136 fileCellRenderer = new FileCellRenderer();
140 SwingUtils.center(this);
148 * @see java.awt.Component#setVisible(boolean)
151 public void setVisible(boolean visible) {
155 super.setVisible(visible);
163 * Initializes all actions.
165 private void initActions() {
166 closeAction = new I18nAction("fileManager.button.close") {
171 public void actionPerformed(ActionEvent e) {
175 rescanAction = new I18nAction("fileManager.button.rescan") {
180 @SuppressWarnings("synthetic-access")
181 public void actionPerformed(ActionEvent actionEvent) {
188 * Initializes all components.
190 private void initComponents() {
191 JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
192 contentPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
194 contentPanel.add(createFileManagerPanel(), BorderLayout.CENTER);
195 contentPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
197 setContentPane(contentPanel);
201 * Creates the main panel with the file tree and the file properties.
203 * @return The mail panel
205 private Component createFileManagerPanel() {
206 JPanel fileManagerPanel = new JPanel(new BorderLayout(12, 12));
208 /* file tree panel */
209 JPanel fileTreePanel = new JPanel(new BorderLayout(12, 12));
210 fileManagerPanel.add(fileTreePanel, BorderLayout.LINE_START);
212 fileTree = new JTree(fileTreeModel);
213 fileTree.setShowsRootHandles(false);
214 fileTree.addTreeSelectionListener(this);
215 fileTree.setCellRenderer(fileCellRenderer);
216 fileTreePanel.add(fileScrollPane = new JScrollPane(fileTree), BorderLayout.CENTER);
217 fileScrollPane.setPreferredSize(new Dimension(250, 400));
219 projectFilesLabel = new I18nLabel("fileManager.label.projectFiles", fileTree);
220 JPanel projectFilesLabelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
221 fileTreePanel.add(projectFilesLabelPanel, BorderLayout.NORTH);
222 projectFilesLabelPanel.add(projectFilesLabel);
224 /* the right panel */
225 JPanel rightPanel = new JPanel(new BorderLayout(12, 12));
226 fileManagerPanel.add(rightPanel, BorderLayout.CENTER);
228 /* properties panel */
229 JPanel propertiesPanel = new JPanel(new GridBagLayout());
230 rightPanel.add(propertiesPanel, BorderLayout.CENTER);
231 propertiesPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
233 filePropertiesLabel = new I18nLabel("fileManager.label.fileProperties");
234 filePropertiesLabel.setFont(filePropertiesLabel.getFont().deriveFont(Font.BOLD));
235 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));
237 filePathLabel = new I18nLabel("fileManager.label.filePath");
238 filePathTextField = new JTextField();
239 filePathTextField.setEditable(false);
240 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));
241 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));
243 fileNameLabel = new I18nLabel("fileManager.label.fileName");
244 fileNameTextField = new JTextField();
245 fileNameTextField.setEditable(false);
246 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));
247 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));
250 propertiesPanel.add(new JPanel(), new GridBagConstraints(0, 3, 2, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
252 /* action button panel */
253 JPanel actionButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
254 rightPanel.add(actionButtonPanel, BorderLayout.PAGE_END);
255 actionButtonPanel.setBorder(BorderFactory.createEtchedBorder());
257 JButton rescanButton = new JButton(rescanAction);
258 actionButtonPanel.add(rescanButton);
260 return fileManagerPanel;
264 * Creates the button panel.
266 * @return The button panel
268 private Component createButtonPanel() {
269 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
271 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
272 JButton closeButton = new JButton(closeAction);
273 buttonPanel.add(closeButton);
275 getRootPane().setDefaultButton(closeButton);
280 * Initiates a file scan.
282 private void initiateFileScan() {
283 swingInterface.getThreadPool().execute(new Runnable() {
286 * @see java.lang.Runnable#run()
288 @SuppressWarnings("synthetic-access")
290 fileTree.setEnabled(false);
291 rescanAction.setEnabled(false);
292 String basePath = project.getBasePath();
293 File basePathDirectory = new File(basePath);
294 fileTreeRoot.removeAll();
296 if (!basePathDirectory.exists() || !basePathDirectory.isDirectory()) {
298 JOptionPane.showMessageDialog(FileManager.this, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
300 scanDirectory(fileTreeRoot, basePathDirectory, "", hiddenFiles);
302 fileTreeModel.reload();
303 // fileScrollPane.revalidate();
304 rescanAction.setEnabled(true);
305 fileTree.setEnabled(true);
308 private void scanDirectory(SortableTreeNode rootNode, File directory, String currentDirectory, List<String> hiddenFiles) {
309 for (File file : directory.listFiles()) {
310 String fileName = file.getName();
311 SortableTreeNode fileNode = new SortableTreeNode(fileName);
312 rootNode.add(fileNode);
313 if (file.isFile() && file.isHidden()) {
314 hiddenFiles.add((currentDirectory + File.separator + fileName).substring(1));
316 if (file.isDirectory()) {
317 scanDirectory(fileNode, file, currentDirectory + File.separator + fileName, hiddenFiles);
327 // INTERFACE I18nable
333 public void updateI18n() {
334 setTitle(I18n.get("fileManager.title", project.getName()));
335 projectFilesLabel.updateI18n();
336 filePropertiesLabel.updateI18n();
337 filePathLabel.updateI18n();
341 // INTERFACE TreeSelectionListener
347 public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
352 // INTERFACE ActionListener
358 public void actionPerformed(ActionEvent actionEvent) {
363 * Tree cell renderer that takes care of certain display properties for
364 * project-specific stuff.
366 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
368 private class FileCellRenderer extends DefaultTreeCellRenderer {
371 * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree,
372 * java.lang.Object, boolean, boolean, boolean, int, boolean)
375 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
376 Component superCellRenderer = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
377 if (!(superCellRenderer instanceof JLabel)) {
378 logger.log(Level.WARNING, "superCellRenderer is not a JLabel!");
379 return superCellRenderer;
381 if (!(value instanceof SortableTreeNode)) {
382 logger.log(Level.WARNING, "value is not a SortableTreeNode!");
383 return superCellRenderer;
385 SortableTreeNode node = (SortableTreeNode) value;
386 TreeNode[] pathToRoot = fileTreeModel.getPathToRoot(node);
387 if (pathToRoot.length > 1) {
388 StringBuilder completePathBuilder = new StringBuilder();
389 boolean first = true;
390 for (TreeNode rootPathNode : pathToRoot) {
395 if (!(rootPathNode instanceof SortableTreeNode)) {
396 logger.log(Level.WARNING, "rootPathNode is not a SortableTreeNode!");
399 completePathBuilder.append(File.separatorChar).append(((SortableTreeNode) rootPathNode).getUserObject());
401 String completePath = completePathBuilder.substring(1);
402 if (project.getDefaultFile().equals(completePath)) {
403 superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.BOLD));
405 if (hiddenFiles.contains(completePath)) {
406 /* fade hidden files’ font */
407 Color foreground = superCellRenderer.getForeground();
408 Color background = selected ? getBackgroundSelectionColor() : getBackgroundNonSelectionColor();
409 Color averageColor = new Color((foreground.getRed() + background.getRed()) / 2, (foreground.getGreen() + background.getGreen()) / 2, (foreground.getBlue() + background.getBlue()) / 2);
410 superCellRenderer.setForeground(averageColor);
412 superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.PLAIN));
416 superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.BOLD));
418 return superCellRenderer;