Show “Insert Project” button on project panel.
[jSite2.git] / src / net / pterodactylus / jsite / gui / ProjectPanel.java
index d009471..5037094 100644 (file)
@@ -20,6 +20,7 @@
 package net.pterodactylus.jsite.gui;
 
 import java.awt.BorderLayout;
+import java.awt.Component;
 import java.awt.FlowLayout;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
@@ -28,15 +29,22 @@ import java.awt.event.ActionEvent;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import javax.swing.AbstractAction;
+import javax.swing.Action;
 import javax.swing.BorderFactory;
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.DefaultListCellRenderer;
 import javax.swing.JButton;
+import javax.swing.JComboBox;
 import javax.swing.JFileChooser;
+import javax.swing.JList;
 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.Node;
 import net.pterodactylus.jsite.core.Project;
 import net.pterodactylus.jsite.i18n.I18n;
 import net.pterodactylus.jsite.i18n.I18nable;
@@ -53,7 +61,6 @@ import net.pterodactylus.util.logging.Logging;
 public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
 
        /** Logger. */
-       @SuppressWarnings("unused")
        private static final Logger logger = Logging.getLogger(ProjectPanel.class.getName());
 
        /** The Swing interface. */
@@ -86,6 +93,15 @@ public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
        /** The “base path” textfield. */
        private JTextField basePathTextField;
 
+       /** The “node” label. */
+       private I18nLabel nodeLabel;
+
+       /** The “node” action. */
+       private Action nodeAction;
+
+       /** The “node” combo box. */
+       private JComboBox nodeComboBox;
+
        /**
         * Creates a new project panel.
         *
@@ -117,6 +133,31 @@ public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
        }
 
        //
+       // ACTIONS
+       //
+
+       /**
+        * Adds the given node to the node combo boxes in all {@link ProjectPanel}s.
+        *
+        * @param node
+        *            The node to add
+        */
+       public void addNode(Node node) {
+               ((DefaultComboBoxModel) nodeComboBox.getModel()).addElement(node);
+       }
+
+       /**
+        * Removes the given node from the node combo boxes in all
+        * {@link ProjectPanel}s.
+        *
+        * @param node
+        *            The node to remove
+        */
+       public void removeNode(Node node) {
+               ((DefaultComboBoxModel) nodeComboBox.getModel()).removeElement(node);
+       }
+
+       //
        // PRIVATE METHODS
        //
 
@@ -129,7 +170,6 @@ public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
                        /**
                         * {@inheritDoc}
                         */
-                       @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent actionEvent) {
                                changeBasePath();
                        }
@@ -144,6 +184,17 @@ public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
                                editFiles();
                        }
                };
+               nodeAction = new AbstractAction() {
+
+                       /**
+                        * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               Node node = (Node) nodeComboBox.getSelectedItem();
+                               project.setNode(node);
+                       }
+               };
        }
 
        /**
@@ -171,25 +222,37 @@ public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
                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, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 6, 0, 0), 0, 0));
+               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));
 
                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, 3, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
+               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));
+               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));
 
                basePathTextField = new JTextField(project.getBasePath());
                basePathTextField.setEditable(false);
                basePathLabel = new I18nLabel("projectPanel.label.basePath");
                JButton changeBasePathButton = new JButton(changeBasePathAction);
                JButton editFilesButton = new JButton(editFilesAction);
-               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(editFilesButton, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
+               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));
+               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));
+               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));
+               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));
+
+               nodeComboBox = new JComboBox(new DefaultComboBoxModel());
+               nodeComboBox.setRenderer(new NodeComboBoxCellRenderer());
+               ((DefaultComboBoxModel) nodeComboBox.getModel()).addElement(null);
+               for (Node node : swingInterface.getNodes()) {
+                       ((DefaultComboBoxModel) nodeComboBox.getModel()).addElement(node);
+               }
+               nodeComboBox.setSelectedItem(project.getNode());
+               nodeComboBox.addActionListener(nodeAction);
+               nodeLabel = new I18nLabel("projectPanel.label.node", nodeComboBox);
+               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));
+               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));
 
-               propertiesPanel.add(new JPanel(), new GridBagConstraints(0, 3, 4, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
+               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));
 
                return propertiesPanel;
        }
@@ -203,8 +266,9 @@ public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
                JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
                buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
 
-               buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction(project)));
+               buttonPanel.add(new JButton(swingInterface.getInsertProjectAction(project)));
                buttonPanel.add(new JButton(swingInterface.getCloneProjectAction(project)));
+               buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction(project)));
 
                return buttonPanel;
        }
@@ -291,4 +355,32 @@ public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
                textFieldsUpdated(documentEvent.getDocument());
        }
 
+       /**
+        * Cell cenderer for the node combo box.
+        *
+        * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+        */
+       private static class NodeComboBoxCellRenderer extends DefaultListCellRenderer {
+
+               /**
+                * Empty constructor.
+                */
+               public NodeComboBoxCellRenderer() {
+                       super();
+               }
+
+               /**
+                * @see javax.swing.DefaultListCellRenderer#getListCellRendererComponent(javax.swing.JList,
+                *      java.lang.Object, int, boolean, boolean)
+                */
+               @Override
+               public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
+                       if (value == null) {
+                               return super.getListCellRendererComponent(list, "\u00a0", index, isSelected, cellHasFocus);
+                       }
+                       return super.getListCellRendererComponent(list, ((Node) value).getName(), index, isSelected, cellHasFocus);
+               }
+
+       }
+
 }