1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4726194 7124209 27 * @summary Tests for 4726194 28 * @author Phil Milne 29 */ 30 import java.awt.*; 31 import java.util.*; 32 import java.util.List; 33 34 import javax.swing.*; 35 36 public class Test4 { 37 private static String[] hConstraints = {SpringLayout.WEST, "Width", SpringLayout.EAST, SpringLayout.HORIZONTAL_CENTER}; 38 private static String[] vConstraints = {SpringLayout.NORTH, "Height", SpringLayout.SOUTH, SpringLayout.VERTICAL_CENTER, SpringLayout.BASELINE}; 39 private static int[] FAIL = new int[3]; 40 41 private static boolean TEST_DUPLICATES = false; 42 43 public static void main(String[] args) { 44 int minLevel = 2; 45 int maxLevel = 2; 46 for (int i = minLevel; i <= maxLevel; i++) { 47 test(i, true); 48 test(i, false); 49 } 50 } 51 52 public static void test(int level, boolean horizontal) { 53 List result = new ArrayList(); 54 String[] constraints = horizontal ? hConstraints : vConstraints; 55 test(level, constraints, result, Arrays.asList(new Object[level])); 56 // System.out.println("result = " + result); 57 JTextField tf = new JTextField(""); 58 tf.setFont(new Font("Dialog", Font.PLAIN, 6)); 59 System.out.print("\t\t"); 60 for (int j = 0; j < constraints.length; j++) { 61 String constraint = constraints[j]; 62 System.out.print(constraint + " ".substring(constraint.length())); 63 } 64 System.out.println(""); 65 for (int i = 0; i < result.size(); i++) { 66 SpringLayout.Constraints c = new SpringLayout.Constraints(tf); 67 // SpringLayout.Constraints c = new SpringLayout.Constraints(); 68 // c.setConstraint(SpringLayout.RELATIVE_BASELINE, Spring.constant(30)); 69 List cc = (List) result.get(i); 70 for (int j = 0; j < cc.size(); j++) { 71 String constraint = (String) cc.get(j); 72 c.setConstraint(constraint, Spring.constant((j + 1) * 10)); 73 } 74 System.out.print(" Input:\t\t"); 75 for (int j = 0; j < constraints.length; j++) { 76 String constraint = constraints[j]; 77 int jj = cc.indexOf(constraint); 78 String val = cc.contains(constraint) ? Integer.toString((jj + 1) * 10) : "?"; 79 System.out.print(val + "\t\t"); 80 } 81 System.out.println(""); 82 System.out.print("Output:\t\t"); 83 // System.out.print(" :: "); 84 for (int j = 0; j < constraints.length; j++) { 85 String constraint = constraints[j]; 86 Spring spring = c.getConstraint(constraint); 87 String springVal = (spring == null) ? "?" : Integer.toString(spring.getValue()); 88 System.out.print(springVal); 89 // if (j != constraints.length-1) { 90 System.out.print("\t\t"); 91 // } 92 } 93 for (int j = 0; j < cc.size(); j++) { 94 String constraint = (String) cc.get(j); 95 Spring con = c.getConstraint(constraint); 96 if (con == null || con.getValue() != (j + 1) * 10) { 97 throw new RuntimeException("Values are wrong!!! "); 98 } 99 } 100 if (horizontal) { 101 int[] a1 = getValues(c, new String[]{SpringLayout.WEST, SpringLayout.WIDTH, SpringLayout.EAST}); 102 if (a1[0] + a1[1] != a1[2]) { 103 throw new RuntimeException("WEST + WIDTH != EAST!!! "); 104 } 105 int[] a2 = getValues(c, new String[]{SpringLayout.WEST, SpringLayout.WIDTH, SpringLayout.HORIZONTAL_CENTER}); 106 if (a2[0] + a2[1] / 2 != a2[2]) { 107 throw new RuntimeException("WEST + WIDTH/2 != HORIZONTAL_CENTER!!! "); 108 } 109 } else { 110 int[] a3 = getValues(c, new String[]{SpringLayout.NORTH, SpringLayout.HEIGHT, SpringLayout.SOUTH}); 111 if (a3[0] + a3[1] != a3[2]) { 112 throw new RuntimeException("NORTH + HEIGHT != SOUTH!!! "); 113 } 114 int[] a4 = getValues(c, new String[]{SpringLayout.NORTH, SpringLayout.HEIGHT, SpringLayout.VERTICAL_CENTER}); 115 int vcDiff = Math.abs(a4[0] + a4[1] / 2 - a4[2]); 116 if (vcDiff > 1) { 117 throw new RuntimeException("NORTH + HEIGHT/2 != VERTICAL_CENTER!!! "); 118 } 119 int[] a5 = getValues(c, new String[]{SpringLayout.NORTH, SpringLayout.BASELINE, SpringLayout.SOUTH}); 120 if (a5[0] > a5[1] != a5[1] > a5[2]) { 121 throw new RuntimeException("BASELINE is not in the range: [NORTH, SOUTH]!!!"); 122 // System.out.print("BASELINE is not in the range: [NORTH, SOUTH]!!!"); 123 } 124 } 125 System.out.println(""); 126 } 127 System.out.println(""); 128 } 129 130 private static int[] getValues(SpringLayout.Constraints con, String[] cNames) { 131 int[] result = new int[cNames.length]; 132 for (int i = 0; i < cNames.length; i++) { 133 String name = cNames[i]; 134 Spring s = con.getConstraint(name); 135 if (s == null) { 136 System.out.print("Warning: " + name + " is undefined. "); 137 return FAIL; 138 } 139 result[i] = s.getValue(); 140 } 141 return result; 142 } 143 144 public static void test(int level, String[] constraints, List result, List soFar) { 145 if (level == 0) { 146 result.add(soFar); 147 return; 148 } 149 for (int i = 0; i < constraints.length; i++) { 150 if (soFar.contains(constraints[i]) && !TEST_DUPLICATES) { 151 continue; 152 } 153 List child = new ArrayList(soFar); 154 child.set(level - 1, constraints[i]); 155 test(level - 1, constraints, result, child); 156 } 157 } 158 }