/Users/lyon/j4p/src/gui/In.java

1    package gui; 
2     
3    import classUtils.dumper.ClassFile; 
4    import futils.Futil; 
5    import gui.run.RunColorChooser; 
6    import gui.run.RunButton; 
7     
8    import javax.swing.*; 
9    import java.awt.*; 
10   import java.io.File; 
11    
12   public final class In { 
13    
14       private In() { 
15       } // prevent others from instancing 
16    
17    
18       private static void testAtomicInput() { 
19           String name = getString( 
20                   "please enter your name"); 
21           int age = getInt( 
22                   "please enter your age[0..150]", 
23                   0, 
24                   150); 
25           message("name=" + name); 
26           message("age=" + age); 
27           float f = getFloat( 
28                   "enter a number between 0.0 and 1.0 inclusive:", 
29                   0, 
30                   1); 
31           In.message("you entered:" + f); 
32       } 
33    
34       /** 
35        * Prompt the user for a string. Use a modal 
36        * dialog. 
37        * 
38        * @param o prompt for the user 
39        * @return a string 
40        */ 
41       public static String getString(Object o) { 
42           return JOptionPane.showInputDialog(o); 
43       } 
44    
45       /** 
46        * Prompt the user for a password. Do not echo 
47        * the input. Use a modal dialog box. 
48        * 
49        * @return A string (not the typical array of 
50        *         char). 
51        */ 
52       public static String getPassword( 
53               String message) { 
54           JTextField passwordField = new JPasswordField( 
55                   20); 
56           Object[] ob = {passwordField}; 
57           int result = 
58                   JOptionPane.showConfirmDialog( 
59                           null, 
60                           ob, 
61                           message, 
62                           JOptionPane.OK_CANCEL_OPTION); 
63           if (result == JOptionPane.OK_OPTION) 
64               return passwordField.getText(); 
65           else 
66               return ""; 
67       } 
68    
69       /** 
70        * Use a modal dialog to display an exception, 
71        * with an error icon. 
72        * 
73        * @param e the exception 
74        */ 
75       public static boolean message(Exception e) { 
76           Object[] options = {"ok"}; 
77           e.printStackTrace(); 
78           int foo = JOptionPane.showOptionDialog( 
79                   null, 
80                   e, 
81                   "Exception", 
82                   JOptionPane.DEFAULT_OPTION, 
83                   JOptionPane.ERROR_MESSAGE, 
84                   null, 
85                   options, 
86                   options[0]); 
87           return foo == 0; 
88       } 
89    
90    
91       private static void testGetColor() { 
92           In.message("you selected:" + 
93                      getColor()); 
94       } 
95    
96       /** 
97        * Present the user with a modal Dialog 
98        * prompting for a color. 
99        * 
100       * @return color upon user selection. 
101       */ 
102      public static Color getColor() { 
103          return RunColorChooser.getColorAtomic(); 
104      } 
105   
106      /** 
107       * Present the user with a modal dialog 
108       * containing a message 
109       */ 
110      public static boolean message( 
111              String messageString) { 
112          Object[] options = {"ok"}; 
113          int foo = JOptionPane.showOptionDialog( 
114                  null, 
115                  messageString, 
116                  "Message", 
117                  JOptionPane.DEFAULT_OPTION, 
118                  JOptionPane.PLAIN_MESSAGE, 
119                  null, 
120                  options, 
121                  options[0]); 
122          return foo == 0; 
123      } 
124   
125      public static void multiPromptTest() { 
126          String s[] = {"tom", "dick", "harry"}; 
127          System.out.println(multiPrompt(s, 
128                                         "select an option", 
129                                         "mutliTestDialog")); 
130      } 
131   
132      /** 
133       * Present the user with a modal dialog that 
134       * 
135       * @param options an array of options to 
136       *                select from 
137       * @param message the message to the user 
138       * @param title   the title of the dialog 
139       * @return the selected option 
140       */ 
141      public static Object multiPrompt( 
142              Object options[], 
143              String message, 
144              String title) { 
145          if (options == null) return null; 
146          //Object showInputDialog(Component parentComponent, 
147          //Object message, String title, int messageType, Icon icon, 
148          //Object[] selectionValues, Object initialSelectionValue) 
149          return JOptionPane.showInputDialog(null, 
150                                             message, 
151                                             title, 
152                                             JOptionPane.QUESTION_MESSAGE, 
153                                             null, 
154                                             options, 
155                                             options[0]); 
156      } 
157   
158      /** 
159       * Present the user with a modal dialog, 
160       * prompting the user for a yes or no 
161       * response. 
162       * 
163       * @param messageString The prompt 
164       * @return true is user selects yes. 
165       */ 
166      public static boolean getBoolean( 
167              String messageString) { 
168          Object[] options = {"yes", "no"}; 
169          int foo = JOptionPane.showOptionDialog( 
170                  null, 
171                  messageString, 
172                  "Warning", 
173                  JOptionPane.DEFAULT_OPTION, 
174                  JOptionPane.WARNING_MESSAGE, 
175                  null, 
176                  options, 
177                  options[0]); 
178          return foo == 0; 
179      } 
180   
181      /** 
182       * Prompt the user for a floating point 
183       * number 
184       * 
185       * @param o prompt for the float string 
186       * @return a parsed primative float 
187       */ 
188      public static float getFloat(Object o) { 
189          float f = 0; 
190          try { 
191              f = 
192              Float.parseFloat( 
193                      JOptionPane.showInputDialog( 
194                              o)); 
195          } catch (NumberFormatException e) { 
196              f = 
197              getFloat(e + 
198                       "I need a float, try again!"); 
199          } 
200          return f; 
201      } 
202   
203      /** 
204       * Prompt the user for an float that is <= the 
205       * lo value and >= the high value. perform 
206       * error checking on the input. 
207       * 
208       * @param lo inclusive lower bound on input 
209       * @param hi inclusive upper bound on input. 
210       * @return a float between lo and hi, 
211       *         inclusive. 
212       */ 
213      public static float getFloat(String prompt, 
214                                   float lo, 
215                                   float hi) { 
216          float f = getFloat(prompt); 
217          if (f <= hi && f >= lo) return f; 
218          In.message(" your last ans:" + 
219                     f + 
220                     " was out of range"); 
221          return getFloat(prompt, lo, hi); 
222      } 
223   
224      public static void rangeTest() { 
225          int anInt = getInt( 
226                  "enter a number from 1 to 10", 
227                  1, 
228                  10); 
229          System.out.println("got a value:" + 
230                             anInt); 
231      } 
232   
233      /** 
234       * Prompt the user for an int that is <= the 
235       * lo value and >= the high value. perform 
236       * error checking on the input. 
237       * 
238       * @param lo inclusive lower bound on input 
239       * @param hi inclusive upper bound on input. 
240       * @return an int between lo and hi, 
241       *         inclusive. 
242       */ 
243      public static int getInt(String prompt, 
244                               int lo, 
245                               int hi) { 
246          int anInt = getInt(prompt); 
247          if (anInt <= hi && anInt >= lo) return anInt; 
248          return getInt(prompt + 
249                        " your last ans:" + 
250                        anInt + 
251                        " was out of range"); 
252      } 
253   
254      /** 
255       * Prompt the user with a modal dialog for an 
256       * int. Perform error checking to make sure 
257       * that an int is returned. 
258       * 
259       * @return an int 
260       */ 
261      public static int getInt(Object o) { 
262          int i = 0; 
263          try { 
264              i = 
265              Integer.parseInt( 
266                      JOptionPane.showInputDialog( 
267                              o)); 
268          } catch (NumberFormatException e) { 
269              i = 
270              getInt(e + 
271                     "I need an int, try again!"); 
272          } 
273          return i; 
274      } 
275   
276      public static void testGetString() { 
277          System.out.println( 
278                  "Hello " + 
279                  getString( 
280                          "Please Enter your class:")); 
281          System.out.println("There are " 
282                             + 
283                             getFloat( 
284                                     "Please Enter the number of students:") 
285                             + 
286                             "Students in your class"); 
287          //System.out.println( "your grade= "+getDouble("Please Enter your grade:")); 
288      } 
289   
290      public static Class getClassFromUser() { 
291          String cn = In.getString( 
292                  "enter a fully qualified class name"); 
293          if (cn == null) return null; 
294          try { 
295              return Class.forName(cn); 
296          } catch (ClassNotFoundException e) { 
297              message(e); 
298              message( 
299                      "did you select a class file in your path?"); 
300              if (In.getBoolean( 
301                      "would you like to try again?")) 
302                  return getClassFromUser(); 
303              System.exit(0); 
304              return null; 
305          } 
306      } 
307   
308      public static Class getClassFileUser() { 
309          try { 
310              File f = Futil.getReadFile( 
311                      "select a class file to be made remote"); 
312              ClassFile cf = ClassFile.getClassFile( 
313                      f); 
314              String cn = cf.getClassName(); 
315              Class c = Class.forName(cn); 
316              return c; 
317          } catch (ClassNotFoundException e) { 
318              message(e); 
319              message( 
320                      "did you select a class file in your path?"); 
321              if (In.getBoolean( 
322                      "would you like to try again?")) 
323                  return getClassFileUser(); 
324              System.exit(0); 
325              return null; 
326          } 
327      } 
328      public static Rectangle getRectangle(String s){ 
329          final JDialog jd = new JDialog(); 
330          jd.setTitle(s); 
331          Container c = jd.getContentPane(); 
332          c.setLayout(new FlowLayout()); 
333          jd.setSize(200,200); 
334          jd.setModal(true); 
335          jd.setResizable(true); 
336          c.add(new RunButton(s){ 
337              public void run(){ 
338                 jd.setVisible(false); 
339   
340              } 
341          }); 
342          jd.setVisible(true); 
343          return new Rectangle(jd.getLocation(), 
344                  jd.getSize()); 
345      } 
346      public static void main(String[] args) { 
347          System.out.println(getRectangle("resize and place this dialog")); 
348      } 
349  } 
350   
351