KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > CreationFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form;
21
22 import java.awt.*;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.util.*;
25 import javax.swing.*;
26 import javax.swing.border.Border JavaDoc;
27 import org.openide.ErrorManager;
28
29 import org.netbeans.modules.form.fakepeer.FakePeerSupport;
30
31 /**
32  * Factory class for creating objects, providing java creation code,
33  * registering CreationDescriptor classes, and related utility methods.
34  *
35  * @author Tomas Pavek
36  */

37
38 public class CreationFactory {
39
40     private static HashMap registry;
41
42     private static boolean defaultDescriptorsCreated = false;
43
44     interface PropertyParameters {
45      
46         public String JavaDoc getPropertyName();
47         
48         public String JavaDoc getJavaParametersString(FormProperty prop);
49
50         public Object JavaDoc[] getPropertyParametersValues(FormProperty prop);
51         
52         public Class JavaDoc[] getPropertyParametersTypes();
53     }
54     
55     static class Property2ParametersMapper {
56         
57         private final String JavaDoc propertyName;
58         private final Class JavaDoc[] propertyType = new Class JavaDoc[1];
59         private PropertyParameters parameters;
60         
61         Property2ParametersMapper(Class JavaDoc propertyClass, String JavaDoc propertyName) {
62             this.propertyType[0] = propertyClass;
63             this.propertyName = propertyName;
64         }
65         
66         public String JavaDoc getPropertyName() {
67             return propertyName;
68         }
69
70         public Class JavaDoc[] getPropertyTypes() {
71             if(parameters!=null){
72                 return parameters.getPropertyParametersTypes();
73             }
74             return propertyType;
75         }
76         
77         public String JavaDoc getJavaParametersString(FormProperty prop) {
78             if(parameters!=null){
79                 return parameters.getJavaParametersString(prop);
80             }
81             return prop.getJavaInitializationString();
82         }
83
84         public Object JavaDoc[] getPropertyParametersValues(FormProperty prop) {
85             if(parameters!=null){
86                 return parameters.getPropertyParametersValues(prop);
87             }
88             try{
89                 return new Object JavaDoc[] { prop.getRealValue() };
90             } catch(InvocationTargetException JavaDoc ite) {
91                 ErrorManager.getDefault().notify(ite);
92             } catch(IllegalAccessException JavaDoc iae){
93                 ErrorManager.getDefault().notify(iae);
94             }
95             return new Object JavaDoc[] {};
96         }
97         
98         public void setPropertyParameters(PropertyParameters parameters) {
99             this.parameters = parameters;
100         }
101     }
102     
103     private CreationFactory() {}
104
105     // -----------
106
// registry methods
107

108     public static CreationDescriptor getDescriptor(Class JavaDoc cls) {
109         CreationDescriptor cd = (CreationDescriptor)
110                                 getRegistry().get(cls.getName());
111         if (cd == null && !defaultDescriptorsCreated
112                 && (cls.getName().startsWith("javax.swing.") // NOI18N
113
|| cls.getName().startsWith("java.awt."))) { // NOI18N
114
createDefaultDescriptors();
115             cd = (CreationDescriptor)getRegistry().get(cls.getName());
116         }
117         return cd;
118     }
119
120     public static void registerDescriptor(CreationDescriptor desc) {
121         getRegistry().put(desc.getDescribedClass().getName(), desc);
122     }
123     
124     public static void unregisterDescriptor(CreationDescriptor desc) {
125         if (registry != null)
126             registry.remove(desc.getDescribedClass().getName());
127     }
128
129     // -----------
130
// creation methods
131

132     public static Object JavaDoc createDefaultInstance(final Class JavaDoc cls)
133         throws Exception JavaDoc
134     {
135         CreationDescriptor cd = getDescriptor(cls);
136         Object JavaDoc instance = cd != null ?
137                               cd.createDefaultInstance() :
138                               cls.newInstance();
139
140         initAfterCreation(instance);
141         return instance;
142     }
143
144     public static Object JavaDoc createInstance(Class JavaDoc cls)
145         throws Exception JavaDoc
146     {
147         Object JavaDoc instance;
148
149         CreationDescriptor cd = CreationFactory.getDescriptor(cls);
150         instance = cd != null ? cd.createDefaultInstance() :
151                                 cls.newInstance();
152
153         initAfterCreation(instance);
154         return instance;
155     }
156
157     public static Object JavaDoc createInstance(Class JavaDoc cls,
158                                         FormProperty[] props,
159                                         int style)
160         throws Exception JavaDoc
161     {
162         CreationDescriptor cd = getDescriptor(cls);
163         if (cd == null)
164             return null;
165
166         CreationDescriptor.Creator creator = cd.findBestCreator(props, style);
167         if (creator == null)
168             return null;
169
170         Object JavaDoc instance = creator.createInstance(props);
171         initAfterCreation(instance);
172         return instance;
173     }
174
175     public static String JavaDoc getJavaCreationCode(Class JavaDoc cls,
176                                              FormProperty[] props,
177                                              int style) {
178         CreationDescriptor cd = getDescriptor(cls);
179         if (cd != null) {
180             CreationDescriptor.Creator creator = cd.findBestCreator(props, style);
181             if (creator != null)
182                    creator.getJavaCreationCode(props, null);
183         }
184         return null;
185     }
186
187     // ------------
188
// utility methods
189

190     public static FormProperty[] getPropertiesForCreator(
191                                            CreationDescriptor.Creator creator,
192                                            FormProperty[] properties) {
193
194         String JavaDoc[] propNames = creator.getPropertyNames();
195         FormProperty[] crProps = new FormProperty[propNames.length];
196
197         for (int i=0; i < propNames.length; i++) {
198             String JavaDoc propName = propNames[i];
199             for (int j=0; j < properties.length; j++)
200                 if (propName.equals(properties[j].getName())) {
201                     crProps[i] = properties[j];
202                     break;
203                 }
204             if (crProps[i] == null)
205                 return null; // missing property, should not happen
206
}
207
208         return crProps;
209     }
210
211     public static FormProperty[] getRemainingProperties(
212                                            CreationDescriptor.Creator creator,
213                                            FormProperty[] properties) {
214
215         String JavaDoc[] propNames = creator.getPropertyNames();
216         FormProperty[] remProps = new FormProperty[properties.length - propNames.length];
217         if (remProps.length == 0) return remProps;
218
219         int ii = 0;
220         for (int i=0; i < properties.length; i++) {
221             String JavaDoc propName = properties[i].getName();
222             for (int j=0; j < propNames.length; j++) {
223                 if (propName.equals(propNames[j])) break;
224                 if (j+1 == propNames.length) {
225                     if (ii > remProps.length)
226                         return null; // should not happen
227
remProps[ii++] = properties[i];
228                 }
229             }
230         }
231
232         return remProps;
233     }
234
235     public static boolean containsProperty(CreationDescriptor desc,
236                                            String JavaDoc propName)
237     {
238         CreationDescriptor.Creator[] creators = desc.getCreators();
239         if (creators == null)
240             return false;
241
242         for (int i=0; i < creators.length; i++) {
243             String JavaDoc[] propNames = creators[i].getPropertyNames();
244             for (int j=0; j < propNames.length; j++)
245                 if (propNames[j].equals(propName))
246                     return true;
247         }
248         return false;
249     }
250
251     public static boolean containsProperty(CreationDescriptor.Creator creator,
252                                            String JavaDoc propName)
253     {
254         String JavaDoc[] propNames = creator.getPropertyNames();
255         for (int j=0; j < propNames.length; j++)
256             if (propNames[j].equals(propName))
257                 return true;
258         return false;
259     }
260
261     public static FormProperty findProperty(String JavaDoc propName,
262                                             FormProperty[] properties) {
263         for (int i=0; i < properties.length; i++)
264             if (properties[i].getName().equals(propName))
265                 return properties[i];
266         return null;
267     }
268
269     public static CreationDescriptor.Creator findCreator(
270                                                  CreationDescriptor desc,
271                                                  Class JavaDoc[] paramTypes)
272     {
273         CreationDescriptor.Creator[] creators = desc.getCreators();
274         for (int i=0; i < creators.length; i++) {
275             CreationDescriptor.Creator cr = creators[i];
276             if (cr.getParameterCount() == paramTypes.length) {
277                 Class JavaDoc[] types = cr.getParameterTypes();
278                 boolean match = true;
279                 for (int j=0; j < types.length; j++)
280                     if (!types[j].isAssignableFrom(paramTypes[j])) {
281                         match = false;
282                         break;
283                     }
284                 if (match)
285                     return cr;
286             }
287         }
288         return null;
289     }
290
291     /** Evaluates creators for array of properties.
292      * (Useful for CreationDescriptor.findBestCreator(...) implementation.)
293      * @return array of int - for each creator a count of placed properties
294      */

295     public static int[] evaluateCreators(CreationDescriptor.Creator[] creators,
296                                          FormProperty[] properties,
297                                          boolean changedOnly) {
298
299         if (creators == null || creators.length == 0) return null;
300
301         int[] placed = new int[creators.length];
302         for (int i=0; i < properties.length; i++) {
303             if (!changedOnly || properties[i].isChanged()) {
304                 String JavaDoc name = properties[i].getName();
305
306                 for (int j=0; j < creators.length; j++) {
307                     String JavaDoc[] crNames = creators[j].getPropertyNames();
308                     for (int k=0; k < crNames.length; k++)
309                         if (name.equals(crNames[k]))
310                             placed[j]++;
311                 }
312             }
313         }
314         return placed;
315     }
316
317     /** Finds the best creator upon given evaluation.
318      * (Useful for CreationDescriptor.findBestCreator(...) implementation.)
319      * @return index of most suitable creator
320      */

321     public static int getBestCreator(CreationDescriptor.Creator[] creators,
322                                      FormProperty[] properties,
323                                      int[] placed,
324                                      boolean placeAllProps)
325     {
326         if (creators == null || creators.length == 0)
327             return -1;
328
329         int best = 0;
330         int[] sizes = new int[creators.length];
331         sizes[0] = creators[0].getParameterCount();
332
333         if (placeAllProps) {
334             // find shortest creator with all properties placed
335
for (int i=1; i < placed.length; i++) {
336                 sizes[i] = creators[i].getParameterCount();
337                 if (placed[i] > placed[best]
338                     || (placed[i] == placed[best]
339                         && (sizes[i] < sizes[best]
340                             || (sizes[i] == sizes[best]
341                                 && compareCreatorsAmbiguity(
342                                      creators[i], creators[best], properties)
343                                    == 1))))
344                     best = i;
345             }
346         }
347         else { // find longest creator with all parameters provided by properties
348
for (int i=1; i < placed.length; i++) {
349                 sizes[i] = creators[i].getParameterCount();
350                 int iDiff = sizes[i] - placed[i];
351                 int bestDiff = sizes[best] - placed[best];
352                 if (iDiff < bestDiff
353                     || (iDiff == bestDiff
354                         && (sizes[i] > sizes[best]
355                             || (sizes[i] == sizes[best]
356                                 && compareCreatorsAmbiguity(
357                                      creators[i], creators[best], properties)
358                                    == 1))))
359                     best = i;
360             }
361         }
362
363         return best;
364     }
365
366     // -----------
367
// non-public methods
368

369     /** Compares two creators with equal number of placed properties and equal
370      * number of all properties. To distinguish which one is better, their
371      * properties are checked for null values which could cause ambiguity in
372      * generated code.
373      * @return 1 if creator1 is better, 2 if creator2 is better, 0 if they
374      * are equal
375      */

376     static int compareCreatorsAmbiguity(CreationDescriptor.Creator cr1,
377                                         CreationDescriptor.Creator cr2,
378                                         FormProperty[] properties)
379     {
380         int nullValues1 = 0;
381         int nullValues2 = 0;
382
383         for (int i=0, n=cr1.getParameterCount(); i < n; i++) {
384             String JavaDoc name1 = cr1.getPropertyNames()[i];
385             String JavaDoc name2 = cr2.getPropertyNames()[i];
386             if (!name1.equals(name2)) {
387                 FormProperty prop1 = null;
388                 FormProperty prop2 = null;
389                 for (int j=0; j < properties.length; j++)
390                     if (prop1 == null && name1.equals(properties[j].getName())) {
391                         prop1 = properties[j];
392                         if (prop2 != null)
393                             break;
394                     }
395                     else if (prop2 == null && name2.equals(properties[j].getName())) {
396                         prop2 = properties[j];
397                         if (prop1 != null)
398                             break;
399                     }
400
401                 if (prop1 != null && !prop1.getValueType().isPrimitive()) {
402                     try {
403                         if (prop1.getRealValue() == null)
404                             nullValues1++;
405                     }
406                     catch (Exception JavaDoc ex) {} // ignore
407
}
408                 if (prop2 != null && !prop2.getValueType().isPrimitive()) {
409                     try {
410                         if (prop2.getRealValue() == null)
411                             nullValues2++;
412                     }
413                     catch (Exception JavaDoc ex) {} // ignore
414
}
415             }
416         }
417
418         if (nullValues1 == nullValues2)
419             return 0;
420         return nullValues1 < nullValues2 ? 1 : 2;
421     }
422
423     static HashMap getRegistry() {
424         if (registry == null)
425             registry = new HashMap(40);
426         return registry;
427     }
428
429     // additional initializations for some components - in fact hacks required
430
// by using fake peers and remapping L&F...
431
private static void initAfterCreation(Object JavaDoc instance) {
432         if (instance instanceof javax.swing.border.TitledBorder JavaDoc)
433             ((javax.swing.border.TitledBorder JavaDoc)instance)
434                 .setTitleFont(UIManager.getFont("TitledBorder.createDefaultInstancefont")); // NOI18N
435
else if (instance instanceof java.awt.Component JavaDoc
436                  && !(instance instanceof javax.swing.JComponent JavaDoc)
437                  && !(instance instanceof javax.swing.RootPaneContainer JavaDoc))
438         {
439             ((Component)instance).setName(null);
440             ((Component)instance).setFont(FakePeerSupport.getDefaultAWTFont());
441         }
442         else if (instance instanceof MenuComponent) {
443             ((MenuComponent)instance).setName(null);
444             ((MenuComponent)instance).setFont(FakePeerSupport.getDefaultAWTFont());
445         }
446     }
447
448     // ---------------------------------------------------
449
// constructors descriptors for some "special" classes...
450
private static void createDefaultDescriptors() {
451         Class JavaDoc[][] constrParamTypes;
452         String JavaDoc[][] constrPropertyNames;
453         Object JavaDoc[] defaultConstrParams;
454         String JavaDoc methodName;
455         CreationDescriptor cd;
456         InsetsPropertyParameters[] insetsPropertyParameters =
457                 new InsetsPropertyParameters[] { new InsetsPropertyParameters("borderInsets") };
458                 
459         try {
460         // borders ------------
461

462         // LineBorder
463
constrParamTypes = new Class JavaDoc[][] {
464             { Color.class, Integer.TYPE , Boolean.TYPE }
465
466         };
467         constrPropertyNames = new String JavaDoc[][] {
468             { "lineColor", "thickness" , "roundedCorners" }
469         };
470                  
471         defaultConstrParams = new Object JavaDoc[] { java.awt.Color.black };
472         cd = new CreationDescriptor();
473         cd.addConstructorCreators(
474                 javax.swing.border.LineBorder JavaDoc.class,
475                 constrParamTypes, constrPropertyNames, defaultConstrParams);
476                 
477         constrParamTypes = new Class JavaDoc[][] {
478             { Color.class },
479             { Color.class, Integer.TYPE }
480         };
481         constrPropertyNames = new String JavaDoc[][] {
482             { "lineColor" },
483             { "lineColor", "thickness" }
484             
485         };
486         methodName = "createLineBorder";
487         
488         cd.addMethodCreators(
489                 javax.swing.BorderFactory JavaDoc.class, javax.swing.border.LineBorder JavaDoc.class, methodName,
490                 constrParamTypes, constrPropertyNames, null, defaultConstrParams);
491         registerDescriptor(cd);
492                 
493         
494         // EtchedBorder
495
defaultConstrParams = new Object JavaDoc[] { };
496         constrParamTypes = new Class JavaDoc[][] {
497             { },
498             { Color.class, Color.class },
499             { Integer.TYPE },
500             { Integer.TYPE, Color.class, Color.class }
501         };
502         constrPropertyNames = new String JavaDoc[][] {
503             { },
504             { "highlightColor", "shadowColor" },
505             { "etchType" },
506             { "etchType", "highlightColor", "shadowColor" }
507         };
508         
509         methodName = "createEtchedBorder";
510         registerDescriptor(new CreationDescriptor(
511                 javax.swing.BorderFactory JavaDoc.class, javax.swing.border.EtchedBorder JavaDoc.class, methodName,
512                 constrParamTypes, constrPropertyNames, null, defaultConstrParams));
513         
514         // EmptyBorder
515
constrParamTypes = new Class JavaDoc[][] {
516             { Insets.class }
517         };
518         constrPropertyNames = new String JavaDoc[][] {
519             { "borderInsets" }
520         };
521         
522         defaultConstrParams = new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(1), new Integer JavaDoc(1), new Integer JavaDoc(1) };
523         methodName = "createEmptyBorder";
524         
525         registerDescriptor(new CreationDescriptor(
526                                    javax.swing.BorderFactory JavaDoc.class, javax.swing.border.EmptyBorder JavaDoc.class,
527                                    methodName, constrParamTypes, constrPropertyNames, insetsPropertyParameters, defaultConstrParams));
528
529         // TitledBorder
530
constrParamTypes = new Class JavaDoc[][] {
531             { String JavaDoc.class },
532             { Border JavaDoc.class, String JavaDoc.class },
533             { Border JavaDoc.class, String JavaDoc.class, Integer.TYPE, Integer.TYPE },
534             { Border JavaDoc.class, String JavaDoc.class, Integer.TYPE, Integer.TYPE, Font.class },
535             { Border JavaDoc.class, String JavaDoc.class, Integer.TYPE, Integer.TYPE, Font.class, Color.class },
536             { Border JavaDoc.class }
537         };
538         constrPropertyNames = new String JavaDoc[][] {
539             { "title" },
540             { "border", "title" },
541             { "border", "title", "titleJustification", "titlePosition" },
542             { "border", "title", "titleJustification", "titlePosition", "titleFont" },
543             { "border", "title", "titleJustification", "titlePosition", "titleFont", "titleColor" },
544             { "border" }
545         };
546         
547         defaultConstrParams = new Object JavaDoc[] { null, "", new Integer JavaDoc(0), new Integer JavaDoc(0) };
548         methodName = "createTitledBorder";
549         registerDescriptor(new CreationDescriptor(
550                 javax.swing.BorderFactory JavaDoc.class, javax.swing.border.TitledBorder JavaDoc.class, methodName,
551                 constrParamTypes, constrPropertyNames, null, defaultConstrParams));
552
553         // CompoundBorder
554
constrParamTypes = new Class JavaDoc[][] {
555             { },
556             { Border JavaDoc.class, Border JavaDoc.class }
557         };
558         constrPropertyNames = new String JavaDoc[][] {
559             { },
560             { "outsideBorder", "insideBorder" }
561         };
562         
563         defaultConstrParams = new Object JavaDoc[0];
564         methodName = "createCompoundBorder";
565         registerDescriptor(new CreationDescriptor(
566                 javax.swing.BorderFactory JavaDoc.class, javax.swing.border.CompoundBorder JavaDoc.class, methodName,
567                 constrParamTypes, constrPropertyNames, null, defaultConstrParams));
568
569         // BevelBorder
570
constrParamTypes = new Class JavaDoc[][] {
571             { Integer.TYPE },
572             { Integer.TYPE, Color.class, Color.class },
573             { Integer.TYPE, Color.class, Color.class, Color.class, Color.class }
574         };
575         constrPropertyNames = new String JavaDoc[][] {
576             { "bevelType" },
577             { "bevelType", "highlightOuterColor", "shadowOuterColor" },
578             { "bevelType", "highlightOuterColor", "highlightInnerColor",
579                            "shadowOuterColor", "shadowInnerColor" }
580         };
581                       
582         defaultConstrParams = new Object JavaDoc[] { new Integer JavaDoc(javax.swing.border.BevelBorder.RAISED) };
583         methodName = "createBevelBorder";
584         registerDescriptor(new CreationDescriptor(
585                 javax.swing.BorderFactory JavaDoc.class, javax.swing.border.BevelBorder JavaDoc.class, methodName,
586                 constrParamTypes, constrPropertyNames, null, defaultConstrParams));
587                          
588         // SoftBevelBorder
589
constrParamTypes = new Class JavaDoc[][] {
590             { Integer.TYPE },
591             { Integer.TYPE, Color.class, Color.class },
592             { Integer.TYPE, Color.class, Color.class, Color.class, Color.class }
593         };
594         constrPropertyNames = new String JavaDoc[][] {
595             { "bevelType" },
596             { "bevelType", "highlightOuterColor", "shadowOuterColor" },
597             { "bevelType", "highlightOuterColor", "highlightInnerColor",
598                            "shadowOuterColor", "shadowInnerColor" }
599         };
600         registerDescriptor(new CreationDescriptor(
601                 javax.swing.border.SoftBevelBorder JavaDoc.class,
602                 constrParamTypes, constrPropertyNames, defaultConstrParams));
603
604         // MatteBorder
605
cd = new CreationDescriptor();
606
607         constrParamTypes = new Class JavaDoc[][] {
608             { Icon.class }
609         };
610         constrPropertyNames = new String JavaDoc[][] {
611             { "tileIcon" }
612         };
613         cd.addConstructorCreators( javax.swing.border.MatteBorder JavaDoc.class,
614                                    constrParamTypes, constrPropertyNames, defaultConstrParams);
615         
616         constrParamTypes = new Class JavaDoc[][] {
617             { Insets.class, Icon.class },
618             { Insets.class, Color.class }
619         };
620         constrPropertyNames = new String JavaDoc[][] {
621             { "borderInsets", "tileIcon" },
622             { "borderInsets", "matteColor" }
623         };
624         defaultConstrParams = new Object JavaDoc[] {
625             new Integer JavaDoc(1), new Integer JavaDoc(1), new Integer JavaDoc(1), new Integer JavaDoc(1),
626             java.awt.Color.black
627         };
628         methodName = "createMatteBorder";
629         cd.addMethodCreators(javax.swing.BorderFactory JavaDoc.class, javax.swing.border.MatteBorder JavaDoc.class, methodName,
630                                 constrParamTypes, constrPropertyNames, insetsPropertyParameters, defaultConstrParams);
631         registerDescriptor(cd);
632
633         // layouts --------------
634

635         // BorderLayout
636
constrParamTypes = new Class JavaDoc[][] {
637             { },
638             { Integer.TYPE, Integer.TYPE }
639         };
640         constrPropertyNames = new String JavaDoc[][] {
641             { },
642             { "hgap", "vgap" }
643         };
644         defaultConstrParams = new Object JavaDoc[0];
645         registerDescriptor(new CreationDescriptor(
646                 java.awt.BorderLayout JavaDoc.class,
647                 constrParamTypes, constrPropertyNames, defaultConstrParams));
648
649         // FlowLayout
650
constrParamTypes = new Class JavaDoc[][] {
651             { },
652             { Integer.TYPE },
653             { Integer.TYPE, Integer.TYPE, Integer.TYPE }
654         };
655         constrPropertyNames = new String JavaDoc[][] {
656             { },
657             { "alignment" },
658             { "alignment", "hgap", "vgap" },
659         };
660         registerDescriptor(new CreationDescriptor(
661                 java.awt.FlowLayout JavaDoc.class,
662                 constrParamTypes, constrPropertyNames, defaultConstrParams));
663
664         // GridBagLayout
665
constrParamTypes = new Class JavaDoc[][] {
666             { }
667         };
668         constrPropertyNames = new String JavaDoc[][] {
669             { }
670         };
671         registerDescriptor(new CreationDescriptor(
672                 java.awt.GridBagLayout JavaDoc.class,
673                 constrParamTypes, constrPropertyNames, defaultConstrParams));
674
675         // GridLayout
676
constrParamTypes = new Class JavaDoc[][] {
677             { },
678             { Integer.TYPE, Integer.TYPE },
679             { Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE }
680         };
681         constrPropertyNames = new String JavaDoc[][] {
682             { },
683             { "rows", "columns" },
684             { "rows", "columns", "hgap", "vgap" }
685         };
686         registerDescriptor(new CreationDescriptor(
687                 java.awt.GridLayout JavaDoc.class,
688                 constrParamTypes, constrPropertyNames, defaultConstrParams));
689
690         // CardLayout
691
constrParamTypes = new Class JavaDoc[][] {
692             { },
693             { Integer.TYPE, Integer.TYPE }
694         };
695         constrPropertyNames = new String JavaDoc[][] {
696             { },
697             { "hgap", "vgap" }
698         };
699         registerDescriptor(new CreationDescriptor(
700                 java.awt.CardLayout JavaDoc.class,
701                 constrParamTypes, constrPropertyNames, defaultConstrParams));
702
703         // AWT --------
704

705         // Dialog
706
constrParamTypes = new Class JavaDoc[][] {
707             { java.awt.Frame JavaDoc.class },
708         };
709         constrPropertyNames = new String JavaDoc[][] {
710             { "owner" },
711         };
712         defaultConstrParams = new Object JavaDoc[] { new java.awt.Frame JavaDoc() };
713         registerDescriptor(new CreationDescriptor(
714             java.awt.Dialog JavaDoc.class,
715             constrParamTypes, constrPropertyNames, defaultConstrParams));
716
717         // other -------
718

719         // JPanel on JDK 1.3 uses one instance of FlowLayout for all instances
720
// created by default constructor - this causes problems
721
registerDescriptor(
722             new CreationDescriptor(javax.swing.JPanel JavaDoc.class) {
723                 public Object JavaDoc createDefaultInstance() {
724                     return new javax.swing.JPanel JavaDoc(new java.awt.FlowLayout JavaDoc());
725                 }
726             }
727         );
728
729         // ----------
730

731         defaultDescriptorsCreated = true;
732
733         }
734         catch (NoSuchMethodException JavaDoc ex) { // should not happen
735
ex.printStackTrace();
736         }
737     }
738     
739     static class InsetsPropertyParameters implements PropertyParameters {
740            
741         private static Class JavaDoc[] parameterTypes = new Class JavaDoc[] {Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE};
742         private final String JavaDoc propertyName;
743         
744         public InsetsPropertyParameters(String JavaDoc propertyName) {
745             this.propertyName = propertyName;
746         }
747         
748         public String JavaDoc getPropertyName() {
749             return propertyName;
750         }
751         
752         public String JavaDoc getJavaParametersString(FormProperty prop) {
753             Insets insets = (Insets) getRealValue (prop);
754             if(insets != null) {
755                 return insets.top + ", " + insets.left + ", " + insets.bottom + ", " + insets.right;
756             } else {
757                 return "";
758             }
759         }
760         public Object JavaDoc[] getPropertyParametersValues(FormProperty prop) {
761             Insets insets = (Insets) getRealValue(prop);
762             if(insets != null) {
763                 return new Object JavaDoc[] { new Integer JavaDoc(insets.top), new Integer JavaDoc(insets.left), new Integer JavaDoc(insets.bottom), new Integer JavaDoc(insets.right)};
764             } else {
765                 return new Object JavaDoc[] { };
766             }
767         }
768         
769         public Class JavaDoc[] getPropertyParametersTypes() {
770             return parameterTypes;
771         }
772         
773         private static Object JavaDoc getRealValue(FormProperty prop){
774             try {
775                 return prop.getRealValue();
776             } catch(InvocationTargetException JavaDoc ite) {
777                 ErrorManager.getDefault().notify(ite);
778             } catch(IllegalAccessException JavaDoc iae){
779                 ErrorManager.getDefault().notify(iae);
780             }
781             return null;
782         }
783     }
784     
785     
786 }
787
Popular Tags