001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form;
043:
044: import java.awt.*;
045: import java.beans.*;
046:
047: import org.openide.nodes.*;
048:
049: /**
050: * RADVisualFormContainer represents the top-level container of the form and
051: * the form itself during design time.
052: *
053: * @author Ian Formanek
054: */
055:
056: public class RADVisualFormContainer extends RADVisualContainer
057: implements FormContainer {
058: public static final String PROP_FORM_SIZE_POLICY = "formSizePolicy"; // NOI18N
059: public static final String PROP_FORM_SIZE = "formSize"; // NOI18N
060: public static final String PROP_FORM_POSITION = "formPosition"; // NOI18N
061: public static final String PROP_GENERATE_POSITION = "generatePosition"; // NOI18N
062: public static final String PROP_GENERATE_SIZE = "generateSize"; // NOI18N
063: public static final String PROP_GENERATE_CENTER = "generateCenter"; // NOI18N
064:
065: public static final int GEN_BOUNDS = 0;
066: public static final int GEN_PACK = 1;
067: public static final int GEN_NOTHING = 2;
068:
069: // Synthetic properties of form
070: private Dimension designerSize;
071: private Dimension formSize;// = new Dimension(FormEditor.DEFAULT_FORM_WIDTH, FormEditor.DEFAULT_FORM_HEIGHT);
072: private Point formPosition;
073: private boolean generatePosition = true;
074: private boolean generateSize = true;
075: private boolean generateCenter = true;
076: private int formSizePolicy = GEN_NOTHING;
077:
078: // ------------------------------------------------------------------------------
079: // Form synthetic properties
080:
081: /**
082: * Getter for the Name property of the component - overriden to provide
083: * non-null value, as the top-level component does not have a variable
084: * @return current value of the Name property
085: */
086: @Override
087: public String getName() {
088: return FormUtils.getBundleString("CTL_FormTopContainerName"); // NOI18N
089: }
090:
091: /**
092: * Setter for the Name property of the component - usually maps to
093: * variable declaration for holding the instance of the component
094: * @param value new value of the Name property
095: */
096: @Override
097: public void setName(String value) {
098: // noop in forms
099: }
100:
101: public Point getFormPosition() {
102: if (formPosition == null) {
103: formPosition = new Point(0, 0);//topContainer.getLocation();
104: }
105: return formPosition;
106: }
107:
108: public void setFormPosition(Point value) {
109: Object old = formPosition;
110: formPosition = value;
111: getFormModel().fireSyntheticPropertyChanged(this ,
112: PROP_FORM_POSITION, old, value);
113: }
114:
115: public Dimension getFormSize() {
116: return formSize;
117: }
118:
119: public void setFormSize(Dimension value) {
120: Dimension old = setFormSizeImpl(value);
121:
122: // this is called when the property is enabled for writing (i.e. policy
123: // is GEN_BOUNDS), but also when loading form (when policy might be not
124: // set yet) - so always propagate to designer size
125: Dimension designerSize;
126: if (getBeanInstance() instanceof Dialog
127: || getBeanInstance() instanceof Frame) {
128: Dimension diffDim = getWindowContentDimensionDiff();
129: designerSize = new Dimension(value.width - diffDim.width,
130: value.height - diffDim.height);
131: } else
132: designerSize = value;
133: setDesignerSizeImpl(designerSize, false);
134:
135: getFormModel().fireSyntheticPropertyChanged(this ,
136: PROP_FORM_SIZE, old, value);
137: getFormModel().fireSyntheticPropertyChanged(this ,
138: FormDesigner.PROP_DESIGNER_SIZE, null, null);
139: }
140:
141: private Dimension setFormSizeImpl(Dimension value) {
142: Dimension old = formSize;
143: formSize = value;
144: if (getNodeReference() != null) { // propagate the change to node
145: getNodeReference().firePropertyChangeHelper(PROP_FORM_SIZE,
146: old, value);
147: }
148: return old;
149: }
150:
151: public Dimension getDesignerSize() {
152: return designerSize;
153: }
154:
155: public void setDesignerSize(Dimension value) {
156: Dimension old = setDesignerSizeImpl(value);
157:
158: if (getFormSizePolicy() == GEN_BOUNDS) { // propagate to form size
159: Dimension formSize;
160: if (getBeanInstance() instanceof Dialog
161: || getBeanInstance() instanceof Frame) {
162: Dimension diffDim = getWindowContentDimensionDiff();
163: formSize = new Dimension(value.width + diffDim.width,
164: value.height + diffDim.height);
165: } else
166: formSize = value;
167: setFormSizeImpl(formSize);
168: }
169:
170: getFormModel().fireSyntheticPropertyChanged(this ,
171: FormDesigner.PROP_DESIGNER_SIZE, old, value);
172: }
173:
174: private boolean shouldPersistDesignerSize() {
175: // don't persist designer size if form size is defined (persisted)
176: // and neither for free design forms
177: return !hasExplicitSize() && (getLayoutSupport() != null);
178: }
179:
180: Dimension setDesignerSizeImpl(Dimension value) {
181: return setDesignerSizeImpl(value, shouldPersistDesignerSize());
182: }
183:
184: private Dimension setDesignerSizeImpl(Dimension value,
185: boolean persistent) {
186: Dimension old = designerSize;
187: designerSize = value;
188: setAuxValue(FormDesigner.PROP_DESIGNER_SIZE, persistent ? value
189: : null);
190: if (getNodeReference() != null) { // propagate the change to node
191: getNodeReference().firePropertyChangeHelper(
192: FormDesigner.PROP_DESIGNER_SIZE, old, value);
193: }
194: return old;
195: }
196:
197: public boolean getGeneratePosition() {
198: return generatePosition;
199: }
200:
201: public void setGeneratePosition(boolean value) {
202: boolean old = generatePosition;
203: generatePosition = value;
204: getFormModel().fireSyntheticPropertyChanged(this ,
205: PROP_GENERATE_POSITION,
206: old ? Boolean.TRUE : Boolean.FALSE,
207: value ? Boolean.TRUE : Boolean.FALSE);
208: }
209:
210: public boolean getGenerateSize() {
211: return generateSize;
212: }
213:
214: public void setGenerateSize(boolean value) {
215: boolean old = generateSize;
216: generateSize = value;
217: getFormModel().fireSyntheticPropertyChanged(this ,
218: PROP_GENERATE_SIZE, old ? Boolean.TRUE : Boolean.FALSE,
219: value ? Boolean.TRUE : Boolean.FALSE);
220: }
221:
222: public boolean getGenerateCenter() {
223: return generateCenter;
224: }
225:
226: public void setGenerateCenter(boolean value) {
227: boolean old = generateCenter;
228: generateCenter = value;
229: getFormModel().fireSyntheticPropertyChanged(this ,
230: PROP_GENERATE_CENTER,
231: old ? Boolean.TRUE : Boolean.FALSE,
232: value ? Boolean.TRUE : Boolean.FALSE);
233: }
234:
235: public boolean hasExplicitSize() {
236: return getFormSizePolicy() == GEN_BOUNDS && getGenerateSize();
237: }
238:
239: public int getFormSizePolicy() {
240: return java.awt.Window.class.isAssignableFrom(getBeanClass())
241: || javax.swing.JInternalFrame.class
242: .isAssignableFrom(getBeanClass()) ? formSizePolicy
243: : GEN_NOTHING;
244: }
245:
246: public void setFormSizePolicy(int value) {
247: int old = formSizePolicy;
248: formSizePolicy = value;
249: if (value == GEN_BOUNDS) {
250: if (designerSize != null) {
251: setDesignerSize(getDesignerSize()); // Force recalculation of formSize
252: // designer size should not be persistent if form size is defined
253: if (getGenerateSize())
254: setAuxValue(FormDesigner.PROP_DESIGNER_SIZE, null);
255: }
256: } else if (!getFormModel().isFreeDesignDefaultLayout()) {
257: // designer size should be persistent
258: setAuxValue(FormDesigner.PROP_DESIGNER_SIZE,
259: getDesignerSize());
260: }
261: getFormModel().fireSyntheticPropertyChanged(this ,
262: PROP_FORM_SIZE_POLICY, new Integer(old),
263: new Integer(value));
264: }
265:
266: // ------------------------------------------------------------------------------
267: // End of form synthetic properties
268:
269: @Override
270: protected Node.Property[] createSyntheticProperties() {
271: java.util.ResourceBundle bundle = FormUtils.getBundle();
272:
273: Node.Property policyProperty = new PropertySupport.ReadWrite(
274: PROP_FORM_SIZE_POLICY, Integer.TYPE, bundle
275: .getString("MSG_FormSizePolicy"), // NOI18N
276: bundle.getString("HINT_FormSizePolicy")) // NOI18N
277: {
278: public Object getValue() throws IllegalAccessException,
279: IllegalArgumentException,
280: java.lang.reflect.InvocationTargetException {
281: return new Integer(getFormSizePolicy());
282: }
283:
284: public void setValue(Object val)
285: throws IllegalAccessException,
286: IllegalArgumentException,
287: java.lang.reflect.InvocationTargetException {
288: if (!(val instanceof Integer))
289: throw new IllegalArgumentException();
290: setFormSizePolicy(((Integer) val).intValue());
291: if (getNodeReference() != null)
292: getNodeReference()
293: .fireComponentPropertySetsChange();
294: }
295:
296: @Override
297: public boolean canWrite() {
298: return !isReadOnly();
299: }
300:
301: /** Editor for alignment */
302: @Override
303: public java.beans.PropertyEditor getPropertyEditor() {
304: return new SizePolicyEditor();
305: }
306:
307: };
308:
309: Node.Property sizeProperty = new PropertySupport.ReadWrite(
310: PROP_FORM_SIZE, Dimension.class, bundle
311: .getString("MSG_FormSize"), // NOI18N
312: bundle.getString("HINT_FormSize")) // NOI18N
313: {
314: public Object getValue() throws IllegalAccessException,
315: IllegalArgumentException,
316: java.lang.reflect.InvocationTargetException {
317: return getFormSize();
318: }
319:
320: public void setValue(Object val)
321: throws IllegalAccessException,
322: IllegalArgumentException,
323: java.lang.reflect.InvocationTargetException {
324: // if (!(val instanceof Dimension)) throw new IllegalArgumentException();
325: setFormSize((Dimension) val);
326: }
327:
328: @Override
329: public boolean canWrite() {
330: return !isReadOnly()
331: && getFormSizePolicy() == GEN_BOUNDS
332: && getGenerateSize();
333: }
334: };
335:
336: Node.Property positionProperty = new PropertySupport.ReadWrite(
337: PROP_FORM_POSITION, Point.class, bundle
338: .getString("MSG_FormPosition"), // NOI18N
339: bundle.getString("HINT_FormPosition")) // NOI18N
340: {
341: public Object getValue() throws IllegalAccessException,
342: IllegalArgumentException,
343: java.lang.reflect.InvocationTargetException {
344: return getFormPosition();
345: }
346:
347: public void setValue(Object val)
348: throws IllegalAccessException,
349: IllegalArgumentException,
350: java.lang.reflect.InvocationTargetException {
351: if (!(val instanceof Point))
352: throw new IllegalArgumentException();
353: setFormPosition((Point) val);
354: }
355:
356: @Override
357: public boolean canWrite() {
358: return !isReadOnly()
359: && getFormSizePolicy() == GEN_BOUNDS
360: && getGeneratePosition()
361: && !getGenerateCenter();
362: }
363: };
364:
365: Node.Property genPositionProperty = new PropertySupport.ReadWrite(
366: PROP_GENERATE_POSITION, Boolean.TYPE, bundle
367: .getString("MSG_GeneratePosition"), // NOI18N
368: bundle.getString("HINT_GeneratePosition")) // NOI18N
369: {
370: public Object getValue() throws IllegalAccessException,
371: IllegalArgumentException,
372: java.lang.reflect.InvocationTargetException {
373: return getGeneratePosition() ? Boolean.TRUE
374: : Boolean.FALSE;
375: }
376:
377: public void setValue(Object val)
378: throws IllegalAccessException,
379: IllegalArgumentException,
380: java.lang.reflect.InvocationTargetException {
381: if (!(val instanceof Boolean))
382: throw new IllegalArgumentException();
383: setGeneratePosition(((Boolean) val).booleanValue());
384: if (getNodeReference() != null)
385: getNodeReference()
386: .fireComponentPropertySetsChange();
387: }
388:
389: @Override
390: public boolean canWrite() {
391: return !isReadOnly()
392: && getFormSizePolicy() == GEN_BOUNDS
393: && !getGenerateCenter();
394: }
395: };
396:
397: Node.Property genSizeProperty = new PropertySupport.ReadWrite(
398: PROP_GENERATE_SIZE, Boolean.TYPE, bundle
399: .getString("MSG_GenerateSize"), // NOI18N
400: bundle.getString("HINT_GenerateSize")) // NOI18N
401: {
402: public Object getValue() throws IllegalAccessException,
403: IllegalArgumentException,
404: java.lang.reflect.InvocationTargetException {
405: return getGenerateSize() ? Boolean.TRUE : Boolean.FALSE;
406: }
407:
408: public void setValue(Object val)
409: throws IllegalAccessException,
410: IllegalArgumentException,
411: java.lang.reflect.InvocationTargetException {
412: if (!(val instanceof Boolean))
413: throw new IllegalArgumentException();
414: setGenerateSize(((Boolean) val).booleanValue());
415: if (getNodeReference() != null)
416: getNodeReference()
417: .fireComponentPropertySetsChange();
418: }
419:
420: @Override
421: public boolean canWrite() {
422: return !isReadOnly()
423: && getFormSizePolicy() == GEN_BOUNDS;
424: }
425: };
426:
427: Node.Property genCenterProperty = new PropertySupport.ReadWrite(
428: PROP_GENERATE_CENTER, Boolean.TYPE, bundle
429: .getString("MSG_GenerateCenter"), // NOI18N
430: bundle.getString("HINT_GenerateCenter")) // NOI18N
431: {
432: public Object getValue() throws IllegalAccessException,
433: IllegalArgumentException,
434: java.lang.reflect.InvocationTargetException {
435: return getGenerateCenter() ? Boolean.TRUE
436: : Boolean.FALSE;
437: }
438:
439: public void setValue(Object val)
440: throws IllegalAccessException,
441: IllegalArgumentException,
442: java.lang.reflect.InvocationTargetException {
443: if (!(val instanceof Boolean))
444: throw new IllegalArgumentException();
445: setGenerateCenter(((Boolean) val).booleanValue());
446: if (getNodeReference() != null)
447: getNodeReference()
448: .fireComponentPropertySetsChange();
449: }
450:
451: @Override
452: public boolean canWrite() {
453: return !isReadOnly()
454: && getFormSizePolicy() == GEN_BOUNDS;
455: }
456: };
457:
458: Node.Property designerSizeProperty = new PropertySupport.ReadOnly(
459: FormDesigner.PROP_DESIGNER_SIZE, Dimension.class,
460: bundle.getString("MSG_DesignerSize"), // NOI18N
461: bundle.getString("HINT_DesignerSize")) // NOI18N
462: {
463: public Object getValue() throws IllegalAccessException,
464: IllegalArgumentException,
465: java.lang.reflect.InvocationTargetException {
466: return getDesignerSize();
467: }
468:
469: @Override
470: public void setValue(Object val)
471: throws IllegalAccessException,
472: IllegalArgumentException,
473: java.lang.reflect.InvocationTargetException {
474: // if (!(val instanceof Dimension))
475: // throw new IllegalArgumentException();
476: setDesignerSize((Dimension) val);
477: }
478: };
479:
480: java.util.List<Node.Property> propList = new java.util.ArrayList<Node.Property>();
481:
482: propList.add(JavaCodeGenerator
483: .createBeanClassNameProperty(this ));
484:
485: if (java.awt.Window.class.isAssignableFrom(getBeanClass())
486: || javax.swing.JInternalFrame.class
487: .isAssignableFrom(getBeanClass())) {
488: propList.add(sizeProperty);
489: propList.add(positionProperty);
490: propList.add(policyProperty);
491: propList.add(genPositionProperty);
492: propList.add(genSizeProperty);
493: propList.add(genCenterProperty);
494: }
495:
496: propList.add(designerSizeProperty);
497:
498: Node.Property[] props = new Node.Property[propList.size()];
499: propList.toArray(props);
500: return props;
501: }
502:
503: // ---------
504: // providing the difference of the whole frame/dialog size and the size
505: // of the content pane
506:
507: private static Dimension windowContentDimensionDiff;
508:
509: public Dimension getWindowContentDimensionDiff() {
510: boolean undecorated = true;
511: Object beanInstance = getBeanInstance();
512: if (beanInstance instanceof java.awt.Frame) {
513: undecorated = ((java.awt.Frame) beanInstance)
514: .isUndecorated();
515: } else if (beanInstance instanceof java.awt.Dialog) {
516: undecorated = ((java.awt.Dialog) beanInstance)
517: .isUndecorated();
518: }
519: return undecorated ? new Dimension(0, 0)
520: : getDecoratedWindowContentDimensionDiff();
521: }
522:
523: public static Dimension getDecoratedWindowContentDimensionDiff() {
524: if (windowContentDimensionDiff == null) {
525: javax.swing.JFrame frame = new javax.swing.JFrame();
526: frame.pack();
527: Dimension d1 = frame.getSize();
528: Dimension d2 = frame.getRootPane().getSize();
529: windowContentDimensionDiff = new Dimension(d1.width
530: - d2.width, d1.height - d2.height);
531: }
532: return windowContentDimensionDiff;
533: }
534:
535: @Override
536: void setNodeReference(RADComponentNode node) {
537: super .setNodeReference(node);
538: if (node != null) {
539: Object beanInstance = getBeanInstance();
540: if ((beanInstance instanceof java.awt.Frame)
541: || (beanInstance instanceof java.awt.Dialog)) {
542: // undecorated is not a bound property => it is not possible to
543: // listen on the beanInstance => we have to listen on the node
544: node
545: .addPropertyChangeListener(new PropertyChangeListener() {
546: public void propertyChange(
547: PropertyChangeEvent evt) {
548: if ("undecorated".equals(evt
549: .getPropertyName())) { // NOI18N
550: // Keep current designer size and force update of form size
551: setDesignerSize(getDesignerSize());
552: }
553: }
554: });
555: }
556: }
557: }
558:
559: // ------------------------------------------------------------------------------------------
560: // Innerclasses
561:
562: final public static class SizePolicyEditor extends
563: java.beans.PropertyEditorSupport {
564: /** Display Names for alignment. */
565: private static final String[] names = {
566: FormUtils.getBundleString("VALUE_sizepolicy_full"), // NOI18N
567: FormUtils.getBundleString("VALUE_sizepolicy_pack"), // NOI18N
568: FormUtils.getBundleString("VALUE_sizepolicy_none"), // NOI18N
569: };
570:
571: /** @return names of the possible directions */
572: @Override
573: public String[] getTags() {
574: return names;
575: }
576:
577: /** @return text for the current value */
578: @Override
579: public String getAsText() {
580: int value = ((Integer) getValue()).intValue();
581: return names[value];
582: }
583:
584: /** Setter.
585: * @param str string equal to one value from directions array
586: */
587: @Override
588: public void setAsText(String str) {
589: if (names[0].equals(str))
590: setValue(new Integer(0));
591: else if (names[1].equals(str))
592: setValue(new Integer(1));
593: else if (names[2].equals(str))
594: setValue(new Integer(2));
595: }
596: }
597: }
|