2 * jSite2 - ProjectPanel.java -
3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.jsite.gui;
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;
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;
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;
51 * A panel that contains all information about a project and lets the user edit
52 * the properties of the project.
54 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
56 public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
59 @SuppressWarnings("unused")
60 private static final Logger logger = Logging.getLogger(ProjectPanel.class.getName());
62 /** The Swing interface. */
63 private final SwingInterface swingInterface;
65 /** The project to show. */
66 private final Project project;
68 /** The “change base path” action. */
69 private I18nAction changeBasePathAction;
71 /** The “edit files” action. */
72 private I18nAction editFilesAction;
74 /** The “name” label. */
75 private I18nLabel nameLabel;
77 /** The “name” textfield. */
78 private JTextField nameTextField;
80 /** The “description” label. */
81 private I18nLabel descriptionLabel;
83 /** The “description” textfield. */
84 private JTextField descriptionTextField;
86 /** The “base path” label. */
87 private I18nLabel basePathLabel;
89 /** The “base path” textfield. */
90 private JTextField basePathTextField;
92 /** The “base path information” label. */
93 private JLabel basePathInformationLabel;
96 * Creates a new project panel.
98 * @param swingInterface
101 * The project to display
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;
117 * Returns the project that is displayed in this panel.
119 * @return The project of this panel
121 public Project getProject() {
130 * Initializes all actions.
132 private void initActions() {
133 changeBasePathAction = new I18nAction("projectPanel.button.changeBasePath") {
138 @SuppressWarnings("synthetic-access")
139 public void actionPerformed(ActionEvent actionEvent) {
143 editFilesAction = new I18nAction("projectPanel.button.editFiles") {
148 @SuppressWarnings("synthetic-access")
149 public void actionPerformed(ActionEvent actionEvent) {
156 * Initializes all components of the panel.
158 private void initComponents() {
159 setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
161 JPanel propertiesPanel = createPropertiesPanel();
162 add(propertiesPanel, BorderLayout.CENTER);
164 JPanel buttonPanel = createButtonPanel();
165 add(buttonPanel, BorderLayout.PAGE_END);
169 * Creates the properties panel.
171 * @return The properties panel
173 private JPanel createPropertiesPanel() {
174 JPanel propertiesPanel = new JPanel(new GridBagLayout());
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));
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));
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));
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));
202 return propertiesPanel;
206 * Creates the button panel.
208 * @return The button panel
210 private JPanel createButtonPanel() {
211 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
212 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
214 buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction(project)));
215 buttonPanel.add(new JButton(swingInterface.getCloneProjectAction(project)));
221 * Updates the project from changes in the text fields.
224 * The document that was changed
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());
239 * Lets the user select a new base path and repopulates the file list.
241 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() {
257 @SuppressWarnings("synthetic-access")
259 project.rescanBasePath();
260 List<Entry> projectEntries = project.getBasePathEntries();
261 basePathInformationLabel.setText(I18n.get("projectPanel.basePathInformation.fileCount", projectEntries.size()));
262 changeBasePathAction.setEnabled(true);
269 * Pops up the file manager and lets the user edit the parameters for the
270 * physical and virtual files.
272 private void editFiles() {
273 FileManager fileManager = new FileManager(swingInterface.getMainWindow(), project);
274 fileManager.setVisible(true);
278 // INTERFACE I18nable
284 public void updateI18n() {
285 nameLabel.updateI18n();
286 descriptionLabel.updateI18n();
287 basePathLabel.updateI18n();
288 changeBasePathAction.updateI18n();
289 editFilesAction.updateI18n();
293 // INTERFACE DocumentListener
299 public void changedUpdate(DocumentEvent documentEvent) {
300 textFieldsUpdated(documentEvent.getDocument());
306 public void insertUpdate(DocumentEvent documentEvent) {
307 textFieldsUpdated(documentEvent.getDocument());
313 public void removeUpdate(DocumentEvent documentEvent) {
314 textFieldsUpdated(documentEvent.getDocument());