throw away all the entry stuff from the file manager
[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.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;
30 import java.io.File;
31 import java.util.List;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34
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;
49
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;
58
59 /**
60  * Manages physical and virtual files in a project.
61  *
62  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
63  */
64 public class FileManager extends JDialog implements I18nable, ActionListener, TreeSelectionListener {
65
66         /** Logger. */
67         private static final Logger logger = Logging.getLogger(FileManager.class.getName());
68
69         /** The Swing interface. */
70         private final SwingInterface swingInterface;
71
72         /** The project whose files to manage. */
73         private final Project project;
74
75         /** The tree model for the project files. */
76         private final TreeModel fileTreeModel;
77
78         /** The “close” action. */
79         private I18nAction closeAction;
80
81         /** The “project files” label. */
82         private I18nLabel projectFilesLabel;
83
84         /** The tree that shows the files. */
85         private JTree fileTree;
86
87         /** The “insert” action. */
88         private I18nAction insertAction;
89
90         /** The “insert” checkbox. */
91         private JCheckBox insertCheckBox;
92
93         /** The “use custom mime type” action. */
94         private I18nAction useCustomMimeTypeAction;
95
96         /** The “use custom mime type” checkbox. */
97         private JCheckBox useCustomMimeTypeCheckBox;
98
99         /** The “mime type” combo box. */
100         private JComboBox mimeTypeComboBox;
101
102         /**
103          * Creates a new file manager.
104          *
105          * @param swingInterface
106          *            The Swing interface
107          * @param project
108          *            The project whose files to manage
109          */
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));
116                 initActions();
117                 initComponents();
118                 pack();
119                 SwingUtils.center(this);
120         }
121
122         //
123         // ACTIONS
124         //
125
126         /**
127          * @see java.awt.Component#setVisible(boolean)
128          */
129         @Override
130         public void setVisible(boolean visible) {
131                 if (visible) {
132                         initiateFileScan();
133                 }
134             super.setVisible(visible);
135         }
136
137         //
138         // PRIVATE METHODS
139         //
140
141         /**
142          * Initializes all actions.
143          */
144         private void initActions() {
145                 closeAction = new I18nAction("fileManager.button.close") {
146
147                         /**
148                          * {@inheritDoc}
149                          */
150                         public void actionPerformed(ActionEvent e) {
151                                 setVisible(false);
152                         }
153                 };
154                 insertAction = new I18nAction("fileManager.checkbox.insertFile") {
155
156                         /**
157                          * {@inheritDoc}
158                          */
159                         @SuppressWarnings("synthetic-access")
160                         public void actionPerformed(ActionEvent actionEvent) {
161                                 /* TODO */
162                         }
163                 };
164                 insertAction.setEnabled(false);
165                 useCustomMimeTypeAction = new I18nAction("fileManager.checkbox.useCustomMimeType") {
166
167                         /**
168                          * {@inheritDoc}
169                          */
170                         @SuppressWarnings("synthetic-access")
171                         public void actionPerformed(ActionEvent actionEvent) {
172                                 /* TODO */
173                         }
174                 };
175                 useCustomMimeTypeAction.setEnabled(false);
176         }
177
178         /**
179          * Initializes all components.
180          */
181         private void initComponents() {
182                 JPanel contentPanel = new JPanel(new BorderLayout(12, 12));
183                 contentPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
184
185                 contentPanel.add(createFileManagerPanel(), BorderLayout.CENTER);
186                 contentPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
187
188                 setContentPane(contentPanel);
189         }
190
191         /**
192          * Creates the main panel with the file tree and the file properties.
193          *
194          * @return The mail panel
195          */
196         private Component createFileManagerPanel() {
197                 JPanel fileManagerPanel = new JPanel(new BorderLayout(12, 12));
198
199                 JPanel fileTreePanel = new JPanel(new BorderLayout(12, 12));
200                 fileManagerPanel.add(fileTreePanel, BorderLayout.LINE_START);
201
202                 fileTree = new JTree(fileTreeModel);
203                 fileTree.setShowsRootHandles(false);
204                 fileTree.addTreeSelectionListener(this);
205                 fileTreePanel.add(new JScrollPane(fileTree), BorderLayout.CENTER);
206
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);
211
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)));
215
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));
218
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));
226
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));
228
229                 return fileManagerPanel;
230         }
231
232         /**
233          * Creates the button panel.
234          *
235          * @return The button panel
236          */
237         private Component createButtonPanel() {
238                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
239
240                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
241                 JButton closeButton = new JButton(closeAction);
242                 buttonPanel.add(closeButton);
243
244                 getRootPane().setDefaultButton(closeButton);
245                 return buttonPanel;
246         }
247
248         private void initiateFileScan() {
249                 swingInterface.getThreadPool().execute(new Runnable() {
250                         /**
251                          * @see java.lang.Runnable#run()
252                          */
253                         public void run() {
254                 String basePath = project.getBasePath();
255                 File basePathDirectory = new File(basePath);
256                         }
257                 });
258         }
259
260         //
261         // INTERFACE I18nable
262         //
263
264         /**
265          * {@inheritDoc}
266          */
267         public void updateI18n() {
268                 setTitle(I18n.get("fileManager.title", project.getName()));
269         }
270
271         //
272         // INTERFACE TreeSelectionListener
273         //
274
275         /**
276          * {@inheritDoc}
277          */
278         public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
279         }
280
281         //
282         // INTERFACE ActionListener
283         //
284
285         /**
286          * {@inheritDoc}
287          */
288         public void actionPerformed(ActionEvent actionEvent) {
289         }
290
291 }