initialize base path information label
[jSite2.git] / src / net / pterodactylus / jsite / gui / ProjectPanel.java
index c16f2a5..cf53482 100644 (file)
 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.awt.event.ActionEvent;
+import java.util.List;
+import java.util.logging.Logger;
 
 import javax.swing.BorderFactory;
+import javax.swing.JButton;
+import javax.swing.JFileChooser;
+import javax.swing.JLabel;
 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.i18n.I18n;
+import net.pterodactylus.jsite.i18n.I18nable;
+import net.pterodactylus.jsite.i18n.gui.I18nAction;
+import net.pterodactylus.jsite.i18n.gui.I18nLabel;
+import net.pterodactylus.jsite.project.Entry;
+import net.pterodactylus.jsite.project.Project;
+import net.pterodactylus.util.logging.Logging;
 
 /**
  * A panel that contains all information about a project and lets the user edit
@@ -31,26 +53,237 @@ import javax.swing.JPanel;
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  * @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;
+
+       /** The project to show. */
+       private final Project project;
+
+       /** The “change base path” action. */
+       private I18nAction changeBasePathAction;
+
+       /** The “name” label. */
+       private I18nLabel nameLabel;
+
+       /** The “name” textfield. */
+       private JTextField nameTextField;
+
+       /** The “description” label. */
+       private I18nLabel descriptionLabel;
+
+       /** The “description” textfield. */
+       private JTextField descriptionTextField;
+
+       /** The “base path” label. */
+       private I18nLabel basePathLabel;
+
+       /** The “base path” textfield. */
+       private JTextField basePathTextField;
+
+       /** The “base path information” label. */
+       private JLabel basePathInformationLabel;
 
        /**
         * Creates a new project panel.
+        * 
+        * @param swingInterface
+        *            The Swing interface
+        * @param project
+        *            The project to display
         */
-       public ProjectPanel() {
+       public ProjectPanel(SwingInterface swingInterface, Project project) {
                super(new BorderLayout(12, 12));
+               this.swingInterface = swingInterface;
+               this.project = project;
+               initActions();
                initComponents();
        }
 
        //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns the project that is displayed in this panel.
+        * 
+        * @return The project of this panel
+        */
+       public Project getProject() {
+               return project;
+       }
+
+       //
        // PRIVATE METHODS
        //
 
        /**
+        * Initializes all actions.
+        */
+       private void initActions() {
+               changeBasePathAction = new I18nAction("projectPanel.button.changeBasePath") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               changeBasePath();
+                       }
+               };
+       }
+
+       /**
         * Initializes all components of the panel.
         */
        private void initComponents() {
                setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
-               
+
+               JPanel propertiesPanel = createPropertiesPanel();
+               add(propertiesPanel, BorderLayout.CENTER);
+
+               JPanel buttonPanel = createButtonPanel();
+               add(buttonPanel, BorderLayout.PAGE_END);
+       }
+
+       /**
+        * 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, 2, 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, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
+
+               basePathTextField = new JTextField(project.getBasePath());
+               basePathTextField.setEditable(false);
+               basePathLabel = new I18nLabel("projectPanel.label.basePath");
+               basePathInformationLabel = new JLabel(I18n.get("projectPanel.basePathInformation.fileCount", project.getBasePathEntries().size()));
+               JButton changeBasePathButton = new JButton(changeBasePathAction);
+               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));
+               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));
+               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));
+               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));
+
+               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));
+
+               return propertiesPanel;
+       }
+
+       /**
+        * Creates the button panel.
+        * 
+        * @return The button panel
+        */
+       private JPanel createButtonPanel() {
+               JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
+               buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
+
+               buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction()));
+               buttonPanel.add(new JButton(swingInterface.getCloneProjectAction()));
+
+               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());
+               }
+       }
+
+       //
+       // PRIVATE ACTIONS
+       //
+
+       /**
+        * Lets the user select a new base path and repopulates the file list.
+        */
+       private void changeBasePath() {
+               JFileChooser fileChooser = new JFileChooser(project.getBasePath());
+               fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
+               fileChooser.setApproveButtonText(I18n.get("projectPanel.button.approve.name"));
+               int chooseAction = fileChooser.showOpenDialog(this);
+               if (chooseAction == JFileChooser.APPROVE_OPTION) {
+                       String newBasePath = fileChooser.getSelectedFile().getPath();
+                       basePathTextField.setText(newBasePath);
+                       basePathInformationLabel.setText(I18n.get("projectPanel.basePathInformation.scanning"));
+                       project.setBasePath(newBasePath);
+                       changeBasePathAction.setEnabled(false);
+                       swingInterface.getThreadPool().execute(new Runnable() {
+
+                               /**
+                                * {@inheritDoc}
+                                */
+                               @SuppressWarnings("synthetic-access")
+                               public void run() {
+                                       project.rescanBasePath();
+                                       List<Entry> projectEntries = project.getBasePathEntries();
+                                       basePathInformationLabel.setText(I18n.get("projectPanel.basePathInformation.fileCount", projectEntries.size()));
+                                       changeBasePathAction.setEnabled(true);
+                               }
+                       });
+               }
+       }
+
+       //
+       // 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());
        }
 
 }