001: /**
002: * Caption: Zaval Java Resource Editor
003: * $Revision: 0.37 $
004: * $Date: 2002/03/28 9:24:42 $
005: *
006: * @author: Victor Krapivin
007: * @version: 1.3
008: *
009: * Zaval JRC Editor is a visual editor which allows you to manipulate
010: * localization strings for all Java based software with appropriate
011: * support embedded.
012: *
013: * For more info on this product read Zaval Java Resource Editor User's Guide
014: * (It comes within this package).
015: * The latest product version is always available from the product's homepage:
016: * http://www.zaval.org/products/jrc-editor/
017: * and from the SourceForge:
018: * http://sourceforge.net/projects/zaval0002/
019: *
020: * Contacts:
021: * Support : support@zaval.org
022: * Change Requests : change-request@zaval.org
023: * Feedback : feedback@zaval.org
024: * Other : info@zaval.org
025: *
026: * Copyright (C) 2001-2002 Zaval Creative Engineering Group (http://www.zaval.org)
027: *
028: * This program is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU General Public License
030: * (version 2) as published by the Free Software Foundation.
031: *
032: * This program is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
035: * GNU General Public License for more details.
036: *
037: * You should have received a copy of the GNU General Public License
038: * along with this program; if not, write to the Free Software
039: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
040: *
041: */package org.zaval.awt;
042:
043: import java.awt.*;
044: import java.util.*;
045:
046: public class MultiLabel extends Canvas {
047: public final static int LEFT = 0;
048: public final static int CENTER = 1;
049: public final static int RIGHT = 2;
050: protected String[] lines;
051: protected int lcount = 0;
052: protected int width = 0, height, ascent;
053: protected int[] widths;
054: protected int align = LEFT;
055: public int LMAX = 50;
056: protected boolean isVc = false;
057: protected boolean withHeader = false;
058: protected Font flf = new Font("Dialog", Font.BOLD, 10);
059:
060: public void setText(String text) {
061: newLabel(text);
062: width = 0;
063: measure();
064: repaint();
065: }
066:
067: public void setTitleFont(Font fx) {
068: flf = fx;
069: width = 0;
070: measure();
071: repaint();
072: }
073:
074: public boolean isHeaderPainting() {
075: return withHeader;
076: }
077:
078: public void headerPaint(boolean s) {
079: withHeader = s;
080: }
081:
082: public Font getTitleFont() {
083: return flf;
084: }
085:
086: public MultiLabel(String text, int align, int LMAX) {
087: super ();
088: this .align = align;
089: this .LMAX = LMAX;
090: newLabel(text);
091: //measure();
092: }
093:
094: public MultiLabel(String text, int align) {
095: this (text, align, 40);
096: }
097:
098: public MultiLabel(String text) {
099: this (text, LEFT, 40);
100: }
101:
102: public void setFont(Font x) {
103: super .setFont(x);
104: width = 0;
105: measure();
106: repaint();
107: }
108:
109: public void setForeground(Color color) {
110: super .setForeground(color);
111: repaint();
112: }
113:
114: public void addNotify() {
115: super .addNotify();
116: measure();
117: }
118:
119: public Dimension preferredSize() {
120: return new Dimension(width, height * lcount);
121: }
122:
123: public Dimension minimumSize() {
124: return new Dimension(width, height * lcount);
125: }
126:
127: public void measure() {
128: Font x = this .getFont();
129: if (x == null)
130: return;
131: FontMetrics fm = getFontMetrics(x);
132: if (fm == null)
133: return;
134: height = fm.getHeight();
135: ascent = fm.getAscent();
136: int i;
137: for (i = 0; i < lcount; ++i) {
138: widths[i] = fm.stringWidth(lines[i]);
139: if (width < widths[i])
140: width = widths[i];
141: }
142: }
143:
144: private void newLabel(String text) {
145: StringTokenizer st = new StringTokenizer(text, "^", true);
146: lcount = st.countTokens() + 1;
147: lines = new String[lcount * 5];
148: widths = new int[lcount * 5];
149: int i, j = 0, f = 0;
150: for (i = 0; i < lcount - 1; ++i) {
151: String x = st.nextToken();
152: if (x.equals("^"))
153: ++f;
154: else
155: f = 0;
156: if (f > 1 || f == 0) {
157: if (f > 0)
158: lines[j++] = " ";
159: else {
160: while (x.length() > LMAX) {
161: int k = LMAX;
162: k = x.lastIndexOf(" ", LMAX);
163: if (k < 0)
164: k = LMAX;
165: lines[j++] = x.substring(0, k);
166: x = x.substring(k, x.length());
167: }
168: lines[j++] = x;
169: }
170: }
171: if (j == lcount * 5 - 1)
172: break;
173: }
174: lcount = j;
175: }
176:
177: public void vertCenter(boolean isVc) {
178: this .isVc = isVc;
179: }
180:
181: private boolean fullrepaint = false;
182:
183: public void repaint() {
184: synchronized (this ) {
185: fullrepaint = true;
186: }
187: super .repaint();
188: }
189:
190: public void repaint(int x, int y, int w, int h) {
191: synchronized (this ) {
192: fullrepaint = true;
193: }
194: super .repaint(x, y, w, h);
195: }
196:
197: public void paint(Graphics gr) {
198: synchronized (this ) {
199: fullrepaint = false;
200: }
201: int x, y, i;
202: if (gr == null)
203: return;
204: else if (width == 0)
205: measure();
206: Dimension d = size();
207: gr.clearRect(0, 0, d.width, d.height);
208: y = ascent + 1;//(d.height-lcount*height)/2;
209: if (withHeader) {
210: x = (d.width - widths[0]) / 2;
211: Font cur = gr.getFont();
212: gr.setFont(flf);
213: gr.drawString(lines[0], x, y);
214: gr.setFont(cur);
215: y += height;
216: i = 1;
217: } else
218: i = 0;
219: if (isVc)
220: y += (d.height - height * lcount) / 2;
221: for (; i < lcount; ++i) {
222: if (align == LEFT)
223: x = 1;
224: else if (align == RIGHT)
225: x = d.width - 1 - widths[i];
226: else
227: x = (d.width - widths[i]) / 2;
228: gr.drawString(lines[i], x, y);
229: y += height;
230: }
231: }
232: }
|