001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.border;
014:
015: import org.wings.SComponent;
016: import org.wings.SConstants;
017: import org.wings.style.CSSAttributeSet;
018: import org.wings.style.CSSProperty;
019: import org.wings.style.CSSStyleSheet;
020: import org.wings.plaf.css.BorderCG;
021:
022: import java.awt.*;
023:
024: /**
025: * This is a an abstract implementation of the <code>SBorder</code>
026: * interface.
027: *
028: * @author <a href="mailto:engels@mercatis.de">Holger Engels</a>
029: */
030: public abstract class SAbstractBorder implements SBorder {
031: protected BorderSpec[] specs = new BorderSpec[] { new BorderSpec(),
032: new BorderSpec(), null, null, new BorderSpec(),
033: new BorderSpec(), };
034:
035: protected Insets insets;
036:
037: private CSSAttributeSet attributes = new CSSAttributeSet();
038: protected SComponent component;
039: private BorderCG borderCG;
040:
041: public SAbstractBorder() {
042: this (null, -1, null);
043: }
044:
045: public SAbstractBorder(Color c, int thickness, Insets insets) {
046: setInsets(insets);
047: setColor(c);
048: setThickness(thickness);
049: setCG(new BorderCG());
050: }
051:
052: public SAbstractBorder(Insets insets) {
053: this (null, -1, insets);
054: }
055:
056: public SAbstractBorder(Color c) {
057: this (c, 1, null);
058: }
059:
060: public SAbstractBorder(int thickness) {
061: this (null, thickness, null);
062: }
063:
064: public void setComponent(SComponent newComponent) {
065: if (this .component != newComponent) {
066: if (this .component != null
067: && this .component.getBorder() == this ) {
068: final SComponent origOwner = this .component;
069: this .component = newComponent; // avoid loopback
070: origOwner.setBorder(null);
071: } else {
072: this .component = newComponent;
073: }
074: }
075: }
076:
077: /**
078: * Set the insets/padding of the border.
079: * <p>
080: * <b>NOTE:</b> Some platforms (namely: Microsoft Internet Explorer) do not support padding in
081: * rare components as they require a workaround.
082: */
083: public void setInsets(Insets insets) {
084: this .insets = insets;
085: attributes = null;
086: if (component != null)
087: component.getSession().getReloadManager().reload(component);
088: }
089:
090: /**
091: * @return the insets of the border
092: */
093: public final Insets getInsets() {
094: return insets;
095: }
096:
097: /**
098: * sets the foreground color of the border
099: */
100: public Color getColor() {
101: return getColor(SConstants.TOP);
102: }
103:
104: public Color getColor(int position) {
105: return specs[position].color;
106: }
107:
108: /**
109: * sets the foreground color of the border
110: */
111: public void setColor(Color color) {
112: setColor(color, SConstants.TOP);
113: setColor(color, SConstants.LEFT);
114: setColor(color, SConstants.RIGHT);
115: setColor(color, SConstants.BOTTOM);
116: }
117:
118: public void setColor(Color color, int position) {
119: specs[position].color = color;
120: attributes = null;
121: if (component != null)
122: component.getSession().getReloadManager().reload(component);
123: }
124:
125: /**
126: * set the thickness of the border
127: * thickness must be > 0
128: */
129: public void setThickness(int thickness) {
130: setThickness(thickness, SConstants.TOP);
131: setThickness(thickness, SConstants.LEFT);
132: setThickness(thickness, SConstants.RIGHT);
133: setThickness(thickness, SConstants.BOTTOM);
134: }
135:
136: public void setThickness(int thickness, int position) {
137: specs[position].thickness = thickness;
138: attributes = null;
139: if (component != null)
140: component.getSession().getReloadManager().reload(component);
141: }
142:
143: /**
144: * @return thickness in pixels
145: */
146: public final int getThickness() {
147: return getThickness(SConstants.TOP);
148: }
149:
150: public int getThickness(int position) {
151: return specs[position].thickness;
152: }
153:
154: /**
155: * set the style of the border
156: * style must be > 0
157: */
158: public void setStyle(String style) {
159: setStyle(style, SConstants.TOP);
160: setStyle(style, SConstants.LEFT);
161: setStyle(style, SConstants.RIGHT);
162: setStyle(style, SConstants.BOTTOM);
163: }
164:
165: public void setStyle(String style, int position) {
166: specs[position].style = style;
167: attributes = null;
168: if (component != null)
169: component.getSession().getReloadManager().reload(component);
170: }
171:
172: /**
173: * @return style in pixels
174: */
175: public final String getStyle() {
176: return getStyle(SConstants.TOP);
177: }
178:
179: public String getStyle(int position) {
180: return specs[position].style;
181: }
182:
183: protected void setCG(BorderCG borderCG) {
184: this .borderCG = borderCG;
185: }
186:
187: public BorderCG getCG() {
188: return this .borderCG;
189: }
190:
191: public CSSAttributeSet getAttributes() {
192: if (attributes == null) {
193: attributes = new CSSAttributeSet();
194: if (insets != null) {
195: if (insets.top == insets.left
196: && insets.left == insets.right
197: && insets.right == insets.bottom) {
198: attributes.put(CSSProperty.PADDING, insets.top
199: + "px");
200: } else {
201: if (insets.top > 0)
202: attributes.put(CSSProperty.PADDING_TOP,
203: insets.top + "px");
204: if (insets.left > 0)
205: attributes.put(CSSProperty.PADDING_LEFT,
206: insets.left + "px");
207: if (insets.right > 0)
208: attributes.put(CSSProperty.PADDING_RIGHT,
209: insets.right + "px");
210: if (insets.bottom > 0)
211: attributes.put(CSSProperty.PADDING_BOTTOM,
212: insets.bottom + "px");
213: }
214: }
215:
216: BorderSpec top = specs[SConstants.TOP];
217: BorderSpec left = specs[SConstants.LEFT];
218: BorderSpec right = specs[SConstants.RIGHT];
219: BorderSpec bottom = specs[SConstants.BOTTOM];
220:
221: if (this instanceof SEmptyBorder)
222: attributes.put(CSSProperty.BORDER, "none");
223: if (top.thickness == left.thickness
224: && left.thickness == right.thickness
225: && right.thickness == bottom.thickness
226: && top.style != null
227: && top.style.equals(left.style)
228: && left.style != null
229: && left.style.equals(right.style)
230: && right.style != null
231: && right.style.equals(bottom.style)
232: && ((top.color != null
233: && top.color.equals(left.color)
234: && left.color != null
235: && left.color.equals(right.color)
236: && right.color != null && right.color
237: .equals(bottom.color)) || (top.color == null
238: && right.color == null
239: && left.color == null && bottom.color == null))) {
240: attributes.put(CSSProperty.BORDER, top.thickness
241: + "px "
242: + top.style
243: + " "
244: + (top.color != null ? CSSStyleSheet
245: .getAttribute(top.color) : ""));
246: } else {
247: if (top.thickness > 0 && top.style != null)
248: attributes
249: .put(
250: CSSProperty.BORDER_TOP,
251: top.thickness
252: + "px "
253: + top.style
254: + " "
255: + (top.color != null ? CSSStyleSheet
256: .getAttribute(top.color)
257: : ""));
258:
259: if (left.thickness > 0 && left.style != null)
260: attributes
261: .put(
262: CSSProperty.BORDER_LEFT,
263: left.thickness
264: + "px "
265: + left.style
266: + " "
267: + (left.color != null ? CSSStyleSheet
268: .getAttribute(left.color)
269: : ""));
270:
271: if (right.thickness > 0 && right.style != null)
272: attributes
273: .put(
274: CSSProperty.BORDER_RIGHT,
275: right.thickness
276: + "px "
277: + right.style
278: + " "
279: + (right.color != null ? CSSStyleSheet
280: .getAttribute(right.color)
281: : ""));
282:
283: if (bottom.thickness > 0 && bottom.style != null)
284: attributes
285: .put(
286: CSSProperty.BORDER_BOTTOM,
287: bottom.thickness
288: + "px "
289: + bottom.style
290: + " "
291: + (bottom.color != null ? CSSStyleSheet
292: .getAttribute(bottom.color)
293: : ""));
294: }
295: }
296: return attributes;
297: }
298:
299: static class BorderSpec {
300: public int thickness;
301: public String style;
302: public Color color;
303: }
304:
305: public Object clone() {
306: try {
307: return super .clone();
308: } catch (CloneNotSupportedException e) {
309: throw new RuntimeException(e);
310: }
311: }
312: }
|