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.logging.Level;
29 import java.util.logging.Logger;
31 import javax.swing.BorderFactory;
32 import javax.swing.JButton;
33 import javax.swing.JFileChooser;
34 import javax.swing.JPanel;
35 import javax.swing.JTextField;
36 import javax.swing.event.DocumentEvent;
37 import javax.swing.event.DocumentListener;
38 import javax.swing.text.Document;
40 import net.pterodactylus.jsite.i18n.I18n;
41 import net.pterodactylus.jsite.i18n.I18nable;
42 import net.pterodactylus.jsite.i18n.gui.I18nAction;
43 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
44 import net.pterodactylus.jsite.project.Project;
45 import net.pterodactylus.util.logging.Logging;
48 * A panel that contains all information about a project and lets the user edit
49 * the properties of the project.
51 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
53 public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
56 @SuppressWarnings("unused")
57 private static final Logger logger = Logging.getLogger(ProjectPanel.class.getName());
59 /** The Swing interface. */
60 private final SwingInterface swingInterface;
62 /** The project to show. */
63 private final Project project;
65 /** The “change base path” action. */
66 private I18nAction changeBasePathAction;
68 /** The “edit files” action. */
69 private I18nAction editFilesAction;
71 /** The “name” label. */
72 private I18nLabel nameLabel;
74 /** The “name” textfield. */
75 private JTextField nameTextField;
77 /** The “description” label. */
78 private I18nLabel descriptionLabel;
80 /** The “description” textfield. */
81 private JTextField descriptionTextField;
83 /** The “base path” label. */
84 private I18nLabel basePathLabel;
86 /** The “base path” textfield. */
87 private JTextField basePathTextField;
90 * Creates a new project panel.
92 * @param swingInterface
95 * The project to display
97 public ProjectPanel(SwingInterface swingInterface, Project project) {
98 super(new BorderLayout(12, 12));
99 logger.log(Level.FINEST, "project: " + project);
100 this.swingInterface = swingInterface;
101 this.project = project;
111 * Returns the project that is displayed in this panel.
113 * @return The project of this panel
115 public Project getProject() {
124 * Initializes all actions.
126 private void initActions() {
127 changeBasePathAction = new I18nAction("projectPanel.button.changeBasePath") {
132 @SuppressWarnings("synthetic-access")
133 public void actionPerformed(ActionEvent actionEvent) {
137 editFilesAction = new I18nAction("projectPanel.button.editFiles") {
142 @SuppressWarnings("synthetic-access")
143 public void actionPerformed(ActionEvent actionEvent) {
150 * Initializes all components of the panel.
152 private void initComponents() {
153 setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
155 JPanel propertiesPanel = createPropertiesPanel();
156 add(propertiesPanel, BorderLayout.CENTER);
158 JPanel buttonPanel = createButtonPanel();
159 add(buttonPanel, BorderLayout.PAGE_END);
163 * Creates the properties panel.
165 * @return The properties panel
167 private JPanel createPropertiesPanel() {
168 JPanel propertiesPanel = new JPanel(new GridBagLayout());
170 nameTextField = new JTextField(project.getName());
171 nameTextField.getDocument().addDocumentListener(this);
172 nameLabel = new I18nLabel("projectPanel.label.name", nameTextField);
173 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));
174 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));
176 descriptionTextField = new JTextField(project.getDescription());
177 descriptionTextField.getDocument().addDocumentListener(this);
178 descriptionLabel = new I18nLabel("projectPanel.label.description", descriptionTextField);
179 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));
180 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));
182 basePathTextField = new JTextField(project.getBasePath());
183 basePathTextField.setEditable(false);
184 basePathLabel = new I18nLabel("projectPanel.label.basePath");
185 JButton changeBasePathButton = new JButton(changeBasePathAction);
186 JButton editFilesButton = new JButton(editFilesAction);
187 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));
188 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));
189 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));
190 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));
192 propertiesPanel.add(new JPanel(), new GridBagConstraints(0, 3, 4, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
194 return propertiesPanel;
198 * Creates the button panel.
200 * @return The button panel
202 private JPanel createButtonPanel() {
203 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
204 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
206 buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction(project)));
207 buttonPanel.add(new JButton(swingInterface.getCloneProjectAction(project)));
213 * Updates the project from changes in the text fields.
216 * The document that was changed
218 private void textFieldsUpdated(Document document) {
219 if (nameTextField.getDocument() == document) {
220 project.setName(nameTextField.getText());
221 } else if (descriptionTextField.getDocument() == document) {
222 project.setDescription(descriptionTextField.getText());
231 * Lets the user select a new base path and repopulates the file list.
233 void changeBasePath() {
234 JFileChooser fileChooser = new JFileChooser(project.getBasePath());
235 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
236 fileChooser.setApproveButtonText(I18n.get("projectPanel.button.approve.name"));
237 int chooseAction = fileChooser.showOpenDialog(this);
238 if (chooseAction == JFileChooser.APPROVE_OPTION) {
239 String newBasePath = fileChooser.getSelectedFile().getPath();
240 basePathTextField.setText(newBasePath);
241 project.setBasePath(newBasePath);
246 * Pops up the file manager and lets the user edit the parameters for the
247 * physical and virtual files.
249 private void editFiles() {
250 FileManager fileManager = new FileManager(swingInterface, project);
251 fileManager.setVisible(true);
255 // INTERFACE I18nable
261 public void updateI18n() {
262 nameLabel.updateI18n();
263 descriptionLabel.updateI18n();
264 basePathLabel.updateI18n();
265 changeBasePathAction.updateI18n();
266 editFilesAction.updateI18n();
270 // INTERFACE DocumentListener
276 public void changedUpdate(DocumentEvent documentEvent) {
277 textFieldsUpdated(documentEvent.getDocument());
283 public void insertUpdate(DocumentEvent documentEvent) {
284 textFieldsUpdated(documentEvent.getDocument());
290 public void removeUpdate(DocumentEvent documentEvent) {
291 textFieldsUpdated(documentEvent.getDocument());