0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package javax.swing;
0018:
0019: import java.awt.Component;
0020: import java.awt.Container;
0021: import java.awt.Dimension;
0022: import java.awt.Rectangle;
0023: import java.awt.TextField;
0024:
0025: import javax.swing.SpringLayout.Constraints;
0026:
0027: public class SpringLayoutTest extends SwingTestCase {
0028: private Component component;
0029: private SpringLayout layout;
0030: private JPanel container;
0031: private JButton button;
0032: private JTextField textField;
0033: private JPanel panel;
0034: private JButton innerButton;
0035:
0036: @Override
0037: protected void setUp() throws Exception {
0038: super .setUp();
0039: layout = new SpringLayout();
0040: component = new JLabel("label");
0041: container = new JPanel(layout);
0042: button = new JButton();
0043: textField = new JTextField();
0044: panel = new JPanel();
0045: innerButton = new JButton();
0046:
0047: container.add(button);
0048: container.add(panel);
0049: container.add(textField);
0050:
0051: panel.add(innerButton);
0052:
0053: setComponentSizes(container, Spring.constant(0, 5,
0054: Integer.MAX_VALUE), Spring.constant(0, 5,
0055: Integer.MAX_VALUE));
0056:
0057: setComponentSizes(button, Spring.constant(2, 5, 100), Spring
0058: .constant(2, 5, 100));
0059:
0060: setComponentSizes(textField, Spring.constant(2, 10,
0061: Integer.MAX_VALUE), Spring.constant(2, 10,
0062: Integer.MAX_VALUE));
0063:
0064: setComponentSizes(panel, Spring.constant(1, 3,
0065: Integer.MAX_VALUE), Spring.constant(1, 3,
0066: Integer.MAX_VALUE));
0067:
0068: setComponentSizes(innerButton, Spring.constant(2, 10, 60),
0069: Spring.constant(2, 10, 60));
0070: }
0071:
0072: public void testSpringLayout() {
0073: SpringLayout.Constraints constrains = new SpringLayout.Constraints();
0074: assertNull(constrains.getX());
0075: assertNull(constrains.getY());
0076: assertNull(constrains.getWidth());
0077: assertNull(constrains.getHeight());
0078: constrains = new SpringLayout.Constraints(Spring
0079: .width(component), Spring.constant(0));
0080: assertNull(constrains.getWidth());
0081: assertNull(constrains.getHeight());
0082: SpringTest.assertSizes(0, 0, 0, 0, constrains.getY());
0083: SpringTest.assertSizes(component.getMinimumSize().width,
0084: component.getPreferredSize().width, component
0085: .getMaximumSize().width, constrains.getX());
0086: assertNull(constrains.getWidth());
0087: assertNull(constrains.getHeight());
0088: constrains = new SpringLayout.Constraints(component);
0089: SpringTest.assertSizes(0, 0, 0, 0, constrains.getX());
0090: SpringTest.assertSizes(0, 0, 0, 0, constrains.getY());
0091: SpringTest.assertSizes(component.getMinimumSize().width,
0092: component.getPreferredSize().width, component
0093: .getMaximumSize().width, constrains.getWidth());
0094: SpringTest.assertSizes(component.getMinimumSize().height,
0095: component.getPreferredSize().height, component
0096: .getMaximumSize().height, constrains
0097: .getHeight());
0098: constrains = new SpringLayout.Constraints(Spring.constant(1),
0099: Spring.constant(2), Spring.constant(3), Spring
0100: .constant(4));
0101: SpringTest.assertSizes(1, 1, 1, 1, constrains.getX());
0102: SpringTest.assertSizes(2, 2, 2, 2, constrains.getY());
0103: SpringTest.assertSizes(3, 3, 3, 3, constrains.getWidth());
0104: SpringTest.assertSizes(4, 4, 4, 4, constrains.getHeight());
0105: constrains = new SpringLayout.Constraints(Spring.constant(1),
0106: null);
0107: SpringTest.assertSizes(1, 1, 1, 1, constrains.getX());
0108: Container container = new JPanel();
0109: container.setLayout(layout);
0110: container.add(new JLabel(""));
0111: constrains = layout.getConstraints(component);
0112: SpringTest.assertSizes(0, 0, 0, 0, constrains.getX());
0113: SpringTest.assertSizes(0, 0, 0, 0, constrains.getY());
0114: SpringTest.assertSizes(component.getMinimumSize().width,
0115: component.getPreferredSize().width, component
0116: .getMaximumSize().width, constrains.getWidth());
0117: SpringTest.assertSizes(component.getMinimumSize().height,
0118: component.getPreferredSize().height, component
0119: .getMaximumSize().height, constrains
0120: .getHeight());
0121: }
0122:
0123: public void testGetLayoutAlignmentX() {
0124: JPanel container = new JPanel(layout);
0125: assertEquals(0.5f, layout.getLayoutAlignmentX(null), 0.01);
0126: assertEquals(0.5f, layout.getLayoutAlignmentX(container), 0.01);
0127: }
0128:
0129: public void testGetLayoutAlignmentY() {
0130: JPanel container = new JPanel(layout);
0131: assertEquals(0.5f, layout.getLayoutAlignmentY(null), 0.01);
0132: assertEquals(0.5f, layout.getLayoutAlignmentY(container), 0.01);
0133: }
0134:
0135: public void testGetConstraints() {
0136: Constraints constraints = layout.getConstraints(null);
0137: assertNotNull(constraints);
0138: layout.addLayoutComponent(component, "not_constraints");
0139: constraints = layout.getConstraints(component);
0140: assertNotNull(constraints);
0141: SpringTest.assertSizes(0, 0, 0, constraints.getX());
0142: SpringTest.assertSizes(0, 0, 0, constraints.getY());
0143: SpringTest
0144: .assertSizes(component.getMinimumSize().width,
0145: component.getPreferredSize().width, component
0146: .getMaximumSize().width, constraints
0147: .getWidth());
0148: SpringTest.assertSizes(component.getMinimumSize().height,
0149: component.getPreferredSize().height, component
0150: .getMaximumSize().height, constraints
0151: .getHeight());
0152: layout.addLayoutComponent(component, null);
0153: constraints = layout.getConstraints(component);
0154: assertNotNull(constraints);
0155: SpringTest.assertSizes(0, 0, 0, constraints.getX());
0156: SpringTest.assertSizes(0, 0, 0, constraints.getY());
0157: SpringTest
0158: .assertSizes(component.getMinimumSize().width,
0159: component.getPreferredSize().width, component
0160: .getMaximumSize().width, constraints
0161: .getWidth());
0162: SpringTest.assertSizes(component.getMinimumSize().height,
0163: component.getPreferredSize().height, component
0164: .getMaximumSize().height, constraints
0165: .getHeight());
0166: Constraints componentConstraints = new SpringLayout.Constraints(
0167: component);
0168: layout.addLayoutComponent(component, constraints);
0169: constraints = layout.getConstraints(component);
0170: assertFalse(componentConstraints.equals(constraints));
0171: SpringTest.assertValues(componentConstraints.getX(),
0172: constraints.getX());
0173: SpringTest.assertValues(componentConstraints.getY(),
0174: constraints.getY());
0175: SpringTest.assertValues(componentConstraints.getWidth(),
0176: constraints.getWidth());
0177: SpringTest.assertValues(componentConstraints.getHeight(),
0178: constraints.getHeight());
0179:
0180: assertNotNull(layout.getConstraints(new JLabel()));
0181: }
0182:
0183: public void testGetConstraint() {
0184: layout.putConstraint(SpringLayout.SOUTH, button, Spring
0185: .constant(5), SpringLayout.NORTH, container);
0186: layout.putConstraint(SpringLayout.EAST, button, Spring
0187: .constant(5), SpringLayout.WEST, container);
0188:
0189: layout.layoutContainer(container);
0190:
0191: Spring constraint = layout.getConstraint(SpringLayout.WEST,
0192: button);
0193: assertEquals(0, constraint.getValue());
0194: layout.getConstraints(button).setConstraint(SpringLayout.EAST,
0195: Spring.constant(100));
0196: layout.layoutContainer(container);
0197: assertEquals(95, constraint.getValue());
0198:
0199: assertNotNull(layout.getConstraint(SpringLayout.EAST,
0200: new JLabel()));
0201: }
0202:
0203: public void testPutConstraint_Cycles() throws Exception {
0204: if (isHarmony()) {
0205: layout.putConstraint(SpringLayout.EAST, button, 5,
0206: SpringLayout.WEST, panel);
0207: layout.putConstraint(SpringLayout.EAST, panel, 5,
0208: SpringLayout.WEST, textField);
0209: layout.putConstraint(SpringLayout.EAST, textField, 5,
0210: SpringLayout.WEST, button);
0211: layout.layoutContainer(container);
0212: layout.layoutContainer(container);
0213:
0214: layout.getConstraints(button);
0215:
0216: assertEdges(layout.getConstraints(button), -3, 2, 0, 5);
0217: assertEdges(layout.getConstraints(panel), -3, 0, 0, 3);
0218: assertEdges(layout.getConstraints(textField), -5, 5, 0, 10);
0219:
0220: layout.putConstraint(SpringLayout.EAST, textField, 5,
0221: SpringLayout.WEST, container);
0222: layout.layoutContainer(container);
0223: }
0224: }
0225:
0226: public void testPutConstraint() throws Exception {
0227: JFrame frame = new JFrame("testPutConstraint");
0228: container.setLayout(layout);
0229: layout.putConstraint(SpringLayout.SOUTH, button, 5,
0230: SpringLayout.NORTH, container);
0231: layout.putConstraint(SpringLayout.EAST, button, Spring
0232: .constant(5), SpringLayout.WEST, container);
0233:
0234: frame.pack();
0235: frame.setVisible(true);
0236: frame.setSize(100, 200);
0237: assertEquals(0, button.getX());
0238: assertEquals(0, button.getY());
0239:
0240: layout.putConstraint(SpringLayout.EAST, button, Spring
0241: .constant(15), SpringLayout.WEST, container);
0242:
0243: layout.layoutContainer(container);
0244: assertEquals(10, button.getX());
0245: assertEquals(0, button.getY());
0246:
0247: layout.putConstraint(SpringLayout.SOUTH, button, null,
0248: SpringLayout.NORTH, container);
0249:
0250: testExceptionalCase(new NullPointerCase() {
0251: @Override
0252: public void exceptionalAction() throws Exception {
0253: layout.layoutContainer(container);
0254: }
0255:
0256: });
0257: assertEquals(10, button.getX());
0258: assertEquals(0, button.getY());
0259:
0260: //Independence from constraints order
0261: layout = new SpringLayout();
0262: container.setLayout(layout);
0263: JTextField textField = new JTextField();
0264: textField.getInsets().set(5, 5, 5, 5);
0265: setComponentSizes(textField, Spring.constant(2, 10,
0266: Integer.MAX_VALUE), Spring.constant(2, 10,
0267: Integer.MAX_VALUE));
0268: container.add(textField);
0269:
0270: layout.getConstraints(button).setHeight(Spring.constant(50));
0271:
0272: layout.putConstraint(SpringLayout.NORTH, textField, Spring
0273: .constant(5), SpringLayout.SOUTH, button);
0274:
0275: layout.putConstraint(SpringLayout.SOUTH, textField, Spring
0276: .constant(-10), SpringLayout.SOUTH, container);
0277:
0278: container.setSize(200, 200);
0279: layout.layoutContainer(container);
0280: if (isHarmony()) {
0281: assertEquals(135, textField.getHeight());
0282: } else {
0283: assertEquals(10, textField.getHeight());
0284: }
0285:
0286: layout = new SpringLayout();
0287: textField = new JTextField();
0288: container.add(textField);
0289: container.setLayout(layout);
0290: layout.getConstraints(button).setHeight(Spring.constant(50));
0291:
0292: layout.putConstraint(SpringLayout.SOUTH, textField, Spring
0293: .constant(-10), SpringLayout.SOUTH, container);
0294:
0295: layout.putConstraint(SpringLayout.NORTH, textField, Spring
0296: .constant(5), SpringLayout.SOUTH, button);
0297:
0298: container.setSize(200, 200);
0299: layout.layoutContainer(container);
0300: assertEquals(135, textField.getHeight());
0301:
0302: component = new TextField();
0303: layout.putConstraint(SpringLayout.NORTH, textField, Spring
0304: .constant(5), SpringLayout.SOUTH, component);
0305: layout.layoutContainer(container);
0306: assertEquals(185, textField.getHeight());
0307: }
0308:
0309: public void testAddLayoutComponent() throws Exception {
0310: Constraints constraints = new Constraints(Spring.constant(1),
0311: Spring.constant(2), Spring.constant(3), Spring
0312: .constant(4));
0313: layout.addLayoutComponent(null, constraints);
0314: Constraints componentCnstraints = layout.getConstraints(null);
0315: assertNotNull(componentCnstraints);
0316: assertSame(componentCnstraints, constraints);
0317:
0318: layout.addLayoutComponent(button, null);
0319: componentCnstraints = layout.getConstraints(button);
0320: assertNotNull(componentCnstraints);
0321: assertEquals(componentCnstraints.getX().getValue(), 0);
0322: assertEquals(componentCnstraints.getY().getValue(), 0);
0323: assertEquals(componentCnstraints.getWidth().getValue(), button
0324: .getPreferredSize().width);
0325: assertEquals(componentCnstraints.getHeight().getValue(), button
0326: .getPreferredSize().height);
0327:
0328: layout.addLayoutComponent(button, constraints);
0329: componentCnstraints = layout.getConstraints(button);
0330: assertSame(componentCnstraints, constraints);
0331: }
0332:
0333: public void testRemoveLayoutComponent() {
0334: layout.removeLayoutComponent(null);
0335: layout.removeLayoutComponent(new JLabel());
0336:
0337: layout.addLayoutComponent(component,
0338: new SpringLayout.Constraints(component));
0339: layout.removeLayoutComponent(null);
0340: layout.removeLayoutComponent(new JLabel());
0341:
0342: JPanel panel = new JPanel(layout);
0343: panel.add(component);
0344: Constraints constraints1 = layout.getConstraints(component);
0345: layout.removeLayoutComponent(component);
0346:
0347: Constraints constraints2 = layout.getConstraints(component);
0348: assertNotSame(constraints1, constraints2);
0349: SpringTest.assertSizes(0, 0, 0, 0, constraints2.getX());
0350: SpringTest.assertSizes(0, 0, 0, 0, constraints2.getY());
0351:
0352: component = new JTextField();
0353: JLabel label = new JLabel("label");
0354: setComponentSizes(label, Spring.constant(5), Spring
0355: .constant(10));
0356: panel.add(component);
0357: panel.add(label);
0358:
0359: layout.getConstraints(label).setX(Spring.constant(15));
0360: layout.putConstraint(SpringLayout.WEST, component, 5,
0361: SpringLayout.EAST, label);
0362: layout.layoutContainer(panel);
0363: assertEquals(15, label.getX());
0364:
0365: layout.removeLayoutComponent(label);
0366: layout.layoutContainer(panel);
0367: assertEquals(0, label.getX());
0368: }
0369:
0370: public void testInvalidateLayout() throws Exception {
0371: JFrame frame = new JFrame("testMinimumLayoutSize");
0372: frame.setContentPane(container);
0373: layout = new SpringLayout();
0374: container.setLayout(layout);
0375: Constraints buttonConstraints = layout.getConstraints(button);
0376: buttonConstraints.setHeight(Spring.constant(33, 34, 35));
0377: Constraints containerConstraints = layout
0378: .getConstraints(container);
0379: containerConstraints.setConstraint(SpringLayout.SOUTH,
0380: buttonConstraints.getConstraint(SpringLayout.SOUTH));
0381: containerConstraints.setConstraint(SpringLayout.EAST,
0382: buttonConstraints.getConstraint(SpringLayout.EAST));
0383: containerConstraints.setConstraint(SpringLayout.WEST, Spring
0384: .constant(3));
0385: frame.pack();
0386: frame.setVisible(true);
0387: frame.setSize(100, 200);
0388:
0389: Spring width = buttonConstraints.getWidth();
0390: if (isHarmony()) {
0391: assertEquals(5, width.getValue());
0392: } else {
0393: assertEquals(115, width.getValue());
0394: }
0395: frame.setSize(200, 300);
0396: layout.invalidateLayout(container);
0397: assertSame(width, buttonConstraints.getWidth());
0398: if (isHarmony()) {
0399: assertEquals(5, width.getValue());
0400: } else {
0401: assertEquals(115, width.getValue());
0402: }
0403: }
0404:
0405: public void testLayoutContainer() throws Exception {
0406: button.setPreferredSize(new Dimension(11, 12));
0407:
0408: Constraints buttonConstraints = layout.getConstraints(button);
0409: buttonConstraints.setHeight(Spring.constant(35));
0410: Constraints containerConstraints = layout
0411: .getConstraints(container);
0412: containerConstraints.setConstraint(SpringLayout.SOUTH,
0413: buttonConstraints.getConstraint(SpringLayout.SOUTH));
0414: container.setSize(100, 200);
0415: layout.layoutContainer(container);
0416: assertBounds(0, 0, 11, 35, button.getBounds());
0417:
0418: containerConstraints.setConstraint(SpringLayout.EAST,
0419: buttonConstraints.getWidth());
0420: layout.layoutContainer(container);
0421: assertBounds(0, 0, 100, 35, button.getBounds());
0422:
0423: container.setSize(101, 201);
0424: buttonConstraints.setHeight(Spring.constant(40));
0425: layout.layoutContainer(innerButton);
0426: assertBounds(0, 0, 100, 35, button.getBounds());
0427:
0428: container.setSize(102, 202);
0429: buttonConstraints.setHeight(Spring.constant(50));
0430: layout.layoutContainer(textField);
0431: assertBounds(0, 0, 100, 35, button.getBounds());
0432:
0433: //putConstraints()
0434: layout = new SpringLayout();
0435: container.setLayout(layout);
0436: layout.putConstraint(SpringLayout.SOUTH, button, Spring
0437: .constant(5), SpringLayout.NORTH, panel);
0438: layout.putConstraint(SpringLayout.SOUTH, button, Spring
0439: .constant(15), SpringLayout.NORTH, panel);
0440: layout.putConstraint(SpringLayout.SOUTH, textField, Spring
0441: .constant(55), SpringLayout.SOUTH, button);
0442: layout.putConstraint(SpringLayout.EAST, button, Spring
0443: .constant(15), SpringLayout.WEST, panel);
0444:
0445: layout.putConstraint(SpringLayout.EAST, textField, Spring
0446: .constant(55), SpringLayout.EAST, button);
0447: container.setSize(100, 200);
0448: layout.layoutContainer(container);
0449:
0450: Spring y = Spring.sum(Spring.sum(layout.getConstraint(
0451: SpringLayout.SOUTH, button), Spring.constant(55)),
0452: Spring.minus(Spring.height(textField)));
0453: Spring height = Spring.height(textField);
0454: assertBounds(4, 3, 11, 12, button.getBounds());
0455: assertBounds(60, y.getValue(), 10, height.getValue(), textField
0456: .getBounds());
0457:
0458: layout = new SpringLayout();
0459: container.setLayout(layout);
0460: layout.putConstraint(SpringLayout.NORTH, button, Spring
0461: .constant(5), SpringLayout.NORTH, panel);
0462: layout.putConstraint(SpringLayout.NORTH, button, Spring
0463: .constant(15), SpringLayout.NORTH, panel);
0464: layout.putConstraint(SpringLayout.NORTH, textField, Spring
0465: .constant(55), SpringLayout.SOUTH, button);
0466: layout.putConstraint(SpringLayout.WEST, button, Spring
0467: .constant(15), SpringLayout.WEST, panel);
0468: layout.putConstraint(SpringLayout.WEST, textField, Spring
0469: .constant(55), SpringLayout.EAST, button);
0470: layout.layoutContainer(container);
0471: y = Spring.sum(
0472: layout.getConstraint(SpringLayout.SOUTH, button),
0473: Spring.constant(55));
0474: height = Spring.height(textField);
0475: assertBounds(15, 15, 11, 12, button.getBounds());
0476: assertBounds(81, y.getValue(), 10, height.getValue(), textField
0477: .getBounds());
0478: }
0479:
0480: public void testMinimumLayoutSize() throws Exception {
0481: //container
0482: setComponentSizes(button, Spring.constant(2, 5, 100), Spring
0483: .constant(3, 6, 101));
0484: Constraints buttonConstraints = layout.getConstraints(button);
0485: buttonConstraints.setHeight(Spring.constant(33, 34, 35));
0486: Constraints containerConstraints = layout
0487: .getConstraints(container);
0488: containerConstraints.setConstraint(SpringLayout.SOUTH,
0489: buttonConstraints.getConstraint(SpringLayout.SOUTH));
0490: containerConstraints.setConstraint(SpringLayout.EAST,
0491: buttonConstraints.getConstraint(SpringLayout.EAST));
0492: containerConstraints.setConstraint(SpringLayout.WEST, Spring
0493: .constant(3));
0494: layout.layoutContainer(container);
0495: assertEquals(2, layout.minimumLayoutSize(container).width);
0496: assertEquals(33, layout.minimumLayoutSize(container).height);
0497:
0498: //putConstraints()
0499: assertEquals(
0500: button.getInsets().left + button.getInsets().right,
0501: layout.minimumLayoutSize(button).width);
0502:
0503: assertEquals(panel.getInsets().left + panel.getInsets().right,
0504: layout.minimumLayoutSize(panel).width);
0505:
0506: assertEquals(innerButton.getInsets().left
0507: + innerButton.getInsets().right, layout
0508: .minimumLayoutSize(innerButton).width);
0509:
0510: buttonConstraints.setX(Spring.constant(20));
0511: assertEquals(
0512: button.getInsets().left + button.getInsets().right,
0513: layout.minimumLayoutSize(button).width);
0514: assertEquals(0, layout.getConstraint(SpringLayout.EAST, button)
0515: .getPreferredValue());
0516:
0517: buttonConstraints.setWidth(Spring.constant(100));
0518: assertEquals(100 + button.getInsets().left
0519: + button.getInsets().right, layout
0520: .minimumLayoutSize(button).width);
0521:
0522: layout.getConstraints(innerButton).setX(Spring.constant(20));
0523: assertEquals(innerButton.getInsets().left
0524: + innerButton.getInsets().right, layout
0525: .minimumLayoutSize(innerButton).width);
0526:
0527: layout.getConstraints(innerButton).setWidth(
0528: Spring.constant(100));
0529: assertEquals(100 + innerButton.getInsets().left
0530: + innerButton.getInsets().right, layout
0531: .minimumLayoutSize(innerButton).width);
0532:
0533: layout.getConstraints(panel).setX(Spring.constant(20));
0534: assertEquals(panel.getInsets().left + panel.getInsets().right,
0535: layout.minimumLayoutSize(panel).width);
0536: assertEquals(0, layout.getConstraint(SpringLayout.EAST, panel)
0537: .getPreferredValue());
0538:
0539: layout.getConstraints(panel).setWidth(Spring.constant(100));
0540: assertEquals(100 + panel.getInsets().left
0541: + panel.getInsets().right, layout
0542: .minimumLayoutSize(panel).width);
0543: assertEquals(100, layout
0544: .getConstraint(SpringLayout.EAST, panel)
0545: .getPreferredValue());
0546:
0547: layout = new SpringLayout();
0548: container.setLayout(layout);
0549: layout.putConstraint(SpringLayout.SOUTH, button, Spring
0550: .constant(15), SpringLayout.NORTH, container);
0551: layout.putConstraint(SpringLayout.EAST, button, Spring
0552: .constant(5), SpringLayout.WEST, container);
0553: assertEquals(panel.getInsets().left + panel.getInsets().right,
0554: layout.minimumLayoutSize(panel).width);
0555: assertEquals(innerButton.getInsets().left
0556: + innerButton.getInsets().right, layout
0557: .minimumLayoutSize(innerButton).width);
0558:
0559: assertEquals(5 + button.getInsets().left
0560: + button.getInsets().right, layout
0561: .minimumLayoutSize(button).width);
0562: }
0563:
0564: public void testPreferredLayoutSize() throws Exception {
0565: //container
0566: setComponentSizes(button, Spring.constant(2, 5, 100), Spring
0567: .constant(3, 6, 101));
0568: Constraints buttonConstraints = layout.getConstraints(button);
0569: buttonConstraints.setHeight(Spring.constant(33, 34, 35));
0570: Constraints containerConstraints = layout
0571: .getConstraints(container);
0572: containerConstraints.setConstraint(SpringLayout.SOUTH,
0573: buttonConstraints.getConstraint(SpringLayout.SOUTH));
0574: containerConstraints.setConstraint(SpringLayout.EAST,
0575: buttonConstraints.getConstraint(SpringLayout.EAST));
0576: containerConstraints.setConstraint(SpringLayout.WEST, Spring
0577: .constant(3));
0578: layout.layoutContainer(container);
0579: assertEquals(5, layout.preferredLayoutSize(container).width);
0580: assertEquals(34, layout.preferredLayoutSize(container).height);
0581:
0582: //putConstraints()
0583: assertEquals(
0584: button.getInsets().left + button.getInsets().right,
0585: layout.preferredLayoutSize(button).width);
0586:
0587: assertEquals(panel.getInsets().left + panel.getInsets().right,
0588: layout.preferredLayoutSize(panel).width);
0589:
0590: assertEquals(innerButton.getInsets().left
0591: + innerButton.getInsets().right, layout
0592: .preferredLayoutSize(innerButton).width);
0593:
0594: buttonConstraints.setX(Spring.constant(20));
0595: assertEquals(
0596: button.getInsets().left + button.getInsets().right,
0597: layout.preferredLayoutSize(button).width);
0598: assertEquals(0, layout.getConstraint(SpringLayout.EAST, button)
0599: .getPreferredValue());
0600:
0601: buttonConstraints.setWidth(Spring.constant(100));
0602: assertEquals(100 + button.getInsets().left
0603: + button.getInsets().right, layout
0604: .preferredLayoutSize(button).width);
0605:
0606: layout.getConstraints(innerButton).setX(Spring.constant(20));
0607: assertEquals(innerButton.getInsets().left
0608: + innerButton.getInsets().right, layout
0609: .preferredLayoutSize(innerButton).width);
0610:
0611: layout.getConstraints(innerButton).setWidth(
0612: Spring.constant(100));
0613: assertEquals(100 + innerButton.getInsets().left
0614: + innerButton.getInsets().right, layout
0615: .preferredLayoutSize(innerButton).width);
0616:
0617: layout.getConstraints(panel).setX(Spring.constant(20));
0618: assertEquals(panel.getInsets().left + panel.getInsets().right,
0619: layout.preferredLayoutSize(panel).width);
0620:
0621: assertEquals(0, layout.getConstraint(SpringLayout.EAST, panel)
0622: .getPreferredValue());
0623:
0624: layout.getConstraints(panel).setWidth(Spring.constant(100));
0625: assertEquals(100 + panel.getInsets().left
0626: + panel.getInsets().right, layout
0627: .preferredLayoutSize(panel).width);
0628: assertEquals(100, layout
0629: .getConstraint(SpringLayout.EAST, panel)
0630: .getPreferredValue());
0631:
0632: layout = new SpringLayout();
0633: container.setLayout(layout);
0634: layout.putConstraint(SpringLayout.SOUTH, button, Spring
0635: .constant(15), SpringLayout.NORTH, container);
0636:
0637: layout.putConstraint(SpringLayout.EAST, button, Spring
0638: .constant(5), SpringLayout.WEST, container);
0639:
0640: assertEquals(panel.getInsets().left + panel.getInsets().right,
0641: layout.preferredLayoutSize(panel).width);
0642:
0643: assertEquals(innerButton.getInsets().left
0644: + innerButton.getInsets().right, layout
0645: .preferredLayoutSize(innerButton).width);
0646:
0647: assertEquals(5 + button.getInsets().left
0648: + button.getInsets().right, layout
0649: .preferredLayoutSize(button).width);
0650: }
0651:
0652: public void testMaximumLayoutSize() throws Exception {
0653: //container
0654: setComponentSizes(button, Spring.constant(2, 5, 100), Spring
0655: .constant(3, 6, 101));
0656: Constraints buttonConstraints = layout.getConstraints(button);
0657: buttonConstraints.setHeight(Spring.constant(33, 34, 35));
0658: Constraints containerConstraints = layout
0659: .getConstraints(container);
0660: containerConstraints.setConstraint(SpringLayout.SOUTH,
0661: buttonConstraints.getConstraint(SpringLayout.SOUTH));
0662: containerConstraints.setConstraint(SpringLayout.EAST,
0663: buttonConstraints.getConstraint(SpringLayout.EAST));
0664: containerConstraints.setConstraint(SpringLayout.WEST, Spring
0665: .constant(3));
0666: layout.layoutContainer(container);
0667: assertEquals(100, layout.maximumLayoutSize(container).width);
0668: assertEquals(35, layout.maximumLayoutSize(container).height);
0669:
0670: //putConstraints()
0671: assertEquals(Integer.MAX_VALUE + button.getInsets().left
0672: + button.getInsets().right, layout
0673: .maximumLayoutSize(button).width);
0674:
0675: assertEquals(Integer.MAX_VALUE + panel.getInsets().left
0676: + panel.getInsets().right, layout
0677: .maximumLayoutSize(panel).width);
0678:
0679: assertEquals(Integer.MAX_VALUE + innerButton.getInsets().left
0680: + innerButton.getInsets().right, layout
0681: .maximumLayoutSize(innerButton).width);
0682:
0683: buttonConstraints.setX(Spring.constant(20));
0684: assertEquals(Integer.MAX_VALUE + button.getInsets().left
0685: + button.getInsets().right, layout
0686: .maximumLayoutSize(button).width);
0687: assertEquals(0, layout.getConstraint(SpringLayout.EAST, button)
0688: .getPreferredValue());
0689:
0690: buttonConstraints.setWidth(Spring.constant(100));
0691: assertEquals(100 + button.getInsets().left
0692: + button.getInsets().right, layout
0693: .maximumLayoutSize(button).width);
0694:
0695: layout.getConstraints(innerButton).setX(Spring.constant(20));
0696: assertEquals(Integer.MAX_VALUE + innerButton.getInsets().left
0697: + innerButton.getInsets().right, layout
0698: .maximumLayoutSize(innerButton).width);
0699:
0700: layout.getConstraints(innerButton).setWidth(
0701: Spring.constant(100));
0702: assertEquals(100 + innerButton.getInsets().left
0703: + innerButton.getInsets().right, layout
0704: .maximumLayoutSize(innerButton).width);
0705:
0706: layout.getConstraints(panel).setX(Spring.constant(20));
0707: assertEquals(Integer.MAX_VALUE + panel.getInsets().left
0708: + panel.getInsets().right, layout
0709: .maximumLayoutSize(panel).width);
0710:
0711: assertEquals(0, layout.getConstraint(SpringLayout.EAST, panel)
0712: .getPreferredValue());
0713:
0714: layout.getConstraints(panel).setWidth(Spring.constant(100));
0715: assertEquals(100 + panel.getInsets().left
0716: + panel.getInsets().right, layout
0717: .maximumLayoutSize(panel).width);
0718: assertEquals(100, layout
0719: .getConstraint(SpringLayout.EAST, panel)
0720: .getPreferredValue());
0721:
0722: layout = new SpringLayout();
0723: container.setLayout(layout);
0724: layout.putConstraint(SpringLayout.SOUTH, button, Spring
0725: .constant(15), SpringLayout.NORTH, container);
0726:
0727: layout.putConstraint(SpringLayout.EAST, button, Spring
0728: .constant(5), SpringLayout.WEST, container);
0729:
0730: assertEquals(Integer.MAX_VALUE + panel.getInsets().left
0731: + panel.getInsets().right, layout
0732: .maximumLayoutSize(panel).width);
0733:
0734: assertEquals(Integer.MAX_VALUE + innerButton.getInsets().left
0735: + innerButton.getInsets().right, layout
0736: .maximumLayoutSize(innerButton).width);
0737:
0738: assertEquals(5 + button.getInsets().left
0739: + button.getInsets().right, layout
0740: .maximumLayoutSize(button).width);
0741: }
0742:
0743: public void testConstraints_Constraints() throws Exception {
0744: Constraints constraints;
0745:
0746: //SpringLayout.Constraints()
0747: constraints = new SpringLayout.Constraints();
0748: assertNull(constraints.getConstraint(SpringLayout.WEST));
0749: assertNull(constraints.getConstraint(SpringLayout.EAST));
0750: assertNull(constraints.getConstraint(SpringLayout.NORTH));
0751: assertNull(constraints.getConstraint(SpringLayout.SOUTH));
0752: assertNull(constraints.getWidth());
0753: assertNull(constraints.getHeight());
0754: assertNull(constraints.getX());
0755: assertNull(constraints.getY());
0756:
0757: //SpringLayout.Constraints(Spring x, Spring y)
0758: constraints = new SpringLayout.Constraints(Spring.constant(1),
0759: Spring.constant(2));
0760: SpringTest.assertSizes(1, 1, 1, constraints
0761: .getConstraint(SpringLayout.WEST));
0762: assertNull(constraints.getConstraint(SpringLayout.EAST));
0763: SpringTest.assertSizes(2, 2, 2, constraints
0764: .getConstraint(SpringLayout.NORTH));
0765: assertNull(constraints.getConstraint(SpringLayout.SOUTH));
0766: assertNull(constraints.getWidth());
0767: assertNull(constraints.getHeight());
0768: SpringTest.assertSizes(1, 1, 1, constraints.getX());
0769: SpringTest.assertSizes(2, 2, 2, constraints.getY());
0770:
0771: //SpringLayout.Constraints(Spring x, Spring y,
0772: // Spring width, Spring height)
0773: constraints = new SpringLayout.Constraints(Spring.constant(1),
0774: Spring.constant(2), Spring.constant(10), Spring
0775: .constant(20));
0776: SpringTest.assertSizes(1, 1, 1, 1, constraints
0777: .getConstraint(SpringLayout.WEST));
0778: SpringTest.assertSizes(11, 11, 11, 11, constraints
0779: .getConstraint(SpringLayout.EAST));
0780: SpringTest.assertSizes(2, 2, 2, 2, constraints
0781: .getConstraint(SpringLayout.NORTH));
0782: SpringTest.assertSizes(22, 22, 22, 22, constraints
0783: .getConstraint(SpringLayout.SOUTH));
0784: SpringTest.assertSizes(1, 1, 1, 1, constraints.getX());
0785: SpringTest.assertSizes(2, 2, 2, 2, constraints.getY());
0786: SpringTest.assertSizes(10, 10, 10, 10, constraints.getWidth());
0787: SpringTest.assertSizes(20, 20, 20, 20, constraints.getHeight());
0788:
0789: //SpringLayout.Constraints(Component c)
0790: setComponentSizes(button, Spring.constant(1, 2, 3), Spring
0791: .constant(11, 12, 13));
0792: constraints = new SpringLayout.Constraints(button);
0793:
0794: SpringTest.assertSizes(0, 0, 0, 0, constraints
0795: .getConstraint(SpringLayout.WEST));
0796: SpringTest.assertSizes(1, 2, 3, 2, constraints
0797: .getConstraint(SpringLayout.EAST));
0798: SpringTest.assertSizes(0, 0, 0, 0, constraints
0799: .getConstraint(SpringLayout.NORTH));
0800: SpringTest.assertSizes(11, 12, 13, 12, constraints
0801: .getConstraint(SpringLayout.SOUTH));
0802: SpringTest.assertSizes(0, 0, 0, 0, constraints.getX());
0803: SpringTest.assertSizes(0, 0, 0, 0, constraints.getY());
0804:
0805: SpringTest.assertSizes(1, 2, 3, 2, constraints.getWidth());
0806: SpringTest.assertSizes(11, 12, 13, 12, constraints.getHeight());
0807: }
0808:
0809: public void testConstraints_SetConstraint() throws Exception {
0810: Constraints constraints;
0811: layout.getConstraints(container).setConstraint(
0812: SpringLayout.WEST, Spring.constant(5));
0813:
0814: layout.getConstraints(button).setConstraint(SpringLayout.WEST,
0815: Spring.constant(6));
0816: layout.getConstraints(button).setX(Spring.constant(7));
0817:
0818: constraints = layout.getConstraints(textField);
0819: constraints.setX(Spring.constant(8));
0820: constraints.setWidth(Spring.constant(10));
0821: constraints.setConstraint(SpringLayout.EAST, Spring
0822: .constant(30));
0823:
0824: layout.getConstraints(panel).setConstraint(SpringLayout.WEST,
0825: Spring.constant(7));
0826: constraints.setWidth(Spring.constant(20));
0827: layout.getConstraints(panel).setConstraint(SpringLayout.EAST,
0828: Spring.constant(17));
0829:
0830: constraints = layout.getConstraints(innerButton);
0831: constraints.setX(Spring.constant(18));
0832: constraints.setWidth(Spring.constant(20));
0833: constraints.setConstraint(SpringLayout.EAST, Spring
0834: .constant(50));
0835:
0836: container.setSize(200, 300);
0837: layout.layoutContainer(container);
0838:
0839: assertEdges(layout.getConstraints(container), 0, 200, 0, 300);
0840:
0841: assertEdges(layout.getConstraints(button), 7, 12, 0, 5);
0842:
0843: assertEdges(layout.getConstraints(textField), 10, 30, 0, 10);
0844:
0845: if (isHarmony()) {
0846: assertEdges(layout.getConstraints(panel), 7, 17, 0, 3);
0847: } else {
0848: assertEdges(layout.getConstraints(panel), 14, 17, 0, 3);
0849: }
0850:
0851: assertEdges(layout.getConstraints(innerButton), 30, 50, 0, 10);
0852:
0853: component = new JPanel();
0854: setComponentSizes(component, Spring.constant(10), Spring
0855: .constant(5));
0856: layout.getConstraints(panel).setConstraint(SpringLayout.NORTH,
0857: Spring.constant(1));
0858: assertEdges(layout.getConstraints(component), 0, 10, 0, 5);
0859: }
0860:
0861: public void testConstraints_GetConstraint() throws Exception {
0862: Constraints constraints;
0863: layout.getConstraints(container).setConstraint(
0864: SpringLayout.WEST, Spring.constant(5));
0865:
0866: layout.getConstraints(button).setConstraint(SpringLayout.WEST,
0867: Spring.constant(6));
0868: layout.getConstraints(button).setX(Spring.constant(7));
0869:
0870: constraints = layout.getConstraints(textField);
0871: constraints.setX(Spring.constant(8));
0872: constraints.setWidth(Spring.constant(10));
0873: constraints.setConstraint(SpringLayout.EAST, Spring
0874: .constant(30));
0875:
0876: layout.getConstraints(panel).setConstraint(SpringLayout.WEST,
0877: Spring.constant(7));
0878: constraints.setWidth(Spring.constant(20));
0879: layout.getConstraints(panel).setConstraint(SpringLayout.EAST,
0880: Spring.constant(17));
0881:
0882: constraints = layout.getConstraints(innerButton);
0883: constraints.setX(Spring.constant(18));
0884: constraints.setWidth(Spring.constant(20));
0885: constraints.setConstraint(SpringLayout.EAST, Spring
0886: .constant(50));
0887:
0888: container.setSize(200, 300);
0889: layout.layoutContainer(container);
0890:
0891: constraints = layout.getConstraints(container);
0892: SpringTest.assertSizes(0, 0, 0, 0, constraints
0893: .getConstraint(SpringLayout.WEST));
0894: SpringTest.assertSizes(0, 0, Integer.MAX_VALUE, 200,
0895: constraints.getConstraint(SpringLayout.EAST));
0896:
0897: constraints = layout.getConstraints(button);
0898: Spring west = constraints.getConstraint(SpringLayout.WEST);
0899: Spring east = constraints.getConstraint(SpringLayout.EAST);
0900: SpringTest.assertSizes(7, 7, 7, 7, west);
0901: SpringTest.assertSizes(9, 12, 107, 12, east);
0902:
0903: constraints.setConstraint(SpringLayout.WEST, Spring
0904: .constant(200));
0905: constraints.setConstraint(SpringLayout.EAST, Spring
0906: .constant(100));
0907: constraints.setWidth(Spring.constant(7));
0908: layout.layoutContainer(container);
0909: SpringTest.assertSizes(7, 7, 7, 7, west);
0910: SpringTest.assertSizes(9, 12, 107, 12, east);
0911:
0912: constraints.getConstraint("wrong value");
0913:
0914: constraints = layout.getConstraints(innerButton);
0915: west = constraints.getConstraint(SpringLayout.WEST);
0916: east = constraints.getConstraint(SpringLayout.EAST);
0917: SpringTest.assertSizes(30, 30, 30, 30, west);
0918: SpringTest.assertSizes(50, 50, 50, 50, east);
0919:
0920: constraints = new SpringLayout.Constraints();
0921: constraints
0922: .setConstraint(SpringLayout.EAST, Spring.constant(5));
0923: assertNull(constraints.getConstraint(SpringLayout.WEST));
0924: SpringTest.assertSizes(5, 5, 5, constraints
0925: .getConstraint(SpringLayout.EAST));
0926: assertNull(constraints.getWidth());
0927:
0928: constraints.setY(Spring.constant(3));
0929: SpringTest.assertSizes(3, 3, 3, constraints.getY());
0930: SpringTest.assertSizes(3, 3, 3, constraints
0931: .getConstraint(SpringLayout.NORTH));
0932: assertNull(constraints.getConstraint(SpringLayout.SOUTH));
0933:
0934: constraints = new SpringLayout.Constraints();
0935: constraints.setHeight(Spring.constant(3));
0936: assertNull(constraints.getY());
0937: assertNull(constraints.getConstraint(SpringLayout.SOUTH));
0938: assertNull(constraints.getConstraint(SpringLayout.NORTH));
0939:
0940: constraints = new SpringLayout.Constraints(Spring.constant(1),
0941: Spring.constant(2));
0942: constraints.setWidth(Spring.constant(10));
0943: SpringTest.assertSizes(11, 11, 11, constraints
0944: .getConstraint(SpringLayout.EAST));
0945:
0946: }
0947:
0948: public void testConstraints_SetHeight() throws Exception {
0949: layout.getConstraints(container).setHeight(Spring.constant(5));
0950: layout.getConstraints(button).setHeight(Spring.constant(6));
0951: layout.getConstraints(button).setHeight(Spring.constant(8));
0952:
0953: Constraints constraints = layout.getConstraints(textField);
0954: constraints.setHeight(Spring.constant(8));
0955: constraints.setY(Spring.constant(10));
0956: constraints.setConstraint(SpringLayout.EAST, Spring
0957: .constant(40));
0958:
0959: layout.getConstraints(panel).setHeight(Spring.constant(6));
0960: constraints.setY(Spring.constant(20));
0961:
0962: layout.getConstraints(innerButton).setHeight(
0963: Spring.constant(15));
0964:
0965: container.setSize(200, 300);
0966: layout.layoutContainer(container);
0967:
0968: assertEquals(300, container.getHeight());
0969: assertEquals(8, button.getHeight());
0970: assertEquals(8, textField.getHeight());
0971: assertEquals(6, panel.getHeight());
0972: assertEquals(0, innerButton.getHeight());
0973: }
0974:
0975: public void testConstraints_GetHeight() throws Exception {
0976: layout.getConstraints(container).setHeight(Spring.constant(5));
0977: layout.getConstraints(button).setHeight(Spring.constant(6));
0978: layout.getConstraints(button).setHeight(Spring.constant(8));
0979:
0980: Constraints constraints = layout.getConstraints(textField);
0981: constraints.setHeight(Spring.constant(8));
0982: constraints.setY(Spring.constant(10));
0983: constraints.setConstraint(SpringLayout.EAST, Spring
0984: .constant(40));
0985:
0986: layout.getConstraints(panel).setHeight(Spring.constant(6));
0987: constraints.setY(Spring.constant(20));
0988:
0989: layout.getConstraints(innerButton).setHeight(
0990: Spring.constant(15));
0991:
0992: container.setSize(200, 300);
0993: layout.layoutContainer(container);
0994:
0995: SpringTest.assertSizes(5, 5, 5, layout
0996: .getConstraints(container).getHeight());
0997: SpringTest.assertSizes(8, 8, 8, layout.getConstraints(button)
0998: .getHeight());
0999: SpringTest.assertSizes(8, 8, 8, layout
1000: .getConstraints(textField).getHeight());
1001: SpringTest.assertSizes(6, 6, 6, layout.getConstraints(panel)
1002: .getHeight());
1003: SpringTest.assertSizes(15, 15, 15, layout.getConstraints(
1004: innerButton).getHeight());
1005:
1006: container.setSize(200, 300);
1007: layout.layoutContainer(button);
1008: SpringTest.assertSizes(8, 8, 8, layout.getConstraints(button)
1009: .getHeight());
1010:
1011: }
1012:
1013: public void testConstraints_SetWidth() throws Exception {
1014: layout.getConstraints(container).setWidth(Spring.constant(5));
1015: layout.getConstraints(button).setWidth(Spring.constant(6));
1016: layout.getConstraints(button).setWidth(Spring.constant(8));
1017:
1018: Constraints constraints = layout.getConstraints(textField);
1019: constraints.setWidth(Spring.constant(8));
1020: constraints.setX(Spring.constant(10));
1021: constraints.setConstraint(SpringLayout.EAST, Spring
1022: .constant(40));
1023:
1024: layout.getConstraints(panel).setWidth(Spring.constant(6));
1025: constraints.setX(Spring.constant(20));
1026:
1027: layout.getConstraints(innerButton)
1028: .setWidth(Spring.constant(15));
1029:
1030: container.setSize(200, 300);
1031: layout.layoutContainer(container);
1032:
1033: assertEquals(200, container.getWidth());
1034: assertEquals(8, button.getWidth());
1035: assertEquals(20, textField.getWidth());
1036: assertEquals(6, panel.getWidth());
1037: assertEquals(0, innerButton.getWidth());
1038: }
1039:
1040: public void testConstraints_GetWidth() throws Exception {
1041: layout.getConstraints(container).setWidth(Spring.constant(5));
1042: layout.getConstraints(button).setWidth(Spring.constant(6));
1043: layout.getConstraints(button).setWidth(Spring.constant(8));
1044:
1045: Constraints constraints = layout.getConstraints(textField);
1046: constraints.setWidth(Spring.constant(8));
1047: constraints.setX(Spring.constant(10));
1048: constraints.setConstraint(SpringLayout.EAST, Spring
1049: .constant(40));
1050:
1051: layout.getConstraints(panel).setWidth(Spring.constant(6));
1052: constraints.setX(Spring.constant(20));
1053:
1054: layout.getConstraints(innerButton)
1055: .setWidth(Spring.constant(15));
1056:
1057: container.setSize(200, 300);
1058: layout.layoutContainer(container);
1059:
1060: SpringTest.assertSizes(5, 5, 5, layout
1061: .getConstraints(container).getWidth());
1062: SpringTest.assertSizes(8, 8, 8, layout.getConstraints(button)
1063: .getWidth());
1064: SpringTest.assertSizes(20, 20, 20, layout.getConstraints(
1065: textField).getWidth());
1066: SpringTest.assertSizes(6, 6, 6, layout.getConstraints(panel)
1067: .getWidth());
1068: SpringTest.assertSizes(15, 15, 15, layout.getConstraints(
1069: innerButton).getWidth());
1070:
1071: container.setSize(200, 300);
1072: layout.layoutContainer(button);
1073: SpringTest.assertSizes(8, 8, 8, layout.getConstraints(button)
1074: .getWidth());
1075: }
1076:
1077: public void testConstraints_SetX() throws Exception {
1078: layout.getConstraints(container).setX(Spring.constant(5));
1079: layout.getConstraints(button).setX(Spring.constant(6));
1080: layout.getConstraints(button).setX(Spring.constant(7));
1081:
1082: Constraints constraints = layout.getConstraints(textField);
1083: constraints.setX(Spring.constant(8));
1084: constraints.setWidth(Spring.constant(10));
1085: constraints.setConstraint(SpringLayout.EAST, Spring
1086: .constant(30));
1087:
1088: layout.getConstraints(panel).setX(Spring.constant(7));
1089: constraints.setWidth(Spring.constant(20));
1090:
1091: layout.getConstraints(innerButton).setX(Spring.constant(17));
1092:
1093: container.setSize(200, 300);
1094: layout.layoutContainer(container);
1095:
1096: assertEquals(0, container.getX());
1097: assertEquals(7, button.getX());
1098: assertEquals(10, textField.getX());
1099: assertEquals(7, panel.getX());
1100: assertEquals(0, innerButton.getX());
1101: }
1102:
1103: public void testConstraints_GetX() throws Exception {
1104: layout.getConstraints(container).setX(Spring.constant(5));
1105: layout.getConstraints(button).setX(Spring.constant(6));
1106: layout.getConstraints(button).setX(Spring.constant(7));
1107:
1108: Constraints constraints = layout.getConstraints(textField);
1109: constraints.setX(Spring.constant(8));
1110: constraints.setWidth(Spring.constant(10));
1111: constraints.setConstraint(SpringLayout.EAST, Spring
1112: .constant(30));
1113:
1114: layout.getConstraints(panel).setX(Spring.constant(7));
1115: constraints.setWidth(Spring.constant(20));
1116:
1117: layout.getConstraints(innerButton).setX(Spring.constant(17));
1118:
1119: container.setSize(200, 300);
1120: layout.layoutContainer(container);
1121:
1122: SpringTest.assertSizes(0, 0, 0, layout
1123: .getConstraints(container).getX());
1124: SpringTest.assertSizes(7, 7, 7, layout.getConstraints(button)
1125: .getX());
1126: SpringTest.assertSizes(10, 10, 10, layout.getConstraints(
1127: textField).getX());
1128: SpringTest.assertSizes(7, 7, 7, layout.getConstraints(panel)
1129: .getX());
1130: SpringTest.assertSizes(17, 17, 17, layout.getConstraints(
1131: innerButton).getX());
1132:
1133: container.setSize(200, 300);
1134: layout.layoutContainer(button);
1135: SpringTest.assertSizes(0, 0, 0, layout.getConstraints(button)
1136: .getX());
1137: }
1138:
1139: public void testConstraints_SetY() throws Exception {
1140: layout.getConstraints(container).setY(Spring.constant(5));
1141: layout.getConstraints(button).setY(Spring.constant(6));
1142: layout.getConstraints(button).setY(Spring.constant(8));
1143:
1144: Constraints constraints = layout.getConstraints(textField);
1145: constraints.setY(Spring.constant(8));
1146: constraints.setHeight(Spring.constant(10));
1147: constraints.setConstraint(SpringLayout.SOUTH, Spring
1148: .constant(40));
1149:
1150: layout.getConstraints(panel).setY(Spring.constant(6));
1151: constraints.setHeight(Spring.constant(20));
1152:
1153: layout.getConstraints(innerButton).setY(Spring.constant(15));
1154:
1155: container.setSize(200, 300);
1156: layout.layoutContainer(container);
1157:
1158: assertEquals(0, container.getY());
1159: assertEquals(8, button.getY());
1160: assertEquals(20, textField.getY());
1161: assertEquals(6, panel.getY());
1162: assertEquals(0, innerButton.getY());
1163: }
1164:
1165: public void testConstraints_GetY() throws Exception {
1166: layout.getConstraints(container).setY(Spring.constant(5));
1167: layout.getConstraints(button).setY(Spring.constant(6));
1168: layout.getConstraints(button).setY(Spring.constant(8));
1169:
1170: Constraints constraints = layout.getConstraints(textField);
1171: constraints.setY(Spring.constant(8));
1172: constraints.setHeight(Spring.constant(10));
1173: constraints.setConstraint(SpringLayout.SOUTH, Spring
1174: .constant(40));
1175:
1176: layout.getConstraints(panel).setY(Spring.constant(6));
1177: constraints.setHeight(Spring.constant(20));
1178:
1179: layout.getConstraints(innerButton).setY(Spring.constant(15));
1180:
1181: container.setSize(200, 300);
1182: layout.layoutContainer(container);
1183:
1184: SpringTest.assertSizes(0, 0, 0, layout
1185: .getConstraints(container).getY());
1186: SpringTest.assertSizes(8, 8, 8, layout.getConstraints(button)
1187: .getY());
1188: SpringTest.assertSizes(20, 20, 20, layout.getConstraints(
1189: textField).getY());
1190: SpringTest.assertSizes(6, 6, 6, layout.getConstraints(panel)
1191: .getY());
1192: SpringTest.assertSizes(15, 15, 15, layout.getConstraints(
1193: innerButton).getY());
1194:
1195: container.setSize(200, 300);
1196: layout.layoutContainer(button);
1197: SpringTest.assertSizes(0, 0, 0, layout.getConstraints(button)
1198: .getY());
1199: }
1200:
1201: public static void setComponentSizes(final Component component,
1202: final Spring width, final Spring height) {
1203: component.setMinimumSize(new Dimension(width.getMinimumValue(),
1204: height.getMinimumValue()));
1205: component.setPreferredSize(new Dimension(width
1206: .getPreferredValue(), height.getPreferredValue()));
1207: component.setMaximumSize(new Dimension(width.getMaximumValue(),
1208: height.getMaximumValue()));
1209: }
1210:
1211: private void assertBounds(final int x, final int y,
1212: final int width, final int height, final Rectangle bounds) {
1213: assertLocation(x, y, bounds);
1214: assertSize(width, height, bounds);
1215: }
1216:
1217: private void assertEdges(final Constraints constraints,
1218: final int west, final int east, final int north,
1219: final int south) {
1220: assertEquals(west, constraints.getConstraint(SpringLayout.WEST)
1221: .getValue());
1222: assertEquals(east, constraints.getConstraint(SpringLayout.EAST)
1223: .getValue());
1224: assertEquals(north, constraints.getConstraint(
1225: SpringLayout.NORTH).getValue());
1226: assertEquals(south, constraints.getConstraint(
1227: SpringLayout.SOUTH).getValue());
1228: }
1229:
1230: private void assertLocation(final int x, final int y,
1231: final Rectangle bounds) {
1232: assertEquals(x, bounds.x);
1233: assertEquals(y, bounds.y);
1234: }
1235:
1236: private void assertSize(final int width, final int height,
1237: final Rectangle bounds) {
1238: assertEquals(width, bounds.width);
1239: assertEquals(height, bounds.height);
1240: }
1241: }
|