KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > layoutsupport > LayoutSupportRegistry


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.layoutsupport;
21
22 import java.awt.*;
23 import java.util.*;
24 import java.lang.ref.*;
25
26 import org.openide.loaders.*;
27 import org.openide.filesystems.*;
28 import org.openide.ErrorManager;
29
30 import org.netbeans.modules.form.FormModel;
31 import org.netbeans.modules.form.FormUtils;
32 import org.netbeans.modules.form.palette.PaletteItem;
33 import org.netbeans.modules.form.palette.PaletteUtils;
34
35 /**
36  * Registry and factory class for LayoutSupportDelegate implementations.
37  *
38  * @author Tomas Pavek
39  */

40
41 public class LayoutSupportRegistry {
42
43     private static Map containerToLayoutDelegate;
44     private static Map layoutToLayoutDelegate;
45
46     private static boolean needPaletteRescan = true;
47
48     public static final String JavaDoc DEFAULT_SUPPORT = "<default>"; // NOI18N
49

50     private static FileChangeListener paletteListener;
51
52     private static Map instanceMap;
53
54     private Reference formModelRef;
55
56     // -------
57

58     private LayoutSupportRegistry(FormModel formModel) {
59         this.formModelRef = new WeakReference(formModel);
60     }
61
62     public static LayoutSupportRegistry getRegistry(FormModel formModel) {
63         LayoutSupportRegistry reg;
64         if (instanceMap == null) {
65             instanceMap = new WeakHashMap();
66             reg = null;
67         }
68         else reg = (LayoutSupportRegistry) instanceMap.get(formModel);
69
70         if (reg == null) {
71             reg = new LayoutSupportRegistry(formModel);
72             instanceMap.put(formModel, reg);
73         }
74
75         return reg;
76     }
77
78     // --------------
79
// get methods
80

81     public Class JavaDoc getSupportClassForContainer(Class JavaDoc containerClass) {
82         String JavaDoc className = (String JavaDoc)
83                            getContainersMap().get(containerClass.getName());
84         if (className == null) {
85             className = findSuperClass(getContainersMap(), containerClass);
86 // if (className == null && needPaletteRescan) {
87
// className = scanPalette(containerClass.getName());
88
// if (className == null) // try container superclass again
89
// className = findSuperClass(getContainersMap(),
90
// containerClass);
91
// }
92
}
93
94         return className != null ? loadClass(className) : null;
95     }
96
97     public String JavaDoc getSupportNameForContainer(String JavaDoc containerClassName) {
98         String JavaDoc className = (String JavaDoc) getContainersMap().get(containerClassName);
99         if (className == null) {
100             Class JavaDoc containerClass = loadClass(containerClassName);
101             if (containerClass != null)
102                 className = findSuperClass(getContainersMap(), containerClass);
103 // if (className == null && needPaletteRescan) {
104
// className = scanPalette(containerClassName);
105
// if (className == null) // try container superclass again
106
// className = findSuperClass(getContainersMap(),
107
// containerClass);
108
// }
109
}
110
111         return className;
112     }
113
114     public Class JavaDoc getSupportClassForLayout(Class JavaDoc layoutClass) {
115         String JavaDoc className = (String JavaDoc) getLayoutsMap().get(layoutClass.getName());
116         if (className == null && needPaletteRescan)
117             className = scanPalette(layoutClass.getName());
118         if (className == null)
119             className = findSuperClass(getLayoutsMap(), layoutClass);
120
121         return className != null ? loadClass(className) : null;
122     }
123
124     public String JavaDoc getSupportNameForLayout(String JavaDoc layoutClassName) {
125         String JavaDoc className = (String JavaDoc) getLayoutsMap().get(layoutClassName);
126         if (className == null && needPaletteRescan)
127             className = scanPalette(layoutClassName);
128         if (className == null) {
129             Class JavaDoc layoutClass = loadClass(layoutClassName);
130             if (layoutClass != null)
131                 className = findSuperClass(getLayoutsMap(), layoutClass);
132         }
133
134         return className;
135     }
136
137     // ------------
138
// registering methods
139

140     public static void registerSupportForContainer(
141                            Class JavaDoc containerClass,
142                            Class JavaDoc layoutDelegateClass)
143     {
144         getContainersMap().put(containerClass.getName(),
145                                layoutDelegateClass.getName());
146     }
147
148     public static void registerSupportForContainer(
149                            String JavaDoc containerClassName,
150                            String JavaDoc layoutDelegateClassName)
151     {
152         getContainersMap().put(containerClassName, layoutDelegateClassName);
153     }
154
155     public static void registertSupportForLayout(
156                            Class JavaDoc layoutClass,
157                            Class JavaDoc layoutDelegateClass)
158     {
159         getLayoutsMap().put(layoutClass.getName(),
160                             layoutDelegateClass.getName());
161     }
162
163     public static void registerSupportForLayout(
164                            String JavaDoc layoutClassName,
165                            String JavaDoc layoutDelegateClassName)
166     {
167         getLayoutsMap().put(layoutClassName, layoutDelegateClassName);
168     }
169
170     // ------------
171
// creation methods
172

173     public LayoutSupportDelegate createSupportForContainer(Class JavaDoc containerClass)
174         throws ClassNotFoundException JavaDoc,
175                InstantiationException JavaDoc,
176                IllegalAccessException JavaDoc
177     {
178         Class JavaDoc delegateClass = getSupportClassForContainer(containerClass);
179         if (delegateClass == null)
180             return null;
181
182         return (LayoutSupportDelegate) delegateClass.newInstance();
183     }
184
185     public LayoutSupportDelegate createSupportForLayout(Class JavaDoc layoutClass)
186         throws ClassNotFoundException JavaDoc,
187                InstantiationException JavaDoc,
188                IllegalAccessException JavaDoc
189     {
190         String JavaDoc delegateClassName = getSupportNameForLayout(layoutClass.getName());
191         if (delegateClassName == null)
192             return null;
193
194         if (delegateClassName == DEFAULT_SUPPORT)
195             return new DefaultLayoutSupport(layoutClass);
196
197         return (LayoutSupportDelegate)
198                loadClass(delegateClassName).newInstance();
199     }
200
201     public static LayoutSupportDelegate createSupportInstance(
202                                             Class JavaDoc layoutDelegateClass)
203         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc
204     {
205         return (LayoutSupportDelegate) layoutDelegateClass.newInstance();
206     }
207
208     // -----------
209
// private methods
210

211     private String JavaDoc findSuperClass(Map map, Class JavaDoc subClass) {
212         for (Iterator it=map.entrySet().iterator(); it.hasNext(); ) {
213             Map.Entry en = (Map.Entry) it.next();
214             String JavaDoc className = (String JavaDoc) en.getKey();
215             Class JavaDoc keyClass = loadClass(className);
216             if (keyClass != null && keyClass.isAssignableFrom(subClass))
217                 return (String JavaDoc) en.getValue();
218         }
219         return null;
220     }
221
222     private static String JavaDoc scanPalette(String JavaDoc wantedClassName) {
223         FileObject paletteFolder = PaletteUtils.getPaletteFolder();
224
225         // create palette content listener - only once
226
boolean newPaletteListener = paletteListener == null;
227         if (newPaletteListener) {
228             paletteListener = new FileChangeAdapter() {
229                 public void fileDataCreated(FileEvent fe) {
230                     needPaletteRescan = true;
231                 }
232                 public void fileFolderCreated(FileEvent fe) {
233                     needPaletteRescan = true;
234                     fe.getFile().addFileChangeListener(this);
235                 }
236                 public void fileDeleted(FileEvent fe) {
237                     fe.getFile().removeFileChangeListener(this);
238                 }
239             };
240
241             paletteFolder.addFileChangeListener(paletteListener);
242         }
243
244         String JavaDoc foundSupportClassName = null;
245
246         FileObject[] paletteCategories = paletteFolder.getChildren();
247         for (int i=0; i < paletteCategories.length; i++) {
248             FileObject categoryFolder = paletteCategories[i];
249             if (!categoryFolder.isFolder())
250                 continue;
251            
252             if (newPaletteListener)
253                 categoryFolder.addFileChangeListener(paletteListener);
254
255             FileObject[] paletteItems = categoryFolder.getChildren();
256             for (int j=0; j < paletteItems.length; j++) {
257                 DataObject itemDO = null;
258                 try {
259                     itemDO = DataObject.find(paletteItems[j]);
260                 }
261                 catch (DataObjectNotFoundException ex) {
262                     continue;
263                 }
264
265                 PaletteItem item = (PaletteItem)
266                                    itemDO.getCookie(PaletteItem.class);
267                 if (item == null || !item.isLayout())
268                     continue;
269
270                 Class JavaDoc itemClass = item.getComponentClass();
271                 if (itemClass == null)
272                     continue; // cannot resolve class - ignore
273

274                 Class JavaDoc delegateClass = null;
275                 Class JavaDoc supportedClass = null;
276
277                 if (LayoutSupportDelegate.class.isAssignableFrom(itemClass)) {
278                     // register LayoutSupportDelegate directly
279
delegateClass = itemClass;
280                     try {
281                         LayoutSupportDelegate delegate =
282                             (LayoutSupportDelegate) delegateClass.newInstance();
283                         supportedClass = delegate.getSupportedClass();
284                     }
285                     catch (Exception JavaDoc ex) {
286                         org.openide.ErrorManager.getDefault().notify(
287                             org.openide.ErrorManager.INFORMATIONAL, ex);
288                         continue; // invalid - ignore
289
}
290                 }
291                 else if (LayoutManager.class.isAssignableFrom(itemClass)) {
292                     // register default support for layout
293
supportedClass = itemClass;
294                 }
295
296                 if (supportedClass != null) {
297                     Map map;
298                     if (Container.class.isAssignableFrom(supportedClass))
299                         map = getContainersMap();
300                     else if (LayoutManager.class.isAssignableFrom(supportedClass))
301                         map = getLayoutsMap();
302                     else continue; // invalid - ignore
303

304                     String JavaDoc supportedClassName = supportedClass.getName();
305                     if (map.get(supportedClassName) == null) {
306                         String JavaDoc delegateClassName = delegateClass != null ?
307                                                      delegateClass.getName():
308                                                      DEFAULT_SUPPORT;
309
310                         map.put(supportedClassName, delegateClassName);
311
312                         if (supportedClassName.equals(wantedClassName))
313                             foundSupportClassName = delegateClassName;
314                     }
315                 }
316             }
317         }
318
319         needPaletteRescan = false;
320         return foundSupportClassName;
321     }
322
323     private Class JavaDoc loadClass(String JavaDoc className) {
324         try {
325             return FormUtils.loadClass(className, (FormModel)formModelRef.get());
326         }
327         catch (Exception JavaDoc ex) {
328             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
329         }
330         catch (LinkageError JavaDoc ex) {
331             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
332         }
333         return null;
334     }
335
336     private static Map getContainersMap() {
337         if (containerToLayoutDelegate == null) {
338             containerToLayoutDelegate = new HashMap();
339             // fill in default containers
340
containerToLayoutDelegate.put(
341                 "javax.swing.JScrollPane", // NOI18N
342
"org.netbeans.modules.form.layoutsupport.delegates.JScrollPaneSupport"); // NOI18N
343
containerToLayoutDelegate.put(
344                 "java.awt.ScrollPane", // NOI18N
345
"org.netbeans.modules.form.layoutsupport.delegates.ScrollPaneSupport"); // NOI18N
346
containerToLayoutDelegate.put(
347                 "javax.swing.JSplitPane", // NOI18N
348
"org.netbeans.modules.form.layoutsupport.delegates.JSplitPaneSupport"); // NOI18N
349
containerToLayoutDelegate.put(
350                 "javax.swing.JTabbedPane", // NOI18N
351
"org.netbeans.modules.form.layoutsupport.delegates.JTabbedPaneSupport"); // NOI18N
352
containerToLayoutDelegate.put(
353                 "javax.swing.JLayeredPane", // NOI18N
354
"org.netbeans.modules.form.layoutsupport.delegates.JLayeredPaneSupport"); // NOI18N
355
containerToLayoutDelegate.put(
356                 "javax.swing.JToolBar", // NOI18N
357
"org.netbeans.modules.form.layoutsupport.delegates.JToolBarSupport"); // NOI18N
358
}
359         return containerToLayoutDelegate;
360     }
361
362     private static Map getLayoutsMap() {
363         if (layoutToLayoutDelegate == null) {
364             layoutToLayoutDelegate = new HashMap();
365             // fill in default layouts
366
layoutToLayoutDelegate.put(
367                 "java.awt.BorderLayout", // NOI18N
368
"org.netbeans.modules.form.layoutsupport.delegates.BorderLayoutSupport"); // NOI18N
369
layoutToLayoutDelegate.put(
370                 "java.awt.FlowLayout", // NOI18N
371
"org.netbeans.modules.form.layoutsupport.delegates.FlowLayoutSupport"); // NOI18N
372
layoutToLayoutDelegate.put(
373                 "javax.swing.BoxLayout", // NOI18N
374
"org.netbeans.modules.form.layoutsupport.delegates.BoxLayoutSupport"); // NOI18N
375
layoutToLayoutDelegate.put(
376                 "java.awt.GridBagLayout", // NOI18N
377
"org.netbeans.modules.form.layoutsupport.delegates.GridBagLayoutSupport"); // NOI18N
378
layoutToLayoutDelegate.put(
379                 "java.awt.GridLayout", // NOI18N
380
"org.netbeans.modules.form.layoutsupport.delegates.GridLayoutSupport"); // NOI18N
381
layoutToLayoutDelegate.put(
382                 "java.awt.CardLayout", // NOI18N
383
"org.netbeans.modules.form.layoutsupport.delegates.CardLayoutSupport"); // NOI18N
384
layoutToLayoutDelegate.put(
385                 "org.netbeans.lib.awtextra.AbsoluteLayout", // NOI18N
386
"org.netbeans.modules.form.layoutsupport.delegates.AbsoluteLayoutSupport"); // NOI18N
387
}
388         return layoutToLayoutDelegate;
389     }
390 }
391
Popular Tags