/Users/lyon/j4p/src/gui/dialogs/BeanDialog.java

1    package gui.dialogs; 
2     
3    import classUtils.reflection.MethodList; 
4    import gui.run.RunPasswordField; 
5    import gui.run.RunTextField; 
6    import security.WebStartBean; 
7     
8    import javax.swing.*; 
9    import java.lang.reflect.InvocationTargetException; 
10   import java.lang.reflect.Method; 
11    
12   /** 
13    * This class demonstrates the usage of the 
14    * StandardDialog class. 
15    */ 
16   public class BeanDialog extends ModalJDialog { 
17       Object object = null; 
18       Class c = null; 
19    
20       MethodList methodList = null; 
21       Method writeMethods[] = null; 
22    
23       private LabelledItemPanel labeledPanelItem = 
24               new LabelledItemPanel(); 
25    
26       public BeanDialog(Object o) { 
27           object = o; 
28           c = o.getClass(); 
29           methodList = new MethodList(c); 
30           writeMethods = 
31                   methodList.getWriteMethods(); 
32           init(); 
33       } 
34    
35       private void init() { 
36           setTitle(c.getName()); 
37           for (int i = 0; 
38                i < writeMethods.length; 
39                i++) { 
40               final Method setter = writeMethods[i]; 
41    
42               if (hasStringClassParameter(setter)) 
43                   addLabeledTextField(setter); 
44    
45           } 
46           labeledPanelItem.setBorder( 
47                   BorderFactory.createEtchedBorder()); 
48    
49           setContentPane(labeledPanelItem); 
50       } 
51    
52       private void addLabeledTextField( 
53               final Method setter) { 
54    
55           String propertyName = MethodList.getPropertyName( 
56                   setter); 
57           final Method getter = methodList.getReadMethod( 
58                   setter); 
59           String value = invoke(getter, null); 
60           if (MethodList.hasPasswordProperty( 
61                   setter)) 
62               addLabeledRunPasswordField(value, 
63                       setter, 
64                       propertyName); 
65           else 
66               addLabeledRunTextField(value, 
67                       setter, 
68                       propertyName); 
69    
70       } 
71    
72       private void addLabeledRunPasswordField( 
73               String value, 
74               final Method setter, 
75               String propertyName) { 
76           RunPasswordField rtf = new RunPasswordField( 
77                   value, 20) { 
78               Object args[] = {""}; 
79    
80               public void run() { 
81                   args[0] = 
82                           this.getPasswordString(); 
83                   invoke(setter, args); 
84               } 
85           }; 
86    
87           labeledPanelItem.addItem(propertyName, 
88                   rtf); 
89       } 
90    
91       private void addLabeledRunTextField( 
92               String value, 
93               final Method setter, 
94               String propertyName) { 
95          // int lengthOfValue = value.length(); 
96    
97           RunTextField rtf = new RunTextField( 
98                   value, 20) { 
99               Object args[] = {""}; 
100   
101              public void run() { 
102                  args[0] = getText(); 
103                  invoke(setter, args); 
104              } 
105          }; 
106   
107          labeledPanelItem.addItem(propertyName, 
108                  rtf); 
109      } 
110   
111   
112   
113      private String invoke(final Method m, 
114                            Object args[]) { 
115          String value = ""; 
116          try { 
117              value = 
118                      (String) m.invoke(object, 
119                              args); 
120          } catch (IllegalAccessException e) { 
121              e.printStackTrace(); 
122   
123          } catch (InvocationTargetException e) { 
124              e.printStackTrace(); 
125   
126          } 
127          return value; 
128      } 
129   
130      private boolean hasStringClassParameter( 
131              Method m) { 
132          Class params[] = m.getParameterTypes(); 
133          Class stringClass = String.class; 
134          return (params[0].equals(stringClass)); 
135      } 
136   
137      /** 
138       * This method gets the values of the panel 
139       * entry fields. 
140       * 
141       * @return Gui modified instance 
142       */ 
143      public Object getData() { 
144          return object; 
145      } 
146   
147   
148      public static void main(String[] args) { 
149          BeanDialog dialog = new BeanDialog( 
150                   WebStartBean.getFromPreferences()); 
151          //WebStartBean.getDefaultWebStartBean()); 
152          dialog.pack(); 
153          dialog.show(); 
154          Object o = dialog.getData(); 
155   
156          System.out.println(o); 
157   
158          System.exit(0); 
159      } 
160  }