move project directory
[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.Level;
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.JPanel;
35 import javax.swing.JTextField;
36 import javax.swing.event.DocumentEvent;
37 import javax.swing.event.DocumentListener;
38 import javax.swing.text.Document;
39
40 import net.pterodactylus.jsite.core.project.Project;
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.util.logging.Logging;
46
47 /**
48  * A panel that contains all information about a project and lets the user edit
49  * the properties of the project.
50  *
51  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
52  */
53 public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
54
55         /** Logger. */
56         @SuppressWarnings("unused")
57         private static final Logger logger = Logging.getLogger(ProjectPanel.class.getName());
58
59         /** The Swing interface. */
60         private final SwingInterface swingInterface;
61
62         /** The project to show. */
63         private final Project project;
64
65         /** The “change base path” action. */
66         private I18nAction changeBasePathAction;
67
68         /** The “edit files” action. */
69         private I18nAction editFilesAction;
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         /**
90          * Creates a new project panel.
91          *
92          * @param swingInterface
93          *            The Swing interface
94          * @param project
95          *            The project to display
96          */
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;
102                 initActions();
103                 initComponents();
104         }
105
106         //
107         // ACCESSORS
108         //
109
110         /**
111          * Returns the project that is displayed in this panel.
112          *
113          * @return The project of this panel
114          */
115         public Project getProject() {
116                 return project;
117         }
118
119         //
120         // PRIVATE METHODS
121         //
122
123         /**
124          * Initializes all actions.
125          */
126         private void initActions() {
127                 changeBasePathAction = new I18nAction("projectPanel.button.changeBasePath") {
128
129                         /**
130                          * {@inheritDoc}
131                          */
132                         @SuppressWarnings("synthetic-access")
133                         public void actionPerformed(ActionEvent actionEvent) {
134                                 changeBasePath();
135                         }
136                 };
137                 editFilesAction = new I18nAction("projectPanel.button.editFiles") {
138
139                         /**
140                          * {@inheritDoc}
141                          */
142                         @SuppressWarnings("synthetic-access")
143                         public void actionPerformed(ActionEvent actionEvent) {
144                                 editFiles();
145                         }
146                 };
147         }
148
149         /**
150          * Initializes all components of the panel.
151          */
152         private void initComponents() {
153                 setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
154
155                 JPanel propertiesPanel = createPropertiesPanel();
156                 add(propertiesPanel, BorderLayout.CENTER);
157
158                 JPanel buttonPanel = createButtonPanel();
159                 add(buttonPanel, BorderLayout.PAGE_END);
160         }
161
162         /**
163          * Creates the properties panel.
164          *
165          * @return The properties panel
166          */
167         private JPanel createPropertiesPanel() {
168                 JPanel propertiesPanel = new JPanel(new GridBagLayout());
169
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));
175
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));
181
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));
191
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));
193
194                 return propertiesPanel;
195         }
196
197         /**
198          * Creates the button panel.
199          *
200          * @return The button panel
201          */
202         private JPanel createButtonPanel() {
203                 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
204                 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
205
206                 buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction(project)));
207                 buttonPanel.add(new JButton(swingInterface.getCloneProjectAction(project)));
208
209                 return buttonPanel;
210         }
211
212         /**
213          * Updates the project from changes in the text fields.
214          *
215          * @param document
216          *            The document that was changed
217          */
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());
223                 }
224         }
225
226         //
227         // PRIVATE ACTIONS
228         //
229
230         /**
231          * Lets the user select a new base path and repopulates the file list.
232          */
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);
242                 }
243         }
244
245         /**
246          * Pops up the file manager and lets the user edit the parameters for the
247          * physical and virtual files.
248          */
249         private void editFiles() {
250                 FileManager fileManager = new FileManager(swingInterface, project);
251                 fileManager.setVisible(true);
252         }
253
254         //
255         // INTERFACE I18nable
256         //
257
258         /**
259          * {@inheritDoc}
260          */
261         public void updateI18n() {
262                 nameLabel.updateI18n();
263                 descriptionLabel.updateI18n();
264                 basePathLabel.updateI18n();
265                 changeBasePathAction.updateI18n();
266                 editFilesAction.updateI18n();
267         }
268
269         //
270         // INTERFACE DocumentListener
271         //
272
273         /**
274          * {@inheritDoc}
275          */
276         public void changedUpdate(DocumentEvent documentEvent) {
277                 textFieldsUpdated(documentEvent.getDocument());
278         }
279
280         /**
281          * {@inheritDoc}
282          */
283         public void insertUpdate(DocumentEvent documentEvent) {
284                 textFieldsUpdated(documentEvent.getDocument());
285         }
286
287         /**
288          * {@inheritDoc}
289          */
290         public void removeUpdate(DocumentEvent documentEvent) {
291                 textFieldsUpdated(documentEvent.getDocument());
292         }
293
294 }