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.FontMetrics;
34: import java.awt.Graphics;
35: import java.awt.Label;
36: import java.util.StringTokenizer;
37:
38: // FIX: convert to RichString which supports font and color runs
39:
40: public class MultiLineString {
41:
42: String[] lines;
43:
44: public MultiLineString(String string) {
45: StringTokenizer tok = new StringTokenizer(string, "\n");
46: lines = new String[tok.countTokens()];
47: for (int i = 0; i < lines.length; ++i)
48: lines[i] = tok.nextToken();
49: }
50:
51: public MultiLineString(String[] lines) {
52: this .lines = lines;
53: }
54:
55: public int countLines() {
56: return lines.length;
57: }
58:
59: public String getLineAt(int i) {
60: return lines[i];
61: }
62:
63: public int getWidth(FontMetrics fm) {
64: int w = 0;
65: for (int i = 0; i < lines.length; ++i)
66: w = Math.max(w, fm.stringWidth(lines[i]));
67: return w;
68: }
69:
70: public int getHeight(FontMetrics fm) {
71: return fm.getHeight() * lines.length;
72: }
73:
74: public void draw(Graphics g, int x, int y, int alignment) {
75: FontMetrics fm = g.getFontMetrics();
76:
77: y += fm.getAscent();
78:
79: int width = alignment != Label.LEFT ? getWidth(fm) : 0; // don't need it if alignment is LEFT
80:
81: for (int i = 0; i < lines.length; ++i) {
82: int x1 = x;
83: switch (alignment) {
84: case Label.LEFT:
85: break;
86: case Label.RIGHT:
87: x += width - fm.stringWidth(lines[i]);
88: break;
89: case Label.CENTER:
90: x += (width - fm.stringWidth(lines[i])) / 2;
91: break;
92: }
93:
94: g.drawString(lines[i], x, y);
95: y += fm.getHeight();
96: }
97: }
98:
99: }
|