update actions when language changes
[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.List;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32 import javax.swing.BorderFactory;
33 import javax.swing.JButton;
34 import javax.swing.JFileChooser;
35 import javax.swing.JLabel;
36 import javax.swing.JPanel;
37 import javax.swing.JTextField;
38 import javax.swing.event.DocumentEvent;
39 import javax.swing.event.DocumentListener;
40 import javax.swing.text.Document;
41
42 import net.pterodactylus.jsite.i18n.I18n;
43 import net.pterodactylus.jsite.i18n.I18nable;
44 import net.pterodactylus.jsite.i18n.gui.I18nAction;
45 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
46 import net.pterodactylus.jsite.project.Entry;
47 import net.pterodactylus.jsite.project.Project;
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 “base path information” label. */
93         private JLabel basePathInformationLabel;
94
95         /**
96          * Creates a new project panel.
97          * 
98          * @param swingInterface
99          *            The Swing interface
100          * @param project
101          *            The project to display
102          */
103         public ProjectPanel(SwingInterface swingInterface, Project project) {
104                 super(new BorderLayout(12, 12));
105                 logger.log(Level.FINEST, "project: " + project);
106                 this.swingInterface = swingInterface;
107                 this.project = project;
108                 initActions();
109                 initComponents();
110         }
111
112         //
113         // ACCESSORS
114         //
115
116         /**
117          * Returns the project that is displayed in this panel.
118          * 
119          * @return The project of this panel
120          */
121         public Project getProject() {
122                 return project;
123         }
124
125         //
126         // PRIVATE METHODS
127         //
128
129         /**
130          * Initializes all actions.
131          */
132         private void initActions() {
133                 changeBasePathAction = new I18nAction("projectPanel.button.changeBasePath") {
134
135                         /**
136                          * {@inheritDoc}
137                          */
138                         @SuppressWarnings("synthetic-access")
139                         public void actionPerformed(ActionEvent actionEvent) {
140                                 changeBasePath();
141                         }
142                 };
143                 editFilesAction = new I18nAction("projectPanel.button.editFiles") {
144
145                         /**
146                          * {@inheritDoc}
147                          */
148                         @SuppressWarnings("synthetic-access")
149                         public void actionPerformed(ActionEvent actionEvent) {
150                                 editFiles();
151                         }
152                 };
153         }
154
155         /**
156          * Initializes all components of the panel.
157          */
158         private void initComponents() {
159                 setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
160
161                 JPanel propertiesPanel = createPropertiesPanel();
162                 add(propertiesPanel, BorderLayout.CENTER);
163
164                 JPanel buttonPanel = createButtonPanel();
165                 add(buttonPanel, BorderLayout.PAGE_END);
166         }
167
168         /**
169          * Creates the properties panel.
170          * 
171          * @return The properties panel
172          */
173         private JPanel createPropertiesPanel() {
174                 JPanel propertiesPanel = new JPanel(new GridBagLayout());
175
176                 nameTextField = new JTextField(project.getName());
177                 nameTextField.getDocument().addDocumentListener(this);
178                 nameLabel = new I18nLabel("projectPanel.label.name", nameTextField);
179                 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));
180                 propertiesPanel.add(nameTextField, new GridBagConstraints(1, 0, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 6, 0, 0), 0, 0));
181
182                 descriptionTextField = new JTextField(project.getDescription());
183                 descriptionTextField.getDocument().addDocumentListener(this);
184                 descriptionLabel = new I18nLabel("projectPanel.label.description", descriptionTextField);
185                 propertiesPanel.add(descriptionLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 0, 0, 0), 0, 0));
186                 propertiesPanel.add(descriptionTextField, new GridBagConstraints(1, 1, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
187
188                 basePathTextField = new JTextField(project.getBasePath());
189                 basePathTextField.setEditable(false);
190                 basePathLabel = new I18nLabel("projectPanel.label.basePath");
191                 basePathInformationLabel = new JLabel(I18n.get("projectPanel.basePathInformation.fileCount", project.getBasePathEntries().size()));
192                 JButton changeBasePathButton = new JButton(changeBasePathAction);
193                 JButton editFilesButton = new JButton(editFilesAction);
194                 propertiesPanel.add(basePathLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 0, 0, 0), 0, 0));
195                 propertiesPanel.add(basePathTextField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
196                 propertiesPanel.add(changeBasePathButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
197                 propertiesPanel.add(editFilesButton, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
198                 propertiesPanel.add(basePathInformationLabel, new GridBagConstraints(1, 3, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0, 0));
199
200                 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));
201
202                 return propertiesPanel;
203         }
204
205         /**
206          * Creates the button panel.
207          * 
208          * @return The button panel
209          */
210         private JPanel createButtonPanel() {
211                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
212                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
213
214                 buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction()));
215                 buttonPanel.add(new JButton(swingInterface.getCloneProjectAction()));
216
217                 return buttonPanel;
218         }
219
220         /**
221          * Updates the project from changes in the text fields.
222          * 
223          * @param document
224          *            The document that was changed
225          */
226         private void textFieldsUpdated(Document document) {
227                 if (nameTextField.getDocument() == document) {
228                         project.setName(nameTextField.getText());
229                 } else if (descriptionTextField.getDocument() == document) {
230                         project.setDescription(descriptionTextField.getText());
231                 }
232         }
233
234         //
235         // PRIVATE ACTIONS
236         //
237
238         /**
239          * Lets the user select a new base path and repopulates the file list.
240          */
241         private void changeBasePath() {
242                 JFileChooser fileChooser = new JFileChooser(project.getBasePath());
243                 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
244                 fileChooser.setApproveButtonText(I18n.get("projectPanel.button.approve.name"));
245                 int chooseAction = fileChooser.showOpenDialog(this);
246                 if (chooseAction == JFileChooser.APPROVE_OPTION) {
247                         String newBasePath = fileChooser.getSelectedFile().getPath();
248                         basePathTextField.setText(newBasePath);
249                         basePathInformationLabel.setText(I18n.get("projectPanel.basePathInformation.scanning"));
250                         project.setBasePath(newBasePath);
251                         changeBasePathAction.setEnabled(false);
252                         swingInterface.getThreadPool().execute(new Runnable() {
253
254                                 /**
255                                  * {@inheritDoc}
256                                  */
257                                 @SuppressWarnings("synthetic-access")
258                                 public void run() {
259                                         project.rescanBasePath();
260                                         List<Entry> projectEntries = project.getBasePathEntries();
261                                         basePathInformationLabel.setText(I18n.get("projectPanel.basePathInformation.fileCount", projectEntries.size()));
262                                         changeBasePathAction.setEnabled(true);
263                                 }
264                         });
265                 }
266         }
267
268         /**
269          * Pops up the file manager and lets the user edit the parameters for the
270          * physical and virtual files.
271          */
272         private void editFiles() {
273                 FileManager fileManager = new FileManager(swingInterface.getMainWindow(), project);
274                 fileManager.setVisible(true);
275         }
276
277         //
278         // INTERFACE I18nable
279         //
280
281         /**
282          * {@inheritDoc}
283          */
284         public void updateI18n() {
285                 nameLabel.updateI18n();
286                 descriptionLabel.updateI18n();
287                 changeBasePathAction.updateI18n();
288                 editFilesAction.updateI18n();
289         }
290
291         //
292         // INTERFACE DocumentListener
293         //
294
295         /**
296          * {@inheritDoc}
297          */
298         public void changedUpdate(DocumentEvent documentEvent) {
299                 textFieldsUpdated(documentEvent.getDocument());
300         }
301
302         /**
303          * {@inheritDoc}
304          */
305         public void insertUpdate(DocumentEvent documentEvent) {
306                 textFieldsUpdated(documentEvent.getDocument());
307         }
308
309         /**
310          * {@inheritDoc}
311          */
312         public void removeUpdate(DocumentEvent documentEvent) {
313                 textFieldsUpdated(documentEvent.getDocument());
314         }
315
316 }