KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > ViewFactory


1 /*
2   Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
3                           Renaud Pawlak <renaud@aopsys.com>
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18   USA */

19
20 package org.objectweb.jac.aspects.gui;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.Hashtable JavaDoc;
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 /**
36  * This is a generic view factory. It can be configured to use any
37  * constructor to build views of any type.
38  */

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     /**
51      * Init a swing factory by setting the constuctor types.
52      */

53     public static void init(String JavaDoc type, ViewFactory f) {
54         f.setViewConstructor("Object", "GenericFactory.createObjectView");
55         f.setViewConstructor("NoTab", "GenericFactory.createObjectViewNoTab");
56         f.setViewConstructor ("InputParameters", "GenericFactory.createParameters");
57         // f.setViewConstructor("java.lang.Throwable", "ExceptionViewer.createExceptionViewer");
58
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             //editors
100
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             // table cells viewers
133
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             //f.setViewConstructor("cell:List", "swing.()");
153

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             // table cells viewers
200
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             //editors
219
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     // viewType -> constructor
257
// views added in web.acc with setViewConstructor
258
Hashtable JavaDoc constructors = new Hashtable JavaDoc();
259
260     /**
261      * Create a view of a given type.
262      *
263      * @param label the name of the view
264      * @param viewType the type of the view (for instance, "Object" or "Tree".
265      * @param params the parameters to pass to the view constructor
266      * @return a view of the demanded type.
267      */

268     public View createView(String JavaDoc label, String JavaDoc viewType, Object JavaDoc[] 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 JavaDoc[] parameters;
281         Class JavaDoc[] 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 JavaDoc[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 JavaDoc[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 JavaDoc("Wrong number of arguments for "+
301                                        constructor.getLongName()+" ("+Arrays.asList(parameters)+")");
302         }
303         View result = (View)constructor.invoke(null,parameters);
304         // We may have problems here if the view constructor is
305
// blocking (as with dialogs for instance)
306
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     /**
316      * Convert Strings into the type expected by the view constructor.
317      */

318     public View createView(String JavaDoc label, String JavaDoc viewType, String JavaDoc[] 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 JavaDoc[] paramTypes = constructor.getParameterTypes();
331         Object JavaDoc[] args = new Object JavaDoc[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 JavaDoc.class.isAssignableFrom(paramTypes[i+offset])) {
340                 // String: leave as is
341
args[i] = params[i];
342             } else if (Boolean JavaDoc.class.isAssignableFrom(paramTypes[i+offset]) ||
343                        boolean.class.isAssignableFrom(paramTypes[i+offset]) ) {
344                 args[i] = Boolean.valueOf(params[i]);
345             } else if (Object JavaDoc.class.isAssignableFrom(paramTypes[i+offset])) {
346                 // Object: object name
347
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     /**
358      * Creates a view for an object, using the most specific view
359      * constructor available for the type of the object.
360      *
361      * @param label the label of the view to create
362      * @param object the object for which to create a view
363      * @param context the display context
364      */

365     public View createObjectView(String JavaDoc label, Object JavaDoc object,
366                                  DisplayContext context) {
367         if (object==null) {
368             return createView(label,"Object",new Object JavaDoc[] {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 JavaDoc[] {object}, context);
376             }
377             cl = cl.getSuperclass();
378         }
379         return createView(label,"Object",new Object JavaDoc[] {"default",object}, context);
380     }
381
382     /**
383      * Creates a view for an object, using the most specific view
384      * constructor available for the type of the object.
385      *
386      * @param label the label of the view to create
387      * @param object the object for which to create a view
388      * @param substance
389      * @param field
390      * @param context the display context
391      */

392     public View createObjectView(String JavaDoc label, Object JavaDoc object,
393                                  Object JavaDoc substance, FieldItem field,
394                                  DisplayContext context) {
395         if (object==null) {
396             return createView(label,"Object",new Object JavaDoc[] {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 JavaDoc[] {object,substance,field}, context);
404             }
405             cl = cl.getSuperclass();
406         }
407         return createView(label,"Object",new Object JavaDoc[] {"default",object}, context);
408     }
409
410     public CompositeView createCompositeView(String JavaDoc label, String JavaDoc viewType,
411                                              Object JavaDoc[] params,
412                                              DisplayContext context)
413         throws UnhandledViewTypeException
414     {
415         return (CompositeView)createView(label,viewType,params,context);
416     }
417    
418     public View createView(String JavaDoc label, String JavaDoc viewType, DisplayContext context)
419         throws UnhandledViewTypeException
420     {
421         return createView(label,viewType,ExtArrays.emptyObjectArray,context);
422     }
423
424     public CompositeView createCompositeView(String JavaDoc label, String JavaDoc viewType,
425                                              DisplayContext context)
426         throws UnhandledViewTypeException
427     {
428         return (CompositeView)createView(label,viewType,context);
429     }
430
431     public FieldEditor createEditor(String JavaDoc label, String JavaDoc viewType,
432                                     Object JavaDoc[] params,
433                                     DisplayContext context)
434         throws UnhandledViewTypeException
435     {
436         return (FieldEditor)createView(label,viewType,params,context);
437     }
438    
439     /**
440      * Set the constructor for a type of view
441      *
442      * @param viewType the type of the view
443      * @param constructor the constructor to be used to build view of
444      * that type. It should take a ViewFactory as the first parameter
445      */

446     public void setViewConstructor(String JavaDoc viewType, AbstractMethodItem constructor) {
447         constructors.put(viewType,constructor);
448     }
449
450     public void setViewConstructor(String JavaDoc viewType, String JavaDoc constructor)
451     {
452         try {
453             setViewConstructor(
454                 viewType,
455                 (AbstractMethodItem)ACConfiguration.convertValue(
456                     constructor,
457                     AbstractMethodItem.class,
458                     imports));
459         } catch (Exception JavaDoc e) {
460             logger.error("setViewConstructor("+viewType+","+constructor+") failed",e);
461         }
462     }
463
464     /**
465      * Tells wether a view factory is able to build a view for given type.
466      * @param viewType the type of the view
467      * @see #setViewConstructor(String,AbstractMethodItem)
468      */

469     public boolean hasViewerFor(String JavaDoc viewType) {
470         return constructors.containsKey(viewType);
471     }
472
473     /**
474      * Thrown to indicate that a ViewFactory is unable to build a view
475      * of the given type because no constructor is associated to this
476      * type of view.
477      * @see #setViewConstructor(String,AbstractMethodItem)
478      */

479     public static class UnhandledViewTypeException extends RuntimeException JavaDoc {
480         /**
481          * @param viewType the type of view that the factory is unable to build.
482          */

483         public UnhandledViewTypeException(String JavaDoc viewType) {
484             super("Unable to build a view of type \""+viewType+"\"");
485         }
486     }
487
488 }
489
Popular Tags