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.Logger;
31 import javax.swing.BorderFactory;
32 import javax.swing.JButton;
33 import javax.swing.JFileChooser;
34 import javax.swing.JLabel;
35 import javax.swing.JPanel;
36 import javax.swing.JTextField;
37 import javax.swing.event.DocumentEvent;
38 import javax.swing.event.DocumentListener;
39 import javax.swing.text.Document;
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.jsite.project.Entry;
46 import net.pterodactylus.jsite.project.Project;
47 import net.pterodactylus.util.logging.Logging;
50 * A panel that contains all information about a project and lets the user edit
51 * the properties of the project.
53 * @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 “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;
89 /** The “base path information” label. */
90 private JLabel basePathInformationLabel;
93 * Creates a new project panel.
95 * @param swingInterface
98 * The project to display
100 public ProjectPanel(SwingInterface swingInterface, Project project) {
101 super(new BorderLayout(12, 12));
102 this.swingInterface = swingInterface;
103 this.project = project;
113 * Returns the project that is displayed in this panel.
115 * @return The project of this panel
117 public Project getProject() {
126 * Initializes all actions.
128 private void initActions() {
129 changeBasePathAction = new I18nAction("projectPanel.button.changeBasePath") {
134 @SuppressWarnings("synthetic-access")
135 public void actionPerformed(ActionEvent actionEvent) {
142 * Initializes all components of the panel.
144 private void initComponents() {
145 setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
147 JPanel propertiesPanel = createPropertiesPanel();
148 add(propertiesPanel, BorderLayout.CENTER);
150 JPanel buttonPanel = createButtonPanel();
151 add(buttonPanel, BorderLayout.PAGE_END);
155 * Creates the properties panel.
157 * @return The properties panel
159 private JPanel createPropertiesPanel() {
160 JPanel propertiesPanel = new JPanel(new GridBagLayout());
162 nameTextField = new JTextField(project.getName());
163 nameTextField.getDocument().addDocumentListener(this);
164 nameLabel = new I18nLabel("projectPanel.label.name", nameTextField);
165 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));
166 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));
168 descriptionTextField = new JTextField(project.getDescription());
169 descriptionTextField.getDocument().addDocumentListener(this);
170 descriptionLabel = new I18nLabel("projectPanel.label.description", descriptionTextField);
171 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));
172 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));
174 basePathTextField = new JTextField(project.getBasePath());
175 basePathTextField.setEditable(false);
176 basePathLabel = new I18nLabel("projectPanel.label.basePath");
177 basePathInformationLabel = new JLabel(I18n.get("projectPanel.basePathInformation.fileCount", project.getBasePathEntries().size()));
178 JButton changeBasePathButton = new JButton(changeBasePathAction);
179 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));
180 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));
181 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));
182 propertiesPanel.add(basePathInformationLabel, new GridBagConstraints(1, 3, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0, 0));
184 propertiesPanel.add(new JPanel(), new GridBagConstraints(0, 4, 3, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
186 return propertiesPanel;
190 * Creates the button panel.
192 * @return The button panel
194 private JPanel createButtonPanel() {
195 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
196 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
198 buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction()));
199 buttonPanel.add(new JButton(swingInterface.getCloneProjectAction()));
205 * Updates the project from changes in the text fields.
208 * The document that was changed
210 private void textFieldsUpdated(Document document) {
211 if (nameTextField.getDocument() == document) {
212 project.setName(nameTextField.getText());
213 } else if (descriptionTextField.getDocument() == document) {
214 project.setDescription(descriptionTextField.getText());
223 * Lets the user select a new base path and repopulates the file list.
225 private void changeBasePath() {
226 JFileChooser fileChooser = new JFileChooser(project.getBasePath());
227 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
228 fileChooser.setApproveButtonText(I18n.get("projectPanel.button.approve.name"));
229 int chooseAction = fileChooser.showOpenDialog(this);
230 if (chooseAction == JFileChooser.APPROVE_OPTION) {
231 String newBasePath = fileChooser.getSelectedFile().getPath();
232 basePathTextField.setText(newBasePath);
233 basePathInformationLabel.setText(I18n.get("projectPanel.basePathInformation.scanning"));
234 project.setBasePath(newBasePath);
235 changeBasePathAction.setEnabled(false);
236 swingInterface.getThreadPool().execute(new Runnable() {
241 @SuppressWarnings("synthetic-access")
243 project.rescanBasePath();
244 List<Entry> projectEntries = project.getBasePathEntries();
245 basePathInformationLabel.setText(I18n.get("projectPanel.basePathInformation.fileCount", projectEntries.size()));
246 changeBasePathAction.setEnabled(true);
253 // INTERFACE I18nable
259 public void updateI18n() {
260 nameLabel.updateI18n();
261 descriptionLabel.updateI18n();
265 // INTERFACE DocumentListener
271 public void changedUpdate(DocumentEvent documentEvent) {
272 textFieldsUpdated(documentEvent.getDocument());
278 public void insertUpdate(DocumentEvent documentEvent) {
279 textFieldsUpdated(documentEvent.getDocument());
285 public void removeUpdate(DocumentEvent documentEvent) {
286 textFieldsUpdated(documentEvent.getDocument());