KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.*;
24
25 import org.openide.nodes.*;
26
27 /**
28  * RADVisualFormContainer represents the top-level container of the form and
29  * the form itself during design time.
30  *
31  * @author Ian Formanek
32  */

33
34 public class RADVisualFormContainer extends RADVisualContainer implements FormContainer
35 {
36     public static final String JavaDoc PROP_FORM_SIZE_POLICY = "formSizePolicy"; // NOI18N
37
public static final String JavaDoc PROP_FORM_SIZE = "formSize"; // NOI18N
38
public static final String JavaDoc PROP_FORM_POSITION = "formPosition"; // NOI18N
39
public static final String JavaDoc PROP_GENERATE_POSITION = "generatePosition"; // NOI18N
40
public static final String JavaDoc PROP_GENERATE_SIZE = "generateSize"; // NOI18N
41
public static final String JavaDoc PROP_GENERATE_CENTER = "generateCenter"; // NOI18N
42

43     public static final int GEN_BOUNDS = 0;
44     public static final int GEN_PACK = 1;
45     public static final int GEN_NOTHING = 2;
46
47     // Synthetic properties of form
48
private Dimension designerSize;
49     private Dimension formSize;// = new Dimension(FormEditor.DEFAULT_FORM_WIDTH, FormEditor.DEFAULT_FORM_HEIGHT);
50
private Point formPosition;
51     private boolean generatePosition = true;
52     private boolean generateSize = true;
53     private boolean generateCenter = true;
54     private int formSizePolicy = GEN_NOTHING;
55
56     // ------------------------------------------------------------------------------
57
// Form synthetic properties
58

59     /**
60      * Getter for the Name property of the component - overriden to provide
61      * non-null value, as the top-level component does not have a variable
62      * @return current value of the Name property
63      */

64     public String JavaDoc getName() {
65         return FormUtils.getBundleString("CTL_FormTopContainerName"); // NOI18N
66
}
67
68     /**
69      * Setter for the Name property of the component - usually maps to
70      * variable declaration for holding the instance of the component
71      * @param value new value of the Name property
72      */

73     public void setName(String JavaDoc value) {
74         // noop in forms
75
}
76
77     public Point getFormPosition() {
78         if (formPosition == null) {
79             formPosition = new Point(0,0);//topContainer.getLocation();
80
}
81         return formPosition;
82     }
83
84     public void setFormPosition(Point value) {
85         Object JavaDoc old = formPosition;
86         formPosition = value;
87         getFormModel().fireSyntheticPropertyChanged(this, PROP_FORM_POSITION,
88                                                     old, value);
89     }
90
91     public Dimension getFormSize() {
92         return formSize;
93     }
94
95     public void setFormSize(Dimension value) {
96         Dimension old = setFormSizeImpl(value);
97
98         // this is called when the property is enabled for writing (i.e. policy
99
// is GEN_BOUNDS), but also when loading form (when policy might be not
100
// set yet) - so always propagate to designer size
101
Dimension designerSize;
102         if (getBeanInstance() instanceof Dialog
103             || getBeanInstance() instanceof Frame)
104         {
105             Dimension diffDim = getWindowContentDimensionDiff();
106             designerSize = new Dimension(value.width - diffDim.width,
107                                          value.height - diffDim.height);
108         }
109         else designerSize = value;
110         setDesignerSizeImpl(designerSize, false);
111
112         getFormModel().fireSyntheticPropertyChanged(this, PROP_FORM_SIZE, old, value);
113         getFormModel().fireSyntheticPropertyChanged(this, FormDesigner.PROP_DESIGNER_SIZE, null, null);
114     }
115
116     private Dimension setFormSizeImpl(Dimension value) {
117         Dimension old = formSize;
118         formSize = value;
119         if (getNodeReference() != null) { // propagate the change to node
120
getNodeReference().firePropertyChangeHelper(PROP_FORM_SIZE, old, value);
121         }
122         return old;
123     }
124
125     public Dimension getDesignerSize() {
126         return designerSize;
127     }
128
129     public void setDesignerSize(Dimension value) {
130         Dimension old = setDesignerSizeImpl(value);
131
132         if (getFormSizePolicy() == GEN_BOUNDS) { // propagate to form size
133
Dimension formSize;
134             if (getBeanInstance() instanceof Dialog
135                 || getBeanInstance() instanceof Frame)
136             {
137                 Dimension diffDim = getWindowContentDimensionDiff();
138                 formSize = new Dimension(value.width + diffDim.width,
139                                       value.height + diffDim.height);
140             }
141             else formSize = value;
142             setFormSizeImpl(formSize);
143         }
144
145         getFormModel().fireSyntheticPropertyChanged(this, FormDesigner.PROP_DESIGNER_SIZE, old, value);
146     }
147
148     private boolean shouldPersistDesignerSize() {
149         // don't persist designer size if form size is defined (persisted)
150
// and neither for free design forms
151
return !hasExplicitSize() && (getLayoutSupport() != null);
152     }
153
154     Dimension setDesignerSizeImpl(Dimension value) {
155         return setDesignerSizeImpl(value, shouldPersistDesignerSize());
156     }
157
158     private Dimension setDesignerSizeImpl(Dimension value, boolean persistent) {
159         Dimension old = designerSize;
160         designerSize = value;
161         setAuxValue(FormDesigner.PROP_DESIGNER_SIZE, persistent ? value : null);
162         if (getNodeReference() != null) { // propagate the change to node
163
getNodeReference().firePropertyChangeHelper(FormDesigner.PROP_DESIGNER_SIZE, old, value);
164         }
165         return old;
166     }
167
168     public boolean getGeneratePosition() {
169         return generatePosition;
170     }
171
172     public void setGeneratePosition(boolean value) {
173         boolean old = generatePosition;
174         generatePosition = value;
175         getFormModel().fireSyntheticPropertyChanged(this, PROP_GENERATE_POSITION,
176                                         old ? Boolean.TRUE : Boolean.FALSE, value ? Boolean.TRUE : Boolean.FALSE);
177     }
178
179     public boolean getGenerateSize() {
180         return generateSize;
181     }
182
183     public void setGenerateSize(boolean value) {
184         boolean old = generateSize;
185         generateSize = value;
186         getFormModel().fireSyntheticPropertyChanged(this, PROP_GENERATE_SIZE,
187                                         old ? Boolean.TRUE : Boolean.FALSE, value ? Boolean.TRUE : Boolean.FALSE);
188     }
189
190     public boolean getGenerateCenter() {
191         return generateCenter;
192     }
193
194     public void setGenerateCenter(boolean value) {
195         boolean old = generateCenter;
196         generateCenter = value;
197         getFormModel().fireSyntheticPropertyChanged(this, PROP_GENERATE_CENTER,
198                                         old ? Boolean.TRUE : Boolean.FALSE, value ? Boolean.TRUE : Boolean.FALSE);
199     }
200
201     public boolean hasExplicitSize() {
202         return getFormSizePolicy() == GEN_BOUNDS && getGenerateSize();
203     }
204
205     public int getFormSizePolicy() {
206         return java.awt.Window JavaDoc.class.isAssignableFrom(getBeanClass())
207                    || javax.swing.JInternalFrame JavaDoc.class.isAssignableFrom(getBeanClass())
208                ? formSizePolicy : GEN_NOTHING;
209     }
210
211     public void setFormSizePolicy(int value) {
212         int old = formSizePolicy;
213         formSizePolicy = value;
214         if (value == GEN_BOUNDS) {
215             if (designerSize != null) {
216                 setDesignerSize(getDesignerSize()); // Force recalculation of formSize
217
// designer size should not be persistent if form size is defined
218
if (getGenerateSize())
219                     setAuxValue(FormDesigner.PROP_DESIGNER_SIZE, null);
220             }
221         }
222         else if (!getFormModel().isFreeDesignDefaultLayout()) {
223             // designer size should be persistent
224
setAuxValue(FormDesigner.PROP_DESIGNER_SIZE, getDesignerSize());
225         }
226         getFormModel().fireSyntheticPropertyChanged(this, PROP_FORM_SIZE_POLICY,
227                                         new Integer JavaDoc(old), new Integer JavaDoc(value));
228     }
229
230     // ------------------------------------------------------------------------------
231
// End of form synthetic properties
232

233     protected Node.Property[] createSyntheticProperties() {
234         java.util.ResourceBundle JavaDoc bundle = FormUtils.getBundle();
235
236         Node.Property policyProperty = new PropertySupport.ReadWrite(
237             PROP_FORM_SIZE_POLICY,
238             Integer.TYPE,
239             bundle.getString("MSG_FormSizePolicy"), // NOI18N
240
bundle.getString("HINT_FormSizePolicy")) // NOI18N
241
{
242             public Object JavaDoc getValue() throws
243                 IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
244                 return new Integer JavaDoc(getFormSizePolicy());
245             }
246
247             public void setValue(Object JavaDoc val) throws IllegalAccessException JavaDoc,
248                                                     IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
249                 if (!(val instanceof Integer JavaDoc)) throw new IllegalArgumentException JavaDoc();
250                 setFormSizePolicy(((Integer JavaDoc)val).intValue());
251                 if (getNodeReference() != null)
252                     getNodeReference().fireComponentPropertySetsChange();
253             }
254
255             public boolean canWrite() {
256                 return !isReadOnly();
257             }
258
259             /** Editor for alignment */
260             public java.beans.PropertyEditor JavaDoc getPropertyEditor() {
261                 return new SizePolicyEditor();
262             }
263
264         };
265
266         Node.Property sizeProperty = new PropertySupport.ReadWrite(
267             PROP_FORM_SIZE,
268             Dimension.class,
269             bundle.getString("MSG_FormSize"), // NOI18N
270
bundle.getString("HINT_FormSize")) // NOI18N
271
{
272             public Object JavaDoc getValue() throws
273                 IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
274                 return getFormSize();
275             }
276
277             public void setValue(Object JavaDoc val) throws IllegalAccessException JavaDoc,
278                                                     IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
279 // if (!(val instanceof Dimension)) throw new IllegalArgumentException();
280
setFormSize((Dimension)val);
281             }
282
283             public boolean canWrite() {
284                 return !isReadOnly()
285                        && getFormSizePolicy() == GEN_BOUNDS
286                        && getGenerateSize();
287             }
288         };
289
290         Node.Property positionProperty = new PropertySupport.ReadWrite(
291             PROP_FORM_POSITION,
292             Point.class,
293             bundle.getString("MSG_FormPosition"), // NOI18N
294
bundle.getString("HINT_FormPosition")) // NOI18N
295
{
296             public Object JavaDoc getValue() throws
297                 IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
298                 return getFormPosition();
299             }
300
301             public void setValue(Object JavaDoc val) throws IllegalAccessException JavaDoc,
302                                                     IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
303                 if (!(val instanceof Point)) throw new IllegalArgumentException JavaDoc();
304                 setFormPosition((Point)val);
305             }
306
307             public boolean canWrite() {
308                 return !isReadOnly()
309                         && getFormSizePolicy() == GEN_BOUNDS
310                         && getGeneratePosition()
311                         && !getGenerateCenter();
312             }
313         };
314
315         Node.Property genPositionProperty = new PropertySupport.ReadWrite(
316             PROP_GENERATE_POSITION,
317             Boolean.TYPE,
318             bundle.getString("MSG_GeneratePosition"), // NOI18N
319
bundle.getString("HINT_GeneratePosition")) // NOI18N
320
{
321             public Object JavaDoc getValue() throws
322                 IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
323                 return getGeneratePosition() ? Boolean.TRUE : Boolean.FALSE;
324             }
325
326             public void setValue(Object JavaDoc val) throws IllegalAccessException JavaDoc,
327                                                     IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
328                 if (!(val instanceof Boolean JavaDoc)) throw new IllegalArgumentException JavaDoc();
329                 setGeneratePosition(((Boolean JavaDoc)val).booleanValue());
330                 if (getNodeReference() != null)
331                     getNodeReference().fireComponentPropertySetsChange();
332             }
333
334             public boolean canWrite() {
335                 return !isReadOnly()
336                         && getFormSizePolicy() == GEN_BOUNDS
337                         && !getGenerateCenter();
338             }
339         };
340
341         Node.Property genSizeProperty = new PropertySupport.ReadWrite(
342             PROP_GENERATE_SIZE,
343             Boolean.TYPE,
344             bundle.getString("MSG_GenerateSize"), // NOI18N
345
bundle.getString("HINT_GenerateSize")) // NOI18N
346
{
347             public Object JavaDoc getValue() throws
348                 IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
349                 return getGenerateSize() ? Boolean.TRUE : Boolean.FALSE;
350             }
351
352             public void setValue(Object JavaDoc val) throws IllegalAccessException JavaDoc,
353                                                     IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
354                 if (!(val instanceof Boolean JavaDoc)) throw new IllegalArgumentException JavaDoc();
355                 setGenerateSize(((Boolean JavaDoc)val).booleanValue());
356                 if (getNodeReference() != null)
357                     getNodeReference().fireComponentPropertySetsChange();
358             }
359
360             public boolean canWrite() {
361                 return !isReadOnly() && getFormSizePolicy() == GEN_BOUNDS;
362             }
363         };
364
365         Node.Property genCenterProperty = new PropertySupport.ReadWrite(
366             PROP_GENERATE_CENTER,
367             Boolean.TYPE,
368             bundle.getString("MSG_GenerateCenter"), // NOI18N
369
bundle.getString("HINT_GenerateCenter")) // NOI18N
370
{
371             public Object JavaDoc getValue() throws
372                 IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
373                 return getGenerateCenter() ? Boolean.TRUE : Boolean.FALSE;
374             }
375
376             public void setValue(Object JavaDoc val) throws IllegalAccessException JavaDoc,
377                                                     IllegalArgumentException JavaDoc, java.lang.reflect.InvocationTargetException JavaDoc {
378                 if (!(val instanceof Boolean JavaDoc)) throw new IllegalArgumentException JavaDoc();
379                 setGenerateCenter(((Boolean JavaDoc)val).booleanValue());
380                 if (getNodeReference() != null)
381                     getNodeReference().fireComponentPropertySetsChange();
382             }
383
384             public boolean canWrite() {
385                 return !isReadOnly() && getFormSizePolicy() == GEN_BOUNDS;
386             }
387         };
388
389         Node.Property designerSizeProperty = new PropertySupport.ReadOnly(
390             FormDesigner.PROP_DESIGNER_SIZE,
391             Dimension.class,
392             bundle.getString("MSG_DesignerSize"), // NOI18N
393
bundle.getString("HINT_DesignerSize")) // NOI18N
394
{
395             public Object JavaDoc getValue()
396                 throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc,
397                        java.lang.reflect.InvocationTargetException JavaDoc
398             {
399                 return getDesignerSize();
400             }
401
402             public void setValue(Object JavaDoc val)
403                 throws IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc,
404                        java.lang.reflect.InvocationTargetException JavaDoc
405             {
406 // if (!(val instanceof Dimension))
407
// throw new IllegalArgumentException();
408
setDesignerSize((Dimension)val);
409             }
410         };
411
412         java.util.List JavaDoc propList = new java.util.ArrayList JavaDoc();
413
414         propList.add(JavaCodeGenerator.createBeanClassNameProperty(this));
415
416         if (java.awt.Window JavaDoc.class.isAssignableFrom(getBeanClass())
417             || javax.swing.JInternalFrame JavaDoc.class.isAssignableFrom(getBeanClass()))
418         {
419             propList.add(sizeProperty);
420             propList.add(positionProperty);
421             propList.add(policyProperty);
422             propList.add(genPositionProperty);
423             propList.add(genSizeProperty);
424             propList.add(genCenterProperty);
425         }
426                 
427         propList.add(designerSizeProperty);
428
429         Node.Property[] props = new Node.Property[propList.size()];
430         propList.toArray(props);
431         return props;
432     }
433
434     // ---------
435
// providing the difference of the whole frame/dialog size and the size
436
// of the content pane
437

438     private static Dimension windowContentDimensionDiff;
439
440     public Dimension getWindowContentDimensionDiff() {
441         boolean undecorated = true;
442         Object JavaDoc beanInstance = getBeanInstance();
443         if (beanInstance instanceof java.awt.Frame JavaDoc) {
444             undecorated = ((java.awt.Frame JavaDoc)beanInstance).isUndecorated();
445         } else if (beanInstance instanceof java.awt.Dialog JavaDoc) {
446             undecorated = ((java.awt.Dialog JavaDoc)beanInstance).isUndecorated();
447         }
448         return undecorated ? new Dimension(0, 0) : getDecoratedWindowContentDimensionDiff();
449     }
450
451     public static Dimension getDecoratedWindowContentDimensionDiff() {
452         if (windowContentDimensionDiff == null) {
453             javax.swing.JFrame JavaDoc frame = new javax.swing.JFrame JavaDoc();
454             frame.pack();
455             Dimension d1 = frame.getSize();
456             Dimension d2 = frame.getRootPane().getSize();
457             windowContentDimensionDiff =
458                 new Dimension(d1.width - d2.width, d1.height - d2.height);
459         }
460         return windowContentDimensionDiff;
461     }
462
463     void setNodeReference(RADComponentNode node) {
464         super.setNodeReference(node);
465         if (node != null) {
466             Object JavaDoc beanInstance = getBeanInstance();
467             if ((beanInstance instanceof java.awt.Frame JavaDoc)
468                 || (beanInstance instanceof java.awt.Dialog JavaDoc)) {
469                 // undecorated is not a bound property => it is not possible to
470
// listen on the beanInstance => we have to listen on the node
471
node.addPropertyChangeListener(new PropertyChangeListener() {
472                     public void propertyChange(PropertyChangeEvent evt) {
473                         if ("undecorated".equals(evt.getPropertyName())) { // NOI18N
474
// Keep current designer size and force update of form size
475
setDesignerSize(getDesignerSize());
476                         }
477                     }
478                 });
479             }
480         }
481     }
482
483     // ------------------------------------------------------------------------------------------
484
// Innerclasses
485

486     final public static class SizePolicyEditor extends java.beans.PropertyEditorSupport JavaDoc {
487         /** Display Names for alignment. */
488         private static final String JavaDoc[] names = {
489             FormUtils.getBundleString("VALUE_sizepolicy_full"), // NOI18N
490
FormUtils.getBundleString("VALUE_sizepolicy_pack"), // NOI18N
491
FormUtils.getBundleString("VALUE_sizepolicy_none"), // NOI18N
492
};
493
494         /** @return names of the possible directions */
495         public String JavaDoc[] getTags() {
496             return names;
497         }
498
499         /** @return text for the current value */
500         public String JavaDoc getAsText() {
501             int value =((Integer JavaDoc)getValue()).intValue();
502             return names[value];
503         }
504
505         /** Setter.
506          * @param str string equal to one value from directions array
507          */

508         public void setAsText(String JavaDoc str) {
509             if (names[0].equals(str))
510                 setValue(new Integer JavaDoc(0));
511             else if (names[1].equals(str))
512                 setValue(new Integer JavaDoc(1));
513             else if (names[2].equals(str))
514                 setValue(new Integer JavaDoc(2));
515         }
516     }
517 }
518
Popular Tags