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