01: /*
02: * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
03: * reserved.
04: *
05: * Redistribution and use in source and binary forms, with or without
06: * modification, are permitted provided that the following conditions
07: * are met:
08: *
09: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: *
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in
14: * the documentation and/or other materials provided with the
15: * distribution.
16: *
17: * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18: * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21: * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: *
29: */
30:
31: package rcm.awt;
32:
33: import java.awt.*;
34:
35: public class MultiLineLabel extends Canvas {
36: private String text;
37: private MultiLineString multilabel;
38: private int alignment;
39:
40: private Dimension minSize; // cached result of getMinimumSize()
41:
42: public MultiLineLabel(String string) {
43: setText(string);
44: setAlignment(Label.LEFT);
45: }
46:
47: public MultiLineLabel(String string, int alignment) {
48: setText(string);
49: setAlignment(alignment);
50: }
51:
52: public String getText() {
53: return text;
54: }
55:
56: public void setText(String string) {
57: text = string;
58: multilabel = new MultiLineString(string);
59: minSize = null;
60: }
61:
62: public int getAlignment() {
63: return alignment;
64: }
65:
66: public void setAlignment(int align) {
67: alignment = align;
68: }
69:
70: public void setFont(Font font) {
71: super .setFont(font);
72: minSize = null;
73: }
74:
75: public Dimension minimumSize() {
76: // FIX: cache this size
77: if (minSize == null) {
78: FontMetrics fm = getFontMetrics(getFont());
79: minSize = new Dimension(multilabel.getWidth(fm), multilabel
80: .getHeight(fm));
81: }
82: return minSize;
83: }
84:
85: public Dimension preferredSize() {
86: return minimumSize();
87: }
88:
89: public synchronized void paint(Graphics g) {
90: g.setFont(getFont());
91: multilabel.draw(g, 0, 0, alignment);
92: }
93: }
|