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