~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to form/src/org/netbeans/modules/form/layoutsupport/delegates/AbsoluteLayoutSupport.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.form.layoutsupport.delegates;
 
43
 
 
44
import java.awt.*;
 
45
import java.beans.*;
 
46
import java.util.*;
 
47
import java.lang.reflect.Constructor;
 
48
 
 
49
import org.openide.nodes.Node;
 
50
import org.openide.util.Utilities;
 
51
 
 
52
import org.netbeans.lib.awtextra.AbsoluteLayout;
 
53
import org.netbeans.lib.awtextra.AbsoluteConstraints;
 
54
 
 
55
import org.netbeans.modules.form.layoutsupport.*;
 
56
import org.netbeans.modules.form.codestructure.*;
 
57
import org.netbeans.modules.form.FormProperty;
 
58
import org.netbeans.modules.form.FormLoaderSettings;
 
59
 
 
60
/**
 
61
 * Support class for AbsoluteLayout - for absolute positioning and sizing of
 
62
 * components using AbsoluteConstraints. This is an example of support for
 
63
 * layout manager using component constraints as complex objects initialized
 
64
 * by constructor with parameters mapped to properties. AbsoluteLayoutSupport
 
65
 * is also the superclass of NullLayoutSupport and JLayeredPane support, so it
 
66
 * is a bit more complicated than would be necessary for simple implementation.
 
67
 *
 
68
 * @author Tomas Pavek
 
69
 */
 
70
 
 
71
public class AbsoluteLayoutSupport extends AbstractLayoutSupport {
 
72
 
 
73
    /** The icon for AbsoluteLayout. */
 
74
    private static String iconURL =
 
75
        "org/netbeans/modules/form/layoutsupport/resources/AbsoluteLayout.gif"; // NOI18N
 
76
    /** The icon for AbsoluteLayout. */
 
77
    private static String icon32URL =
 
78
        "org/netbeans/modules/form/layoutsupport/resources/AbsoluteLayout32.gif"; // NOI18N
 
79
 
 
80
    private static Constructor constrConstructor;
 
81
 
 
82
    private static FormLoaderSettings formSettings = FormLoaderSettings.getInstance();
 
83
 
 
84
    /** Gets the supported layout manager class - AbsoluteLayout.
 
85
     * @return the class supported by this delegate
 
86
     */
 
87
    public Class getSupportedClass() {
 
88
        return AbsoluteLayout.class;
 
89
    }
 
90
 
 
91
    /** Provides an icon to be used for the layout node in Component
 
92
     * Inspector. Only 16x16 color icon is required.
 
93
     * @param type is one of BeanInfo constants: ICON_COLOR_16x16,
 
94
     *        ICON_COLOR_32x32, ICON_MONO_16x16, ICON_MONO_32x32
 
95
     * @return icon to be displayed for node in Component Inspector
 
96
     */
 
97
    @Override
 
98
    public Image getIcon(int type) {
 
99
        switch (type) {
 
100
            case BeanInfo.ICON_COLOR_16x16:
 
101
            case BeanInfo.ICON_MONO_16x16:
 
102
                return Utilities.loadImage(iconURL);
 
103
            default:
 
104
                return Utilities.loadImage(icon32URL);
 
105
        }
 
106
    }
 
107
 
 
108
    /** This method is called when switching layout - giving an opportunity to
 
109
     * convert the previous constrainst of components to constraints of the new
 
110
     * layout (this layout). For AbsoluteLayout, we can simply create new
 
111
     * constraints from positions and sizes of real components.
 
112
     * @param previousConstraints [input] layout constraints of components in
 
113
     *                                    the previous layout
 
114
     * @param currentConstraints [output] array of converted constraints for
 
115
     *                                    the new layout - to be filled
 
116
     * @param components [input] real components in a real container having the
 
117
     *                           previous layout
 
118
     */
 
119
    @Override
 
120
    public void convertConstraints(LayoutConstraints[] previousConstraints,
 
121
                                   LayoutConstraints[] currentConstraints,
 
122
                                   Component[] components)
 
123
    {
 
124
        if (currentConstraints == null || components == null)
 
125
            return;
 
126
 
 
127
        for (int i=0; i < currentConstraints.length; i++)
 
128
            if (currentConstraints[i] == null) {
 
129
                Rectangle bounds = components[i].getBounds();
 
130
                Dimension prefSize = components[i].getPreferredSize();
 
131
                int x = bounds.x;
 
132
                int y = bounds.y;
 
133
                int w = computeConstraintSize(bounds.width, -1, prefSize.width);
 
134
                int h = computeConstraintSize(bounds.height, -1, prefSize.height);
 
135
 
 
136
                currentConstraints[i] = new AbsoluteLayoutConstraints(x, y, w, h);
 
137
            }
 
138
    }
 
139
 
 
140
    /** This method calculates layout constraints for a component dragged
 
141
     * over a container (or just for mouse cursor being moved over container,
 
142
     * without any component).
 
143
     * @param container instance of a real container over/in which the
 
144
     *        component is dragged
 
145
     * @param containerDelegate effective container delegate of the container
 
146
     *        (for layout managers we always use container delegate instead of
 
147
     *        the container)
 
148
     * @param component the real component being dragged, can be null
 
149
     * @param index position (index) of the component in its container;
 
150
     *        -1 if there's no dragged component
 
151
     * @param posInCont position of mouse in the container delegate
 
152
     * @param posInComp position of mouse in the dragged component; null if
 
153
     *        there's no dragged component
 
154
     * @return new LayoutConstraints object corresponding to the position of
 
155
     *         the component in the container
 
156
     */
 
157
    @Override
 
158
    public LayoutConstraints getNewConstraints(Container container,
 
159
                                               Container containerDelegate,
 
160
                                               Component component,
 
161
                                               int index,
 
162
                                               Point posInCont,
 
163
                                               Point posInComp)
 
164
    {
 
165
        int x = posInCont.x;
 
166
        int y = posInCont.y;
 
167
        int w = -1;
 
168
        int h = -1;
 
169
 
 
170
        LayoutConstraints constr = getConstraints(index);
 
171
 
 
172
        if (component != null) {
 
173
            int currentW;
 
174
            int currentH;
 
175
 
 
176
            if (constr instanceof AbsoluteLayoutConstraints) {
 
177
                currentW = ((AbsoluteLayoutConstraints)constr).w;
 
178
                currentH = ((AbsoluteLayoutConstraints)constr).h;
 
179
            }
 
180
            else {
 
181
                currentW = -1;
 
182
                currentH = -1;
 
183
            }
 
184
 
 
185
            Dimension size = component.getSize();
 
186
            Dimension prefSize = component.getPreferredSize();
 
187
 
 
188
            w = computeConstraintSize(size.width, currentW, prefSize.width);
 
189
            h = computeConstraintSize(size.height, currentH, prefSize.height);
 
190
        }
 
191
 
 
192
        if (posInComp != null) {
 
193
            x -= posInComp.x;
 
194
            y -= posInComp.y;
 
195
        }
 
196
 
 
197
        if (formSettings.getApplyGridToPosition()) {
 
198
            x = computeGridSize(x, formSettings.getGridX());
 
199
            y = computeGridSize(y, formSettings.getGridY());
 
200
        }
 
201
 
 
202
        assistantParams = new Object[] {Integer.valueOf(x), Integer.valueOf(y)};
 
203
        return createNewConstraints(constr, x, y, w, h);
 
204
    }
 
205
 
 
206
    private Object[] assistantParams;
 
207
    @Override
 
208
    public String getAssistantContext() {
 
209
        return "absoluteLayout"; // NOI18N
 
210
    }
 
211
 
 
212
    @Override
 
213
    public Object[] getAssistantParams() {
 
214
        return assistantParams;
 
215
    }
 
216
 
 
217
    /** This method paints a dragging feedback for a component dragged over
 
218
     * a container (or just for mouse cursor being moved over container,
 
219
     * without any component). For AbsoluteLayout, it simply paints a rectangle
 
220
     * corresponding to the component position and size.
 
221
     * @param container instance of a real container over/in which the
 
222
     *        component is dragged
 
223
     * @param containerDelegate effective container delegate of the container
 
224
     *        (for layout managers we always use container delegate instead of
 
225
     *        the container)
 
226
     * @param component the real component being dragged, can be null
 
227
     * @param newConstraints component layout constraints to be presented
 
228
     * @param newIndex component's index position to be presented; not used
 
229
     *        for AbsoluteLayout
 
230
     * @param g Graphics object for painting (with color and line style set)
 
231
     * @return whether any feedback was painted (true in this case)
 
232
     */
 
233
    @Override
 
234
    public boolean paintDragFeedback(Container container, 
 
235
                                     Container containerDelegate,
 
236
                                     Component component,
 
237
                                     LayoutConstraints newConstraints,
 
238
                                     int newIndex,
 
239
                                     Graphics g)
 
240
    {
 
241
        Rectangle r = ((AbsoluteLayoutConstraints)newConstraints).getBounds();
 
242
        int w = r.width;
 
243
        int h = r.height;
 
244
 
 
245
        if (w == -1 || h == -1) {
 
246
            // JInternalFrame.getPreferredSize() behaves suspiciously
 
247
            Dimension pref = component instanceof javax.swing.JInternalFrame ?
 
248
                             component.getSize() : component.getPreferredSize();
 
249
            if (w == -1) w = pref.width;
 
250
            if (h == -1) h = pref.height;
 
251
        }
 
252
 
 
253
        if (w < 4) w = 4;
 
254
        if (h < 4) h = 4;
 
255
 
 
256
        g.drawRect(r.x, r.y, w, h);
 
257
 
 
258
        return true;
 
259
    }
 
260
 
 
261
    /** Provides resizing options for given component. It can combine the
 
262
     * bit-flag constants RESIZE_UP, RESIZE_DOWN, RESIZE_LEFT, RESIZE_RIGHT.
 
263
     * @param container instance of a real container in which the
 
264
     *        component is to be resized
 
265
     * @param containerDelegate effective container delegate of the container
 
266
     *        (e.g. like content pane of JFrame)
 
267
     * @param component real component to be resized
 
268
     * @param index position of the component in its container
 
269
     * @return resizing options for the component; 0 if no resizing is possible
 
270
     */
 
271
    @Override
 
272
    public int getResizableDirections(Container container,
 
273
                                      Container containerDelegate,
 
274
                                      Component component,
 
275
                                      int index)
 
276
    {
 
277
        return RESIZE_UP | RESIZE_DOWN | RESIZE_LEFT | RESIZE_RIGHT;
 
278
    }
 
279
 
 
280
    /** This method should calculate layout constraints for a component being
 
281
     * resized.
 
282
     * @param container instance of a real container in which the
 
283
     *        component is resized
 
284
     * @param containerDelegate effective container delegate of the container
 
285
     *        (e.g. like content pane of JFrame)
 
286
     * @param component real component being resized
 
287
     * @param index position of the component in its container
 
288
     * @param sizeChanges Insets object with size differences
 
289
     * @param posInCont position of mouse in the container delegate
 
290
     * @return component layout constraints for resized component; null if
 
291
     *         resizing is not possible or not implemented
 
292
     */
 
293
    @Override
 
294
    public LayoutConstraints getResizedConstraints(Container container,
 
295
                                                   Container containerDelegate,
 
296
                                                   Component component,
 
297
                                                   int index,
 
298
                                                   Rectangle originalBounds,
 
299
                                                   Insets sizeChanges,
 
300
                                                   Point posInCont)
 
301
    {
 
302
        int x, y, w, h;
 
303
        x = originalBounds.x;
 
304
        y = originalBounds.y;
 
305
        w = originalBounds.width;
 
306
        h = originalBounds.height;
 
307
 
 
308
        Dimension prefSize = component.getPreferredSize();
 
309
        int currentW, currentH;
 
310
 
 
311
        LayoutConstraints constr = getConstraints(index);
 
312
        if (constr instanceof AbsoluteLayoutConstraints) {
 
313
            Rectangle r = ((AbsoluteLayoutConstraints)constr).getBounds();
 
314
            currentW = r.width;
 
315
            currentH = r.height;
 
316
        }
 
317
        else {
 
318
            currentW = computeConstraintSize(w, -1, prefSize.width);
 
319
            currentH = computeConstraintSize(h, -1, prefSize.height);
 
320
        }
 
321
 
 
322
        int x2 = x + w;
 
323
        int y2 = y + h;
 
324
 
 
325
        if (sizeChanges.left + sizeChanges.right == 0)
 
326
            w = currentW; // no change
 
327
        else { // compute resized width and x coordinate
 
328
            w += sizeChanges.left + sizeChanges.right;
 
329
            w = w <= 0 ? -1 : computeConstraintSize(w, currentW, prefSize.width);
 
330
 
 
331
            if (w > 0) {
 
332
                if (formSettings.getApplyGridToSize()) {
 
333
                    int gridW = computeGridSize(w, formSettings.getGridX());
 
334
                    x -= sizeChanges.left +
 
335
                         (gridW - w) * sizeChanges.left
 
336
                         / (sizeChanges.left + sizeChanges.right);
 
337
                    w = gridW;
 
338
                }
 
339
            }
 
340
            else if (sizeChanges.left != 0)
 
341
                x = x2 - prefSize.width;
 
342
        }
 
343
 
 
344
        if (sizeChanges.top + sizeChanges.bottom == 0)
 
345
            h = currentH; // no change
 
346
        else { // compute resized height and y coordinate
 
347
            h += sizeChanges.top + sizeChanges.bottom;
 
348
            h = h <= 0 ? -1 : computeConstraintSize(h, currentH, prefSize.height);
 
349
 
 
350
            if (h > 0) {
 
351
                if (formSettings.getApplyGridToSize()) {
 
352
                    int gridH = computeGridSize(h, formSettings.getGridY());
 
353
                    y -= sizeChanges.top +
 
354
                         (gridH - h) * sizeChanges.top
 
355
                         / (sizeChanges.top + sizeChanges.bottom);
 
356
                    h = gridH;
 
357
                }
 
358
            }
 
359
            else if (sizeChanges.top != 0)
 
360
                y = y2 - prefSize.height;
 
361
        }
 
362
 
 
363
        return createNewConstraints(constr, x, y, w, h);
 
364
    }
 
365
 
 
366
    // -------
 
367
 
 
368
    /** This method is called from readComponentCode method to read layout
 
369
     * constraints of a component from code (AbsoluteConstraints in this case).
 
370
     * @param constrExp CodeExpression object of the constraints (taken from
 
371
     *        add method in the code)
 
372
     * @param constrCode CodeGroup to be filled with the relevant constraints
 
373
     *        initialization code; not needed here because AbsoluteConstraints
 
374
     *        object is represented only by a single code expression (based on
 
375
     *        constructor) and no statements
 
376
     * @param compExp CodeExpression of the component for which the constraints
 
377
     *        are read (not needed here)
 
378
     * @return LayoutConstraints based on information read form code
 
379
     */
 
380
    @Override
 
381
    protected LayoutConstraints readConstraintsCode(CodeExpression constrExp,
 
382
                                                    CodeGroup constrCode,
 
383
                                                    CodeExpression compExp)
 
384
    {
 
385
        AbsoluteLayoutConstraints constr =
 
386
            new AbsoluteLayoutConstraints(0, 0, -1, -1);
 
387
 
 
388
        CodeExpression[] params = constrExp.getOrigin().getCreationParameters();
 
389
        if (params.length == 4) {
 
390
            // reading is done in AbsoluteLayoutConstraints
 
391
            constr.readPropertyExpressions(params, 0);
 
392
        }
 
393
 
 
394
        return constr;
 
395
    }
 
396
 
 
397
    /** Called from createComponentCode method, creates code for a component
 
398
     * layout constraints (opposite to readConstraintsCode).
 
399
     * @param constrCode CodeGroup to be filled with constraints code; not
 
400
     *        needed here because AbsoluteConstraints object is represented
 
401
     *        only by a single constructor code expression and no statements
 
402
     * @param constr layout constraints metaobject representing the constraints
 
403
     * @param compExp CodeExpression object representing the component; not
 
404
     *        needed here
 
405
     * @return created CodeExpression representing the layout constraints
 
406
     */
 
407
    @Override
 
408
    protected CodeExpression createConstraintsCode(CodeGroup constrCode,
 
409
                                                   LayoutConstraints constr,
 
410
                                                   CodeExpression compExp,
 
411
                                                   int index)
 
412
    {
 
413
        if (!(constr instanceof AbsoluteLayoutConstraints))
 
414
            return null;
 
415
 
 
416
        AbsoluteLayoutConstraints absConstr = (AbsoluteLayoutConstraints)constr;
 
417
        // code expressions for constructor parameters are created in
 
418
        // AbsoluteLayoutConstraints
 
419
        CodeExpression[] params = absConstr.createPropertyExpressions(
 
420
                                                 getCodeStructure(), 0);
 
421
        return getCodeStructure().createExpression(getConstraintsConstructor(),
 
422
                                                   params);
 
423
    }
 
424
 
 
425
    /** This method is called to get a default component layout constraints
 
426
     * metaobject in case it is not provided (e.g. in addComponents method).
 
427
     * @return the default LayoutConstraints object for the supported layout
 
428
     */
 
429
    @Override
 
430
    protected LayoutConstraints createDefaultConstraints() {
 
431
        return new AbsoluteLayoutConstraints(0, 0, -1, -1);
 
432
    }
 
433
 
 
434
    // --------
 
435
 
 
436
    protected LayoutConstraints createNewConstraints(
 
437
                                    LayoutConstraints currentConstr,
 
438
                                    int x, int y, int w, int h)
 
439
    {
 
440
        return new AbsoluteLayoutConstraints(x, y, w, h);
 
441
    }
 
442
 
 
443
    private static int computeConstraintSize(int newSize,
 
444
                                             int currSize,
 
445
                                             int prefSize) {
 
446
        return newSize != -1 && (newSize != prefSize
 
447
                                 || (currSize != -1 && currSize == prefSize)) ?
 
448
               newSize : -1;
 
449
    }
 
450
 
 
451
    private static int computeGridSize(int size, int step) {
 
452
        if (step <= 0) return size;
 
453
        int mod = size % step;
 
454
        return mod >= step/2 ? size + step - mod : size - mod;
 
455
    }
 
456
 
 
457
    private static Constructor getConstraintsConstructor() {
 
458
        if (constrConstructor == null) {
 
459
            try {
 
460
                constrConstructor = AbsoluteConstraints.class.getConstructor(
 
461
                                    new Class[] { Integer.TYPE, Integer.TYPE,
 
462
                                                  Integer.TYPE, Integer.TYPE });
 
463
            }
 
464
            catch (NoSuchMethodException ex) { // should not happen
 
465
                ex.printStackTrace();
 
466
            }
 
467
        }
 
468
        return constrConstructor;
 
469
    }
 
470
 
 
471
    // -------------
 
472
 
 
473
    /** LayoutConstraints implementation class for AbsoluteConstraints.
 
474
     */
 
475
    public static class AbsoluteLayoutConstraints implements LayoutConstraints {
 
476
        int x, y, w, h; // position and size
 
477
 
 
478
        private Node.Property[] properties;
 
479
        boolean nullMode;
 
480
        Component refComponent;
 
481
 
 
482
        public AbsoluteLayoutConstraints(int x, int y, int w, int h)
 
483
        {
 
484
            this.x = x;
 
485
            this.y = y;
 
486
            this.w = w;
 
487
            this.h = h;
 
488
        }
 
489
 
 
490
        public Node.Property[] getProperties() {
 
491
            if (properties == null) {
 
492
                properties = createProperties();
 
493
                reinstateProperties();
 
494
            }
 
495
            return properties;
 
496
        }
 
497
 
 
498
        public Object getConstraintsObject() {
 
499
            return new AbsoluteConstraints(x, y, w, h);
 
500
        }
 
501
 
 
502
        public LayoutConstraints cloneConstraints() {
 
503
            return new AbsoluteLayoutConstraints(x, y, w, h);
 
504
        }
 
505
 
 
506
        // --------
 
507
 
 
508
        public Rectangle getBounds() {
 
509
            return new Rectangle(x, y, w, h);
 
510
        }
 
511
 
 
512
        protected Node.Property[] createProperties() {
 
513
            return new Node.Property[] {
 
514
                new FormProperty("AbsoluteLayoutConstraints posx", // NOI18N
 
515
                                 Integer.TYPE,
 
516
                             getBundle().getString("PROP_posx"), // NOI18N
 
517
                             getBundle().getString("HINT_posx")) { // NOI18N
 
518
 
 
519
                    public Object getTargetValue() {
 
520
                        return new Integer(x);
 
521
                    }
 
522
                    public void setTargetValue(Object value) {
 
523
                        x = ((Integer)value).intValue();
 
524
                    }
 
525
                    @Override
 
526
                    public void setPropertyContext(
 
527
                        org.netbeans.modules.form.FormPropertyContext ctx)
 
528
                    { // disabling this method due to limited persistence
 
529
                    } // capabilities (compatibility with previous versions)
 
530
                },
 
531
 
 
532
                new FormProperty("AbsoluteLayoutConstraints posy", // NOI18N
 
533
                                 Integer.TYPE,
 
534
                             getBundle().getString("PROP_posy"), // NOI18N
 
535
                             getBundle().getString("HINT_posy")) { // NOI18N
 
536
 
 
537
                    public Object getTargetValue() {
 
538
                        return new Integer(y);
 
539
                    }
 
540
                    public void setTargetValue(Object value) {
 
541
                        y = ((Integer)value).intValue();
 
542
                    }
 
543
                    @Override
 
544
                    public void setPropertyContext(
 
545
                        org.netbeans.modules.form.FormPropertyContext ctx)
 
546
                    { // disabling this method due to limited persistence
 
547
                    } // capabilities (compatibility with previous versions)
 
548
                },
 
549
 
 
550
                new FormProperty("AbsoluteLayoutConstraints width", // NOI18N
 
551
                                 Integer.TYPE,
 
552
                             getBundle().getString("PROP_width"), // NOI18N
 
553
                             getBundle().getString("HINT_width")) { // NOI18N
 
554
 
 
555
                    public Object getTargetValue() {
 
556
                        return new Integer(w);
 
557
                    }
 
558
                    public void setTargetValue(Object value) {
 
559
                        w = ((Integer)value).intValue();
 
560
                    }
 
561
                    @Override
 
562
                    public boolean supportsDefaultValue () {
 
563
                        return true;
 
564
                    }
 
565
                    @Override
 
566
                    public Object getDefaultValue() {
 
567
                        return new Integer(-1);
 
568
                    }
 
569
                    @Override
 
570
                    public PropertyEditor getExpliciteEditor() {
 
571
                        return new SizeEditor();
 
572
                    }
 
573
                    @Override
 
574
                    public Object getValue(String key) {
 
575
                        if ("canEditAsText".equals(key)) // NOI18N
 
576
                            return Boolean.TRUE;
 
577
                        return super.getValue(key);
 
578
                    }
 
579
                    @Override
 
580
                    public String getJavaInitializationString() {
 
581
                        if (nullMode && refComponent != null && !isChanged())
 
582
                            return Integer.toString(
 
583
                                     refComponent.getPreferredSize().width);
 
584
                        return super.getJavaInitializationString();
 
585
                    }
 
586
                    @Override
 
587
                    public void setPropertyContext(
 
588
                        org.netbeans.modules.form.FormPropertyContext ctx)
 
589
                    { // disabling this method due to limited persistence
 
590
                    } // capabilities (compatibility with previous versions)
 
591
                },
 
592
 
 
593
                new FormProperty("AbsoluteLayoutConstraints height", // NOI18N
 
594
                                 Integer.TYPE,
 
595
                             getBundle().getString("PROP_height"), // NOI18N
 
596
                             getBundle().getString("HINT_height")) { // NOI18N
 
597
 
 
598
                    public Object getTargetValue() {
 
599
                        return new Integer(h);
 
600
                    }
 
601
                    public void setTargetValue(Object value) {
 
602
                        h = ((Integer)value).intValue();
 
603
                    }
 
604
                    @Override
 
605
                    public boolean supportsDefaultValue () {
 
606
                        return true;
 
607
                    }
 
608
                    @Override
 
609
                    public Object getDefaultValue() {
 
610
                        return new Integer(-1);
 
611
                    }
 
612
                    @Override
 
613
                    public PropertyEditor getExpliciteEditor() {
 
614
                        return new SizeEditor();
 
615
                    }
 
616
                    @Override
 
617
                    public Object getValue(String key) {
 
618
                        if ("canEditAsText".equals(key)) // NOI18N
 
619
                            return Boolean.TRUE;
 
620
                        return super.getValue(key);
 
621
                    }
 
622
                    @Override
 
623
                    public String getJavaInitializationString() {
 
624
                        if (nullMode && refComponent != null && !isChanged())
 
625
                            return Integer.toString(
 
626
                                     refComponent.getPreferredSize().height);
 
627
                        return super.getJavaInitializationString();
 
628
                    }
 
629
                    @Override
 
630
                    public void setPropertyContext(
 
631
                        org.netbeans.modules.form.FormPropertyContext ctx)
 
632
                    { // disabling this method due to limited persistence
 
633
                    } // capabilities (compatibility with previous versions)
 
634
                }
 
635
            };
 
636
        }
 
637
 
 
638
        private void reinstateProperties() {
 
639
            try {
 
640
                for (int i=0; i < properties.length; i++) {
 
641
                    FormProperty prop = (FormProperty) properties[i];
 
642
                    prop.reinstateProperty();
 
643
                }
 
644
            }
 
645
            catch(IllegalAccessException e1) {} // should not happen
 
646
            catch(java.lang.reflect.InvocationTargetException e2) {} // should not happen
 
647
        }
 
648
 
 
649
        /** This method creates CodeExpression objects for properties of
 
650
         * AbsoluteConstraints - this is used by the layout delegate's method
 
651
         * createConstraintsCode which uses the expressions as parameters
 
652
         * in AbsoluteConstraints constructor.
 
653
         * @param codeStructure main CodeStructure object in which the code
 
654
         *        expressions are created
 
655
         * @param shift this parameter is used only by subclasses of
 
656
         *        AbsoluteLayoutConstraints (which may insert another
 
657
         *        constructor parameters before x, y, w and h)
 
658
         * @return array of created code expressions
 
659
         */
 
660
        protected final CodeExpression[] createPropertyExpressions(
 
661
                                             CodeStructure codeStructure,
 
662
                                             int shift)
 
663
        {
 
664
            // first make sure properties are created...
 
665
            getProperties();
 
666
 
 
667
            // ...then create code expressions based on the properties
 
668
            CodeExpression xEl = codeStructure.createExpression(
 
669
                           FormCodeSupport.createOrigin(properties[shift++]));
 
670
            CodeExpression yEl = codeStructure.createExpression(
 
671
                           FormCodeSupport.createOrigin(properties[shift++]));
 
672
            CodeExpression wEl = codeStructure.createExpression(
 
673
                           FormCodeSupport.createOrigin(properties[shift++]));
 
674
            CodeExpression hEl = codeStructure.createExpression(
 
675
                           FormCodeSupport.createOrigin(properties[shift]));
 
676
            return new CodeExpression[] { xEl, yEl, wEl, hEl };
 
677
        }
 
678
 
 
679
        /** This method reads CodeExpression objects for properties (used as
 
680
         * AbsoluteConstraints constructor parameters). Called by layout
 
681
         * delegate's readConstraintsCode method.
 
682
         * @param exps array of code expressions to read to properties
 
683
         * @param shift this parameter is used only by subclasses of
 
684
         *        AbsoluteLayoutConstraints (which may insert another
 
685
         *        constructor parameters before x, y, w and h)
 
686
         */
 
687
        protected final void readPropertyExpressions(CodeExpression[] exps,
 
688
                                                     int shift)
 
689
        {
 
690
            // first make sure properties are created...
 
691
            getProperties();
 
692
 
 
693
            // ...then map the properties to the code expressions
 
694
            for (int i=0; i < exps.length; i++)
 
695
                FormCodeSupport.readPropertyExpression(exps[i],
 
696
                                                       properties[i+shift],
 
697
                                                       false);
 
698
        }
 
699
    }
 
700
 
 
701
    // -----------
 
702
 
 
703
    /** PropertyEditor for width and height properties of
 
704
     * AbsoluteLayoutConstraints.
 
705
     */
 
706
    public static final class SizeEditor extends PropertyEditorSupport {
 
707
 
 
708
        final Integer prefValue = new Integer(-1);
 
709
        final String prefTag = getBundle().getString("VALUE_preferred"); // NOI18N
 
710
 
 
711
        @Override
 
712
        public String[] getTags() {
 
713
            return new String[] { prefTag };
 
714
        }
 
715
 
 
716
        @Override
 
717
        public String getAsText() {
 
718
            Object value = getValue();
 
719
            return prefValue.equals(value) ?
 
720
                     prefTag : value.toString();
 
721
        }
 
722
 
 
723
        @Override
 
724
        public void setAsText(String str) {
 
725
            if (prefTag.equals(str))
 
726
                setValue(prefValue);
 
727
            else
 
728
                try {
 
729
                    setValue(new Integer(Integer.parseInt(str)));
 
730
                } 
 
731
                catch (NumberFormatException e) {} // ignore
 
732
        }
 
733
 
 
734
        @Override
 
735
        public String getJavaInitializationString() {
 
736
            Object value = getValue();
 
737
            return value != null ? value.toString() : null;
 
738
        }
 
739
    }
 
740
}