KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > ui > MVCUtils


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * MVCUtils.java
26  *
27  * Created on March 18, 2001, 1:30 PM
28  */

29
30 package com.sun.enterprise.tools.common.ui;
31
32 import java.lang.reflect.Method JavaDoc;
33 import java.beans.PropertyDescriptor JavaDoc;
34
35 import javax.swing.JTable JavaDoc;
36 import javax.swing.JList JavaDoc;
37
38 import com.sun.enterprise.tools.common.util.diagnostics.Reporter;
39 import com.sun.enterprise.tools.common.util.diagnostics.StackTrace;
40
41 import com.sun.enterprise.tools.common.BooleanStringItemListener;
42 import com.sun.enterprise.tools.common.Validator;
43
44 import com.sun.enterprise.tools.common.PropertyUtils;
45 /**
46  *
47  * @author vkraemer
48  * @version
49  */

50 public class MVCUtils {
51
52     /** Creates new MVCUtils */
53     private MVCUtils() {
54     }
55
56     private static final String JavaDoc TRUE = "true"; //NOI18N
57
private static final String JavaDoc EMPTY = ""; //NOI18N
58

59     public static void linkBooleanStringElement(Object JavaDoc model, javax.swing.AbstractButton JavaDoc view, String JavaDoc propName)
60         throws java.beans.IntrospectionException JavaDoc {
61         view.addItemListener(new BooleanStringItemListener(model, propName));
62         view.setSelected(getStringElementValue(model, propName).equals(TRUE));
63     }
64     
65     public static void linkBooleanStringElement(Object JavaDoc model, javax.swing.AbstractButton JavaDoc view)
66         throws java.beans.IntrospectionException JavaDoc {
67         String JavaDoc propName = view.getName();
68         if (null != propName) {
69             linkBooleanStringElement(model,view,propName);
70         }
71         else
72             Reporter.critical("the name of the " + model + " is null"); //NOI18N
73
}
74     
75     public static void linkStringElement(Object JavaDoc model, javax.swing.text.JTextComponent JavaDoc view, String JavaDoc propName)
76         throws java.beans.IntrospectionException JavaDoc {
77         view.addKeyListener(new StringItemListener(model,propName));
78         view.setText(getStringElementValue(model,propName));
79     }
80     
81     public static void linkStringElement(Object JavaDoc model, javax.swing.text.JTextComponent JavaDoc view)
82         throws java.beans.IntrospectionException JavaDoc {
83         String JavaDoc propName = view.getName();
84         if (null != propName) {
85             linkStringElement(model,view,propName);
86         }
87         else
88             Reporter.critical("the name of the " + model + " is null"); //NOI18N
89
}
90     
91     public static void linkIntStringElement(Object JavaDoc model, javax.swing.text.JTextComponent JavaDoc view, String JavaDoc propName)
92         throws java.beans.IntrospectionException JavaDoc {
93         view.addKeyListener(new IntStringItemListener(model,propName));
94         view.setText(getStringElementValue(model,propName));
95     }
96
97     public static void linkIntStringElement(Object JavaDoc model,
98         javax.swing.text.JTextComponent JavaDoc view, String JavaDoc propName, int defaultValue)
99         throws java.beans.IntrospectionException JavaDoc {
100         view.addKeyListener(new IntStringItemListener(model,propName));
101         String JavaDoc tval = getStringElementValue(model,propName);
102         view.setText(getStringElementValue(model,propName));
103         if (null == tval || tval.equals(EMPTY))
104             view.setText(EMPTY + defaultValue);
105     }
106
107         public static void linkIntStringElement(Object JavaDoc model,
108         javax.swing.text.JTextComponent JavaDoc view, String JavaDoc propName, int min, int max)
109         throws java.beans.IntrospectionException JavaDoc {
110         view.addKeyListener(new IntStringItemListener(model,propName, min, max));
111 // String tval = getStringElementValue(model,propName);
112
view.setText(getStringElementValue(model,propName));
113 // if (null == tval || tval.equals(""))
114
// view.setText("" + defaultValue);
115
}
116
117         public static void linkIntStringElement(Object JavaDoc model,
118         javax.swing.text.JTextComponent JavaDoc view, String JavaDoc propName, int defaultValue,
119         int min, int max)
120         throws java.beans.IntrospectionException JavaDoc {
121         view.addKeyListener(new IntStringItemListener(model,propName, min, max));
122         String JavaDoc tval = getStringElementValue(model,propName);
123         view.setText(getStringElementValue(model,propName));
124         if (null == tval || tval.equals(EMPTY))
125             view.setText(EMPTY + defaultValue);
126     }
127
128     // this should work for a JList and a JTable, though they don't implement a shared interface for these two
129
// routines, but the two routines do implement the same interface....
130
public static void selectionSensitive(Object JavaDoc hasListSelectionModel, java.awt.Component JavaDoc sensitiveItem) {
131         try {
132             Method JavaDoc getter = hasListSelectionModel.getClass().getMethod("getSelectionModel",null); //NOI18N
133
javax.swing.ListSelectionModel JavaDoc lsm[] = new javax.swing.ListSelectionModel JavaDoc[1];
134             Class JavaDoc lsmArgs[] = { javax.swing.ListSelectionModel JavaDoc.class };
135             if (null != getter) {
136                 lsm[0] = (javax.swing.ListSelectionModel JavaDoc) getter.invoke(hasListSelectionModel,null);
137                 if (null == lsm[0]) {
138                     lsm[0] = new javax.swing.DefaultListSelectionModel JavaDoc();
139                     Method JavaDoc putter = hasListSelectionModel.getClass().getMethod("setSelectionModel", lsmArgs); //NOI18N
140
putter.invoke(hasListSelectionModel,lsm);
141                 }
142                 lsm[0].addListSelectionListener(new SelectionActivator(sensitiveItem, hasListSelectionModel));
143             }
144         }
145         catch (Throwable JavaDoc t) {
146             Reporter.critical(new StackTrace(t)); //NOI18N
147
}
148     }
149     
150     public static void validationSensitive(javax.swing.text.JTextComponent JavaDoc f, java.awt.Component JavaDoc sensitive, Validator v) {
151         StringValidationListener svl =
152             new StringValidationListener(sensitive,v);
153         
154         f.addKeyListener(svl);
155     }
156     
157     
158     private static String JavaDoc getStringElementValue(Object JavaDoc model, String JavaDoc propName) {
159         String JavaDoc retVal = EMPTY;
160         Method JavaDoc reader = null;
161         try {
162             PropertyDescriptor JavaDoc destPd = new PropertyDescriptor JavaDoc(propName, model.getClass());
163             reader = destPd.getReadMethod();
164             Object JavaDoc tmp = reader.invoke(model,null);
165             if (null != tmp)
166                 retVal =tmp.toString();
167         }
168         catch (Throwable JavaDoc t) {
169             Reporter.critical(new StackTrace(t)); //NOI18N
170
}
171         return retVal;
172     }
173     
174     /*private static String getIntStringElementValue(Object model, String propName) {
175         String retVal = "";
176         Method reader = null;
177         try {
178             PropertyDescriptor destPd = new PropertyDescriptor(propName, model.getClass());
179             reader = destPd.getReadMethod();
180             Object tmp = reader.invoke(model,null);
181             if (null != tmp)
182                 retVal =tmp.toString();
183         }
184         catch (Throwable t) {
185             Reporter.critical(new StackTrace(t)); //NOI18N
186         }
187         return retVal;
188     }*/

189
190     static class StringValidationListener extends java.awt.event.KeyAdapter JavaDoc {
191         java.awt.Component JavaDoc sensitive;
192         Validator validation;
193         public StringValidationListener(java.awt.Component JavaDoc sensitive, Validator validation) {
194             this.validation = validation;
195             this.sensitive = sensitive;
196         }
197         
198         public void keyReleased(java.awt.event.KeyEvent JavaDoc ev) {
199             javax.swing.text.JTextComponent JavaDoc tc = (javax.swing.text.JTextComponent JavaDoc) ev.getSource();
200             if (validation.isValid(tc.getText()))
201                 sensitive.setEnabled(true);
202             else
203                 sensitive.setEnabled(false);
204         }
205
206     }
207     
208     static class SelectionActivator implements javax.swing.event.ListSelectionListener JavaDoc {
209         java.awt.Component JavaDoc comp = null;
210         Object JavaDoc foo;
211         
212         public SelectionActivator (java.awt.Component JavaDoc comp, Object JavaDoc foo) {
213             this.comp = comp;
214             this.foo = foo;
215         }
216         
217         public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc lse) {
218             Object JavaDoc src = lse.getSource();
219             Reporter.verbose(src); //NOI18N
220
int selected = -1;
221             if (foo instanceof javax.swing.JTable JavaDoc) {
222                 JTable JavaDoc t = (JTable JavaDoc) foo;
223                 selected = t.getSelectedRow();
224             }
225             else if (src instanceof JList JavaDoc) {
226                 JList JavaDoc l = (JList JavaDoc) foo;
227                 selected = l.getSelectedIndex();
228             }
229             if (-1 != selected)
230                 comp.setEnabled(true);
231             else
232                 comp.setEnabled(false);
233         }
234     }
235    
236     static class StringItemListener extends java.awt.event.KeyAdapter JavaDoc {
237         Object JavaDoc target = null;
238         java.lang.reflect.Method JavaDoc writer = null;
239
240         protected Object JavaDoc args[] = { target };
241     
242
243     /** Creates new BooleanStringItemListener */
244         public StringItemListener(Object JavaDoc target, String JavaDoc destName) throws java.beans.IntrospectionException JavaDoc {
245             this.target = target;
246             writer = PropertyUtils.getWriter(target,destName);
247             
248         }
249         
250         public void keyReleased(java.awt.event.KeyEvent JavaDoc ev) {
251             try {
252                 //java.lang.reflect.Method lwriter = writer;
253
javax.swing.text.JTextComponent JavaDoc src = (javax.swing.text.JTextComponent JavaDoc) ev.getSource();
254                 /*if (null == lwriter) {
255                     java.awt.Component src = (java.awt.Component) itemEvent.getSource();
256                     lwriter = PropertyUtils.getWriter(target, src.getName());
257                 }*/

258                 //Object args[] = FALSE;
259
//if (itemEvent.getStateChange() == java.awt.event.ItemEvent.SELECTED)
260
//args = TRUE;
261
args[0] = src.getText();
262                 if (null == args[0]) {
263                     args[0] = EMPTY;
264                 }
265                 writer.invoke(target, args);
266             }
267             catch (Throwable JavaDoc t) {
268                 Reporter.critical(new StackTrace(t)); //NOI18N
269
}
270         }
271     }
272     
273     static class IntStringItemListener extends StringItemListener {
274         java.lang.reflect.Method JavaDoc reader = null;
275         /*Object target = null;
276         java.lang.reflect.Method writer = null;*/

277
278         //private Object args[] = { target };
279

280         int min = Integer.MIN_VALUE;
281         int max = Integer.MAX_VALUE;
282     
283
284     /** Creates new BooleanStringItemListener */
285         public IntStringItemListener(Object JavaDoc target, String JavaDoc destName) throws java.beans.IntrospectionException JavaDoc {
286             super(target,destName);
287             //this.target = target;
288
//writer = PropertyUtils.getWriter(target,destName);
289
}
290         /**/
291         public IntStringItemListener(Object JavaDoc target, String JavaDoc destName, int min, int max)
292             throws java.beans.IntrospectionException JavaDoc {
293             super(target,destName);
294             this.min = min;
295             this.max = max;
296             reader = PropertyUtils.getReader(target,destName);
297             //this.target = target;
298
//writer = PropertyUtils.getWriter(target,destName);
299
}
300
301         public void keyReleased(java.awt.event.KeyEvent JavaDoc ev) {
302             try {
303                 //java.lang.reflect.Method lwriter = writer;
304
javax.swing.text.JTextComponent JavaDoc src = (javax.swing.text.JTextComponent JavaDoc) ev.getSource();
305                 /*if (null == lwriter) {
306                     java.awt.Component src = (java.awt.Component) itemEvent.getSource();
307                     lwriter = PropertyUtils.getWriter(target, src.getName());
308                 }*/

309                 //Object args[] = FALSE;
310
//if (itemEvent.getStateChange() == java.awt.event.ItemEvent.SELECTED)
311
//args = TRUE;
312
//try {
313
args[0] = new Integer JavaDoc(src.getText());
314                     Integer JavaDoc argVal = (Integer JavaDoc) args[0];
315                     int argIVal = argVal.intValue();
316                     /*args[0] = src.getText();
317                     if (null == args[0]) {
318                         args[0] = "";
319                     }*/

320                 
321                     if (argIVal >= min && argIVal <= max)
322                         writer.invoke(target, args);
323                     else {
324                         if (null != reader) {
325                             Integer JavaDoc prev = (Integer JavaDoc) reader.invoke(target,null);
326                             src.setText(prev.toString());
327                         }
328                     }
329                 //}
330
//catch (
331
}
332             catch (Throwable JavaDoc t) {
333                 Reporter.critical(new StackTrace(t)); //NOI18N
334
}
335         }
336     }
337 }
338
Popular Tags