update project when textfields are updated
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 17 May 2008 18:20:24 +0000 (18:20 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 17 May 2008 18:20:24 +0000 (18:20 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@914 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/jsite/gui/ProjectPanel.java

index 13ff716..09060ea 100644 (file)
@@ -21,22 +21,36 @@ package net.pterodactylus.jsite.gui;
 
 import java.awt.BorderLayout;
 import java.awt.FlowLayout;
+import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
+import java.awt.Insets;
+import java.util.logging.Logger;
 
 import javax.swing.BorderFactory;
 import javax.swing.JButton;
 import javax.swing.JPanel;
+import javax.swing.JTextField;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
+import javax.swing.text.Document;
 
 import net.pterodactylus.jsite.core.Project;
+import net.pterodactylus.jsite.i18n.I18nable;
+import net.pterodactylus.jsite.i18n.gui.I18nLabel;
+import net.pterodactylus.util.logging.Logging;
 
 /**
  * A panel that contains all information about a project and lets the user edit
  * the properties of the project.
- *
+ * 
  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
  * @version $Id$
  */
-public class ProjectPanel extends JPanel {
+public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
+
+       /** Logger. */
+       @SuppressWarnings("unused")
+       private static final Logger logger = Logging.getLogger(ProjectPanel.class.getName());
 
        /** The Swing interface. */
        private final SwingInterface swingInterface;
@@ -44,9 +58,21 @@ public class ProjectPanel extends JPanel {
        /** The project to show. */
        private final Project project;
 
+       /** The “name” label. */
+       private I18nLabel nameLabel;
+
+       /** The “name” textfield. */
+       private JTextField nameTextField;
+
+       /** The “description” label. */
+       private I18nLabel descriptionLabel;
+
+       /** The “description” textfield. */
+       private JTextField descriptionTextField;
+
        /**
         * Creates a new project panel.
-        *
+        * 
         * @param swingInterface
         *            The Swing interface
         * @param project
@@ -65,7 +91,7 @@ public class ProjectPanel extends JPanel {
 
        /**
         * Returns the project that is displayed in this panel.
-        *
+        * 
         * @return The project of this panel
         */
        public Project getProject() {
@@ -91,18 +117,30 @@ public class ProjectPanel extends JPanel {
 
        /**
         * Creates the properties panel.
-        *
+        * 
         * @return The properties panel
         */
        private JPanel createPropertiesPanel() {
                JPanel propertiesPanel = new JPanel(new GridBagLayout());
 
+               nameTextField = new JTextField(project.getName());
+               nameTextField.getDocument().addDocumentListener(this);
+               nameLabel = new I18nLabel("projectPanel.label.name", nameTextField);
+               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));
+               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));
+
+               descriptionTextField = new JTextField(project.getDescription());
+               descriptionTextField.getDocument().addDocumentListener(this);
+               descriptionLabel = new I18nLabel("projectPanel.label.description", descriptionTextField);
+               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));
+               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));
+
                return propertiesPanel;
        }
 
        /**
         * Creates the button panel.
-        *
+        * 
         * @return The button panel
         */
        private JPanel createButtonPanel() {
@@ -115,4 +153,55 @@ public class ProjectPanel extends JPanel {
                return buttonPanel;
        }
 
+       /**
+        * Updates the project from changes in the text fields.
+        * 
+        * @param document
+        *            The document that was changed
+        */
+       private void textFieldsUpdated(Document document) {
+               if (nameTextField.getDocument() == document) {
+                       project.setName(nameTextField.getText());
+               } else if (descriptionTextField.getDocument() == document) {
+                       project.setDescription(descriptionTextField.getText());
+               }
+       }
+
+       //
+       // INTERFACE I18nable
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public void updateI18n() {
+               nameLabel.updateI18n();
+               descriptionLabel.updateI18n();
+       }
+
+       //
+       // INTERFACE DocumentListener
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public void changedUpdate(DocumentEvent documentEvent) {
+               textFieldsUpdated(documentEvent.getDocument());
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void insertUpdate(DocumentEvent documentEvent) {
+               textFieldsUpdated(documentEvent.getDocument());
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void removeUpdate(DocumentEvent documentEvent) {
+               textFieldsUpdated(documentEvent.getDocument());
+       }
+
 }