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