001 
002 /**
003  * Title:        Advanced Network Client Sample<p>
004  * Description:  <p>
005  * Copyright:    Copyright (C) 2009 Alexey Veremenko<p>
006  * Company:      <p>
007  @author Alexey Veremenko
008  @version 1.0
009  */
010 package networking;
011 
012 import java.awt.*;
013 import java.io.*;
014 import java.net.*;
015 import javax.swing.*;
016 
017 public class Util
018 {
019     /**
020      * Get name of the localhost
021      @return name of the localhost or empty string
022      */
023     public static String getLocalHost()
024     {
025         String s = "";
026 
027         try
028         {
029             s = InetAddress.getLocalHost().getHostName();
030         }
031         catch (Exception e)
032         {
033         }
034 
035         return s;
036     }
037 
038     /**
039      * Get file object from file name
040      @param fileName file name relative to the current user directory
041      @return file object
042      */
043     public static File getFile(String fileName)
044     {
045         return new File(getFilePath(fileName));
046     }
047 
048     /**
049      * Get file path from file name
050      @param fileName file name relative to the current user directory
051      @return full file path
052      */
053     public static String getFilePath(String fileName)
054     {
055         return System.getProperty("user.dir"+
056            System.getProperty("file.separator"+ fileName;
057     }
058 
059     /**
060      * Center a window on the screen
061      @param w window
062      */
063     public static void setCentered(Window w)
064     {
065         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
066         w.setLocation((d.width - w.getSize().width2,
067                                    (d.height - w.getSize().height2);
068     }
069 
070     /**
071      * Center a window relative to the other window
072      @param w front window
073      @param wBack back window
074      */
075     public static void setCentered(Window w, Window wBack)
076     {
077         Rectangle r = wBack.getBounds();
078         w.setLocation((int)r.getCenterX() - w.getSize().width / 2,
079             (int)r.getCenterY() - w.getSize().height / 2);
080     }
081 
082     /**
083      * Ensure that text field is not empty
084      @param tf text field
085      @param who name of control
086      @return not empty contents
087      * @throw Exception if parsing failed
088      */
089     public static String getString(TextField tf, String whothrows Exception
090     {
091         String s = tf.getText().trim();
092 
093         if (s.length() == 0)
094            throw new Exception(who + " field is empty");
095 
096         return s;
097     }
098 
099     /**
100      * Parse text field
101      @param tf text field
102      @param who name of control
103      @return parsed integer
104      * @throw Exception if parsing failed
105      */
106     public static int getInt(TextField tf, String whothrows Exception
107     {
108         String s = getString(tf, who);
109         return Integer.parseInt(s);
110     }
111 
112     /**
113      * Ensure that text field is not empty
114      @param tf text field
115      @param who name of control
116      @return not empty contents
117      * @throw Exception if parsing failed
118      */
119     public static String getString(JTextField tf, String whothrows Exception
120     {
121         String s = tf.getText().trim();
122 
123         if (s.length() == 0)
124            throw new Exception(who + " field is empty");
125 
126         return s;
127     }
128 
129     /**
130      * Parse text field
131      @param tf text field
132      @param who name of control
133      @return parsed integer
134      * @throw Exception if parsing failed
135      */
136     public static int getInt(JTextField tf, String whothrows Exception
137     {
138         String s = getString(tf, who);
139         return Integer.parseInt(s);
140     }
141 }
Java2html