From: David ‘Bombe’ Roden Date: Fri, 4 Apr 2008 06:57:52 +0000 (+0000) Subject: dialog for editing node settings X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=6223be7f836ab24df6dc48dd755c9ae550b23fdf;p=jSite2.git dialog for editing node settings git-svn-id: http://trooper/svn/projects/jSite/trunk@585 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/src/net/pterodactylus/jsite/gui/EditNodeDialog.java b/src/net/pterodactylus/jsite/gui/EditNodeDialog.java new file mode 100644 index 0000000..2ad2b4b --- /dev/null +++ b/src/net/pterodactylus/jsite/gui/EditNodeDialog.java @@ -0,0 +1,374 @@ +/* + * 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 java.net.InetAddress; +import java.net.UnknownHostException; + +import javax.swing.Action; +import javax.swing.BorderFactory; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.border.EtchedBorder; + +import net.pterodactylus.jsite.i18n.I18n; +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> + * @version $Id$ + */ +public class EditNodeDialog extends JDialog { + + /** 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; + + /** Whether the node is on the same machine. */ + private boolean sameMachine; + + /** Action of the okay button. */ + private Action okayAction; + + /** Action of the cancel button. */ + private Action cancelAction; + + /** The name textfield. */ + private JTextField nameTextField; + + /** The hostname textfield. */ + private JTextField hostnameTextField; + + /** The port textfield. */ + private JTextField portTextField; + + /** The same machine checkbox. */ + private JCheckBox sameMachineCheckBox; + + /** Whether the dialog was cancelled. */ + private boolean cancelled; + + /** + * Creates a new node edit dialog with the given parent. + * + * @param parentDialog + * The parent dialog of this dialog + */ + public EditNodeDialog(JDialog parentDialog) { + super(parentDialog, I18n.get("editNodeDialog.title") + " – jSite " + Version.getVersion(), true); + initActions(); + initComponents(); + pack(); + 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 node is on the same machine as jSite. + * + * @return true if the node is on the same machine as jSite, + * false otherwise + */ + public boolean isNodeOnSameMachine() { + return sameMachine; + } + + /** + * Sets whether the node is on the same machine as jSite. + * + * @param sameMachine + * true if the node is on the same machine as + * jSite, false otherwise + */ + public void setNodeOnSameMachine(boolean sameMachine) { + this.sameMachine = sameMachine; + sameMachineCheckBox.setSelected(sameMachine); + } + + /** + * Returns whether the dialog was cancelled. + * + * @return true if the dialog was cancelled, + * false 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(createLabel(I18n.get("editNodeDialog.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, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 12, 0, 0), 0, 0)); + + hostnameTextField = new JTextField(); + contentPanel.add(createLabel(I18n.get("editNodeDialog.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, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0)); + + portTextField = new JTextField(); + contentPanel.add(createLabel(I18n.get("editNodeDialog.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, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 0, 0), 0, 0)); + + sameMachineCheckBox = new JCheckBox(new I18nAction("editNodeDialog.checkbox.sameMachine") { + + public void actionPerformed(ActionEvent e) { + /* don't do anything. */ + } + }); + contentPanel.add(sameMachineCheckBox, new GridBagConstraints(0, 3, 2, 1, 1, 1, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(12, 0, 0, 0), 0, 0)); + } + + /** + * Creates a label whose name and mnemonic are initialized from the given + * i18n property. + * + * @param i18nBasename + * The i18n property basename of the label + * @param labelFor + * The component this label describes + * @return The created label + */ + private JLabel createLabel(String i18nBasename, JComponent labelFor) { + JLabel label = new JLabel(I18n.get(i18nBasename + ".name")); + label.setDisplayedMnemonic(I18n.getKey(i18nBasename + ".mnemonic")); + label.setLabelFor(labelFor); + return label; + } + + // + // PRIVATE ACTIONS + // + + /** + * Checks the name textfield for valid input. + * + * @return true if the name textfield seem okay, + * false if there is an error + */ + private boolean verifyName() { + return (nameTextField.getText().trim().length() != 0); + } + + /** + * Verifies the hostname textfield by resolving the given name. + * + * @return true if the hostname is not empty and can be + * resolved, false otherwise + */ + private boolean verifyHostname() { + if (hostnameTextField.getText().trim().length() == 0) { + return false; + } + try { + InetAddress.getByName(hostnameTextField.getText().trim()); + return true; + } catch (UnknownHostException uhe1) { + return false; + } + } + + /** + * Verifies that the port number is numeric and in the range from + * 0 to 65535. + * + * @return true if the port number is okay, + * false otherwise + */ + private boolean verifyPort() { + try { + int portNumber = Integer.valueOf(portTextField.getText().trim()); + if ((portNumber > 0) && (portNumber < 65536)) { + return true; + } + } catch (NumberFormatException nfe1) { + /* ignore. */ + } + return false; + } + + /** + * Confirms the node settings and closes the dialog. + */ + private void confirm() { + if (!verifyName()) { + JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.name.message"), I18n.get("editNodeDialog.error.name.title"), JOptionPane.ERROR_MESSAGE); + return; + } + if (!verifyHostname()) { + JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.hostname.message"), I18n.get("editNodeDialog.error.hostname.title"), JOptionPane.ERROR_MESSAGE); + return; + } + if (!verifyPort()) { + JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.port.message"), I18n.get("editNodeDialog.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!"; + } + sameMachine = sameMachineCheckBox.isSelected(); + cancelled = false; + setVisible(false); + } + + /** + * Cancels the node settings and closes the dialog. + */ + private void cancel() { + cancelled = true; + setVisible(false); + } + +}