1 19 20 package org.objectweb.jac.aspects.gui; 21 22 import java.util.Arrays ; 23 import java.util.Hashtable ; 24 import org.apache.log4j.Level; 25 import org.apache.log4j.Logger; 26 import org.objectweb.jac.core.ACConfiguration; 27 import org.objectweb.jac.core.Imports; 28 import org.objectweb.jac.core.NameRepository; 29 import org.objectweb.jac.core.rtti.AbstractMethodItem; 30 import org.objectweb.jac.core.rtti.ClassItem; 31 import org.objectweb.jac.core.rtti.ClassRepository; 32 import org.objectweb.jac.core.rtti.FieldItem; 33 import org.objectweb.jac.util.ExtArrays; 34 35 39 public class ViewFactory { 40 static Logger logger = Logger.getLogger("gui.factory"); 41 42 static Imports imports = new Imports(); 43 44 static { 45 imports.add("org.objectweb.jac.aspects.gui.*"); 46 imports.add("org.objectweb.jac.core.rtti.*"); 47 imports.add("java.lang.*"); 48 } 49 50 53 public static void init(String type, ViewFactory f) { 54 f.setViewConstructor("Object", "GenericFactory.createObjectView"); 55 f.setViewConstructor("NoTab", "GenericFactory.createObjectViewNoTab"); 56 f.setViewConstructor ("InputParameters", "GenericFactory.createParameters"); 57 if (type.equals("swing")) { 59 f.setViewConstructor("ObjectView","swing.ObjectView"); 60 f.setViewConstructor("Customized", "swing.SwingCustomized"); 61 f.setViewConstructor("Reference", "swing.ReferenceView(Object,Object,FieldItem)"); 62 f.setViewConstructor("ObjectChooser", "swing.ObjectChooser"); 63 f.setViewConstructor("Tree", "swing.Tree"); 64 f.setViewConstructor("Panel", "swing.SwingPanelView"); 65 f.setViewConstructor("Label", "swing.SwingLabel"); 66 f.setViewConstructor("Tabbed", "swing.SwingTabbedView"); 67 f.setViewConstructor("ParameterContainer", "swing.SwingEditorContainer"); 68 f.setViewConstructor("Container", "swing.SwingContainerView"); 69 f.setViewConstructor("SingleSlotContainer", "swing.SingleSlotContainer"); 70 f.setViewConstructor("Desktop", "swing.DesktopView"); 71 f.setViewConstructor("Method", "swing.SwingMethodView"); 72 f.setViewConstructor("Field", "swing.SwingFieldView"); 73 f.setViewConstructor("Enum", "swing.EnumViewer(Object,Object,FieldItem)"); 74 f.setViewConstructor("Table", "swing.SwingTableView"); 75 f.setViewConstructor("List", "swing.List"); 76 f.setViewConstructor("Dialog", "swing.Dialog"); 77 f.setViewConstructor("MenuBar", "swing.MenuBar()"); 78 f.setViewConstructor("Menu", "swing.Menu()"); 79 f.setViewConstructor("ToolBar", "swing.ToolBar"); 80 f.setViewConstructor("StatusBar", "swing.StatusBar"); 81 82 f.setViewConstructor("Empty", "swing.SwingEmptyView"); 83 84 f.setViewConstructor("dateHour", "swing.DateHourViewer(Object,Object,FieldItem)"); 85 f.setViewConstructor("timestamp", "swing.TimestampViewer(Object,Object,FieldItem)"); 86 f.setViewConstructor("java.util.Date", "swing.DateViewer(Object,Object,FieldItem)"); 87 f.setViewConstructor("imageURL", "swing.ImageURLViewer(java.net.URL,Object,FieldItem)"); 88 f.setViewConstructor("image", "swing.ImageViewer(byte[],Object,FieldItem)"); 89 f.setViewConstructor("text", "swing.TextViewer(Object,Object,FieldItem)"); 90 f.setViewConstructor("Float","swing.FloatViewer(Object,Object,FieldItem)"); 91 f.setViewConstructor("Double","swing.FloatViewer(Object,Object,FieldItem)"); 92 f.setViewConstructor("double","swing.FloatViewer(Object,Object,FieldItem)"); 93 f.setViewConstructor("float","swing.FloatViewer(Object,Object,FieldItem)"); 94 f.setViewConstructor("percentage","swing.PercentViewer(Object,Object,FieldItem)"); 95 f.setViewConstructor("editor:percentage","swing.PercentEditor(Object,FieldItem)"); 96 97 f.setViewConstructor("org.objectweb.jac.lib.Attachment", "swing.AttachmentViewer(Object,Object,FieldItem)"); 98 99 f.setViewConstructor("editor:java.lang.Boolean", "swing.BooleanEditor"); 101 f.setViewConstructor("editor:boolean", "swing.BooleanEditor"); 102 f.setViewConstructor("editor:java.io.File", "swing.FileEditor"); 103 f.setViewConstructor("editor:filePath", "swing.FilePathEditor"); 104 f.setViewConstructor("editor:org.objectweb.jac.util.File", "swing.FileEditor"); 105 f.setViewConstructor("editor:directory", "swing.DirectoryEditor"); 106 f.setViewConstructor("editor:java.net.URL", "swing.URLEditor"); 107 f.setViewConstructor("editor:java.util.Date", "swing.DateEditor"); 108 f.setViewConstructor("editor:java.awt.Point", "swing.PointEditor"); 109 f.setViewConstructor("editor:dateHour", "swing.DateHourEditor"); 110 f.setViewConstructor("editor:text", "swing.TextEditor"); 111 f.setViewConstructor("editor:javaCode", "swing.JavaCodeEditor"); 112 f.setViewConstructor("editor:password", "swing.PasswordFieldEditor"); 113 f.setViewConstructor("editor:org.objectweb.jac.lib.Attachment", "swing.AttachmentEditor"); 114 f.setViewConstructor("editor:java.lang.Float", "swing.FloatEditor"); 115 f.setViewConstructor("editor:java.lang.Double", "swing.FloatEditor"); 116 f.setViewConstructor("editor:java.lang.Integer", "swing.PrimitiveFieldEditor(Object,FieldItem)"); 117 f.setViewConstructor("editor:java.lang.Long", "swing.PrimitiveFieldEditor(Object,FieldItem)"); 118 f.setViewConstructor("editor:java.lang.Short", "swing.PrimitiveFieldEditor(Object,FieldItem)"); 119 f.setViewConstructor("editor:java.lang.Character", "swing.PrimitiveFieldEditor(Object,FieldItem)"); 120 f.setViewConstructor("editor:java.lang.Byte", "swing.PrimitiveFieldEditor(Object,FieldItem)"); 121 f.setViewConstructor("editor:float", "swing.FloatEditor"); 122 f.setViewConstructor("editor:double", "swing.FloatEditor"); 123 f.setViewConstructor("editor:integer", "swing.PrimitiveFieldEditor(Object,FieldItem)"); 124 f.setViewConstructor("editor:long", "swing.PrimitiveFieldEditor(Object,FieldItem)"); 125 f.setViewConstructor("editor:short", "swing.PrimitiveFieldEditor(Object,FieldItem)"); 126 f.setViewConstructor("editor:char", "swing.PrimitiveFieldEditor(Object,FieldItem)"); 127 f.setViewConstructor("editor:java.lang.String", "swing.PrimitiveFieldEditor(Object,FieldItem)"); 128 f.setViewConstructor("PrimitiveTypeEditor", "swing.PrimitiveFieldEditor(Object,FieldItem,boolean)"); 129 130 f.setViewConstructor("CollectionItemView", "swing.CollectionItemView"); 131 132 f.setViewConstructor("cell:java.util.Date", "swing.DateViewer()"); 134 f.setViewConstructor("cell:dateHour", "swing.DateHourViewer()"); 135 f.setViewConstructor("cell:timestamp", "swing.TimestampViewer()"); 136 f.setViewConstructor("cell:imageURL", "swing.ImageURLViewer()"); 137 f.setViewConstructor("cell:Reference", "swing.ReferenceView()"); 138 f.setViewConstructor("cell:Enum", "swing.EnumViewer()"); 139 f.setViewConstructor("cell:java.lang.Integer", "swing.IntViewer()"); 140 f.setViewConstructor("cell:java.lang.Long", "swing.IntViewer()"); 141 f.setViewConstructor("cell:int", "swing.IntViewer()"); 142 f.setViewConstructor("cell:long", "swing.IntViewer()"); 143 f.setViewConstructor("cell:java.lang.Float", "swing.FloatViewer()"); 144 f.setViewConstructor("cell:java.lang.Double", "swing.FloatViewer()"); 145 f.setViewConstructor("cell:float", "swing.FloatViewer()"); 146 f.setViewConstructor("cell:double", "swing.FloatViewer()"); 147 f.setViewConstructor("cell:percentage","swing.PercentViewer()"); 148 f.setViewConstructor("cell:org.objectweb.jac.lib.Attachment", "swing.AttachmentViewer()"); 149 f.setViewConstructor("cell:Method", "swing.SwingMethodView"); 150 f.setViewConstructor("cell:java.lang.Boolean", "swing.BooleanViewer()"); 151 f.setViewConstructor("cell:boolean", "swing.BooleanViewer()"); 152 154 } else if(type.equals("web")) { 155 156 f.setViewConstructor("ObjectView", "web.ObjectView"); 157 f.setViewConstructor("Empty", "web.Empty"); 158 f.setViewConstructor("Customized", "web.Customized"); 159 f.setViewConstructor("Panel", "web.Panel"); 160 f.setViewConstructor("Container", "web.Container"); 161 f.setViewConstructor("SingleSlotContainer", "web.SingleSlotContainer"); 162 f.setViewConstructor("Label", "web.Label"); 163 f.setViewConstructor("Reference", "web.ReferenceView(Object,Object,FieldItem)"); 164 f.setViewConstructor("Enum", "web.EnumViewer(Object,Object,FieldItem)"); 165 f.setViewConstructor("Field", "web.PrimitiveField"); 166 f.setViewConstructor("Table", "web.Table"); 167 f.setViewConstructor("ChoiceCollection", "web.ChoiceCollection"); 168 f.setViewConstructor("List", "web.List"); 169 f.setViewConstructor("Tree", "web.Tree"); 170 f.setViewConstructor("Tabbed", "web.Tabs"); 171 f.setViewConstructor("Method", "web.Method"); 172 f.setViewConstructor("EmbeddedMethod", "web.EmbeddedMethod"); 173 f.setViewConstructor("Window", "web.Page"); 174 f.setViewConstructor("RefreshWindow", "web.RefreshPage"); 175 f.setViewConstructor("Dialog", "web.Dialog"); 176 f.setViewConstructor("MenuBar", "web.MenuBar"); 177 f.setViewConstructor("StatusBar", "web.StatusBar"); 178 f.setViewConstructor("ToolBar", "web.ToolBar"); 179 f.setViewConstructor("Menu", "web.Menu"); 180 181 f.setViewConstructor("dateHour", "web.DateHourViewer(Object,Object,FieldItem)"); 182 f.setViewConstructor("timestamp", "web.TimestampViewer(Object,Object,FieldItem)"); 183 f.setViewConstructor("java.util.Date", "web.DateViewer(Object,Object,FieldItem)"); 184 185 f.setViewConstructor("Float", "web.FloatViewer(Object,Object,FieldItem)"); 186 f.setViewConstructor("Integer","web.IntViewer(Object,Object,FieldItem)"); 187 f.setViewConstructor("Long","web.IntViewer(Object,Object,FieldItem)"); 188 f.setViewConstructor("int","web.IntViewer(Object,Object,FieldItem)"); 189 f.setViewConstructor("long","web.IntViewer(Object,Object,FieldItem)"); 190 f.setViewConstructor("Double", "web.FloatViewer(Object,Object,FieldItem)"); 191 f.setViewConstructor("float", "web.FloatViewer(Object,Object,FieldItem)"); 192 f.setViewConstructor("double", "web.FloatViewer(Object,Object,FieldItem)"); 193 f.setViewConstructor("percentage", "web.PercentViewer(Object,Object,FieldItem)"); 194 f.setViewConstructor("org.objectweb.jac.util.Matrix", "web.MatrixView(Object,Object,FieldItem)"); 195 196 f.setViewConstructor("imageURL", "web.ImageURLViewer(Object,Object,FieldItem)"); 197 f.setViewConstructor("text", "web.TextViewer"); 198 199 f.setViewConstructor("cell:imageURL", "web.ImageURLViewer()"); 201 f.setViewConstructor("cell:java.util.Date", "web.DateViewer()"); 202 f.setViewConstructor("cell:dateHour", "web.DateHourViewer()"); 203 f.setViewConstructor("cell:timestamp", "web.TimestampViewer()"); 204 f.setViewConstructor("cell:java.lang.Integer", "web.IntViewer()"); 205 f.setViewConstructor("cell:java.lang.Long", "web.IntViewer()"); 206 f.setViewConstructor("cell:int", "web.IntViewer()"); 207 f.setViewConstructor("cell:long", "web.IntViewer()"); 208 f.setViewConstructor("cell:java.lang.Float", "web.FloatViewer()"); 209 f.setViewConstructor("cell:java.lang.Double", "web.FloatViewer()"); 210 f.setViewConstructor("cell:float", "web.FloatViewer()"); 211 f.setViewConstructor("cell:double", "web.FloatViewer()"); 212 f.setViewConstructor("cell:percentage", "web.PercentViewer()"); 213 f.setViewConstructor("cell:Reference", "web.ReferenceView()"); 214 f.setViewConstructor("cell:Enum", "web.EnumViewer()"); 215 f.setViewConstructor("cell:org.objectweb.jac.lib.Attachment", "web.AttachmentViewer()"); 216 f.setViewConstructor("cell:java.util.List", "web.CompactList()"); 217 218 f.setViewConstructor("ParameterContainer", "web.EditorContainer"); 220 f.setViewConstructor("PrimitiveTypeEditor", "web.PrimitiveFieldEditor(Object,FieldItem,boolean)"); 221 f.setViewConstructor("ObjectChooser", "web.ObjectChooser"); 222 f.setViewConstructor("IndexSelector", "web.IndexSelector"); 223 f.setViewConstructor("IndicesSelector", "web.IndicesSelector"); 224 f.setViewConstructor("editor:java.util.Date", "web.DateEditor"); 225 f.setViewConstructor("editor:dateHour", "web.DateHourEditor"); 226 f.setViewConstructor("editor:java.net.URL", "web.URLEditor"); 227 f.setViewConstructor("editor:java.lang.Boolean", "web.BooleanEditor"); 228 f.setViewConstructor("editor:boolean", "web.BooleanEditor"); 229 f.setViewConstructor("editor:text", "web.TextEditor"); 230 f.setViewConstructor("editor:javaCode", "web.TextEditor"); 231 f.setViewConstructor("editor:password", "web.PasswordFieldEditor"); 232 f.setViewConstructor("editor:java.lang.Float", "web.FloatEditor"); 233 f.setViewConstructor("editor:java.lang.Double", "web.FloatEditor"); 234 f.setViewConstructor("editor:java.lang.Integer", "web.PrimitiveFieldEditor(Object,FieldItem)"); 235 f.setViewConstructor("editor:java.lang.Long", "web.PrimitiveFieldEditor(Object,FieldItem)"); 236 f.setViewConstructor("editor:java.lang.Short", "web.PrimitiveFieldEditor(Object,FieldItem)"); 237 f.setViewConstructor("editor:java.lang.Character", "web.PrimitiveFieldEditor(Object,FieldItem)"); 238 f.setViewConstructor("editor:float", "web.FloatEditor"); 239 f.setViewConstructor("editor:double", "web.FloatEditor"); 240 f.setViewConstructor("cell:percentage", "web.PercentEditor(Object,FieldItem)"); 241 f.setViewConstructor("editor:integer", "web.PrimitiveFieldEditor(Object,FieldItem)"); 242 f.setViewConstructor("editor:long", "web.PrimitiveFieldEditor(Object,FieldItem)"); 243 f.setViewConstructor("editor:short", "web.PrimitiveFieldEditor(Object,FieldItem)"); 244 f.setViewConstructor("editor:char", "web.PrimitiveFieldEditor(Object,FieldItem)"); 245 f.setViewConstructor("editor:java.lang.Byte", "web.PrimitiveFieldEditor(Object,FieldItem)"); 246 f.setViewConstructor("editor:java.lang.String", "web.PrimitiveFieldEditor(Object,FieldItem)"); 247 f.setViewConstructor("org.objectweb.jac.lib.Attachment", "web.AttachmentViewer(Object,Object,FieldItem)"); 248 f.setViewConstructor("editor:org.objectweb.jac.lib.Attachment", "web.AttachmentEditor"); 249 f.setViewConstructor("editor:java.io.Reader", "web.ReaderEditor"); 250 f.setViewConstructor("editor:java.io.InputStream", "web.InputStreamEditor"); 251 252 f.setViewConstructor("CollectionItemView", "web.CollectionItemView"); 253 } 254 } 255 256 Hashtable constructors = new Hashtable (); 259 260 268 public View createView(String label, String viewType, Object [] params, 269 DisplayContext context) 270 throws UnhandledViewTypeException 271 { 272 logger.debug("createView(\""+label+"\","+viewType+","+ 273 Arrays.asList(params)+")"); 274 AbstractMethodItem constructor = 275 (AbstractMethodItem)constructors.get(viewType); 276 if (constructor==null) { 277 throw new UnhandledViewTypeException(viewType); 278 } 279 logger.debug(" constructor = "+constructor.getLongName()); 280 Object [] parameters; 281 Class [] paramTypes = constructor.getParameterTypes(); 282 if (paramTypes.length>0 && paramTypes[0]==ViewFactory.class) { 283 logger.debug(" prepending factory to params"); 284 if (paramTypes.length>1 && paramTypes[1]==DisplayContext.class) { 285 logger.debug(" prepending context to params"); 286 parameters = new Object [params.length+2]; 287 System.arraycopy(params,0,parameters,2,params.length); 288 parameters[0] = this; 289 parameters[1] = context; 290 } else { 291 parameters = new Object [params.length+1]; 292 System.arraycopy(params,0,parameters,1,params.length); 293 parameters[0] = this; 294 } 295 } else { 296 parameters = params; 297 } 298 logger.debug(" invoking constructor..."+Arrays.asList(parameters)); 299 if (constructor.getParameterCount()!=parameters.length) { 300 throw new RuntimeException ("Wrong number of arguments for "+ 301 constructor.getLongName()+" ("+Arrays.asList(parameters)+")"); 302 } 303 View result = (View)constructor.invoke(null,parameters); 304 result.setLabel(label); 307 result.setFactory(this); 308 result.setParameters(params); 309 result.setType(viewType); 310 result.setContext(context); 311 logger.debug(" createView("+viewType+","+label+") -> "+result); 312 return result; 313 } 314 315 318 public View createView(String label, String viewType, String [] params, 319 DisplayContext context) 320 throws UnhandledViewTypeException 321 { 322 logger.debug("createView(\""+label+"\","+viewType+","+ 323 Arrays.asList(params)+")"); 324 AbstractMethodItem constructor = 325 (AbstractMethodItem)constructors.get(viewType); 326 if (constructor==null) { 327 throw new UnhandledViewTypeException(viewType); 328 } 329 logger.debug(" constructor = "+constructor); 330 Class [] paramTypes = constructor.getParameterTypes(); 331 Object [] args = new Object [params.length]; 332 int offset = 0; 333 if (paramTypes.length>0 && paramTypes[0]==ViewFactory.class) 334 offset += 1; 335 if (paramTypes.length>1 && paramTypes[1]==DisplayContext.class) 336 offset += 1; 337 338 for (int i=0; i<params.length;i++) { 339 if (String .class.isAssignableFrom(paramTypes[i+offset])) { 340 args[i] = params[i]; 342 } else if (Boolean .class.isAssignableFrom(paramTypes[i+offset]) || 343 boolean.class.isAssignableFrom(paramTypes[i+offset]) ) { 344 args[i] = Boolean.valueOf(params[i]); 345 } else if (Object .class.isAssignableFrom(paramTypes[i+offset])) { 346 args[i] = NameRepository.get().getObject(params[i]); 348 logger.debug(params[i]+" -> "+args[i]); 349 if (args[i]==null) { 350 logger.warn("Could not find object named "+params[i]); 351 } 352 } 353 } 354 return createView(label,viewType,args,context); 355 } 356 357 365 public View createObjectView(String label, Object object, 366 DisplayContext context) { 367 if (object==null) { 368 return createView(label,"Object",new Object [] {GuiAC.DEFAULT_VIEW,object}, 369 context); 370 } 371 ClassItem cl = ClassRepository.get().getClass(object); 372 while (cl!=null) { 373 if (hasViewerFor(cl.getName())) { 374 return createView(label,cl.getName(), 375 new Object [] {object}, context); 376 } 377 cl = cl.getSuperclass(); 378 } 379 return createView(label,"Object",new Object [] {"default",object}, context); 380 } 381 382 392 public View createObjectView(String label, Object object, 393 Object substance, FieldItem field, 394 DisplayContext context) { 395 if (object==null) { 396 return createView(label,"Object",new Object [] {GuiAC.DEFAULT_VIEW,object}, 397 context); 398 } 399 ClassItem cl = ClassRepository.get().getClass(object); 400 while (cl!=null) { 401 if (hasViewerFor(cl.getName())) { 402 return createView(label,cl.getName(), 403 new Object [] {object,substance,field}, context); 404 } 405 cl = cl.getSuperclass(); 406 } 407 return createView(label,"Object",new Object [] {"default",object}, context); 408 } 409 410 public CompositeView createCompositeView(String label, String viewType, 411 Object [] params, 412 DisplayContext context) 413 throws UnhandledViewTypeException 414 { 415 return (CompositeView)createView(label,viewType,params,context); 416 } 417 418 public View createView(String label, String viewType, DisplayContext context) 419 throws UnhandledViewTypeException 420 { 421 return createView(label,viewType,ExtArrays.emptyObjectArray,context); 422 } 423 424 public CompositeView createCompositeView(String label, String viewType, 425 DisplayContext context) 426 throws UnhandledViewTypeException 427 { 428 return (CompositeView)createView(label,viewType,context); 429 } 430 431 public FieldEditor createEditor(String label, String viewType, 432 Object [] params, 433 DisplayContext context) 434 throws UnhandledViewTypeException 435 { 436 return (FieldEditor)createView(label,viewType,params,context); 437 } 438 439 446 public void setViewConstructor(String viewType, AbstractMethodItem constructor) { 447 constructors.put(viewType,constructor); 448 } 449 450 public void setViewConstructor(String viewType, String constructor) 451 { 452 try { 453 setViewConstructor( 454 viewType, 455 (AbstractMethodItem)ACConfiguration.convertValue( 456 constructor, 457 AbstractMethodItem.class, 458 imports)); 459 } catch (Exception e) { 460 logger.error("setViewConstructor("+viewType+","+constructor+") failed",e); 461 } 462 } 463 464 469 public boolean hasViewerFor(String viewType) { 470 return constructors.containsKey(viewType); 471 } 472 473 479 public static class UnhandledViewTypeException extends RuntimeException { 480 483 public UnhandledViewTypeException(String viewType) { 484 super("Unable to build a view of type \""+viewType+"\""); 485 } 486 } 487 488 } 489 | Popular Tags |