1:
46:
47: package ;
48:
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60:
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66:
67:
72: public class TextBox implements Serializable {
73:
74:
75: private static final long serialVersionUID = 3360220213180203706L;
76:
77:
78: private transient Paint outlinePaint;
79:
80:
81: private transient Stroke outlineStroke;
82:
83:
84: private RectangleInsets interiorGap;
85:
86:
87: private transient Paint backgroundPaint;
88:
89:
90: private transient Paint shadowPaint;
91:
92:
93: private double shadowXOffset = 2.0;
94:
95:
96: private double shadowYOffset = 2.0;
97:
98:
99: private TextBlock textBlock;
100:
101:
104: public TextBox() {
105: this((TextBlock) null);
106: }
107:
108:
113: public TextBox(final String text) {
114: this((TextBlock) null);
115: if (text != null) {
116: this.textBlock = new TextBlock();
117: this.textBlock.addLine(
118: text, new Font("SansSerif", Font.PLAIN, 10),
119: Color.black
120: );
121: }
122: }
123:
124:
129: public TextBox(final TextBlock block) {
130: this.outlinePaint = Color.black;
131: this.outlineStroke = new BasicStroke(1.0f);
132: this.interiorGap = new RectangleInsets(1.0, 3.0, 1.0, 3.0);
133: this.backgroundPaint = new Color(255, 255, 192);
134: this.shadowPaint = Color.gray;
135: this.shadowXOffset = 2.0;
136: this.shadowYOffset = 2.0;
137: this.textBlock = block;
138: }
139:
140:
145: public Paint getOutlinePaint() {
146: return this.outlinePaint;
147: }
148:
149:
154: public void setOutlinePaint(final Paint paint) {
155: this.outlinePaint = paint;
156: }
157:
158:
163: public Stroke getOutlineStroke() {
164: return this.outlineStroke;
165: }
166:
167:
172: public void setOutlineStroke(final Stroke stroke) {
173: this.outlineStroke = stroke;
174: }
175:
176:
181: public RectangleInsets getInteriorGap() {
182: return this.interiorGap;
183: }
184:
185:
190: public void setInteriorGap(final RectangleInsets gap) {
191: this.interiorGap = gap;
192: }
193:
194:
199: public Paint getBackgroundPaint() {
200: return this.backgroundPaint;
201: }
202:
203:
208: public void setBackgroundPaint(final Paint paint) {
209: this.backgroundPaint = paint;
210: }
211:
212:
217: public Paint getShadowPaint() {
218: return this.shadowPaint;
219: }
220:
221:
226: public void setShadowPaint(final Paint paint) {
227: this.shadowPaint = paint;
228: }
229:
230:
235: public double getShadowXOffset() {
236: return this.shadowXOffset;
237: }
238:
239:
244: public void setShadowXOffset(final double offset) {
245: this.shadowXOffset = offset;
246: }
247:
248:
253: public double getShadowYOffset() {
254: return this.shadowYOffset;
255: }
256:
257:
262: public void setShadowYOffset(final double offset) {
263: this.shadowYOffset = offset;
264: }
265:
266:
271: public TextBlock getTextBlock() {
272: return this.textBlock;
273: }
274:
275:
280: public void setTextBlock(final TextBlock block) {
281: this.textBlock = block;
282: }
283:
284:
292: public void draw(final Graphics2D g2,
293: final float x, final float y,
294: final RectangleAnchor anchor) {
295: final Size2D d1 = this.textBlock.calculateDimensions(g2);
296: final double w = this.interiorGap.extendWidth(d1.getWidth());
297: final double h = this.interiorGap.extendHeight(d1.getHeight());
298: final Size2D d2 = new Size2D(w, h);
299: final Rectangle2D bounds
300: = RectangleAnchor.createRectangle(d2, x, y, anchor);
301:
302: if (this.shadowPaint != null) {
303: final Rectangle2D shadow = new Rectangle2D.Double(
304: bounds.getX() + this.shadowXOffset,
305: bounds.getY() + this.shadowYOffset,
306: bounds.getWidth(), bounds.getHeight()
307: );
308: g2.setPaint(this.shadowPaint);
309: g2.fill(shadow);
310: }
311: if (this.backgroundPaint != null) {
312: g2.setPaint(this.backgroundPaint);
313: g2.fill(bounds);
314: }
315:
316: if (this.outlinePaint != null && this.outlineStroke != null) {
317: g2.setPaint(this.outlinePaint);
318: g2.setStroke(this.outlineStroke);
319: g2.draw(bounds);
320: }
321:
322: this.textBlock.draw(
323: g2, (float) bounds.getCenterX(), (float) bounds.getCenterY(),
324: TextBlockAnchor.CENTER
325: );
326:
327: }
328:
329:
336: public double getHeight(final Graphics2D g2) {
337: final Size2D d = this.textBlock.calculateDimensions(g2);
338: return this.interiorGap.extendHeight(d.getHeight());
339: }
340:
341:
348: public boolean equals(final Object obj) {
349: if (obj == this) {
350: return true;
351: }
352: if (!(obj instanceof TextBox)) {
353: return false;
354: }
355: final TextBox that = (TextBox) obj;
356: if (!ObjectUtilities.equal(this.outlinePaint, that.outlinePaint)) {
357: return false;
358: }
359: if (!ObjectUtilities.equal(this.outlineStroke, that.outlineStroke)) {
360: return false;
361: }
362: if (!ObjectUtilities.equal(this.interiorGap, that.interiorGap)) {
363: return false;
364: }
365: if (!ObjectUtilities.equal(this.backgroundPaint,
366: that.backgroundPaint)) {
367: return false;
368: }
369: if (!ObjectUtilities.equal(this.shadowPaint, that.shadowPaint)) {
370: return false;
371: }
372: if (this.shadowXOffset != that.shadowXOffset) {
373: return false;
374: }
375: if (this.shadowYOffset != that.shadowYOffset) {
376: return false;
377: }
378: if (!ObjectUtilities.equal(this.textBlock, that.textBlock)) {
379: return false;
380: }
381:
382: return true;
383: }
384:
385:
390: public int hashCode() {
391: int result;
392: long temp;
393: result = (this.outlinePaint != null ? this.outlinePaint.hashCode() : 0);
394: result = 29 * result + (this.outlineStroke != null
395: ? this.outlineStroke.hashCode() : 0);
396: result = 29 * result + (this.interiorGap != null
397: ? this.interiorGap.hashCode() : 0);
398: result = 29 * result + (this.backgroundPaint != null
399: ? this.backgroundPaint.hashCode() : 0);
400: result = 29 * result + (this.shadowPaint != null
401: ? this.shadowPaint.hashCode() : 0);
402: temp = this.shadowXOffset != +0.0d
403: ? Double.doubleToLongBits(this.shadowXOffset) : 0L;
404: result = 29 * result + (int) (temp ^ (temp >>> 32));
405: temp = this.shadowYOffset != +0.0d
406: ? Double.doubleToLongBits(this.shadowYOffset) : 0L;
407: result = 29 * result + (int) (temp ^ (temp >>> 32));
408: result = 29 * result + (this.textBlock != null
409: ? this.textBlock.hashCode() : 0);
410: return result;
411: }
412:
413:
420: private void writeObject(final ObjectOutputStream stream)
421: throws IOException {
422: stream.defaultWriteObject();
423: SerialUtilities.writePaint(this.outlinePaint, stream);
424: SerialUtilities.writeStroke(this.outlineStroke, stream);
425: SerialUtilities.writePaint(this.backgroundPaint, stream);
426: SerialUtilities.writePaint(this.shadowPaint, stream);
427: }
428:
429:
437: private void readObject(final ObjectInputStream stream)
438: throws IOException, ClassNotFoundException {
439: stream.defaultReadObject();
440: this.outlinePaint = SerialUtilities.readPaint(stream);
441: this.outlineStroke = SerialUtilities.readStroke(stream);
442: this.backgroundPaint = SerialUtilities.readPaint(stream);
443: this.shadowPaint = SerialUtilities.readPaint(stream);
444: }
445:
446:
447: }