/Users/lyon/j4p/src/classUtils/gui/EditProperties.java

1    package classUtils.gui; 
2     
3    import javax.swing.*; 
4    import javax.swing.border.EtchedBorder; 
5    import java.awt.*; 
6    import java.lang.reflect.Method; 
7    import java.util.StringTokenizer; 
8    import java.util.Vector; 
9     
10    
11   class EditProperties { 
12    
13       static private Object o; 
14       protected Class c; 
15       Vector listOfClasses = new Vector(); 
16       Vector listOfGetMethods = new Vector(); 
17       Vector listOfGetValues = new Vector(); 
18       Vector listOfSetMethods = new Vector(); 
19       Vector namesOfSetMethods = new Vector(); 
20       Vector primitiveSetMethod = new Vector(); 
21    
22       EditProperties(Object o_) { 
23           o = o_; 
24           c = o.getClass(); 
25           loadInstanceMethods(); 
26           //printToConsole(); 
27           printToSwing(); 
28       } 
29    
30       EditProperties() { 
31           o = this; 
32           c = this.getClass(); 
33           loadInstanceMethods(); 
34           //printToConsole(); 
35           printToSwing(); 
36       } 
37    
38       private void loadInstanceMethods() { 
39           do { 
40               Method allMethods[] = c.getDeclaredMethods(); 
41               for (int x = 0; x < allMethods.length; x++) { 
42                   listOfClasses.addElement(c); 
43                   loadPublicGetters(allMethods[x]); 
44                   loadPublicSetters(allMethods[x]); 
45               } 
46               c = c.getSuperclass(); 
47           } while (c != Object.class); 
48       } 
49    
50       private void loadPublicGetters(Method m) { 
51           if (m.toString().indexOf("public") != -1 && (m.getName().substring(0, 3).compareTo("get") == 0 || m.getName().substring(0, 2).compareTo("is") == 0)) { 
52               listOfGetMethods.addElement(m.getName()); 
53               loadGetValues(m); 
54           } 
55       } 
56    
57       private void loadPublicSetters(Method m) { 
58           if (m.toString().indexOf("public") != -1 && m.getName().substring(0, 3).compareTo("set") == 0) { 
59               listOfSetMethods.addElement(m); 
60               namesOfSetMethods.addElement(m.getName()); 
61               primitiveSetMethod.addElement(isPrimitive(m.getParameterTypes())); 
62           } 
63       } 
64    
65       private String isPrimitive(Class[] c) { 
66           String primitiveTest = "true"; 
67           for (int i = 0; i < c.length; i++) { 
68               if (!c[i].isPrimitive() && c[i] != primitiveTest.getClass()) { 
69                   primitiveTest = "false"; 
70               } 
71           } 
72           return primitiveTest; 
73       } 
74    
75       private void loadGetValues(Method m) { 
76           try { 
77               Method m1 = c.getMethod(m.getName(), new Class[]{}); 
78               Object result = m1.invoke(o, null); 
79               if (result == null) { 
80                   listOfGetValues.addElement("null"); 
81               } else { 
82                   listOfGetValues.addElement(result); 
83               } 
84           } catch (Exception e) { 
85               //the cause of this is most likely that the method requires parameters 
86               listOfGetValues.addElement("Can't Determine Value"); 
87           } 
88       } 
89    
90       public void printToSwing() { 
91    
92           JInternalFrame frame = new JInternalFrame(("Edit Properties"), true, true, true, true); 
93           frame.setLocation(100, 100); 
94           frame.setSize(400, 200); 
95           frame.setBackground(Color.white); 
96           ComponentEditor.desktop.add(frame); 
97           frame.moveToFront(); 
98           frame.setVisible(true); 
99    
100   
101          Container content = frame.getContentPane(); 
102          content.setLayout(new GridLayout()); 
103   
104          JPanel p = new JPanel(new BorderLayout()); 
105          JPanel leftp = new JPanel(new GridLayout(0, 1)); 
106          JPanel rightp = new JPanel(new GridLayout(0, 1)); 
107          p.add(leftp, BorderLayout.WEST); 
108          p.add(rightp, BorderLayout.CENTER); 
109   
110          JLabel property[] = new JLabel[1000]; 
111          JTextField value[] = new JTextField[1000]; 
112          String propertyName; 
113          String propertyFlag; 
114   
115   
116          for (int x = 0; x < listOfGetMethods.size(); x++) { 
117              //test for corresponding setter 
118              int validSetMethodFromGet = namesOfSetMethods.indexOf("s" + listOfGetMethods.elementAt(x).toString().substring(1, listOfGetMethods.elementAt(x).toString().length())); 
119              int validSetMethodFromIs = namesOfSetMethods.indexOf("set" + listOfGetMethods.elementAt(x).toString().substring(2, listOfGetMethods.elementAt(x).toString().length())); 
120              int validSetMethod = (validSetMethodFromGet > validSetMethodFromIs ? validSetMethodFromGet : validSetMethodFromIs); 
121   
122              if (validSetMethod >= 0) { 
123                  propertyFlag = listOfGetMethods.elementAt(x).toString().substring(0, 1); 
124                  if (propertyFlag.equals("g")) { 
125                      propertyName = listOfGetMethods.elementAt(x).toString().substring(3, listOfGetMethods.elementAt(x).toString().length()); 
126                  } else { 
127                      propertyName = listOfGetMethods.elementAt(x).toString().substring(2, listOfGetMethods.elementAt(x).toString().length()); 
128                  } 
129                  property[x] = new JLabel(propertyName); 
130                  property[x].setBorder(new EtchedBorder()); 
131                  property[x].setOpaque(true); 
132                  property[x].setBackground(Color.white); 
133                  leftp.add(property[x]); 
134   
135   
136                  if (listOfGetValues.elementAt(x).getClass() == Dimension.class) { 
137                      Dimension _d = (Dimension) listOfGetValues.elementAt(x); 
138                      listOfGetValues.setElementAt((int) _d.getWidth() + "," + (int) _d.getHeight(), x); 
139                  } else if (listOfGetValues.elementAt(x).getClass() == Point.class) { 
140                      Point _p = (Point) listOfGetValues.elementAt(x); 
141                      listOfGetValues.setElementAt((int) _p.getX() + "," + (int) _p.getY(), x); 
142                  } else if (listOfGetValues.elementAt(x).getClass() == Rectangle.class) { 
143                      Rectangle _r = (Rectangle) listOfGetValues.elementAt(x); 
144                      listOfGetValues.setElementAt((int) _r.getX() + "," + (int) _r.getY() + "," + (int) _r.getWidth() + "," + (int) _r.getHeight(), x); 
145                  } 
146   
147                  //if the setter has primitive arguments allow it to be changed 
148                  if (validSetMethod >= 0 && primitiveSetMethod.elementAt(validSetMethod).equals("true")) { 
149                      value[x] = new RunPropertyTextField(listOfGetValues.elementAt(x).toString(), validSetMethod, o.getClass()) { 
150                          public void run() { 
151   
152                              StringTokenizer st = new StringTokenizer(this.getText(), ","); 
153                              Method t = (Method) listOfSetMethods.elementAt(this.location); 
154                              Class[] paramTypes = t.getParameterTypes(); 
155                              if (paramTypes.length != st.countTokens()) { 
156                                  JOptionPane.showMessageDialog(null, "Invalid Data. The property was not changed", "Error", JOptionPane.ERROR_MESSAGE); 
157                                  return; 
158                              } 
159                              int n = 0; 
160   
161                              Object arg[] = new Object[st.countTokens()]; 
162                              while (st.hasMoreTokens()) { 
163                                  arg[n] = convertArg(st.nextToken(), paramTypes[n]); 
164                                  n++; 
165                              } 
166                              setProperty(this.type, t.getName(), t, arg); 
167   
168                          } 
169                      }; 
170                      value[x].setBorder(new EtchedBorder()); 
171                      value[x].setEnabled(true); 
172                  } else { 
173   
174                      value[x] = new JTextField(listOfGetValues.elementAt(x).toString()); 
175                      value[x].setBorder(new EtchedBorder()); 
176                      value[x].setEnabled(false); 
177                  } 
178                  rightp.add(value[x]); 
179              } 
180          } 
181          content.setVisible(true); 
182          p.setVisible(true); 
183   
184          JScrollPane sp = new JScrollPane(p); 
185          content.add(sp); 
186          frame.updateUI(); 
187   
188      } 
189   
190      public static void setProperty(Class c, String setterName, Method t, Object[] args) { 
191          int flag = 0; 
192          do { 
193              c = c.getSuperclass(); 
194              try { 
195                  Method m1 = c.getMethod(setterName, t.getParameterTypes()); 
196                  m1.invoke(o, args); 
197                  flag = 1; 
198              } catch (Exception e) { 
199                  //e.printStackTrace(); 
200              } 
201   
202          } while (c != Object.class); 
203          if (flag == 0) { 
204              JOptionPane.showMessageDialog(null, "Invalid Data. The property was not changed", "Error", JOptionPane.ERROR_MESSAGE); 
205          } 
206      } 
207   
208   
209      protected Object convertArg(String val, Class type) { 
210          if (val == null) { 
211              return null; 
212          } 
213   
214          String v = val.trim(); 
215          if (String.class.isAssignableFrom(type)) { 
216              return val; 
217          } else if (Integer.TYPE.isAssignableFrom(type)) { 
218              return new Integer(v); 
219          } else if (Long.TYPE.isAssignableFrom(type)) { 
220              return new Long(v); 
221          } else if (Boolean.TYPE.isAssignableFrom(type)) { 
222              if ("true".equalsIgnoreCase(v)) { 
223                  return Boolean.TRUE; 
224              } else if ("false".equalsIgnoreCase(v)) { 
225                  return Boolean.FALSE; 
226              } 
227          } 
228          return null; 
229      } 
230   
231   
232      public void printToConsole() { 
233          for (int x = 0; x < listOfGetMethods.size(); x++) { 
234              System.out.println(listOfGetMethods.elementAt(x) + " = " + listOfGetValues.elementAt(x) + "\n"); 
235          } 
236          for (int x = 0; x < listOfSetMethods.size(); x++) { 
237              System.out.println(listOfSetMethods.elementAt(x) + " - " + primitiveSetMethod.elementAt(x) + "\n"); 
238          } 
239      } 
240   
241      public static void main(String[] args) { 
242          EditProperties e = new EditProperties(new Container()); 
243      } 
244   
245  } 
246