KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.*;
24 import java.util.*;
25
26 import org.openide.nodes.*;
27
28 import org.netbeans.modules.form.*;
29 import org.netbeans.modules.form.codestructure.*;
30 import org.netbeans.modules.form.layoutsupport.delegates.NullLayoutSupport;
31 import org.netbeans.modules.form.fakepeer.FakePeerSupport;
32
33 /**
34  * Main class of general layout support infrastructure. Connects form editor
35  * metadata with specialized LayoutSupportDelegate implementations (layout
36  * specific functionality is delegated to the right LayoutSupportDelegate).
37  *
38  * @author Tomas Pavek
39  */

40
41 public final class LayoutSupportManager implements LayoutSupportContext {
42
43     // possible component resizing directions (bit flag constants)
44
public static final int RESIZE_UP = 1;
45     public static final int RESIZE_DOWN = 2;
46     public static final int RESIZE_LEFT = 4;
47     public static final int RESIZE_RIGHT = 8;
48
49     private LayoutSupportDelegate layoutDelegate;
50     private boolean needInit;
51     private boolean initializeFromInstance;
52     private boolean initializeFromCode;
53
54     private Node.PropertySet[] propertySets;
55
56     private LayoutListener layoutListener;
57
58     private RADVisualContainer metaContainer;
59
60     private Container primaryContainer; // bean instance from metaContainer
61
private Container primaryContainerDelegate; // container delegate for it
62

63     private CodeStructure codeStructure;
64
65     private CodeExpression containerCodeExpression;
66     private CodeExpression containerDelegateCodeExpression;
67
68     // ----------
69
// initialization
70

71     // initialization for a new container, layout delegate is set to null
72
public LayoutSupportManager(RADVisualContainer container,
73                                 CodeStructure codeStructure)
74     {
75         this.metaContainer = container;
76         this.codeStructure = codeStructure;
77     }
78
79     /**
80      * Creation and initialization of a layout delegate for a new container.
81      * @return false if suitable layout delegate is not found
82      * @throw IllegalArgumentException if the container instance is not empty
83      */

84     public boolean prepareLayoutDelegate(boolean fromCode, boolean initialize)
85         throws Exception JavaDoc
86     {
87         LayoutSupportDelegate delegate = null;
88         LayoutManager lmInstance = null;
89
90         FormModel formModel = metaContainer.getFormModel();
91         LayoutSupportRegistry layoutRegistry =
92             LayoutSupportRegistry.getRegistry(formModel);
93
94         // first try to find a dedicated layout delegate (for the container)
95
Class JavaDoc layoutDelegateClass = layoutRegistry.getSupportClassForContainer(
96                                                   metaContainer.getBeanClass());
97
98         if (layoutDelegateClass != null) {
99             delegate = layoutRegistry.createSupportInstance(layoutDelegateClass);
100             if (!fromCode && !delegate.checkEmptyContainer(getPrimaryContainer())) {
101                 RuntimeException JavaDoc ex = new IllegalArgumentException JavaDoc();
102                 org.openide.ErrorManager.getDefault().annotate(
103                     ex, AbstractLayoutSupport.getBundle().getString(
104                                         "MSG_ERR_NonEmptyContainer")); // NOI18N
105
throw ex;
106             }
107         }
108         else {
109             // find a general layout delegate (for LayoutManager of the container)
110
if (fromCode) { // initialization from code
111
Iterator it = CodeStructure.getDefinedStatementsIterator(
112                                       getContainerDelegateCodeExpression());
113                 CodeStatement[] statements =
114                     CodeStructure.filterStatements(
115                         it, AbstractLayoutSupport.getSetLayoutMethod());
116
117                 if (statements.length > 0) { // setLayout method found
118
CodeExpressionOrigin layoutOrigin =
119                         statements[0].getStatementParameters()[0].getOrigin();
120                     delegate = layoutRegistry.createSupportForLayout(
121                                                   layoutOrigin.getType());
122                     // handle special case of null layout
123
if (delegate == null)
124                         if (layoutOrigin.getType() == LayoutManager.class
125                             && layoutOrigin.getCreationParameters().length == 0
126                             && layoutOrigin.getParentExpression() == null
127                             && "null".equals(layoutOrigin.getJavaCodeString( // NOI18N
128
null, null)))
129                         {
130                             delegate = new NullLayoutSupport();
131                         }
132                         else return false;
133                 }
134             }
135
136             if (delegate == null) { // initialization from LayoutManager instance
137
Container contDel = getPrimaryContainerDelegate();
138                 if (contDel.getComponentCount() == 0) {
139                     // we can still handle only empty containers ...
140
lmInstance = contDel.getLayout();
141                     delegate = lmInstance != null ?
142                         layoutRegistry.createSupportForLayout(lmInstance.getClass()) :
143                         new NullLayoutSupport();
144                 }
145                 else {
146                     RuntimeException JavaDoc ex = new IllegalArgumentException JavaDoc();
147                     org.openide.ErrorManager.getDefault().annotate(
148                         ex, AbstractLayoutSupport.getBundle().getString(
149                                             "MSG_ERR_NonEmptyContainer")); // NOI18N
150
throw ex;
151                 }
152             }
153         }
154
155         if (delegate == null)
156             return false;
157
158         if (initialize) {
159             setLayoutDelegate(delegate, lmInstance, fromCode);
160         }
161         else {
162             layoutDelegate = delegate;
163             needInit = true;
164             initializeFromInstance = lmInstance != null;
165             initializeFromCode = fromCode;
166         }
167
168         return true;
169     }
170
171     public void initializeLayoutDelegate() throws Exception JavaDoc {
172         if (layoutDelegate != null && needInit) {
173             LayoutManager lmInstance = initializeFromInstance ?
174                     getPrimaryContainerDelegate().getLayout() : null;
175             layoutDelegate.initialize(this, lmInstance, initializeFromCode);
176             fillLayout(null);
177             getPropertySets(); // force properties and listeners creation
178
needInit = false;
179         }
180     }
181
182     public void setLayoutDelegate(LayoutSupportDelegate newDelegate,
183                                   LayoutManager lmInstance,
184                                   boolean fromCode)
185         throws Exception JavaDoc
186     {
187         LayoutConstraints[] oldConstraints;
188         LayoutSupportDelegate oldDelegate = layoutDelegate;
189
190         if (layoutDelegate != null
191                 && (layoutDelegate != newDelegate || !fromCode))
192             oldConstraints = removeLayoutDelegate(true);
193         else
194             oldConstraints = null;
195
196         layoutDelegate = newDelegate;
197         propertySets = null;
198         needInit = false;
199
200         if (layoutDelegate != null) {
201             try {
202                 layoutDelegate.initialize(this, lmInstance, fromCode);
203                 if (!fromCode)
204                     fillLayout(oldConstraints);
205                 getPropertySets(); // force properties and listeners creation
206
}
207             catch (Exception JavaDoc ex) {
208                 removeLayoutDelegate(false);
209                 layoutDelegate = oldDelegate;
210                 if (layoutDelegate != null)
211                     fillLayout(null);
212                 throw ex;
213             }
214         }
215     }
216
217     public LayoutSupportDelegate getLayoutDelegate() {
218         return layoutDelegate;
219     }
220
221     public void setUnknownLayoutDelegate(boolean fromCode) {
222         try {
223             setLayoutDelegate(new UnknownLayoutSupport(), null, fromCode);
224         }
225         catch (Exception JavaDoc ex) { // nothing should happen, ignore
226
ex.printStackTrace();
227         }
228     }
229
230     public boolean isUnknownLayout() {
231         return layoutDelegate == null
232                || layoutDelegate instanceof UnknownLayoutSupport;
233     }
234
235     // copy layout delegate from another container
236
public void copyLayoutDelegateFrom(
237                     LayoutSupportManager sourceLayoutSupport,
238                     RADVisualComponent[] newMetaComps)
239     {
240         LayoutSupportDelegate sourceDelegate =
241             sourceLayoutSupport.getLayoutDelegate();
242
243         int componentCount = sourceDelegate.getComponentCount();
244
245         Container cont = getPrimaryContainer();
246         Container contDel = getPrimaryContainerDelegate();
247
248         if (layoutDelegate != null)
249             removeLayoutDelegate(false);
250
251         CodeExpression[] compExps = new CodeExpression[componentCount];
252         Component[] primaryComps = new Component[componentCount];
253
254         for (int i=0; i < componentCount; i++) {
255             RADVisualComponent metacomp = newMetaComps[i];
256             compExps[i] = metacomp.getCodeExpression();
257             primaryComps[i] = (Component) metacomp.getBeanInstance();
258             ensureFakePeerAttached(primaryComps[i]);
259         }
260
261         LayoutSupportDelegate newDelegate =
262             sourceDelegate.cloneLayoutSupport(this, compExps);
263
264         newDelegate.setLayoutToContainer(cont, contDel);
265         newDelegate.addComponentsToContainer(cont, contDel, primaryComps, 0);
266
267         layoutDelegate = newDelegate;
268
269         // Ensure correct propagation of copied properties (issue 50011, 72351)
270
try {
271             layoutDelegate.acceptContainerLayoutChange(null);
272         } catch (PropertyVetoException pvex) {
273             // should not happen
274
}
275     }
276
277     public void clearPrimaryContainer() {
278         layoutDelegate.clearContainer(getPrimaryContainer(),
279                                       getPrimaryContainerDelegate());
280     }
281
282     public RADVisualContainer getMetaContainer() {
283         return metaContainer;
284     }
285
286 // public boolean supportsArranging() {
287
// return layoutDelegate instanceof LayoutSupportArranging;
288
// }
289

290     private LayoutConstraints[] removeLayoutDelegate(
291                                     boolean extractConstraints)
292     {
293         CodeGroup code = layoutDelegate.getLayoutCode();
294         if (code != null)
295             CodeStructure.removeStatements(code.getStatementsIterator());
296
297         int componentCount = layoutDelegate.getComponentCount();
298         LayoutConstraints[] constraints = null;
299
300         if (componentCount > 0) {
301             RADVisualComponent[] metacomps = metaContainer.getSubComponents();
302             if (extractConstraints)
303                 constraints = new LayoutConstraints[componentCount];
304
305             for (int i=0; i < componentCount; i++) {
306                 LayoutConstraints constr = layoutDelegate.getConstraints(i);
307                 if (extractConstraints)
308                     constraints[i] = constr;
309                 if (constr != null)
310                     metacomps[i].setLayoutConstraints(layoutDelegate.getClass(),
311                                                       constr);
312                 code = layoutDelegate.getComponentCode(i);
313                 if (code != null)
314                     CodeStructure.removeStatements(code.getStatementsIterator());
315             }
316         }
317
318         layoutDelegate.removeAll();
319         layoutDelegate.clearContainer(getPrimaryContainer(),
320                                       getPrimaryContainerDelegate());
321         layoutDelegate = null;
322
323         return constraints;
324     }
325
326     private void fillLayout(LayoutConstraints[] oldConstraints) {
327         RADVisualComponent[] metacomps = metaContainer.getSubComponents();
328         int componentCount = metacomps.length;
329
330         CodeExpression[] compExps = new CodeExpression[componentCount];
331         Component[] designComps = new Component[componentCount];
332         Component[] primaryComps = new Component[componentCount];
333         LayoutConstraints[] newConstraints = new LayoutConstraints[componentCount];
334
335         FormDesigner designer = FormEditor.getFormDesigner(metaContainer.getFormModel());
336
337         for (int i=0; i < componentCount; i++) {
338             RADVisualComponent metacomp = metacomps[i];
339
340             compExps[i] = metacomp.getCodeExpression();
341             primaryComps[i] = (Component) metacomp.getBeanInstance();
342             ensureFakePeerAttached(primaryComps[i]);
343             newConstraints[i] = metacomp.getLayoutConstraints(
344                                              layoutDelegate.getClass());
345
346             Component comp = designer != null ?
347                             (Component) designer.getComponent(metacomp) : null;
348             designComps[i] = comp != null ?
349                              comp : (Component) metacomp.getBeanInstance();
350         }
351
352         if (oldConstraints != null)
353             layoutDelegate.convertConstraints(oldConstraints,
354                                               newConstraints,
355                                               designComps);
356
357         if (componentCount > 0) {
358             layoutDelegate.acceptNewComponents(compExps, newConstraints, 0);
359             layoutDelegate.addComponents(compExps, newConstraints, 0);
360
361             for (int i=0; i < componentCount; i++)
362                 metacomps[i].resetConstraintsProperties();
363         }
364
365         // setup primary container
366
Container cont = getPrimaryContainer();
367         Container contDel = getPrimaryContainerDelegate();
368 // layoutDelegate.clearContainer(cont, contDel);
369
layoutDelegate.setLayoutToContainer(cont, contDel);
370         if (componentCount > 0)
371             layoutDelegate.addComponentsToContainer(cont, contDel,
372                                                     primaryComps, 0);
373     }
374
375     // ---------
376
// public API delegated to LayoutSupportDelegate
377

378     public boolean isDedicated() {
379         return layoutDelegate.isDedicated();
380     }
381
382     // node presentation
383
public boolean shouldHaveNode() {
384         return layoutDelegate.shouldHaveNode();
385     }
386
387     public String JavaDoc getDisplayName() {
388         return layoutDelegate.getDisplayName();
389     }
390
391     public Image getIcon(int type) {
392         return layoutDelegate.getIcon(type);
393     }
394
395     // properties and customizer
396
public Node.PropertySet[] getPropertySets() {
397         if (propertySets == null) {
398             if (layoutDelegate == null) return new Node.PropertySet[0]; // Issue 63916
399
propertySets = layoutDelegate.getPropertySets();
400
401             for (int i=0; i < propertySets.length; i++) {
402                 Node.Property[] props = propertySets[i].getProperties();
403                 for (int j=0; j < props.length; j++)
404                     if (props[j] instanceof FormProperty) {
405                         FormProperty prop = (FormProperty) props[j];
406                         prop.addVetoableChangeListener(getLayoutListener());
407                         prop.addPropertyChangeListener(getLayoutListener());
408                     }
409             }
410         }
411         return propertySets;
412     }
413
414     public Node.Property[] getAllProperties() {
415         if (layoutDelegate instanceof AbstractLayoutSupport)
416             return ((AbstractLayoutSupport)layoutDelegate).getAllProperties();
417
418         ArrayList allPropsList = new ArrayList();
419         for (int i=0; i < propertySets.length; i++) {
420             Node.Property[] props = propertySets[i].getProperties();
421             for (int j=0; j < props.length; j++)
422                 allPropsList.add(props[j]);
423         }
424
425         Node.Property[] allProperties = new Node.Property[allPropsList.size()];
426         allPropsList.toArray(allProperties);
427         return allProperties;
428     }
429
430     public Node.Property getLayoutProperty(String JavaDoc name) {
431         if (layoutDelegate instanceof AbstractLayoutSupport)
432             return ((AbstractLayoutSupport)layoutDelegate).getProperty(name);
433
434         Node.Property[] properties = getAllProperties();
435         for (int i=0; i < properties.length; i++)
436             if (name.equals(properties[i].getName()))
437                 return properties[i];
438
439         return null;
440     }
441
442     public Class JavaDoc getCustomizerClass() {
443         return layoutDelegate.getCustomizerClass();
444     }
445
446     public Component getSupportCustomizer() {
447         return layoutDelegate.getSupportCustomizer();
448     }
449
450     // code meta data
451
public CodeGroup getLayoutCode() {
452         return layoutDelegate.getLayoutCode();
453     }
454
455     public CodeGroup getComponentCode(int index) {
456         return layoutDelegate.getComponentCode(index);
457     }
458
459     public CodeGroup getComponentCode(RADVisualComponent metacomp) {
460         int index = metaContainer.getIndexOf(metacomp);
461         return index >= 0 && index < layoutDelegate.getComponentCount() ?
462                layoutDelegate.getComponentCode(index) : null;
463     }
464
465     public int getComponentCount() {
466         return layoutDelegate.getComponentCount();
467     }
468
469     // data validation
470
public void acceptNewComponents(RADVisualComponent[] components,
471                                     LayoutConstraints[] constraints,
472                                     int index)
473     {
474         CodeExpression[] compExps = new CodeExpression[components.length];
475         for (int i=0; i < components.length; i++)
476             compExps[i] = components[i].getCodeExpression();
477
478         layoutDelegate.acceptNewComponents(compExps, constraints, index);
479     }
480
481     // components adding/removing
482
public void addComponents(RADVisualComponent[] components,
483                               LayoutConstraints[] constraints,
484                               int index)
485     {
486         CodeExpression[] compExps = new CodeExpression[components.length];
487         Component[] comps = new Component[components.length];
488
489         for (int i=0; i < components.length; i++) {
490             compExps[i] = components[i].getCodeExpression();
491             comps[i] = (Component) components[i].getBeanInstance();
492             ensureFakePeerAttached(comps[i]);
493         }
494
495         if (index < 0)
496             index = layoutDelegate.getComponentCount();
497
498         layoutDelegate.addComponents(compExps, constraints, index);
499
500         for (int i=0; i < components.length; i++)
501             components[i].resetConstraintsProperties();
502
503         layoutDelegate.addComponentsToContainer(getPrimaryContainer(),
504                                                 getPrimaryContainerDelegate(),
505                                                 comps, index);
506     }
507
508     public void removeComponent(RADVisualComponent metacomp, int index) {
509         // first store constraints in the meta component
510
LayoutConstraints constr = layoutDelegate.getConstraints(index);
511         if (constr != null)
512             metacomp.setLayoutConstraints(layoutDelegate.getClass(), constr);
513
514         // remove code
515
CodeStructure.removeStatements(
516             layoutDelegate.getComponentCode(index).getStatementsIterator());
517
518         // remove the component from layout
519
layoutDelegate.removeComponent(index);
520
521         // remove the component instance from the primary container instance
522
if (!layoutDelegate.removeComponentFromContainer(
523                                 getPrimaryContainer(),
524                                 getPrimaryContainerDelegate(),
525                                 (Component)metacomp.getBeanInstance()))
526         { // layout delegate does not support removing individual components,
527
// so we clear the container and add the remaining components again
528
layoutDelegate.clearContainer(getPrimaryContainer(),
529                                           getPrimaryContainerDelegate());
530
531             RADVisualComponent[] metacomps = metaContainer.getSubComponents();
532             if (metacomps.length > 1) {
533                 // we rely on that metacomp was not removed from the model yet
534
Component[] comps = new Component[metacomps.length-1];
535                 for (int i=0; i < metacomps.length; i++) {
536                     if (i != index) {
537                         Component comp = (Component) metacomps[i].getBeanInstance();
538                         ensureFakePeerAttached(comp);
539                         comps[i < index ? i : i-1] = comp;
540                     }
541                 }
542                 layoutDelegate.addComponentsToContainer(
543                                    getPrimaryContainer(),
544                                    getPrimaryContainerDelegate(),
545                                    comps,
546                                    0);
547             }
548         }
549     }
550
551     public void removeAll() {
552         // first store constraints in meta components
553
RADVisualComponent[] components = metaContainer.getSubComponents();
554         for (int i=0; i < components.length; i++) {
555             LayoutConstraints constr =
556                 layoutDelegate.getConstraints(i);
557             if (constr != null)
558                 components[i].setLayoutConstraints(layoutDelegate.getClass(),
559                                                    constr);
560         }
561
562         // remove code of all components
563
for (int i=0, n=layoutDelegate.getComponentCount(); i < n; i++)
564             CodeStructure.removeStatements(
565                 layoutDelegate.getComponentCode(i).getStatementsIterator());
566
567         // remove components from layout
568
layoutDelegate.removeAll();
569
570         // clear the primary container instance
571
layoutDelegate.clearContainer(getPrimaryContainer(),
572                                       getPrimaryContainerDelegate());
573     }
574
575     public boolean isLayoutChanged() {
576         Container defaultContainer = (Container)
577                 BeanSupport.getDefaultInstance(metaContainer.getBeanClass());
578         Container defaultContDelegate =
579                 metaContainer.getContainerDelegate(defaultContainer);
580
581         return layoutDelegate.isLayoutChanged(defaultContainer,
582                                               defaultContDelegate);
583     }
584
585     // managing constraints
586
public LayoutConstraints getConstraints(int index) {
587         return layoutDelegate.getConstraints(index);
588     }
589
590     public LayoutConstraints getConstraints(RADVisualComponent metacomp) {
591         if (layoutDelegate == null)
592             return null;
593
594         int index = metaContainer.getIndexOf(metacomp);
595         return index >= 0 && index < layoutDelegate.getComponentCount() ?
596                layoutDelegate.getConstraints(index) : null;
597     }
598
599     public static LayoutConstraints storeConstraints(
600                                         RADVisualComponent metacomp)
601     {
602         RADVisualContainer parent = metacomp.getParentContainer();
603         if (parent == null)
604             return null;
605
606         LayoutSupportManager layoutSupport = parent.getLayoutSupport();
607         if (layoutSupport == null)
608             return null;
609         LayoutConstraints constr = layoutSupport.getConstraints(metacomp);
610         if (constr != null)
611             metacomp.setLayoutConstraints(
612                          layoutSupport.getLayoutDelegate().getClass(),
613                          constr);
614         return constr;
615     }
616
617     public LayoutConstraints getStoredConstraints(RADVisualComponent metacomp) {
618         return metacomp.getLayoutConstraints(layoutDelegate.getClass());
619     }
620
621     // managing live components
622
public void setLayoutToContainer(Container container,
623                                      Container containerDelegate)
624     {
625         layoutDelegate.setLayoutToContainer(container, containerDelegate);
626     }
627
628     public void addComponentsToContainer(Container container,
629                                          Container containerDelegate,
630                                          Component[] components,
631                                          int index)
632     {
633         layoutDelegate.addComponentsToContainer(container, containerDelegate,
634                                                 components, index);
635     }
636
637     public boolean removeComponentFromContainer(Container container,
638                                                 Container containerDelegate,
639                                                 Component component)
640     {
641         return layoutDelegate.removeComponentFromContainer(
642                             container, containerDelegate, component);
643     }
644
645     public boolean clearContainer(Container container,
646                                   Container containerDelegate)
647     {
648         return layoutDelegate.clearContainer(container, containerDelegate);
649     }
650
651     // drag and drop support
652
public LayoutConstraints getNewConstraints(Container container,
653                                                Container containerDelegate,
654                                                Component component,
655                                                int index,
656                                                Point posInCont,
657                                                Point posInComp)
658     {
659         
660         LayoutConstraints constraints = layoutDelegate.getNewConstraints(container, containerDelegate,
661                                                 component, index,
662                                                 posInCont, posInComp);
663         String JavaDoc context = null;
664         Object JavaDoc[] params = null;
665         if (layoutDelegate instanceof AbstractLayoutSupport) {
666             AbstractLayoutSupport support = (AbstractLayoutSupport)layoutDelegate;
667             context = support.getAssistantContext();
668             params = support.getAssistantParams();
669         }
670         context = (context == null) ? "generalPosition" : context; // NOI18N
671
FormEditor.getAssistantModel(metaContainer.getFormModel()).setContext(context, params);
672         return constraints;
673     }
674
675     public int getNewIndex(Container container,
676                            Container containerDelegate,
677                            Component component,
678                            int index,
679                            Point posInCont,
680                            Point posInComp)
681     {
682         return layoutDelegate.getNewIndex(container, containerDelegate,
683                                           component, index,
684                                           posInCont, posInComp);
685     }
686
687     public boolean paintDragFeedback(Container container,
688                                      Container containerDelegate,
689                                      Component component,
690                                      LayoutConstraints newConstraints,
691                                      int newIndex,
692                                      Graphics g)
693     {
694         return layoutDelegate.paintDragFeedback(container, containerDelegate,
695                                                 component,
696                                                 newConstraints, newIndex,
697                                                 g);
698     }
699
700     // resizing support
701
public int getResizableDirections(Container container,
702                                       Container containerDelegate,
703                                       Component component,
704                                       int index)
705     {
706         return layoutDelegate.getResizableDirections(container,
707                                                      containerDelegate,
708                                                      component, index);
709     }
710
711     public LayoutConstraints getResizedConstraints(Container container,
712                                                    Container containerDelegate,
713                                                    Component component,
714                                                    int index,
715                                                    Rectangle originalBounds,
716                                                    Insets sizeChanges,
717                                                    Point posInCont)
718     {
719         return layoutDelegate.getResizedConstraints(container,
720                                                     containerDelegate,
721                                                     component, index,
722                                                     originalBounds,
723                                                     sizeChanges,
724                                                     posInCont);
725     }
726
727     // arranging support
728
public void processMouseClick(Point p,
729                                   Container cont,
730                                   Container contDelegate)
731     {
732         layoutDelegate.processMouseClick(p, cont, contDelegate);
733     }
734
735     // arranging support
736
public void selectComponent(int index) {
737         layoutDelegate.selectComponent(index);
738     }
739
740     // arranging support
741
public void arrangeContainer(Container container,
742                                  Container containerDelegate)
743     {
744         layoutDelegate.arrangeContainer(container, containerDelegate);
745     }
746
747     // -----------
748
// API for layout delegates (LayoutSupportContext implementation)
749

750     public CodeStructure getCodeStructure() {
751         return codeStructure;
752     }
753
754     public CodeExpression getContainerCodeExpression() {
755         if (containerCodeExpression == null) {
756             containerCodeExpression = metaContainer.getCodeExpression();
757             containerDelegateCodeExpression = null;
758         }
759         return containerCodeExpression;
760     }
761
762     public CodeExpression getContainerDelegateCodeExpression() {
763         if (containerDelegateCodeExpression == null) {
764             containerDelegateCodeExpression =
765                     containerDelegateCodeExpression(metaContainer, codeStructure);
766         }
767
768         return containerDelegateCodeExpression;
769     }
770     
771     public static CodeExpression containerDelegateCodeExpression(
772                                      RADVisualContainer metaContainer,
773                                      CodeStructure codeStructure)
774     {
775         CodeExpression containerCodeExpression = metaContainer.getCodeExpression();
776         CodeExpression containerDelegateCodeExpression;
777         java.lang.reflect.Method JavaDoc delegateGetter =
778             metaContainer.getContainerDelegateMethod();
779
780         if (delegateGetter != null) { // there should be a container delegate
781
Iterator it = CodeStructure.getDefinedExpressionsIterator(
782                                               containerCodeExpression);
783             CodeExpression[] expressions = CodeStructure.filterExpressions(
784                                                         it, delegateGetter);
785             if (expressions.length > 0) {
786                 // the expresion for the container delegate already exists
787
containerDelegateCodeExpression = expressions[0];
788             }
789             else { // create a new expresion for the container delegate
790
CodeExpressionOrigin origin = CodeStructure.createOrigin(
791                                                 containerCodeExpression,
792                                                 delegateGetter,
793                                                 null);
794                 containerDelegateCodeExpression =
795                     codeStructure.createExpression(origin);
796             }
797         }
798         else // no special container delegate
799
containerDelegateCodeExpression = containerCodeExpression;
800         return containerDelegateCodeExpression;
801     }
802
803     // return container instance of meta container
804
public Container getPrimaryContainer() {
805         return (Container) metaContainer.getBeanInstance();
806     }
807
808     // return container delegate of container instance of meta container
809
public Container getPrimaryContainerDelegate() {
810         Container defCont = (Container) metaContainer.getBeanInstance();
811         if (primaryContainerDelegate == null || primaryContainer != defCont) {
812             primaryContainer = defCont;
813             primaryContainerDelegate =
814                 metaContainer.getContainerDelegate(defCont);
815         }
816         return primaryContainerDelegate;
817     }
818
819     // return component instance of meta component
820
public Component getPrimaryComponent(int index) {
821         return (Component) metaContainer.getSubComponent(index).getBeanInstance();
822     }
823
824     public void updatePrimaryContainer() {
825         Container cont = getPrimaryContainer();
826         Container contDel = getPrimaryContainerDelegate();
827
828         layoutDelegate.clearContainer(cont, contDel);
829         layoutDelegate.setLayoutToContainer(cont, contDel);
830
831         RADVisualComponent[] components = metaContainer.getSubComponents();
832         if (components.length > 0) {
833             Component[] comps = new Component[components.length];
834             for (int i=0; i < components.length; i++) {
835                 comps[i] = (Component) components[i].getBeanInstance();
836                 ensureFakePeerAttached(comps[i]);
837             }
838
839             layoutDelegate.addComponentsToContainer(cont, contDel, comps, 0);
840         }
841     }
842
843     public void containerLayoutChanged(PropertyChangeEvent ev)
844         throws PropertyVetoException
845     {
846         if (ev != null && ev.getPropertyName() != null) {
847             layoutDelegate.acceptContainerLayoutChange(getEventWithValues(ev));
848
849             FormModel formModel = metaContainer.getFormModel();
850             formModel.fireContainerLayoutChanged(metaContainer,
851                                                  ev.getPropertyName(),
852                                                  ev.getOldValue(),
853                                                  ev.getNewValue());
854         }
855         else propertySets = null;
856
857         LayoutNode node = metaContainer.getLayoutNodeReference();
858         if (node != null) {
859             // propagate the change to node
860
if (ev != null && ev.getPropertyName() != null)
861                 node.fireLayoutPropertiesChange();
862             else
863                 node.fireLayoutPropertySetsChange();
864         }
865     }
866
867     public void componentLayoutChanged(int index, PropertyChangeEvent ev)
868         throws PropertyVetoException
869     {
870         RADVisualComponent metacomp = metaContainer.getSubComponent(index);
871
872         if (ev != null && ev.getPropertyName() != null) {
873             layoutDelegate.acceptComponentLayoutChange(index,
874                                                        getEventWithValues(ev));
875
876             FormModel formModel = metaContainer.getFormModel();
877             formModel.fireComponentLayoutChanged(metacomp,
878                                                  ev.getPropertyName(),
879                                                  ev.getOldValue(),
880                                                  ev.getNewValue());
881
882             if (metacomp.getNodeReference() != null) // propagate the change to node
883
metacomp.getNodeReference().firePropertyChangeHelper(
884 // null, null, null);
885
ev.getPropertyName(),
886                                               ev.getOldValue(),
887                                               ev.getNewValue());
888         }
889         else {
890             if (metacomp.getNodeReference() != null) // propagate the change to node
891
metacomp.getNodeReference().fireComponentPropertySetsChange();
892             metacomp.resetConstraintsProperties();
893         }
894     }
895
896     private static PropertyChangeEvent getEventWithValues(PropertyChangeEvent ev) {
897         Object JavaDoc oldVal = ev.getOldValue();
898         Object JavaDoc newVal = ev.getNewValue();
899         if (oldVal instanceof FormProperty.ValueWithEditor)
900             ev = new PropertyChangeEvent(
901                          ev.getSource(),
902                          ev.getPropertyName(),
903                          ((FormProperty.ValueWithEditor)oldVal).getValue(),
904                          ((FormProperty.ValueWithEditor)newVal).getValue());
905         return ev;
906     }
907
908     // ---------
909

910     private LayoutListener getLayoutListener() {
911         if (layoutListener == null)
912             layoutListener = new LayoutListener();
913         return layoutListener;
914     }
915
916     private class LayoutListener implements VetoableChangeListener,
917                                             PropertyChangeListener
918     {
919         public void vetoableChange(PropertyChangeEvent ev)
920             throws PropertyVetoException
921         {
922             Object JavaDoc source = ev.getSource();
923             String JavaDoc eventName = ev.getPropertyName();
924             if (source instanceof FormProperty
925                 && (FormProperty.PROP_VALUE.equals(eventName)
926                     || FormProperty.PROP_VALUE_AND_EDITOR.equals(eventName)))
927             {
928                 ev = new PropertyChangeEvent(layoutDelegate,
929                                              ((FormProperty)source).getName(),
930                                              ev.getOldValue(),
931                                              ev.getNewValue());
932
933                 containerLayoutChanged(ev);
934             }
935         }
936
937         public void propertyChange(PropertyChangeEvent ev) {
938             Object JavaDoc source = ev.getSource();
939             if (source instanceof FormProperty
940                 && FormProperty.CURRENT_EDITOR.equals(ev.getPropertyName()))
941             {
942                 ev = new PropertyChangeEvent(layoutDelegate,
943                                              null, null, null);
944                 try {
945                     containerLayoutChanged(ev);
946                 }
947                 catch (PropertyVetoException ex) {} // should not happen
948
}
949         }
950     }
951
952     private static void ensureFakePeerAttached(Component comp) {
953         FakePeerSupport.attachFakePeer(comp);
954         if (comp instanceof Container)
955             FakePeerSupport.attachFakePeerRecursively((Container)comp);
956     }
957 }
958
Popular Tags