001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.layout; 017 018 import java.awt.Component; 019 import java.awt.GridBagConstraints; 020 import java.awt.GridBagLayout; 021 022 import javax.swing.JLabel; 023 import javax.swing.JPanel; 024 025 import junit.framework.TestCase; 026 027 public class GridBagLayoutBuilderTests extends TestCase { 028 // static { 029 // Logger.getLogger(GridBagLayoutBuilder.class).setLevel(Level.DEBUG); 030 // } 031 032 public void testAppend1() throws Exception { 033 GridBagLayoutBuilder builder = new GridBagLayoutBuilder(); 034 builder.append(new JPanel()); 035 builder.append(new JPanel(), 2, 2); 036 builder.append(new JPanel()); 037 builder.nextLine(); 038 builder.append(new JPanel()).append(new JPanel()).nextLine(); 039 builder.append(new JPanel()).append(new JPanel()).append(new JPanel()).append(new JPanel()).nextLine(); 040 041 JPanel panel = builder.getPanel(); 042 final Component[] comps = panel.getComponents(); 043 final GridBagLayout layout = ((GridBagLayout)panel.getLayout()); 044 045 check(layout.getConstraints(comps[0]), 0, 0, 1, 1, false, false); 046 check(layout.getConstraints(comps[1]), 1, 0, 2, 2, false, false); 047 check(layout.getConstraints(comps[2]), 3, 0, 1, 1, false, false); 048 check(layout.getConstraints(comps[3]), 0, 1, 1, 1, false, false); 049 check(layout.getConstraints(comps[4]), 3, 1, 1, 1, false, false); 050 check(layout.getConstraints(comps[5]), 0, 2, 1, 1, false, false); 051 check(layout.getConstraints(comps[6]), 1, 2, 1, 1, false, false); 052 check(layout.getConstraints(comps[7]), 2, 2, 1, 1, false, false); 053 check(layout.getConstraints(comps[8]), 3, 2, 1, 1, false, false); 054 } 055 056 public void testAppend2() throws Exception { 057 GridBagLayoutBuilder builder = new GridBagLayoutBuilder(); 058 builder.append(new JPanel()).append(new JPanel(), 2, 1).append(new JPanel()).nextLine(); 059 builder.append(new JPanel()).append(new JPanel()).append(new JPanel()).append(new JPanel()).nextLine(); 060 061 JPanel panel = builder.getPanel(); 062 final Component[] comps = panel.getComponents(); 063 final GridBagLayout layout = ((GridBagLayout)panel.getLayout()); 064 065 check(layout.getConstraints(comps[0]), 0, 0, 1, 1, false, false); 066 check(layout.getConstraints(comps[1]), 1, 0, 2, 1, false, false); 067 check(layout.getConstraints(comps[2]), 3, 0, 1, 1, false, false); 068 check(layout.getConstraints(comps[3]), 0, 1, 1, 1, false, false); 069 check(layout.getConstraints(comps[4]), 1, 1, 1, 1, false, false); 070 check(layout.getConstraints(comps[5]), 2, 1, 1, 1, false, false); 071 check(layout.getConstraints(comps[6]), 3, 1, 1, 1, false, false); 072 } 073 074 private void check(GridBagConstraints gbc, final int x, final int y, final int width, final int height, 075 boolean expandX, boolean expandY) { 076 assertEquals(x, gbc.gridx); 077 assertEquals(y, gbc.gridy); 078 assertEquals(width, gbc.gridwidth); 079 assertEquals(height, gbc.gridheight); 080 assertEquals(expandX, gbc.weightx > 0.0); 081 assertEquals(expandY, gbc.weighty > 0.0); 082 } 083 084 public void testAppendLabeledField() throws Exception { 085 GridBagLayoutBuilder builder = new GridBagLayoutBuilder(); 086 087 builder.appendLabeledField(new JLabel("0"), new JLabel("3"), LabelOrientation.TOP, 1, 2, true, true); 088 builder.appendLabeledField(new JLabel("1"), new JLabel("2"), LabelOrientation.LEFT, 2, 1, true, false); 089 builder.nextLine(); 090 091 builder.appendLabeledField(new JLabel("5"), new JLabel("4"), LabelOrientation.BOTTOM, 3, 1, true, false); 092 builder.nextLine(); 093 builder.nextLine(); 094 095 builder.appendLabeledField(new JLabel("7"), new JLabel("6"), LabelOrientation.RIGHT, 3, 1, true, false); 096 builder.nextLine(); 097 098 JPanel panel = builder.getPanel(); 099 final Component[] comps = panel.getComponents(); 100 final GridBagLayout layout = ((GridBagLayout)panel.getLayout()); 101 102 check(layout.getConstraints(comps[0]), 0, 0, 1, 1, true, false); 103 check(layout.getConstraints(comps[1]), 1, 0, 1, 1, false, false); 104 check(layout.getConstraints(comps[2]), 2, 0, 2, 1, true, false); 105 check(layout.getConstraints(comps[3]), 0, 1, 1, 2, true, true); 106 check(layout.getConstraints(comps[4]), 1, 1, 3, 1, true, false); 107 check(layout.getConstraints(comps[5]), 1, 2, 3, 1, true, false); 108 check(layout.getConstraints(comps[6]), 0, 3, 3, 1, true, false); 109 check(layout.getConstraints(comps[7]), 3, 3, 1, 1, false, false); 110 } 111 112 }