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