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 TextAlignArea extends AlignArea {
047: public static final int STRING_GAP = 0;
048:
049: private String text;
050: private String[] strs = new String[0];
051: private String deliver = "/p";
052: private FontMetrics fontMetrics = null;
053: private boolean isMulti = false;
054:
055: public TextAlignArea() {
056: setAlign(Align.LEFT);
057: }
058:
059: protected int getWidth(int s, Dimension size) {
060: if (fontMetrics == null || text == null)
061: return -1;
062:
063: if (!isMulti)
064: return fontMetrics.stringWidth(text);
065:
066: int max = -1;
067: for (int i = 0; i < strs.length; i++) {
068: int len = fontMetrics.stringWidth(strs[i]);
069: if (max < len)
070: max = len;
071: }
072: return max;
073: }
074:
075: protected int getHeight(int s, Dimension size) {
076: if (fontMetrics == null || text == null)
077: return -1;
078: if (!isMulti)
079: return fontMetrics.getHeight();
080: return fontMetrics.getHeight() * strs.length
081: + (strs.length - 1) * STRING_GAP;
082: }
083:
084: protected void recalc() {
085: Dimension d = getSize();
086: if (fontMetrics == null)
087: strs = null;
088: else
089: strs = breakString(text, fontMetrics, d.width);
090: }
091:
092: public void setMultiLine(boolean b) {
093: if (isMulti == b)
094: return;
095: isMulti = b;
096: invalidate();
097: }
098:
099: public void setFontMetrics(FontMetrics f) {
100: fontMetrics = f;
101: invalidate();
102: }
103:
104: public void setText(String t) {
105: if (t != null && text != null && t.equals(text))
106: return;
107: text = t;
108: invalidate();
109: }
110:
111: public String getText() {
112: return text;
113: }
114:
115: public FontMetrics getFontMetrics() {
116: return fontMetrics;
117: }
118:
119: public void draw(Graphics g, Color col) {
120: draw(g, 0, 0, col);
121: }
122:
123: private void drawText(Graphics gr, String text, int xs, int ys,
124: int max) {
125: int comml = 0, commr = 0, j = 0;
126: StringTokenizer st = new StringTokenizer(text, ":");
127: String left = st.nextToken() + ":";
128: String right = st.nextToken();
129:
130: FontMetrics fm = fontMetrics;
131: comml += fm.stringWidth(left);
132: commr += fm.stringWidth(right);
133:
134: gr.drawString(left, xs, ys);
135: gr.drawString(right, xs + max - commr, ys);
136: }
137:
138: private void drawJText(Graphics gr, String text, int xs, int ys,
139: int max) {
140: int j = 0, sizes = 0;
141: StringTokenizer st = new StringTokenizer(text, "\t ");
142: String[] words = new String[st.countTokens()];
143: FontMetrics fm = fontMetrics;
144:
145: for (; st.hasMoreTokens(); ++j) {
146: words[j] = st.nextToken();
147: sizes += fm.stringWidth(words[j]);
148: }
149: double space = (max - sizes) / (words.length - 1);
150: for (j = 0; j < words.length; ++j) {
151: gr.drawString(words[j], xs, ys);
152: xs += space;
153: }
154: }
155:
156: public void draw(Graphics g, int offx, int offy, Color col) {
157: Dimension d = getSize();
158: Insets ins = getInsets();
159: Rectangle r = getAlignRectangle();
160:
161: int h = fontMetrics.getHeight();
162: int y = r.y + fontMetrics.getHeight() + offy
163: - fontMetrics.getDescent();
164: g.setColor(col);
165:
166: int x = r.x + offx;
167: if (isMulti)
168: for (int i = 0; i < strs.length; i++) {
169: if ((getAlign() & Align.RIGHT) > 0) {
170: int len = fontMetrics.stringWidth(strs[i]);
171: if (len > d.width)
172: x = ins.left + offx;
173: else
174: x = r.x + offx + r.width - len;
175: }
176: if ((getAlign() & Align.FIT) > 0)
177: drawText(g, strs[i], x, y, d.width);
178: else if (i + 1 != strs.length
179: && (getAlign() & Align.JUSTIFY) > 0)
180: drawJText(g, strs[i], x, y, d.width);
181: else
182: g.drawString(strs[i], x, y);
183: y += (h + STRING_GAP);
184: }
185:
186: else
187: g.drawString(text, r.x, y);
188: }
189:
190: public static void next(String s, String[] res) {
191: int index = s.indexOf(' ');
192: if (index < 0) {
193: res[0] = s;
194: res[1] = null;
195: return;
196: }
197:
198: if (index == 0)
199: index++;
200: res[0] = s.substring(index);
201: res[1] = s.substring(0, index);
202: }
203:
204: public static String[] breakString(String t, FontMetrics fm,
205: int width) {
206: if (t != null) {
207: StringTokenizer st = new StringTokenizer(t, "\n");
208: Vector vv = new Vector();
209:
210: while (st.hasMoreTokens()) {
211: vv.addElement(st.nextToken());
212: }
213:
214: Vector vvv = new Vector();
215: String[] ps = new String[2];
216:
217: for (int i = 0; i < vv.size(); i++) {
218: String ss = (String) vv.elementAt(i);
219:
220: String tk = "";
221: int tw = 0;
222: int c = 0;
223:
224: for (;;) {
225: String token = null;
226: next(ss, ps);
227:
228: if (ps[1] != null)
229: token = ps[1];
230: else
231: token = ps[0];
232: ss = ps[0];
233:
234: int len = fm.stringWidth(token);
235: if ((tw + len) > width) {
236: if (tk.length() > 0) {
237: vvv.addElement(tk);
238: tk = token;
239: tw = len;
240: } else {
241: vvv.addElement(token);
242: tk = "";
243: tw = 0;
244: }
245: } else {
246: c++;
247: tk += token;
248: tw += len;
249: }
250:
251: if (ps[1] == null)
252: break;
253: }
254:
255: if (c > 0)
256: vvv.addElement(tk);
257:
258: }
259:
260: String[] strs = new String[vvv.size()];
261: for (int i = 0; i < vvv.size(); i++)
262: strs[i] = (String) vvv.elementAt(i);
263: return strs;
264: }
265: return null;
266: }
267: }
|