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;
53 import javax.swing.tree.TreePath;
55 import net.pterodactylus.jsite.i18n.I18n;
56 import net.pterodactylus.jsite.i18n.I18nable;
57 import net.pterodactylus.jsite.i18n.gui.I18nAction;
58 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
59 import net.pterodactylus.jsite.project.Project;
60 import net.pterodactylus.util.logging.Logging;
61 import net.pterodactylus.util.swing.SortableTreeNode;
62 import net.pterodactylus.util.swing.SwingUtils;
65 * Manages physical and virtual files in a project.
67 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
69 public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener {
72 private static final Logger logger = Logging.getLogger(FileManager.class.getName());
74 /** The Swing interface. */
75 private final SwingInterface swingInterface;
77 /** The project whose files to manage. */
78 private final Project project;
80 /** The root of the file tree. */
81 private final SortableTreeNode fileTreeRoot;
83 /** The tree model for the project files. */
84 private final DefaultTreeModel fileTreeModel;
86 /** Files that are hidden as per {@link File#isHidden()}. */
87 private final List<String> hiddenFiles = new ArrayList<String>();
89 /** The tree cell renderer. */
90 private final FileCellRenderer fileCellRenderer;
92 /** The “rescan” action. */
93 private I18nAction rescanAction;
95 /** The “close” action. */
96 private I18nAction closeAction;
98 /** The “project files” label. */
99 private I18nLabel projectFilesLabel;
101 /** The tree that shows the files. */
102 private JTree fileTree;
104 /** The scroll pane that holds the file tree. */
105 private JScrollPane fileScrollPane;
107 /** The “file properties” label. */
108 private I18nLabel filePropertiesLabel;
110 /** The “file path” label. */
111 private I18nLabel filePathLabel;
113 /** The “file path” textfield. */
114 private JTextField filePathTextField;
116 /** The “file name” label. */
117 private I18nLabel fileNameLabel;
119 /** The “file name” textfield. */
120 private JTextField fileNameTextField;
123 * Creates a new file manager.
125 * @param swingInterface
126 * The Swing interface
128 * The project whose files to manage
130 public FileManager(SwingInterface swingInterface, Project project) {
131 super(swingInterface.getMainWindow(), I18n.get("fileManager.title", project.getName()), true);
132 logger.log(Level.FINEST, "project: " + project);
133 this.swingInterface = swingInterface;
134 this.project = project;
135 fileTreeRoot = new SortableTreeNode(project.getName());
136 fileTreeModel = new DefaultTreeModel(fileTreeRoot);
137 fileCellRenderer = new FileCellRenderer();
141 SwingUtils.center(this);
149 * @see java.awt.Component#setVisible(boolean)
152 public void setVisible(boolean visible) {
156 super.setVisible(visible);
164 * Initializes all actions.
166 private void initActions() {
167 closeAction = new I18nAction("fileManager.button.close") {
172 public void actionPerformed(ActionEvent e) {
176 rescanAction = new I18nAction("fileManager.button.rescan") {
181 @SuppressWarnings("synthetic-access")
182 public void actionPerformed(ActionEvent actionEvent) {
189 * Initializes all components.
191 private void initComponents() {
192 JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
193 contentPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
195 contentPanel.add(createFileManagerPanel(), BorderLayout.CENTER);
196 contentPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
198 setContentPane(contentPanel);
202 * Creates the main panel with the file tree and the file properties.
204 * @return The mail panel
206 private Component createFileManagerPanel() {
207 JPanel fileManagerPanel = new JPanel(new BorderLayout(12, 12));
209 /* file tree panel */
210 JPanel fileTreePanel = new JPanel(new BorderLayout(12, 12));
211 fileManagerPanel.add(fileTreePanel, BorderLayout.LINE_START);
213 fileTree = new JTree(fileTreeModel);
214 fileTree.setShowsRootHandles(false);
215 fileTree.addTreeSelectionListener(this);
216 fileTree.setCellRenderer(fileCellRenderer);
217 fileTreePanel.add(fileScrollPane = new JScrollPane(fileTree), BorderLayout.CENTER);
218 fileScrollPane.setPreferredSize(new Dimension(250, 400));
220 projectFilesLabel = new I18nLabel("fileManager.label.projectFiles", fileTree);
221 JPanel projectFilesLabelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
222 fileTreePanel.add(projectFilesLabelPanel, BorderLayout.NORTH);
223 projectFilesLabelPanel.add(projectFilesLabel);
225 /* the right panel */
226 JPanel rightPanel = new JPanel(new BorderLayout(12, 12));
227 fileManagerPanel.add(rightPanel, BorderLayout.CENTER);
229 /* properties panel */
230 JPanel propertiesPanel = new JPanel(new GridBagLayout());
231 rightPanel.add(propertiesPanel, BorderLayout.CENTER);
232 propertiesPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
234 filePropertiesLabel = new I18nLabel("fileManager.label.fileProperties");
235 filePropertiesLabel.setFont(filePropertiesLabel.getFont().deriveFont(Font.BOLD));
236 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));
238 filePathLabel = new I18nLabel("fileManager.label.filePath");
239 filePathTextField = new JTextField();
240 filePathTextField.setEditable(false);
241 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));
242 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));
244 fileNameLabel = new I18nLabel("fileManager.label.fileName");
245 fileNameTextField = new JTextField();
246 fileNameTextField.setEditable(false);
247 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));
248 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));
251 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));
253 /* action button panel */
254 JPanel actionButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 12, 12));
255 rightPanel.add(actionButtonPanel, BorderLayout.PAGE_END);
256 actionButtonPanel.setBorder(BorderFactory.createEtchedBorder());
258 JButton rescanButton = new JButton(rescanAction);
259 actionButtonPanel.add(rescanButton);
261 return fileManagerPanel;
265 * Creates the button panel.
267 * @return The button panel
269 private Component createButtonPanel() {
270 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
272 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
273 JButton closeButton = new JButton(closeAction);
274 buttonPanel.add(closeButton);
276 getRootPane().setDefaultButton(closeButton);
281 * Initiates a file scan.
283 private void initiateFileScan() {
284 swingInterface.getThreadPool().execute(new Runnable() {
287 * @see java.lang.Runnable#run()
289 @SuppressWarnings("synthetic-access")
291 fileTree.setEnabled(false);
292 rescanAction.setEnabled(false);
293 String basePath = project.getBasePath();
294 File basePathDirectory = new File(basePath);
295 fileTreeRoot.removeAll();
297 if (!basePathDirectory.exists() || !basePathDirectory.isDirectory()) {
299 JOptionPane.showMessageDialog(FileManager.this, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
301 scanDirectory(fileTreeRoot, basePathDirectory, "", hiddenFiles);
303 fileTreeModel.reload();
304 // fileScrollPane.revalidate();
305 rescanAction.setEnabled(true);
306 fileTree.setEnabled(true);
309 private void scanDirectory(SortableTreeNode rootNode, File directory, String currentDirectory, List<String> hiddenFiles) {
310 for (File file : directory.listFiles()) {
311 String fileName = file.getName();
312 SortableTreeNode fileNode = new SortableTreeNode(fileName);
313 rootNode.add(fileNode);
314 if (file.isFile() && file.isHidden()) {
315 hiddenFiles.add((currentDirectory + File.separator + fileName).substring(1));
317 if (file.isDirectory()) {
318 scanDirectory(fileNode, file, currentDirectory + File.separator + fileName, hiddenFiles);
328 * Returns the complete path for the given node.
332 * @return The complete path for the node, or the empty string (“”) for the
335 private String getPathForNode(TreeNode treeNode) {
336 TreeNode[] pathToNode = fileTreeModel.getPathToRoot(treeNode);
337 StringBuilder completePathBuilder = new StringBuilder();
338 boolean first = true;
339 for (TreeNode nodePathNode : pathToNode) {
344 if (!(nodePathNode instanceof SortableTreeNode)) {
345 logger.log(Level.WARNING, "nodePathNode is not a SortableTreeNode!");
348 completePathBuilder.append(File.separatorChar).append(((SortableTreeNode) nodePathNode).getUserObject());
350 String completePath = (completePathBuilder.length() == 0) ? "" : completePathBuilder.substring(1);
355 // INTERFACE I18nable
361 public void updateI18n() {
362 setTitle(I18n.get("fileManager.title", project.getName()));
363 projectFilesLabel.updateI18n();
364 filePropertiesLabel.updateI18n();
365 filePathLabel.updateI18n();
369 // INTERFACE TreeSelectionListener
375 public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
376 TreePath[] selectedPaths = fileTree.getSelectionPaths();
377 if ((selectedPaths != null) && (selectedPaths.length == 1)) {
378 Object lastPathComponent = selectedPaths[0].getLastPathComponent();
379 if (!(lastPathComponent instanceof SortableTreeNode)) {
380 logger.log(Level.WARNING, "lastPathComponent is not a SortableTreeNode!");
383 SortableTreeNode node = (SortableTreeNode) lastPathComponent;
384 String completePath = getPathForNode(node);
385 int lastSeparator = completePath.lastIndexOf(File.separatorChar);
386 String filePath = "";
388 if (lastSeparator == -1) {
389 fileName = completePath;
391 filePath = completePath.substring(0, lastSeparator);
392 fileName = completePath.substring(lastSeparator + 1);
394 filePathTextField.setText(filePath);
395 fileNameTextField.setText(fileName);
398 filePathTextField.setText("");
399 fileNameTextField.setText("");
403 // INTERFACE ActionListener
409 public void actionPerformed(ActionEvent actionEvent) {
414 * Tree cell renderer that takes care of certain display properties for
415 * project-specific stuff.
417 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
419 private class FileCellRenderer extends DefaultTreeCellRenderer {
422 * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree,
423 * java.lang.Object, boolean, boolean, boolean, int, boolean)
426 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
427 Component superCellRenderer = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
428 if (!(superCellRenderer instanceof JLabel)) {
429 logger.log(Level.WARNING, "superCellRenderer is not a JLabel!");
430 return superCellRenderer;
432 if (!(value instanceof SortableTreeNode)) {
433 logger.log(Level.WARNING, "value is not a SortableTreeNode!");
434 return superCellRenderer;
436 SortableTreeNode node = (SortableTreeNode) value;
437 TreeNode[] pathToRoot = fileTreeModel.getPathToRoot(node);
438 if (pathToRoot.length > 1) {
439 String completePath = getPathForNode(node);
440 if (project.getDefaultFile().equals(completePath)) {
441 superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.BOLD));
443 if (hiddenFiles.contains(completePath)) {
444 /* fade hidden files’ font */
445 Color foreground = superCellRenderer.getForeground();
446 Color background = selected ? getBackgroundSelectionColor() : getBackgroundNonSelectionColor();
447 Color averageColor = new Color((foreground.getRed() + background.getRed()) / 2, (foreground.getGreen() + background.getGreen()) / 2, (foreground.getBlue() + background.getBlue()) / 2);
448 superCellRenderer.setForeground(averageColor);
450 superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.PLAIN));
454 superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.BOLD));
456 return superCellRenderer;