KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.*;
24 import java.util.ArrayList JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import org.netbeans.modules.form.fakepeer.FakePeerSupport;
27
28 import org.netbeans.modules.form.layoutsupport.*;
29 import org.openide.ErrorManager;
30
31
32 public class RADVisualContainer extends RADVisualComponent implements ComponentContainer {
33     private ArrayList JavaDoc<RADVisualComponent> subComponents = new ArrayList JavaDoc(10);
34     private LayoutSupportManager layoutSupport; // = new LayoutSupportManager();
35
private LayoutNode layoutNode; // [move to LayoutSupportManager?]
36

37     private RADMenuComponent containerMenu;
38
39     private Method JavaDoc containerDelegateGetter;
40     private boolean noContainerDelegate;
41
42
43 // public boolean initialize(FormModel formModel) {
44
// if (super.initialize(formModel)) {
45
// if (getBeanClass() != null)
46
// layoutSupport.initialize(this, formModel.getCodeStructure());
47
// return true;
48
// }
49
// return false;
50
// }
51

52     protected void setBeanInstance(Object JavaDoc beanInstance) {
53 // if (layoutSupport != null && layoutSupport.getLayoutDelegate() != null)
54
// layoutSupport.clearPrimaryContainer();
55

56         containerDelegateGetter = null;
57         noContainerDelegate = false;
58
59         super.setBeanInstance(beanInstance);
60
61         if (layoutSupport != null) // need new layout support for new container bean
62
layoutSupport = new LayoutSupportManager(this, getFormModel().getCodeStructure());
63     }
64
65     void setInModel(boolean in) {
66         boolean alreadyIn = isInModel();
67         super.setInModel(in);
68         if (in && !alreadyIn && layoutSupport != null) {
69             // deferred initialization from pre-creation
70
try {
71                 layoutSupport.initializeLayoutDelegate();
72             }
73             catch (Exception JavaDoc ex) {
74                 // [not reported - but very unlikely to happen - only for new container with custom layout]
75
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
76                 layoutSupport.setUnknownLayoutDelegate(false);
77             }
78         }
79     }
80
81     public void setLayoutSupportDelegate(LayoutSupportDelegate layoutDelegate,
82                                          LayoutManager lmInstance)
83         throws Exception JavaDoc
84     {
85         layoutSupport.setLayoutDelegate(layoutDelegate, lmInstance, false);
86         setLayoutNodeReference(null);
87     }
88
89     public LayoutSupportManager getLayoutSupport() {
90         return layoutSupport;
91     }
92
93 // public boolean isLayoutSupportSet() {
94
// return layoutSupport.getLayoutDelegate() != null;
95
// }
96

97     public static boolean isFreeDesignContainer(RADComponent metacomp) {
98         return metacomp instanceof RADVisualContainer
99                && ((RADVisualContainer)metacomp).getLayoutSupport() == null;
100     }
101
102     public static boolean isInFreeDesign(RADComponent metacomp) {
103         return metacomp instanceof RADVisualComponent
104                && isFreeDesignContainer(metacomp.getParentComponent());
105     }
106
107     void setOldLayoutSupport(boolean old) {
108         if (old) {
109             if (layoutSupport == null) {
110                 layoutSupport = new LayoutSupportManager(this, getFormModel().getCodeStructure());
111             }
112         }
113         else {
114             if (layoutSupport != null) { // clean the layout delegate and related code structre objects
115
try {
116                     layoutSupport.setLayoutDelegate(null, null, false);
117                 } catch (Exception JavaDoc ex) {
118                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
119                 }
120                 refillContainerInstance();
121             }
122             layoutSupport = null;
123             setLayoutNodeReference(null);
124         }
125     }
126
127     private void refillContainerInstance() {
128         Container cont = getContainerDelegate(getBeanInstance());
129         cont.removeAll();
130         cont.setLayout(null); // Issue 77904
131
for (RADVisualComponent sub : subComponents) {
132             Component comp = (Component) sub.getBeanInstance();
133             FakePeerSupport.attachFakePeer(comp);
134             if (comp instanceof Container)
135                 FakePeerSupport.attachFakePeerRecursively((Container)comp);
136             cont.add(comp);
137         }
138     }
139
140 // public boolean hasOldLayoutSupport() {
141
// return layoutSupport != null;
142
// }
143

144     public boolean hasDedicatedLayoutSupport() {
145         return layoutSupport != null && layoutSupport.isDedicated();
146     }
147
148     /**
149      * @return The JavaBean visual container represented by this
150      * RADVisualComponent
151      */

152     
153     public Container getContainerDelegate(Object JavaDoc container) {
154         if (container instanceof RootPaneContainer
155                 && container.getClass().getName().startsWith("javax.swing.")) // NOI18N
156
return ((RootPaneContainer)container).getContentPane();
157         if (container.getClass().equals(JRootPane.class))
158             return ((JRootPane)container).getContentPane();
159
160         Container containerDelegate = (Container) container;
161         Method JavaDoc m = getContainerDelegateMethod();
162         if (m != null) {
163             try {
164                 containerDelegate =
165                     (Container) m.invoke(container, new Object JavaDoc[0]);
166             }
167             catch (Exception JavaDoc ex) {
168                 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
169             }
170         }
171         return containerDelegate;
172     }
173
174     public Method JavaDoc getContainerDelegateMethod() {
175         if (containerDelegateGetter == null && !noContainerDelegate) {
176             String JavaDoc delegateGetterName = getContainerDelegateGetterName();
177             if (delegateGetterName == null
178                 && (RootPaneContainer.class.isAssignableFrom(getBeanClass())
179                     || JRootPane.class.isAssignableFrom(getBeanClass())))
180                 delegateGetterName = "getContentPane"; // NOI18N
181

182             if (delegateGetterName != null) {
183                 try {
184                     containerDelegateGetter =
185                         getBeanClass().getMethod(
186                             delegateGetterName, new Class JavaDoc[0]);
187                 }
188                 catch (NoSuchMethodException JavaDoc ex) {
189                     org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
190                 }
191             }
192             else noContainerDelegate = true;
193         }
194         return containerDelegateGetter;
195     }
196
197     String JavaDoc getContainerDelegateGetterName() {
198         Object JavaDoc value = getBeanInfo().getBeanDescriptor()
199                                         .getValue("containerDelegate"); // NOI18N
200

201         if (value instanceof String JavaDoc)
202             return (String JavaDoc) value;
203         else
204             return null;
205     }
206
207     public void setLayoutNodeReference(LayoutNode node) {
208         this.layoutNode = node;
209     }
210
211     public LayoutNode getLayoutNodeReference() {
212         return layoutNode;
213     }
214
215     boolean shouldHaveLayoutNode() {
216         return layoutSupport != null && layoutSupport.shouldHaveNode();
217     }
218
219     RADMenuComponent getContainerMenu() {
220         return containerMenu;
221     }
222
223     boolean canHaveMenu(Class JavaDoc menuClass) {
224         return (JMenuBar.class.isAssignableFrom(menuClass)
225                   && RootPaneContainer.class.isAssignableFrom(getBeanClass()))
226                ||
227                (MenuBar.class.isAssignableFrom(menuClass)
228                   && Frame.class.isAssignableFrom(getBeanClass())
229                   && !JFrame.class.isAssignableFrom(getBeanClass()));
230     }
231
232     // -----------------------------------------------------------------------------
233
// SubComponents Management
234

235     /** @return visual subcomponents (not the menu component) */
236     public RADVisualComponent[] getSubComponents() {
237         RADVisualComponent[] components = new RADVisualComponent[subComponents.size()];
238         subComponents.toArray(components);
239         return components;
240     }
241
242     public RADVisualComponent getSubComponent(int index) {
243         return (RADVisualComponent) subComponents.get(index);
244     }
245
246     // the following methods implement ComponentContainer interface
247

248     /** @return all subcomponents (including the menu component) */
249     public RADComponent[] getSubBeans() {
250         int n = subComponents.size();
251         if (containerMenu != null)
252             n++;
253
254         RADComponent[] components = new RADComponent[n];
255         subComponents.toArray(components);
256         if (containerMenu != null)
257             components[n-1] = containerMenu;
258
259         return components;
260     }
261
262     public void initSubComponents(RADComponent[] initComponents) {
263         if (subComponents == null)
264             subComponents = new ArrayList JavaDoc(initComponents.length);
265         else {
266             subComponents.clear();
267             subComponents.ensureCapacity(initComponents.length);
268         }
269
270         for (int i=0; i < initComponents.length; i++) {
271             RADComponent metacomp = initComponents[i];
272
273             if (metacomp instanceof RADVisualComponent)
274                 subComponents.add((RADVisualComponent)metacomp);
275             else if (metacomp instanceof RADMenuComponent)
276                 containerMenu = (RADMenuComponent) metacomp; // [what with the current menu?]
277
else
278                 continue; // [just ignore?]
279

280             metacomp.setParentComponent(this);
281         }
282
283         if (layoutSupport == null)
284             refillContainerInstance();
285     }
286
287     public void reorderSubComponents(int[] perm) {
288         RADVisualComponent[] components = new RADVisualComponent[subComponents.size()];
289         LayoutConstraints[] constraints;
290         if (layoutSupport != null) {
291             layoutSupport.removeAll();
292             constraints = new LayoutConstraints[subComponents.size()];
293         }
294         else constraints = null;
295
296         for (int i=0; i < perm.length; i++) {
297             RADVisualComponent metacomp = subComponents.get(i);
298             components[perm[i]] = metacomp;
299             if (constraints != null)
300                 constraints[perm[i]] = layoutSupport.getStoredConstraints(metacomp);
301         }
302
303         subComponents.clear();
304         subComponents.addAll(java.util.Arrays.asList(components));
305
306         if (layoutSupport != null) {
307             layoutSupport.addComponents(components, constraints, 0);
308         }
309         else {
310             refillContainerInstance();
311         }
312     }
313
314     public void add(RADComponent comp) {
315         add(comp, -1);
316     }
317
318     public void add(RADComponent metacomp, int index) {
319         RADVisualComponent visual = metacomp instanceof RADVisualComponent ?
320                                     (RADVisualComponent) metacomp : null;
321         if (visual != null) {
322             if (index == -1) {
323                 index = subComponents.size();
324                 subComponents.add(visual);
325             } else {
326                 subComponents.add(index, visual);
327             }
328             if (layoutSupport == null) {
329                 Component comp = (Component) visual.getBeanInstance();
330                 FakePeerSupport.attachFakePeer(comp);
331                 if (comp instanceof Container)
332                     FakePeerSupport.attachFakePeerRecursively((Container)comp);
333                 getContainerDelegate(getBeanInstance()).add(comp, index);
334             }
335         }
336         else if (metacomp instanceof RADMenuComponent)
337             containerMenu = (RADMenuComponent) metacomp; // [what with the current menu?]
338
else
339             return; // [just ignore?]
340

341         metacomp.setParentComponent(this);
342         if (visual != null) { // force constraints properties creation
343
visual.getConstraintsProperties();
344         }
345     }
346
347     public void remove(RADComponent comp) {
348         if (comp instanceof RADVisualComponent) {
349             int index = subComponents.indexOf(comp);
350             if (layoutSupport != null) {
351                 layoutSupport.removeComponent((RADVisualComponent) comp, index);
352             }
353             else {
354                 getContainerDelegate(getBeanInstance()).remove(index);
355             }
356             if (subComponents.remove(comp))
357                 comp.setParentComponent(null);
358         }
359         else if (comp == containerMenu) {
360             containerMenu = null;
361             comp.setParentComponent(null);
362         }
363         else return;
364     }
365
366     public int getIndexOf(RADComponent comp) {
367         if (comp != null && comp == containerMenu)
368             return subComponents.size();
369
370         return subComponents.lastIndexOf(comp);
371     }
372 }
373
Popular Tags