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.Component;
24 import java.awt.FlowLayout;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import java.awt.event.ActionEvent;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
32 import javax.swing.AbstractAction;
33 import javax.swing.Action;
34 import javax.swing.BorderFactory;
35 import javax.swing.DefaultComboBoxModel;
36 import javax.swing.DefaultListCellRenderer;
37 import javax.swing.JButton;
38 import javax.swing.JComboBox;
39 import javax.swing.JFileChooser;
40 import javax.swing.JList;
41 import javax.swing.JPanel;
42 import javax.swing.JTextField;
43 import javax.swing.event.DocumentEvent;
44 import javax.swing.event.DocumentListener;
45 import javax.swing.text.Document;
47 import net.pterodactylus.jsite.core.Node;
48 import net.pterodactylus.jsite.core.Project;
49 import net.pterodactylus.jsite.i18n.I18n;
50 import net.pterodactylus.jsite.i18n.I18nable;
51 import net.pterodactylus.jsite.i18n.gui.I18nAction;
52 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
53 import net.pterodactylus.util.logging.Logging;
56 * A panel that contains all information about a project and lets the user edit
57 * the properties of the project.
59 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
61 public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
64 private static final Logger logger = Logging.getLogger(ProjectPanel.class.getName());
66 /** The Swing interface. */
67 private final SwingInterface swingInterface;
69 /** The project to show. */
70 private final Project project;
72 /** The “change base path” action. */
73 private I18nAction changeBasePathAction;
75 /** The “edit files” action. */
76 private I18nAction editFilesAction;
78 /** The “name” label. */
79 private I18nLabel nameLabel;
81 /** The “name” textfield. */
82 private JTextField nameTextField;
84 /** The “description” label. */
85 private I18nLabel descriptionLabel;
87 /** The “description” textfield. */
88 private JTextField descriptionTextField;
90 /** The “base path” label. */
91 private I18nLabel basePathLabel;
93 /** The “base path” textfield. */
94 private JTextField basePathTextField;
96 /** The “node” label. */
97 private I18nLabel nodeLabel;
99 /** The “node” action. */
100 private Action nodeAction;
102 /** The “node” combo box. */
103 private JComboBox nodeComboBox;
106 * Creates a new project panel.
108 * @param swingInterface
109 * The Swing interface
111 * The project to display
113 public ProjectPanel(SwingInterface swingInterface, Project project) {
114 super(new BorderLayout(12, 12));
115 logger.log(Level.FINEST, "project: " + project);
116 this.swingInterface = swingInterface;
117 this.project = project;
127 * Returns the project that is displayed in this panel.
129 * @return The project of this panel
131 public Project getProject() {
140 * Adds the given node to the node combo boxes in all {@link ProjectPanel}s.
145 public void addNode(Node node) {
146 ((DefaultComboBoxModel) nodeComboBox.getModel()).addElement(node);
150 * Removes the given node from the node combo boxes in all
151 * {@link ProjectPanel}s.
156 public void removeNode(Node node) {
157 ((DefaultComboBoxModel) nodeComboBox.getModel()).removeElement(node);
165 * Initializes all actions.
167 private void initActions() {
168 changeBasePathAction = new I18nAction("projectPanel.button.changeBasePath") {
173 public void actionPerformed(ActionEvent actionEvent) {
177 editFilesAction = new I18nAction("projectPanel.button.editFiles") {
182 @SuppressWarnings("synthetic-access")
183 public void actionPerformed(ActionEvent actionEvent) {
187 nodeAction = new AbstractAction() {
190 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
192 @SuppressWarnings("synthetic-access")
193 public void actionPerformed(ActionEvent actionEvent) {
194 Node node = (Node) nodeComboBox.getSelectedItem();
195 project.setNode(node);
201 * Initializes all components of the panel.
203 private void initComponents() {
204 setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
206 JPanel propertiesPanel = createPropertiesPanel();
207 add(propertiesPanel, BorderLayout.CENTER);
209 JPanel buttonPanel = createButtonPanel();
210 add(buttonPanel, BorderLayout.PAGE_END);
214 * Creates the properties panel.
216 * @return The properties panel
218 private JPanel createPropertiesPanel() {
219 JPanel propertiesPanel = new JPanel(new GridBagLayout());
221 nameTextField = new JTextField(project.getName());
222 nameTextField.getDocument().addDocumentListener(this);
223 nameLabel = new I18nLabel("projectPanel.label.name", nameTextField);
224 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));
225 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));
227 descriptionTextField = new JTextField(project.getDescription());
228 descriptionTextField.getDocument().addDocumentListener(this);
229 descriptionLabel = new I18nLabel("projectPanel.label.description", descriptionTextField);
230 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));
231 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));
233 basePathTextField = new JTextField(project.getBasePath());
234 basePathTextField.setEditable(false);
235 basePathLabel = new I18nLabel("projectPanel.label.basePath");
236 JButton changeBasePathButton = new JButton(changeBasePathAction);
237 JButton editFilesButton = new JButton(editFilesAction);
238 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));
239 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));
240 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));
241 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));
243 nodeComboBox = new JComboBox(new DefaultComboBoxModel());
244 nodeComboBox.setRenderer(new NodeComboBoxCellRenderer());
245 ((DefaultComboBoxModel) nodeComboBox.getModel()).addElement(null);
246 for (Node node : swingInterface.getNodes()) {
247 ((DefaultComboBoxModel) nodeComboBox.getModel()).addElement(node);
249 nodeComboBox.setSelectedItem(project.getNode());
250 nodeComboBox.addActionListener(nodeAction);
251 nodeLabel = new I18nLabel("projectPanel.label.node", nodeComboBox);
252 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));
253 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));
255 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));
257 return propertiesPanel;
261 * Creates the button panel.
263 * @return The button panel
265 private JPanel createButtonPanel() {
266 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
267 buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
269 buttonPanel.add(new JButton(swingInterface.getInsertProjectAction(project)));
270 buttonPanel.add(new JButton(swingInterface.getCloneProjectAction(project)));
271 buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction(project)));
277 * Updates the project from changes in the text fields.
280 * The document that was changed
282 private void textFieldsUpdated(Document document) {
283 if (nameTextField.getDocument() == document) {
284 project.setName(nameTextField.getText());
285 } else if (descriptionTextField.getDocument() == document) {
286 project.setDescription(descriptionTextField.getText());
295 * Lets the user select a new base path and repopulates the file list.
297 void changeBasePath() {
298 JFileChooser fileChooser = new JFileChooser(project.getBasePath());
299 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
300 fileChooser.setApproveButtonText(I18n.get("projectPanel.button.approve.name"));
301 int chooseAction = fileChooser.showOpenDialog(this);
302 if (chooseAction == JFileChooser.APPROVE_OPTION) {
303 String newBasePath = fileChooser.getSelectedFile().getPath();
304 basePathTextField.setText(newBasePath);
305 project.setBasePath(newBasePath);
310 * Pops up the file manager and lets the user edit the parameters for the
311 * physical and virtual files.
313 private void editFiles() {
314 FileManager fileManager = new FileManager(swingInterface, project);
315 fileManager.setVisible(true);
319 // INTERFACE I18nable
325 public void updateI18n() {
326 nameLabel.updateI18n();
327 descriptionLabel.updateI18n();
328 basePathLabel.updateI18n();
329 changeBasePathAction.updateI18n();
330 editFilesAction.updateI18n();
334 // INTERFACE DocumentListener
340 public void changedUpdate(DocumentEvent documentEvent) {
341 textFieldsUpdated(documentEvent.getDocument());
347 public void insertUpdate(DocumentEvent documentEvent) {
348 textFieldsUpdated(documentEvent.getDocument());
354 public void removeUpdate(DocumentEvent documentEvent) {
355 textFieldsUpdated(documentEvent.getDocument());
359 * Cell cenderer for the node combo box.
361 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
363 private static class NodeComboBoxCellRenderer extends DefaultListCellRenderer {
368 public NodeComboBoxCellRenderer() {
373 * @see javax.swing.DefaultListCellRenderer#getListCellRendererComponent(javax.swing.JList,
374 * java.lang.Object, int, boolean, boolean)
377 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
379 return super.getListCellRendererComponent(list, "\u00a0", index, isSelected, cellHasFocus);
381 return super.getListCellRendererComponent(list, ((Node) value).getName(), index, isSelected, cellHasFocus);