001: package net.xoetrope.xui.style;
002:
003: import java.awt.Color;
004: import java.lang.Cloneable;
005:
006: /**
007: * A style for storing information about the look of a component.
008: * Information such as font (face, size, bold, italic), color (fore, back).
009: * <p>Copyright (c) Xoetrope Ltd., 1998-2003<br>
010: * License: see license.txt
011: * $Revision: 1.4 $
012: */
013: public class XStyle implements Cloneable {
014: // Text and component styles
015: public static final int FONT_FACE = 0;
016: public static final int FONT_SIZE = 1;
017: public static final int FONT_WEIGHT = 2;
018: public static final int FONT_ITALIC = 3;
019: public static final int COLOR_BACK = 4;
020: public static final int COLOR_FORE = 5;
021: private static final int NUM_DEFAULT_STYLES = 6;
022:
023: Object values[] = new Object[NUM_DEFAULT_STYLES];
024:
025: private static final Integer zero = new Integer(0);
026: private Integer fontHashCode = zero;
027:
028: /**
029: * Create a new style object
030: */
031: public XStyle() {
032: }
033:
034: /**
035: * Set the number of styles. The current data is copied to the new style array.
036: * The number of styles can only be increased with this method so as to
037: * prevent any loss of data.
038: * @param num the number of new parameters
039: */
040: public void setNumStyles(int num) {
041: // Do not permit shrinking of the style array
042: if (num <= values.length)
043: return;
044:
045: Object temp[] = new Object[NUM_DEFAULT_STYLES];
046: System.arraycopy(values, 0, temp, 0, values.length);
047: values = temp;
048: }
049:
050: /**
051: * Get the number of styles managed by this style object
052: * @return
053: */
054: public int getNumStyles() {
055: return values.length;
056: }
057:
058: /**
059: * Create a copy of this component
060: * @return the copy
061: */
062: public Object clone() {
063: XStyle copy = new XStyle();
064: copy.setNumStyles(values.length);
065: for (int i = 0; i < values.length; i++)
066: copy.values[i] = values[i];
067:
068: return copy;
069: }
070:
071: /**
072: * Set a style element.
073: * @param key The index of the element
074: * @param value The Object value of the element
075: */
076: public void setStyle(int key, Object value) {
077: if (value instanceof String) {
078: String temp = value.toString();
079: switch (key) {
080: case COLOR_FORE:
081: case COLOR_BACK:
082: try {
083: int r = Integer.parseInt(temp.substring(0, 2), 16);
084: int g = Integer.parseInt(temp.substring(2, 4), 16);
085: int b = Integer.parseInt(temp.substring(4, 6), 16);
086: values[key] = new Color(r, g, b);
087: } catch (Exception e) {
088: values[key] = new Integer(temp);
089: }
090: break;
091:
092: case FONT_SIZE:
093: case FONT_WEIGHT:
094: case FONT_ITALIC:
095: values[key] = new Integer(temp);
096: break;
097:
098: default:
099: values[key] = value;
100: break;
101: }
102: } else
103: values[key] = value;
104: }
105:
106: /**
107: * Set a style element.
108: * @param key The index of the element
109: * @param value The int value of the element
110: */
111: public void setStyle(int key, int value) {
112: values[key % XStyle.NUM_DEFAULT_STYLES] = new Integer(value);
113: }
114:
115: /**
116: * Retrieve a style element as an int.
117: * @param key The index of the element
118: */
119: public int getStyleAsInt(int key) {
120: Object o = values[key % XStyle.NUM_DEFAULT_STYLES];
121: if (o instanceof Integer)
122: return ((Integer) o).intValue();
123:
124: return 0;
125: }
126:
127: /**
128: * Retrieve a style element as a String.
129: * @param key The index of the element
130: */
131: public String getStyleAsString(int key) {
132: Object o = values[key % XStyle.NUM_DEFAULT_STYLES];
133: if (o == null)
134: return null;
135:
136: return (o).toString();
137: }
138:
139: /**
140: * Retrieve a style element as a Color.
141: * @param key The index of the element
142: */
143: public Color getStyleAsColor(int key) {
144: Object o = values[key];
145: if (o instanceof Color)
146: return (Color) o;
147:
148: return null;
149: }
150:
151: /**
152: * For applying a hierarchy of styles to a new XStyle.
153: * Only apply the style if it's found.
154: * @param newStyle the style to merge with the current object.
155: */
156: public void mergeStyle(XStyle newStyle) {
157: if (newStyle == null)
158: return;
159:
160: for (int i = 0; i < newStyle.values.length; i++) {
161: if (newStyle.values[i] != null)
162: values[i] = newStyle.values[i];
163: }
164: }
165:
166: /**
167: * Gets the font hashcode
168: * @return the hashcode
169: */
170: Integer getFontHashcode() {
171: return fontHashCode;
172: }
173:
174: /**
175: * Sets the hashcode for the font
176: * @param h the new hashcode for the font
177: */
178: void setFontHashcode(int h) {
179: fontHashCode = new Integer(h);
180: }
181: }
|