001: /*
002: #IFNDEF ALT_LICENSE
003: ThinWire(R) RIA Ajax Framework
004: Copyright (C) 2003-2007 Custom Credit Systems
005:
006: This library is free software; you can redistribute it and/or modify it under
007: the terms of the GNU Lesser General Public License as published by the Free
008: Software Foundation; either version 2.1 of the License, or (at your option) any
009: later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY
012: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
013: PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
014:
015: You should have received a copy of the GNU Lesser General Public License along
016: with this library; if not, write to the Free Software Foundation, Inc., 59
017: Temple Place, Suite 330, Boston, MA 02111-1307 USA
018:
019: Users who would rather have a commercial license, warranty or support should
020: contact the following company who invented, built and supports the technology:
021:
022: Custom Credit Systems, Richardson, TX 75081, USA.
023: email: info@thinwire.com ph: +1 (888) 644-6405
024: http://www.thinwire.com
025: #ENDIF
026: [ v1.2_RC2 ]
027: */
028: package thinwire.ui.style;
029:
030: import java.util.*;
031: import java.util.regex.Matcher;
032: import java.util.regex.Pattern;
033:
034: import thinwire.util.ImageInfo;
035:
036: /**
037: * @author Joshua J. Gertzen
038: */
039: public class Background {
040: public static final String PROPERTY_BACKGROUND_COLOR = "backgroundColor";
041: public static final String PROPERTY_BACKGROUND_IMAGE = "backgroundImage";
042: public static final String PROPERTY_BACKGROUND_REPEAT = "backgroundRepeat";
043: public static final String PROPERTY_BACKGROUND_POSITION = "backgroundPosition";
044:
045: public enum Repeat {
046: NONE, BOTH, X, Y;
047:
048: public String toString() {
049: return name().toLowerCase();
050: }
051: }
052:
053: public static class Position {
054: private static List<Position> VALUES = new ArrayList<Position>();
055:
056: public static final Position LEFT_TOP = new Position(
057: "left top", 0, 0);
058: public static final Position LEFT_CENTER = new Position(
059: "left center", 0, 50);
060: public static final Position LEFT_BOTTOM = new Position(
061: "left bottom", 0, 100);
062: public static final Position CENTER_TOP = new Position(
063: "center top", 50, 0);
064: public static final Position CENTER = new Position("center",
065: 50, 50);
066: public static final Position CENTER_BOTTOM = new Position(
067: "center bottom", 50, 100);
068: public static final Position RIGHT_TOP = new Position(
069: "right top", 100, 0);
070: public static final Position RIGHT_CENTER = new Position(
071: "right center", 100, 50);
072: public static final Position RIGHT_BOTTOM = new Position(
073: "right bottom", 100, 100);
074: static {
075: Collections.sort(VALUES, new Comparator<Position>() {
076: public int compare(Position c1, Position c2) {
077: int o1 = c1.ordinal();
078: int o2 = c2.ordinal();
079:
080: if (o1 == o2) {
081: return 0;
082: } else if (o1 > o2) {
083: return 1;
084: } else {
085: return -1;
086: }
087: }
088: });
089: }
090:
091: private static final Pattern REGEX_PERCENT = Pattern
092: .compile("(\\d{1,3})[%]\\s+(\\d{1,3})[%]");
093: private static int nextOrdinal = 0;
094:
095: public static final Position valueOf(String positionId) {
096: if (positionId == null)
097: throw new IllegalArgumentException("positionId == null");
098: positionId = positionId.trim();
099: if (positionId.equals(""))
100: throw new IllegalArgumentException(
101: "positionId.equals(\"\")");
102: Matcher m = REGEX_PERCENT.matcher(positionId);
103:
104: if (m.find()) {
105: int x = Integer.parseInt(m.group(1));
106: int y = Integer.parseInt(m.group(2));
107:
108: if (x < 0 || x > 100 || y < 0 || y > 100)
109: throw new IllegalArgumentException(
110: "x < 0 || x > 100 || y < 0 || y > 100 : "
111: + positionId);
112:
113: for (Position p : VALUES) {
114: if (p.x == x && p.y == y)
115: return p;
116: }
117:
118: return new Position(null, x, y);
119: } else {
120: positionId = positionId.toUpperCase().replace(' ', '_');
121:
122: for (Position p : VALUES) {
123: if (p.name.equals(positionId)) {
124: return p;
125: }
126: }
127:
128: throw new IllegalArgumentException("positionId '"
129: + positionId
130: + "' is not a valid position format name");
131: }
132: }
133:
134: public static final Position[] values() {
135: return VALUES.toArray(new Position[VALUES.size()]);
136: }
137:
138: private int x;
139: private int y;
140: private int ordinal;
141: private String name;
142: private String stringName;
143:
144: private Position(String name, int x, int y) {
145: this .x = x;
146: this .y = y;
147:
148: if (name == null) {
149: this .ordinal = -1;
150: this .stringName = x + "% " + y + "%";
151: this .name = "";
152: } else {
153: this .ordinal = nextOrdinal++;
154: this .stringName = name;
155: this .name = name.toUpperCase().replaceAll("[- ]", "_");
156: VALUES.add(this );
157: }
158: }
159:
160: public int getX() {
161: return x;
162: }
163:
164: public int getY() {
165: return y;
166: }
167:
168: public String name() {
169: return name;
170: }
171:
172: public int ordinal() {
173: return ordinal;
174: }
175:
176: public int hashCode() {
177: return toString().hashCode();
178: }
179:
180: public boolean equals(Object o) {
181: if (o == null || !(o instanceof Position)) {
182: return false;
183: } else {
184: Position p = (Position) o;
185: return this .x == p.x && this .y == p.y;
186: }
187: }
188:
189: public String toString() {
190: return stringName;
191: }
192: }
193:
194: private Style parent;
195: private Color color;
196: private ImageInfo imageInfo = new ImageInfo(null);
197: private Repeat repeat;
198: private Position position;
199: private String stringValue;
200:
201: Background(Style parent) {
202: this .parent = parent;
203: if (parent.defaultStyle != null)
204: copy(parent.defaultStyle.getBackground());
205: }
206:
207: private void clearStringValue() {
208: this .stringValue = null;
209: if (parent != null)
210: parent.stringValue = null;
211: }
212:
213: public String toString() {
214: if (stringValue == null)
215: stringValue = "Background{color:" + getColor() + ",image:"
216: + getImage() + ",position:" + getPosition()
217: + ",repeat:" + getRepeat() + "}";
218: return stringValue;
219: }
220:
221: public int hashCode() {
222: return toString().hashCode();
223: }
224:
225: public boolean equals(Object o) {
226: if (!(o instanceof Background))
227: return false;
228: if (this == o)
229: return true;
230: return this .toString().equals(o.toString());
231: }
232:
233: public void copy(Background background) {
234: copy(background, false);
235: }
236:
237: public void copy(Background background, boolean onlyIfDefault) {
238: if (background == null)
239: throw new IllegalArgumentException("background == null");
240:
241: if (onlyIfDefault) {
242: Background db = parent.defaultStyle.getBackground();
243: if (getColor().equals(db.getColor()))
244: setColor(background.getColor());
245: if (getImage().equals(db.getImage()))
246: setImage(background.getImage());
247: if (getRepeat().equals(db.getRepeat()))
248: setRepeat(background.getRepeat());
249: if (getPosition().equals(db.getPosition()))
250: setPosition(background.getPosition());
251: } else {
252: setColor(background.getColor());
253: setImage(background.getImage());
254: setRepeat(background.getRepeat());
255: setPosition(background.getPosition());
256: }
257: }
258:
259: public void setProperty(String name, Object value) {
260: if (name.equals(Background.PROPERTY_BACKGROUND_COLOR)) {
261: setColor((Color) value);
262: } else if (name.equals(Background.PROPERTY_BACKGROUND_IMAGE)) {
263: setImage((String) value);
264: } else if (name.equals(Background.PROPERTY_BACKGROUND_POSITION)) {
265: setPosition((Background.Position) value);
266: } else if (name.equals(Background.PROPERTY_BACKGROUND_REPEAT)) {
267: setRepeat((Background.Repeat) value);
268: } else {
269: throw new IllegalArgumentException(
270: "unknown style property '" + name + "'");
271: }
272: }
273:
274: public Object getProperty(String name) {
275: Object ret;
276:
277: if (name.equals(Background.PROPERTY_BACKGROUND_COLOR)) {
278: ret = getColor();
279: } else if (name.equals(Background.PROPERTY_BACKGROUND_IMAGE)) {
280: ret = getImage();
281: } else if (name.equals(Background.PROPERTY_BACKGROUND_POSITION)) {
282: ret = getPosition();
283: } else if (name.equals(Background.PROPERTY_BACKGROUND_REPEAT)) {
284: ret = getRepeat();
285: } else {
286: throw new IllegalArgumentException(
287: "unknown style property '" + name + "'");
288: }
289:
290: return ret;
291: }
292:
293: public Style getParent() {
294: return parent;
295: }
296:
297: public Color getColor() {
298: if (color == null)
299: throw new IllegalStateException("color == null");
300: return color;
301: }
302:
303: public void setColor(Color color) {
304: if (color == null && parent.defaultStyle != null)
305: color = parent.defaultStyle.getBackground().getColor();
306: if (color == null)
307: throw new IllegalArgumentException(
308: "color == null && defaultStyle.getBackground().getColor() == null");
309: Color oldColor = this .color;
310: this .clearStringValue();
311: this .color = color;
312: if (parent != null)
313: parent.firePropertyChange(this , PROPERTY_BACKGROUND_COLOR,
314: oldColor, this .color);
315: }
316:
317: public String getImage() {
318: return imageInfo.getName();
319: }
320:
321: public void setImage(String image) {
322: if (image == null && parent.defaultStyle != null)
323: image = parent.defaultStyle.getBackground().getImage();
324: if (image == null)
325: throw new IllegalArgumentException(
326: "image == null && defaultStyle.getBackground().getImage() == null");
327: String oldImage = imageInfo.getName();
328: this .clearStringValue();
329: imageInfo = new ImageInfo(image);
330: if (parent != null)
331: parent.firePropertyChange(this , PROPERTY_BACKGROUND_IMAGE,
332: oldImage, imageInfo.getName());
333: }
334:
335: public ImageInfo getImageInfo() {
336: return imageInfo;
337: }
338:
339: public void setRepeat(Repeat repeat) {
340: if (repeat == null && parent.defaultStyle != null)
341: repeat = parent.defaultStyle.getBackground().getRepeat();
342: if (repeat == null)
343: throw new IllegalArgumentException(
344: "repeat == null && defaultStyle.getBackground().getRepeat() == null");
345: Repeat oldRepeat = this .repeat;
346: this .clearStringValue();
347: this .repeat = repeat;
348: if (parent != null)
349: parent.firePropertyChange(this , PROPERTY_BACKGROUND_REPEAT,
350: oldRepeat, repeat);
351: }
352:
353: public Repeat getRepeat() {
354: if (repeat == null)
355: throw new IllegalStateException("repeat not initialized");
356: return repeat;
357: }
358:
359: public void setPosition(Position position) {
360: if (position == null && parent.defaultStyle != null)
361: position = parent.defaultStyle.getBackground()
362: .getPosition();
363: if (position == null)
364: throw new IllegalArgumentException(
365: "position == null && defaultStyle.getBackground().getPosition() == null");
366: Position oldPosition = this .position;
367: this .clearStringValue();
368: this .position = position;
369: if (parent != null)
370: parent
371: .firePropertyChange(this ,
372: PROPERTY_BACKGROUND_POSITION, oldPosition,
373: position);
374: }
375:
376: public Position getPosition() {
377: if (position == null)
378: throw new IllegalStateException("position not initialized");
379: return position;
380: }
381: }
|