remove old gui
[jSite2.git] / src / net / pterodactylus / jsite / gui / FileManager.java
1 /*
2  * jSite2 - FileManager.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.jsite.gui;
21
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;
27 import java.awt.Font;
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;
33 import java.io.File;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38
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
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;
62
63 /**
64  * Manages physical and virtual files in a project.
65  *
66  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
67  */
68 public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener {
69
70         /** Logger. */
71         private static final Logger logger = Logging.getLogger(FileManager.class.getName());
72
73         /** The Swing interface. */
74         private final SwingInterface swingInterface;
75
76         /** The project whose files to manage. */
77         private final Project project;
78
79         /** The root of the file tree. */
80         private final SortableTreeNode fileTreeRoot;
81
82         /** The tree model for the project files. */
83         private final DefaultTreeModel fileTreeModel;
84
85         /** Files that are hidden as per {@link File#isHidden()}. */
86         private final List<String> hiddenFiles = new ArrayList<String>();
87
88         /** The tree cell renderer. */
89         private final FileCellRenderer fileCellRenderer;
90
91         /** The “rescan” action. */
92         private I18nAction rescanAction;
93
94         /** The “close” action. */
95         private I18nAction closeAction;
96
97         /** The “project files” label. */
98         private I18nLabel projectFilesLabel;
99
100         /** The tree that shows the files. */
101         private JTree fileTree;
102
103         /** The scroll pane that holds the file tree. */
104         private JScrollPane fileScrollPane;
105
106         /** The “file properties” label. */
107         private I18nLabel filePropertiesLabel;
108
109         /** The “file path” label. */
110         private I18nLabel filePathLabel;
111
112         /** The “file path” textfield. */
113         private JTextField filePathTextField;
114
115         /** The “file name” label. */
116         private I18nLabel fileNameLabel;
117
118         /** The “file name” textfield. */
119         private JTextField fileNameTextField;
120
121         /**
122          * Creates a new file manager.
123          *
124          * @param swingInterface
125          *            The Swing interface
126          * @param project
127          *            The project whose files to manage
128          */
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();
137                 initActions();
138                 initComponents();
139                 pack();
140                 SwingUtils.center(this);
141         }
142
143         //
144         // ACTIONS
145         //
146
147         /**
148          * @see java.awt.Component#setVisible(boolean)
149          */
150         @Override
151         public void setVisible(boolean visible) {
152                 if (visible) {
153                         initiateFileScan();
154                 }
155                 super.setVisible(visible);
156         }
157
158         //
159         // PRIVATE METHODS
160         //
161
162         /**
163          * Initializes all actions.
164          */
165         private void initActions() {
166                 closeAction = new I18nAction("fileManager.button.close") {
167
168                         /**
169                          * {@inheritDoc}
170                          */
171                         public void actionPerformed(ActionEvent e) {
172                                 setVisible(false);
173                         }
174                 };
175                 rescanAction = new I18nAction("fileManager.button.rescan") {
176
177                         /**
178                          * {@inheritDoc}
179                          */
180                         @SuppressWarnings("synthetic-access")
181                         public void actionPerformed(ActionEvent actionEvent) {
182                                 initiateFileScan();
183                         }
184                 };
185         }
186
187         /**
188          * Initializes all components.
189          */
190         private void initComponents() {
191                 JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
192                 contentPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
193
194                 contentPanel.add(createFileManagerPanel(), BorderLayout.CENTER);
195                 contentPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
196
197                 setContentPane(contentPanel);
198         }
199
200         /**
201          * Creates the main panel with the file tree and the file properties.
202          *
203          * @return The mail panel
204          */
205         private Component createFileManagerPanel() {
206                 JPanel fileManagerPanel = new JPanel(new BorderLayout(12, 12));
207
208                 /* file tree panel */
209                 JPanel fileTreePanel = new JPanel(new BorderLayout(12, 12));
210                 fileManagerPanel.add(fileTreePanel, BorderLayout.LINE_START);
211
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));
218
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);
223
224                 /* the right panel */
225                 JPanel rightPanel = new JPanel(new BorderLayout(12, 12));
226                 fileManagerPanel.add(rightPanel, BorderLayout.CENTER);
227
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)));
232
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));
236
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));
242
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));
248
249                 /* glue panel. */
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));
251
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());
256
257                 JButton rescanButton = new JButton(rescanAction);
258                 actionButtonPanel.add(rescanButton);
259
260                 return fileManagerPanel;
261         }
262
263         /**
264          * Creates the button panel.
265          *
266          * @return The button panel
267          */
268         private Component createButtonPanel() {
269                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
270
271                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
272                 JButton closeButton = new JButton(closeAction);
273                 buttonPanel.add(closeButton);
274
275                 getRootPane().setDefaultButton(closeButton);
276                 return buttonPanel;
277         }
278
279         /**
280          * Initiates a file scan.
281          */
282         private void initiateFileScan() {
283                 swingInterface.getThreadPool().execute(new Runnable() {
284
285                         /**
286                          * @see java.lang.Runnable#run()
287                          */
288                         @SuppressWarnings("synthetic-access")
289                         public void run() {
290                                 fileTree.setEnabled(false);
291                                 rescanAction.setEnabled(false);
292                                 String basePath = project.getBasePath();
293                                 File basePathDirectory = new File(basePath);
294                                 fileTreeRoot.removeAll();
295                                 hiddenFiles.clear();
296                                 if (!basePathDirectory.exists() || !basePathDirectory.isDirectory()) {
297                                         /* TODO - i18n */
298                                         JOptionPane.showMessageDialog(FileManager.this, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
299                                 } else {
300                                         scanDirectory(fileTreeRoot, basePathDirectory, "", hiddenFiles);
301                                 }
302                                 fileTreeModel.reload();
303                                 // fileScrollPane.revalidate();
304                                 rescanAction.setEnabled(true);
305                                 fileTree.setEnabled(true);
306                         }
307
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));
315                                         }
316                                         if (file.isDirectory()) {
317                                                 scanDirectory(fileNode, file, currentDirectory + File.separator + fileName, hiddenFiles);
318                                         }
319                                 }
320                                 rootNode.sort();
321                         }
322
323                 });
324         }
325
326         //
327         // INTERFACE I18nable
328         //
329
330         /**
331          * {@inheritDoc}
332          */
333         public void updateI18n() {
334                 setTitle(I18n.get("fileManager.title", project.getName()));
335                 projectFilesLabel.updateI18n();
336                 filePropertiesLabel.updateI18n();
337                 filePathLabel.updateI18n();
338         }
339
340         //
341         // INTERFACE TreeSelectionListener
342         //
343
344         /**
345          * {@inheritDoc}
346          */
347         public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
348                 /* TODO */
349         }
350
351         //
352         // INTERFACE ActionListener
353         //
354
355         /**
356          * {@inheritDoc}
357          */
358         public void actionPerformed(ActionEvent actionEvent) {
359                 /* TODO */
360         }
361
362         /**
363          * Tree cell renderer that takes care of certain display properties for
364          * project-specific stuff.
365          *
366          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
367          */
368         private class FileCellRenderer extends DefaultTreeCellRenderer {
369
370                 /**
371                  * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree,
372                  *      java.lang.Object, boolean, boolean, boolean, int, boolean)
373                  */
374                 @Override
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;
380                         }
381                         if (!(value instanceof SortableTreeNode)) {
382                                 logger.log(Level.WARNING, "value is not a SortableTreeNode!");
383                                 return superCellRenderer;
384                         }
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) {
391                                         if (first) {
392                                                 first = false;
393                                                 continue;
394                                         }
395                                         if (!(rootPathNode instanceof SortableTreeNode)) {
396                                                 logger.log(Level.WARNING, "rootPathNode is not a SortableTreeNode!");
397                                                 continue;
398                                         }
399                                         completePathBuilder.append(File.separatorChar).append(((SortableTreeNode) rootPathNode).getUserObject());
400                                 }
401                                 String completePath = completePathBuilder.substring(1);
402                                 if (project.getDefaultFile().equals(completePath)) {
403                                         superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.BOLD));
404                                 } else {
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);
411                                         } else {
412                                                 superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.PLAIN));
413                                         }
414                                 }
415                         } else {
416                                 superCellRenderer.setFont(superCellRenderer.getFont().deriveFont(Font.BOLD));
417                         }
418                         return superCellRenderer;
419                 }
420
421         }
422
423 }