externalize verifier for variouos stuff
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 3 May 2008 17:20:37 +0000 (17:20 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 3 May 2008 17:20:37 +0000 (17:20 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@783 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/jsite/core/Verifier.java [new file with mode: 0644]
src/net/pterodactylus/jsite/gui/EditNodeDialog.java

diff --git a/src/net/pterodactylus/jsite/core/Verifier.java b/src/net/pterodactylus/jsite/core/Verifier.java
new file mode 100644 (file)
index 0000000..ec9d348
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * jSite2 - Verifier.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.core;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+/**
+ * Contains verifier for various settings.
+ *
+ * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+ * @version $Id$
+ */
+public class Verifier {
+
+       /**
+        * Checks whether the given name is a valid node name.
+        *
+        * @param name
+        *            The name to verify
+        * @return <code>true</code> if the name is okay, <code>false</code> if
+        *         there is an error
+        */
+       public static boolean verifyNodeName(String name) {
+               return (name != null) && (name.trim().length() != 0);
+       }
+
+       /**
+        * Verifies the given hostname by trying to resolve it.
+        *
+        * @param hostname
+        *            The hostname to verify
+        * @return <code>true</code> if the hostname is not empty and can be
+        *         resolved, <code>false</code> otherwise
+        */
+       public static boolean verifyHostname(String hostname) {
+               if ((hostname == null) || (hostname.trim().length() == 0)) {
+                       return false;
+               }
+               try {
+                       InetAddress.getByName(hostname.trim());
+                       return true;
+               } catch (UnknownHostException uhe1) {
+                       return false;
+               }
+       }
+
+       /**
+        * Verifies that the port number is numeric and in the range from
+        * <code>0</code> to <code>65535</code>.
+        *
+        * @param portString
+        *            The port number string
+        * @return <code>true</code> if the port number is okay,
+        *         <code>false</code> otherwise
+        */
+       public static boolean verifyPort(String portString) {
+               if ((portString == null) || (portString.trim().length() == 0)) {
+                       return false;
+               }
+               try {
+                       int portNumber = Integer.valueOf(portString.trim());
+                       if ((portNumber > 0) && (portNumber < 65536)) {
+                               return true;
+                       }
+               } catch (NumberFormatException nfe1) {
+                       /* ignore. */
+               }
+               return false;
+       }
+
+}
index 260466e..2e49055 100644 (file)
@@ -25,8 +25,6 @@ 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.BorderFactory;
 import javax.swing.JButton;
@@ -36,6 +34,7 @@ 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;
@@ -246,65 +245,18 @@ public class EditNodeDialog extends JDialog implements I18nable {
        //
 
        /**
-        * Checks the name textfield for valid input.
-        *
-        * @return <code>true</code> if the name textfield seem okay,
-        *         <code>false</code> if there is an error
-        */
-       private boolean verifyName() {
-               return (nameTextField.getText().trim().length() != 0);
-       }
-
-       /**
-        * Verifies the hostname textfield by resolving the given name.
-        *
-        * @return <code>true</code> if the hostname is not empty and can be
-        *         resolved, <code>false</code> 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
-        * <code>0</code> to <code>65535</code>.
-        *
-        * @return <code>true</code> if the port number is okay,
-        *         <code>false</code> 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()) {
+               if (!Verifier.verifyNodeName(nameTextField.getText())) {
                        JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.name.message"), I18n.get("editNodeDialog.error.name.title"), JOptionPane.ERROR_MESSAGE);
                        return;
                }
-               if (!verifyHostname()) {
+               if (!Verifier.verifyHostname(hostnameTextField.getText())) {
                        JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.hostname.message"), I18n.get("editNodeDialog.error.hostname.title"), JOptionPane.ERROR_MESSAGE);
                        return;
                }
-               if (!verifyPort()) {
+               if (!Verifier.verifyPort(portTextField.getText())) {
                        JOptionPane.showMessageDialog(this, I18n.get("editNodeDialog.error.port.message"), I18n.get("editNodeDialog.error.port.title"), JOptionPane.ERROR_MESSAGE);
                        return;
                }