1 23 24 29 30 package com.sun.enterprise.tools.common.ui; 31 32 import java.lang.reflect.Method ; 33 import java.beans.PropertyDescriptor ; 34 35 import javax.swing.JTable ; 36 import javax.swing.JList ; 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 50 public class MVCUtils { 51 52 53 private MVCUtils() { 54 } 55 56 private static final String TRUE = "true"; private static final String EMPTY = ""; 59 public static void linkBooleanStringElement(Object model, javax.swing.AbstractButton view, String propName) 60 throws java.beans.IntrospectionException { 61 view.addItemListener(new BooleanStringItemListener(model, propName)); 62 view.setSelected(getStringElementValue(model, propName).equals(TRUE)); 63 } 64 65 public static void linkBooleanStringElement(Object model, javax.swing.AbstractButton view) 66 throws java.beans.IntrospectionException { 67 String 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"); } 74 75 public static void linkStringElement(Object model, javax.swing.text.JTextComponent view, String propName) 76 throws java.beans.IntrospectionException { 77 view.addKeyListener(new StringItemListener(model,propName)); 78 view.setText(getStringElementValue(model,propName)); 79 } 80 81 public static void linkStringElement(Object model, javax.swing.text.JTextComponent view) 82 throws java.beans.IntrospectionException { 83 String 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"); } 90 91 public static void linkIntStringElement(Object model, javax.swing.text.JTextComponent view, String propName) 92 throws java.beans.IntrospectionException { 93 view.addKeyListener(new IntStringItemListener(model,propName)); 94 view.setText(getStringElementValue(model,propName)); 95 } 96 97 public static void linkIntStringElement(Object model, 98 javax.swing.text.JTextComponent view, String propName, int defaultValue) 99 throws java.beans.IntrospectionException { 100 view.addKeyListener(new IntStringItemListener(model,propName)); 101 String 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 model, 108 javax.swing.text.JTextComponent view, String propName, int min, int max) 109 throws java.beans.IntrospectionException { 110 view.addKeyListener(new IntStringItemListener(model,propName, min, max)); 111 view.setText(getStringElementValue(model,propName)); 113 } 116 117 public static void linkIntStringElement(Object model, 118 javax.swing.text.JTextComponent view, String propName, int defaultValue, 119 int min, int max) 120 throws java.beans.IntrospectionException { 121 view.addKeyListener(new IntStringItemListener(model,propName, min, max)); 122 String 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 public static void selectionSensitive(Object hasListSelectionModel, java.awt.Component sensitiveItem) { 131 try { 132 Method getter = hasListSelectionModel.getClass().getMethod("getSelectionModel",null); javax.swing.ListSelectionModel lsm[] = new javax.swing.ListSelectionModel [1]; 134 Class lsmArgs[] = { javax.swing.ListSelectionModel .class }; 135 if (null != getter) { 136 lsm[0] = (javax.swing.ListSelectionModel ) getter.invoke(hasListSelectionModel,null); 137 if (null == lsm[0]) { 138 lsm[0] = new javax.swing.DefaultListSelectionModel (); 139 Method putter = hasListSelectionModel.getClass().getMethod("setSelectionModel", lsmArgs); putter.invoke(hasListSelectionModel,lsm); 141 } 142 lsm[0].addListSelectionListener(new SelectionActivator(sensitiveItem, hasListSelectionModel)); 143 } 144 } 145 catch (Throwable t) { 146 Reporter.critical(new StackTrace(t)); } 148 } 149 150 public static void validationSensitive(javax.swing.text.JTextComponent f, java.awt.Component sensitive, Validator v) { 151 StringValidationListener svl = 152 new StringValidationListener(sensitive,v); 153 154 f.addKeyListener(svl); 155 } 156 157 158 private static String getStringElementValue(Object model, String propName) { 159 String retVal = EMPTY; 160 Method reader = null; 161 try { 162 PropertyDescriptor destPd = new PropertyDescriptor (propName, model.getClass()); 163 reader = destPd.getReadMethod(); 164 Object tmp = reader.invoke(model,null); 165 if (null != tmp) 166 retVal =tmp.toString(); 167 } 168 catch (Throwable t) { 169 Reporter.critical(new StackTrace(t)); } 171 return retVal; 172 } 173 174 189 190 static class StringValidationListener extends java.awt.event.KeyAdapter { 191 java.awt.Component sensitive; 192 Validator validation; 193 public StringValidationListener(java.awt.Component sensitive, Validator validation) { 194 this.validation = validation; 195 this.sensitive = sensitive; 196 } 197 198 public void keyReleased(java.awt.event.KeyEvent ev) { 199 javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent ) 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 { 209 java.awt.Component comp = null; 210 Object foo; 211 212 public SelectionActivator (java.awt.Component comp, Object foo) { 213 this.comp = comp; 214 this.foo = foo; 215 } 216 217 public void valueChanged(javax.swing.event.ListSelectionEvent lse) { 218 Object src = lse.getSource(); 219 Reporter.verbose(src); int selected = -1; 221 if (foo instanceof javax.swing.JTable ) { 222 JTable t = (JTable ) foo; 223 selected = t.getSelectedRow(); 224 } 225 else if (src instanceof JList ) { 226 JList l = (JList ) 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 { 237 Object target = null; 238 java.lang.reflect.Method writer = null; 239 240 protected Object args[] = { target }; 241 242 243 244 public StringItemListener(Object target, String destName) throws java.beans.IntrospectionException { 245 this.target = target; 246 writer = PropertyUtils.getWriter(target,destName); 247 248 } 249 250 public void keyReleased(java.awt.event.KeyEvent ev) { 251 try { 252 javax.swing.text.JTextComponent src = (javax.swing.text.JTextComponent ) ev.getSource(); 254 258 args[0] = src.getText(); 262 if (null == args[0]) { 263 args[0] = EMPTY; 264 } 265 writer.invoke(target, args); 266 } 267 catch (Throwable t) { 268 Reporter.critical(new StackTrace(t)); } 270 } 271 } 272 273 static class IntStringItemListener extends StringItemListener { 274 java.lang.reflect.Method reader = null; 275 277 278 280 int min = Integer.MIN_VALUE; 281 int max = Integer.MAX_VALUE; 282 283 284 285 public IntStringItemListener(Object target, String destName) throws java.beans.IntrospectionException { 286 super(target,destName); 287 } 290 291 public IntStringItemListener(Object target, String destName, int min, int max) 292 throws java.beans.IntrospectionException { 293 super(target,destName); 294 this.min = min; 295 this.max = max; 296 reader = PropertyUtils.getReader(target,destName); 297 } 300 301 public void keyReleased(java.awt.event.KeyEvent ev) { 302 try { 303 javax.swing.text.JTextComponent src = (javax.swing.text.JTextComponent ) ev.getSource(); 305 309 args[0] = new Integer (src.getText()); 314 Integer argVal = (Integer ) args[0]; 315 int argIVal = argVal.intValue(); 316 320 321 if (argIVal >= min && argIVal <= max) 322 writer.invoke(target, args); 323 else { 324 if (null != reader) { 325 Integer prev = (Integer ) reader.invoke(target,null); 326 src.setText(prev.toString()); 327 } 328 } 329 } 332 catch (Throwable t) { 333 Reporter.critical(new StackTrace(t)); } 335 } 336 } 337 } 338 | Popular Tags |