rename edit node dialog
[jSite2.git] / src / net / pterodactylus / jsite / gui / AddNodeDialog.java
diff --git a/src/net/pterodactylus/jsite/gui/AddNodeDialog.java b/src/net/pterodactylus/jsite/gui/AddNodeDialog.java
new file mode 100644 (file)
index 0000000..cb85b24
--- /dev/null
@@ -0,0 +1,300 @@
+/*
+ * jSite2 - NodeEditDialog.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+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 javax.swing.BorderFactory;
+import javax.swing.JButton;
+import javax.swing.JDialog;
+import javax.swing.JFrame;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+import javax.swing.border.EtchedBorder;
+
+import net.pterodactylus.jsite.core.Verifier;
+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.main.Version;
+import net.pterodactylus.util.swing.SwingUtils;
+
+/**
+ * Dialog that lets the user edit the properties of a node.
+ * 
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+ */
+public class AddNodeDialog extends JDialog implements I18nable {
+
+       /** The user-given name of the node. */
+       private String name;
+
+       /** The hostname of the node. */
+       private String hostname;
+
+       /** The FNP port number of the node. */
+       private int port;
+
+       /** Action of the okay button. */
+       private I18nAction okayAction;
+
+       /** Action of the cancel button. */
+       private I18nAction cancelAction;
+
+       /** The name label. */
+       private I18nLabel nameLabel;
+
+       /** The name textfield. */
+       private JTextField nameTextField;
+
+       /** The hostname label. */
+       private I18nLabel hostnameLabel;
+
+       /** The hostname textfield. */
+       private JTextField hostnameTextField;
+
+       /** The port label. */
+       private I18nLabel portLabel;
+
+       /** The port textfield. */
+       private JTextField portTextField;
+
+       /** Whether the dialog was cancelled. */
+       private boolean cancelled;
+
+       /**
+        * Creates a new node edit dialog with the given parent.
+        * 
+        * @param parentFrame
+        *            The parent frame of this dialog
+        */
+       public AddNodeDialog(JFrame parentFrame) {
+               super(parentFrame, I18n.get("addNodeDialog.title") + " – jSite " + Version.getVersion(), true);
+               initActions();
+               initComponents();
+               pack();
+               setSize(getWidth() * 2, getHeight());
+               I18n.registerI18nable(this);
+               SwingUtils.center(this);
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns the user-given name of the node.
+        * 
+        * @return The user-given name of the node
+        */
+       public String getNodeName() {
+               return name;
+       }
+
+       /**
+        * Sets the user-given name of the node.
+        * 
+        * @param name
+        *            The name of the node
+        */
+       public void setNodeName(String name) {
+               this.name = name;
+               nameTextField.setText(name);
+       }
+
+       /**
+        * Returns the hostname of the node.
+        * 
+        * @return The hostname of the node
+        */
+       public String getNodeHostname() {
+               return hostname;
+       }
+
+       /**
+        * Sets the hostname of the node.
+        * 
+        * @param hostname
+        *            The hostname of the node
+        */
+       public void setNodeHostname(String hostname) {
+               this.hostname = hostname;
+               hostnameTextField.setText(hostname);
+       }
+
+       /**
+        * Returns the FCP port number of the node.
+        * 
+        * @return The FCP port number of the node
+        */
+       public int getNodePort() {
+               return port;
+       }
+
+       /**
+        * Sets the FCP port number of the node.
+        * 
+        * @param port
+        *            The FCP port number of the node
+        */
+       public void setNodePort(int port) {
+               this.port = port;
+               portTextField.setText(String.valueOf(port));
+       }
+
+       /**
+        * Returns whether the dialog was cancelled.
+        * 
+        * @return <code>true</code> if the dialog was cancelled,
+        *         <code>false</code> if the user clicked “okay”
+        */
+       public boolean wasCancelled() {
+               return cancelled;
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Initializes all actions.
+        */
+       private void initActions() {
+               okayAction = new I18nAction("general.button.okay") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent e) {
+                               confirm();
+                       }
+               };
+               cancelAction = new I18nAction("general.button.cancel") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent e) {
+                               cancel();
+                       }
+               };
+       }
+
+       /**
+        * Initializes all components.
+        */
+       private void initComponents() {
+               JPanel rootPanel = new JPanel(new BorderLayout(12, 12));
+               setContentPane(rootPanel);
+               rootPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
+
+               JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
+               rootPanel.add(buttonPanel, BorderLayout.PAGE_END);
+               buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
+               buttonPanel.add(new JButton(cancelAction));
+               JButton okayButton = new JButton(okayAction);
+               buttonPanel.add(okayButton);
+               getRootPane().setDefaultButton(okayButton);
+
+               JPanel contentPanel = new JPanel(new GridBagLayout());
+               rootPanel.add(contentPanel, BorderLayout.CENTER);
+               contentPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(12, 12, 12, 12)));
+
+               nameTextField = new JTextField();
+               contentPanel.add(nameLabel = new I18nLabel("addNodeDialog.label.name", nameTextField), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
+               contentPanel.add(nameTextField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 12, 0, 0), 0, 0));
+
+               hostnameTextField = new JTextField();
+               contentPanel.add(hostnameLabel = new I18nLabel("addNodeDialog.label.hostname", hostnameTextField), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
+               contentPanel.add(hostnameTextField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
+
+               portTextField = new JTextField();
+               contentPanel.add(portLabel = new I18nLabel("addNodeDialog.label.port", portTextField), new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0));
+               contentPanel.add(portTextField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0));
+
+               contentPanel.add(new JPanel(), new GridBagConstraints(0, 3, 2, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
+       }
+
+       //
+       // PRIVATE ACTIONS
+       //
+
+       /**
+        * Confirms the node settings and closes the dialog.
+        */
+       private void confirm() {
+               if (!Verifier.verifyNodeName(nameTextField.getText())) {
+                       JOptionPane.showMessageDialog(this, I18n.get("addNodeDialog.error.name.message"), I18n.get("addNodeDialog.error.name.title"), JOptionPane.ERROR_MESSAGE);
+                       return;
+               }
+               if (!Verifier.verifyHostname(hostnameTextField.getText())) {
+                       JOptionPane.showMessageDialog(this, I18n.get("addNodeDialog.error.hostname.message"), I18n.get("addNodeDialog.error.hostname.title"), JOptionPane.ERROR_MESSAGE);
+                       return;
+               }
+               if (!Verifier.verifyPort(portTextField.getText())) {
+                       JOptionPane.showMessageDialog(this, I18n.get("addNodeDialog.error.port.message"), I18n.get("addNodeDialog.error.port.title"), JOptionPane.ERROR_MESSAGE);
+                       return;
+               }
+               name = nameTextField.getText().trim();
+               hostname = hostnameTextField.getText().trim();
+               try {
+                       port = Integer.parseInt(portTextField.getText().trim());
+               } catch (NumberFormatException nfe1) {
+                       /* should not occur, the value was checked! */
+                       assert false: "port number is invalid though it was checked!";
+               }
+               cancelled = false;
+               setVisible(false);
+       }
+
+       /**
+        * Cancels the node settings and closes the dialog.
+        */
+       private void cancel() {
+               cancelled = true;
+               setVisible(false);
+       }
+
+       //
+       // INTERFACE I18nable
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public void updateI18n() {
+               okayAction.updateI18n();
+               cancelAction.updateI18n();
+               nameLabel.updateI18n();
+               hostnameLabel.updateI18n();
+               portLabel.updateI18n();
+               setTitle(I18n.get("addNodeDialog.title") + " – jSite " + Version.getVersion());
+       }
+
+}