c3b9a7803671482d35c75feba341b2c51cdd4c98
[jSite2.git] / src / net / pterodactylus / jsite / gui / ProjectPanel.java
1 /*
2  * jSite2 - ProjectPanel.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.FlowLayout;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Insets;
27 import java.awt.event.ActionEvent;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 import javax.swing.AbstractAction;
32 import javax.swing.Action;
33 import javax.swing.BorderFactory;
34 import javax.swing.JButton;
35 import javax.swing.JComboBox;
36 import javax.swing.JFileChooser;
37 import javax.swing.JPanel;
38 import javax.swing.JTextField;
39 import javax.swing.event.DocumentEvent;
40 import javax.swing.event.DocumentListener;
41 import javax.swing.text.Document;
42
43 import net.pterodactylus.jsite.core.Project;
44 import net.pterodactylus.jsite.i18n.I18n;
45 import net.pterodactylus.jsite.i18n.I18nable;
46 import net.pterodactylus.jsite.i18n.gui.I18nAction;
47 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
48 import net.pterodactylus.util.logging.Logging;
49
50 /**
51  * A panel that contains all information about a project and lets the user edit
52  * the properties of the project.
53  * 
54  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
55  */
56 public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
57
58         /** Logger. */
59         @SuppressWarnings("unused")
60         private static final Logger logger = Logging.getLogger(ProjectPanel.class.getName());
61
62         /** The Swing interface. */
63         private final SwingInterface swingInterface;
64
65         /** The project to show. */
66         private final Project project;
67
68         /** The “change base path” action. */
69         private I18nAction changeBasePathAction;
70
71         /** The “edit files” action. */
72         private I18nAction editFilesAction;
73
74         /** The “name” label. */
75         private I18nLabel nameLabel;
76
77         /** The “name” textfield. */
78         private JTextField nameTextField;
79
80         /** The “description” label. */
81         private I18nLabel descriptionLabel;
82
83         /** The “description” textfield. */
84         private JTextField descriptionTextField;
85
86         /** The “base path” label. */
87         private I18nLabel basePathLabel;
88
89         /** The “base path” textfield. */
90         private JTextField basePathTextField;
91
92         /** The “node” label. */
93         private I18nLabel nodeLabel;
94
95         /** The “node” action. */
96         private Action nodeAction;
97
98         /** The “node” combo box. */
99         private JComboBox nodeComboBox;
100
101         /**
102          * Creates a new project panel.
103          * 
104          * @param swingInterface
105          *            The Swing interface
106          * @param project
107          *            The project to display
108          */
109         public ProjectPanel(SwingInterface swingInterface, Project project) {
110                 super(new BorderLayout(12, 12));
111                 logger.log(Level.FINEST, "project: " + project);
112                 this.swingInterface = swingInterface;
113                 this.project = project;
114                 initActions();
115                 initComponents();
116         }
117
118         //
119         // ACCESSORS
120         //
121
122         /**
123          * Returns the project that is displayed in this panel.
124          * 
125          * @return The project of this panel
126          */
127         public Project getProject() {
128                 return project;
129         }
130
131         //
132         // PRIVATE METHODS
133         //
134
135         /**
136          * Initializes all actions.
137          */
138         private void initActions() {
139                 changeBasePathAction = new I18nAction("projectPanel.button.changeBasePath") {
140
141                         /**
142                          * {@inheritDoc}
143                          */
144                         @SuppressWarnings("synthetic-access")
145                         public void actionPerformed(ActionEvent actionEvent) {
146                                 changeBasePath();
147                         }
148                 };
149                 editFilesAction = new I18nAction("projectPanel.button.editFiles") {
150
151                         /**
152                          * {@inheritDoc}
153                          */
154                         @SuppressWarnings("synthetic-access")
155                         public void actionPerformed(ActionEvent actionEvent) {
156                                 editFiles();
157                         }
158                 };
159                 nodeAction = new AbstractAction() {
160
161                         /**
162                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
163                          */
164                         public void actionPerformed(ActionEvent actionEvent) {
165                                 /* TODO */
166                         }
167                 };
168         }
169
170         /**
171          * Initializes all components of the panel.
172          */
173         private void initComponents() {
174                 setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
175
176                 JPanel propertiesPanel = createPropertiesPanel();
177                 add(propertiesPanel, BorderLayout.CENTER);
178
179                 JPanel buttonPanel = createButtonPanel();
180                 add(buttonPanel, BorderLayout.PAGE_END);
181         }
182
183         /**
184          * Creates the properties panel.
185          * 
186          * @return The properties panel
187          */
188         private JPanel createPropertiesPanel() {
189                 JPanel propertiesPanel = new JPanel(new GridBagLayout());
190
191                 nameTextField = new JTextField(project.getName());
192                 nameTextField.getDocument().addDocumentListener(this);
193                 nameLabel = new I18nLabel("projectPanel.label.name", nameTextField);
194                 propertiesPanel.add(nameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
195                 propertiesPanel.add(nameTextField, new GridBagConstraints(1, 0, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 12, 0, 0), 0, 0));
196
197                 descriptionTextField = new JTextField(project.getDescription());
198                 descriptionTextField.getDocument().addDocumentListener(this);
199                 descriptionLabel = new I18nLabel("projectPanel.label.description", descriptionTextField);
200                 propertiesPanel.add(descriptionLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 0, 0, 0), 0, 0));
201                 propertiesPanel.add(descriptionTextField, new GridBagConstraints(1, 1, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
202
203                 basePathTextField = new JTextField(project.getBasePath());
204                 basePathTextField.setEditable(false);
205                 basePathLabel = new I18nLabel("projectPanel.label.basePath");
206                 JButton changeBasePathButton = new JButton(changeBasePathAction);
207                 JButton editFilesButton = new JButton(editFilesAction);
208                 propertiesPanel.add(basePathLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 0, 0, 0), 0, 0));
209                 propertiesPanel.add(basePathTextField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
210                 propertiesPanel.add(changeBasePathButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
211                 propertiesPanel.add(editFilesButton, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
212
213                 nodeComboBox = new JComboBox();
214                 nodeComboBox.addActionListener(nodeAction);
215                 nodeLabel = new I18nLabel("projectPanel.label.node", nodeComboBox);
216                 propertiesPanel.add(nodeLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 0, 0, 0), 0, 0));
217                 propertiesPanel.add(nodeComboBox, new GridBagConstraints(1, 3, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
218
219                 propertiesPanel.add(new JPanel(), new GridBagConstraints(0, 4, 4, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
220
221                 return propertiesPanel;
222         }
223
224         /**
225          * Creates the button panel.
226          * 
227          * @return The button panel
228          */
229         private JPanel createButtonPanel() {
230                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
231                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
232
233                 buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction(project)));
234                 buttonPanel.add(new JButton(swingInterface.getCloneProjectAction(project)));
235
236                 return buttonPanel;
237         }
238
239         /**
240          * Updates the project from changes in the text fields.
241          * 
242          * @param document
243          *            The document that was changed
244          */
245         private void textFieldsUpdated(Document document) {
246                 if (nameTextField.getDocument() == document) {
247                         project.setName(nameTextField.getText());
248                 } else if (descriptionTextField.getDocument() == document) {
249                         project.setDescription(descriptionTextField.getText());
250                 }
251         }
252
253         //
254         // PRIVATE ACTIONS
255         //
256
257         /**
258          * Lets the user select a new base path and repopulates the file list.
259          */
260         void changeBasePath() {
261                 JFileChooser fileChooser = new JFileChooser(project.getBasePath());
262                 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
263                 fileChooser.setApproveButtonText(I18n.get("projectPanel.button.approve.name"));
264                 int chooseAction = fileChooser.showOpenDialog(this);
265                 if (chooseAction == JFileChooser.APPROVE_OPTION) {
266                         String newBasePath = fileChooser.getSelectedFile().getPath();
267                         basePathTextField.setText(newBasePath);
268                         project.setBasePath(newBasePath);
269                 }
270         }
271
272         /**
273          * Pops up the file manager and lets the user edit the parameters for the
274          * physical and virtual files.
275          */
276         private void editFiles() {
277                 FileManager fileManager = new FileManager(swingInterface, project);
278                 fileManager.setVisible(true);
279         }
280
281         //
282         // INTERFACE I18nable
283         //
284
285         /**
286          * {@inheritDoc}
287          */
288         public void updateI18n() {
289                 nameLabel.updateI18n();
290                 descriptionLabel.updateI18n();
291                 basePathLabel.updateI18n();
292                 changeBasePathAction.updateI18n();
293                 editFilesAction.updateI18n();
294         }
295
296         //
297         // INTERFACE DocumentListener
298         //
299
300         /**
301          * {@inheritDoc}
302          */
303         public void changedUpdate(DocumentEvent documentEvent) {
304                 textFieldsUpdated(documentEvent.getDocument());
305         }
306
307         /**
308          * {@inheritDoc}
309          */
310         public void insertUpdate(DocumentEvent documentEvent) {
311                 textFieldsUpdated(documentEvent.getDocument());
312         }
313
314         /**
315          * {@inheritDoc}
316          */
317         public void removeUpdate(DocumentEvent documentEvent) {
318                 textFieldsUpdated(documentEvent.getDocument());
319         }
320
321 }