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.AbstractAction;
32 import javax.swing.Action;
33 import javax.swing.BorderFactory;
34 import javax.swing.JButton;
35 import javax.swing.JComboBox;
36 import javax.swing.JFileChooser;
37 import javax.swing.JPanel;
38 import javax.swing.JTextField;
39 import javax.swing.event.DocumentEvent;
40 import javax.swing.event.DocumentListener;
41 import javax.swing.text.Document;
43 import net.pterodactylus.jsite.core.Project;
44 import net.pterodactylus.jsite.i18n.I18n;
45 import net.pterodactylus.jsite.i18n.I18nable;
46 import net.pterodactylus.jsite.i18n.gui.I18nAction;
47 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
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 “node” label. */
93 private I18nLabel nodeLabel;
95 /** The “node” action. */
96 private Action nodeAction;
98 /** The “node” combo box. */
99 private JComboBox nodeComboBox;
102 * Creates a new project panel.
104 * @param swingInterface
105 * The Swing interface
107 * The project to display
109 public ProjectPanel(SwingInterface swingInterface, Project project) {
110 super(new BorderLayout(12, 12));
111 logger.log(Level.FINEST, "project: " + project);
112 this.swingInterface = swingInterface;
113 this.project = project;
123 * Returns the project that is displayed in this panel.
125 * @return The project of this panel
127 public Project getProject() {
136 * Initializes all actions.
138 private void initActions() {
139 changeBasePathAction = new I18nAction("projectPanel.button.changeBasePath") {
144 @SuppressWarnings("synthetic-access")
145 public void actionPerformed(ActionEvent actionEvent) {
149 editFilesAction = new I18nAction("projectPanel.button.editFiles") {
154 @SuppressWarnings("synthetic-access")
155 public void actionPerformed(ActionEvent actionEvent) {
159 nodeAction = new AbstractAction() {
162 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
164 public void actionPerformed(ActionEvent actionEvent) {
171 * Initializes all components of the panel.
173 private void initComponents() {
174 setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
176 JPanel propertiesPanel = createPropertiesPanel();
177 add(propertiesPanel, BorderLayout.CENTER);
179 JPanel buttonPanel = createButtonPanel();
180 add(buttonPanel, BorderLayout.PAGE_END);
184 * Creates the properties panel.
186 * @return The properties panel
188 private JPanel createPropertiesPanel() {
189 JPanel propertiesPanel = new JPanel(new GridBagLayout());
191 nameTextField = new JTextField(project.getName());
192 nameTextField.getDocument().addDocumentListener(this);
193 nameLabel = new I18nLabel("projectPanel.label.name", nameTextField);
194 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));
195 propertiesPanel.add(nameTextField, new GridBagConstraints(1, 0, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 12, 0, 0), 0, 0));
197 descriptionTextField = new JTextField(project.getDescription());
198 descriptionTextField.getDocument().addDocumentListener(this);
199 descriptionLabel = new I18nLabel("projectPanel.label.description", descriptionTextField);
200 propertiesPanel.add(descriptionLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 0, 0, 0), 0, 0));
201 propertiesPanel.add(descriptionTextField, new GridBagConstraints(1, 1, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
203 basePathTextField = new JTextField(project.getBasePath());
204 basePathTextField.setEditable(false);
205 basePathLabel = new I18nLabel("projectPanel.label.basePath");
206 JButton changeBasePathButton = new JButton(changeBasePathAction);
207 JButton editFilesButton = new JButton(editFilesAction);
208 propertiesPanel.add(basePathLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 0, 0, 0), 0, 0));
209 propertiesPanel.add(basePathTextField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
210 propertiesPanel.add(changeBasePathButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
211 propertiesPanel.add(editFilesButton, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
213 nodeComboBox = new JComboBox();
214 nodeComboBox.addActionListener(nodeAction);
215 nodeLabel = new I18nLabel("projectPanel.label.node", nodeComboBox);
216 propertiesPanel.add(nodeLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 0, 0, 0), 0, 0));
217 propertiesPanel.add(nodeComboBox, new GridBagConstraints(1, 3, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(12, 12, 0, 0), 0, 0));
219 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));
221 return propertiesPanel;
225 * Creates the button panel.
227 * @return The button panel
229 private JPanel createButtonPanel() {
230 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
231 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
233 buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction(project)));
234 buttonPanel.add(new JButton(swingInterface.getCloneProjectAction(project)));
240 * Updates the project from changes in the text fields.
243 * The document that was changed
245 private void textFieldsUpdated(Document document) {
246 if (nameTextField.getDocument() == document) {
247 project.setName(nameTextField.getText());
248 } else if (descriptionTextField.getDocument() == document) {
249 project.setDescription(descriptionTextField.getText());
258 * Lets the user select a new base path and repopulates the file list.
260 void changeBasePath() {
261 JFileChooser fileChooser = new JFileChooser(project.getBasePath());
262 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
263 fileChooser.setApproveButtonText(I18n.get("projectPanel.button.approve.name"));
264 int chooseAction = fileChooser.showOpenDialog(this);
265 if (chooseAction == JFileChooser.APPROVE_OPTION) {
266 String newBasePath = fileChooser.getSelectedFile().getPath();
267 basePathTextField.setText(newBasePath);
268 project.setBasePath(newBasePath);
273 * Pops up the file manager and lets the user edit the parameters for the
274 * physical and virtual files.
276 private void editFiles() {
277 FileManager fileManager = new FileManager(swingInterface, project);
278 fileManager.setVisible(true);
282 // INTERFACE I18nable
288 public void updateI18n() {
289 nameLabel.updateI18n();
290 descriptionLabel.updateI18n();
291 basePathLabel.updateI18n();
292 changeBasePathAction.updateI18n();
293 editFilesAction.updateI18n();
297 // INTERFACE DocumentListener
303 public void changedUpdate(DocumentEvent documentEvent) {
304 textFieldsUpdated(documentEvent.getDocument());
310 public void insertUpdate(DocumentEvent documentEvent) {
311 textFieldsUpdated(documentEvent.getDocument());
317 public void removeUpdate(DocumentEvent documentEvent) {
318 textFieldsUpdated(documentEvent.getDocument());