1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * ident "%Z%%M% %I% %E% SMI" 24 * 25 * @(#)GBConstraints.java 1.12 97/09/03 Doug Stein 26 * 27 * Copyright (c) 1996, 2001 by Sun Microsystems, Inc. 28 * All rights reserved. 29 * 30 * Permission to use, copy, modify, and distribute this software 31 * and its documentation for NON-COMMERCIAL purposes and without 32 * fee is hereby granted provided that this copyright notice 33 * appears in all copies. Please refer to the file "copyright.html" 34 * for further important copyright and licensing information. 35 * 36 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF 37 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 38 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 39 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR 40 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR 41 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. 42 */ 43 package sunsoft.jws.visual.rt.awt; 44 45 import sunsoft.jws.visual.rt.base.Global; 46 47 import java.awt.*; 48 import java.util.StringTokenizer; 49 import java.util.NoSuchElementException; 50 51 /** 52 * GBConstraints is used to specify constraints for components 53 * laid out using the GBLayout class. 54 * 55 * @see java.awt.GBLayout 56 * @version 1.12, 06/17/97 57 * @author Doug Stein 58 */ 59 public class GBConstraints implements Cloneable { 60 public static final int RELATIVE = -1; 61 public static final int REMAINDER = 0; 62 63 public static final int NONE = 0; 64 public static final int BOTH = 1; 65 public static final int HORIZONTAL = 2; 66 public static final int VERTICAL = 3; 67 68 public static final int CENTER = 10; 69 public static final int NORTH = 11; 70 public static final int NORTHEAST = 12; 71 public static final int EAST = 13; 72 public static final int SOUTHEAST = 14; 73 public static final int SOUTH = 15; 74 public static final int SOUTHWEST = 16; 75 public static final int WEST = 17; 76 public static final int NORTHWEST = 18; 77 78 public int gridx, gridy, gridwidth, gridheight; 79 public double weightx, weighty; 80 public int anchor, fill; 81 82 // Regular insets and pads will shrink when space gets tight 83 public Insets insets; 84 public int ipadx, ipady; 85 86 // Hard insets and pads never shrink 87 public Insets hardinsets; 88 public int hardipadx, hardipady; 89 90 // Normally a component will not shrink below it minimum size. Setting 91 // shrinkx or shrinky to true indicates that the component may shrink 92 // below its minimum size. 93 public boolean shrinkx; 94 public boolean shrinky; 95 96 // The following variables are filled in during layout and 97 // can be accessed, but should not be modified: 98 public Point location; // location of the component 99 public Dimension size; // size of the component 100 public Dimension minsize; // minimum size of the component 101 102 int tempX, tempY; 103 int tempWidth, tempHeight; 104 int tinyWidth, tinyHeight; 105 106 /** 107 * Creates a set of gridbag constraints. 108 */ GBConstraints()109 public GBConstraints() { 110 gridx = RELATIVE; 111 gridy = RELATIVE; 112 gridwidth = 1; 113 gridheight = 1; 114 115 weightx = 0; 116 weighty = 0; 117 anchor = CENTER; 118 fill = NONE; 119 120 insets = new Insets(0, 0, 0, 0); 121 ipadx = 0; 122 ipady = 0; 123 124 hardinsets = new Insets(0, 0, 0, 0); 125 hardipadx = 0; 126 hardipady = 0; 127 } 128 129 /** 130 * Creates a set of gridbag constraints by parsing the given 131 * constraints option string. Each option has the form key=value. 132 * Options are separated by semicolons (;). 133 */ GBConstraints(String constraints)134 public GBConstraints(String constraints) { 135 this(); 136 parseConstraints(constraints); 137 } 138 clone()139 public Object clone() { 140 GBConstraints c; 141 try { 142 c = (GBConstraints)super.clone(); 143 } catch (CloneNotSupportedException e) { 144 // this shouldn't happen, since we are Cloneable 145 throw new InternalError(); 146 } 147 148 if (c.insets != null) 149 c.insets = (Insets)c.insets.clone(); 150 if (c.hardinsets != null) 151 c.hardinsets = (Insets)c.hardinsets.clone(); 152 153 return c; 154 } 155 parseConstraints(String constraints)156 private void parseConstraints(String constraints) { 157 StringTokenizer st = new StringTokenizer( 158 constraints, /* NOI18N */";", true); 159 160 String option_string = null; 161 try { 162 while (st.hasMoreTokens()) { 163 option_string = st.nextToken(); 164 if (option_string.equals(/* NOI18N */";")) 165 continue; 166 167 StringTokenizer op = new StringTokenizer(option_string, 168 /* NOI18N */"=", true); 169 String option = op.nextToken(); 170 171 if (option.equals(/* NOI18N */"gridx") || 172 option.equals(/* NOI18N */"x")) 173 gridx = convertSymbolicValue(getValueToken(op)); 174 else if (option.equals(/* NOI18N */"gridy") || 175 option.equals(/* NOI18N */"y")) 176 gridy = convertSymbolicValue(getValueToken(op)); 177 else if (option.equals(/* NOI18N */"gridwidth") || 178 option.equals(/* NOI18N */"width")) 179 gridwidth = convertSymbolicValue(getValueToken( 180 op)); 181 else if (option.equals(/* NOI18N */"gridheight") || 182 option.equals(/* NOI18N */"height")) 183 gridheight = convertSymbolicValue(getValueToken(op)); 184 185 else if (option.equals(/* NOI18N */"weightx")) { 186 Double x = new Double(getValueToken(op)); 187 weightx = x.doubleValue(); 188 } else if (option.equals(/* NOI18N */"weighty")) { 189 Double x = new Double(getValueToken(op)); 190 weighty = x.doubleValue(); 191 } else if (option.equals(/* NOI18N */"anchor")) 192 anchor = convertSymbolicValue(getValueToken(op)); 193 else if (option.equals(/* NOI18N */"fill")) 194 fill = convertSymbolicValue(getValueToken(op)); 195 196 else if (option.equals(/* NOI18N */"insets.top")) 197 insets.top = convertSymbolicValue(getValueToken(op)); 198 else if (option.equals(/* NOI18N */"insets.left")) 199 insets.left = convertSymbolicValue(getValueToken(op)); 200 else if (option.equals(/* NOI18N */"insets.bottom")) 201 insets.bottom = convertSymbolicValue( 202 getValueToken(op)); 203 else if (option.equals(/* NOI18N */"insets.right")) 204 insets.right = convertSymbolicValue(getValueToken(op)); 205 206 else if (option.equals(/* NOI18N */"ipadx")) 207 ipadx = convertSymbolicValue(getValueToken(op)); 208 else if (option.equals(/* NOI18N */"ipady")) 209 ipady = convertSymbolicValue(getValueToken(op)); 210 211 else if (option.equals(/* NOI18N */"shrinkx")) { 212 Boolean x = new Boolean(getValueToken(op)); 213 shrinkx = x.booleanValue(); 214 } else if (option.equals(/* NOI18N */"shrinky")) { 215 Boolean x = new Boolean(getValueToken(op)); 216 shrinky = x.booleanValue(); 217 } 218 219 else if (option.equals(/* NOI18N */"hardinsets.top")) 220 hardinsets.top = convertSymbolicValue( 221 getValueToken(op)); 222 else if (option.equals(/* NOI18N */"hardinsets.left")) 223 hardinsets.left = convertSymbolicValue( 224 getValueToken(op)); 225 else if (option.equals(/* NOI18N */"hardinsets.bottom")) 226 hardinsets.bottom = convertSymbolicValue( 227 getValueToken(op)); 228 else if (option.equals(/* NOI18N */"hardinsets.right")) 229 hardinsets.right = convertSymbolicValue( 230 getValueToken(op)); 231 232 else if (option.equals(/* NOI18N */"hardipadx")) 233 hardipadx = convertSymbolicValue( 234 getValueToken(op)); 235 else if (option.equals(/* NOI18N */"hardipady")) 236 hardipady = convertSymbolicValue( 237 getValueToken(op)); 238 239 else 240 throw new NoSuchElementException(); 241 } 242 } 243 catch (Exception e) { 244 /* JSTYLED */ 245 throw new Error(Global.getMsg("sunsoft.jws.visual.rt.awt.GBConstraints.-ba-r-ba-n-ba-tSyntax__error__i.3") + 246 /* NOI18N */"\t\t" + constraints + /* NOI18N */": " 247 + option_string); 248 } 249 } 250 getValueToken(StringTokenizer op)251 private String getValueToken(StringTokenizer op) 252 throws NoSuchElementException 253 { 254 if (op.hasMoreTokens()) { 255 String assign = op.nextToken(); 256 if (assign.equals(/* NOI18N */"=")) 257 if (op.hasMoreTokens()) 258 return op.nextToken(); 259 } 260 throw new NoSuchElementException(); 261 } 262 convertSymbolicValue(String value)263 private int convertSymbolicValue(String value) { 264 if (value.equals(/* NOI18N */"relative")) 265 return GBConstraints.RELATIVE; 266 else if (value.equals(/* NOI18N */"remainder")) 267 return GBConstraints.REMAINDER; 268 else if (value.equals(/* NOI18N */"none")) 269 return GBConstraints.NONE; 270 else if (value.equals(/* NOI18N */"both")) 271 return GBConstraints.BOTH; 272 else if (value.equals(/* NOI18N */"horizontal")) 273 return GBConstraints.HORIZONTAL; 274 else if (value.equals(/* NOI18N */"vertical")) 275 return GBConstraints.VERTICAL; 276 else if (value.equals(/* NOI18N */"center")) 277 return GBConstraints.CENTER; 278 else if (value.equals(/* NOI18N */"north")) 279 return GBConstraints.NORTH; 280 else if (value.equals(/* NOI18N */"northeast")) 281 return GBConstraints.NORTHEAST; 282 else if (value.equals(/* NOI18N */"east")) 283 return GBConstraints.EAST; 284 else if (value.equals(/* NOI18N */"southeast")) 285 return GBConstraints.SOUTHEAST; 286 else if (value.equals(/* NOI18N */"south")) 287 return GBConstraints.SOUTH; 288 else if (value.equals(/* NOI18N */"southwest")) 289 return GBConstraints.SOUTHWEST; 290 else if (value.equals(/* NOI18N */"west")) 291 return GBConstraints.WEST; 292 else if (value.equals(/* NOI18N */"northwest")) 293 return GBConstraints.NORTHWEST; 294 295 Integer int_val = new Integer(value); 296 return int_val.intValue(); 297 } 298 } 299