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