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 IELabel extends Canvas {
047: public static final int LEFT = 0;
048: public static final int CENTER = 1;
049: public static final int RIGHT = 2;
050: public static final int FIT = 3;
051:
052: private int wsize = 0;
053: private int hsize = 0;
054:
055: private int ascent;
056:
057: private String name;
058: private int align;
059:
060: public IELabel() {
061: align = LEFT;
062: name = "";
063: }
064:
065: public IELabel(String name) {
066: align = LEFT;
067: this .name = name;
068: }
069:
070: public IELabel(String name, int align) {
071: this .align = align;
072: this .name = name;
073: }
074:
075: public int getAlignment() {
076: return align;
077: }
078:
079: public String getText() {
080: return name;
081: }
082:
083: public void setAlignment(int al) {
084: align = al;
085: }
086:
087: public void setText(String s) {
088: name = s;
089: measure();
090: }
091:
092: public String toString() {
093: return "IELabel[text="
094: + name
095: + ",align="
096: + (align == LEFT ? "LEFT" : align == CENTER ? "CENTER"
097: : "RIGHT") + "]";
098: }
099:
100: public void setFont(Font s) {
101: super .setFont(s);
102: measure();
103: }
104:
105: public void paint(Graphics gr) {
106: try {
107: measure();
108: Dimension b = size();
109: int xs, ys = ascent + (b.height - hsize) / 2;
110: if (RIGHT == align)
111: xs = b.width - wsize;
112: else if (CENTER == align)
113: xs = (b.width - wsize) / 2;
114: else
115: xs = 1;
116:
117: gr.setColor(getBackground());
118:
119: //gr.setColor(Color.white);
120:
121: gr.fillRect(0, 0, b.width - 1, b.height - 1);
122:
123: gr.setColor(getForeground());
124: if (align != FIT || b.width <= wsize)
125: gr.drawString(name, xs, ys);
126: else
127: drawText(gr, name, xs, ys, b.width);
128: } catch (Exception eee) {
129: eee.printStackTrace();
130: }
131: }
132:
133: private void drawText(Graphics gr, String text, int xs, int ys,
134: int max) {
135: int comml = 0, j = 0;
136: StringTokenizer st = new StringTokenizer(text, " \t");
137: if (st.countTokens() == 0)
138: return;
139: String[] words = new String[st.countTokens()];
140:
141: FontMetrics fm = getFontMetrics(getFont());
142: while (st.hasMoreTokens()) {
143: words[j] = st.nextToken();
144: comml += fm.stringWidth(words[j]);
145: ++j;
146: }
147: int spc = (max - comml) / words.length;
148: int spcd = (max - comml) % words.length;
149:
150: int k = spcd, v = 0;
151: for (j = 0; j < words.length; ++j) {
152: gr.drawString(words[j], xs, ys);
153: v = k / words.length;
154: if (v > 0)
155: k = k % words.length;
156: k += spcd;
157: xs += fm.stringWidth(words[j]) + spc + v;
158: }
159: }
160:
161: public Dimension preferredSize() {
162: measure();
163: return new Dimension(wsize, hsize);
164: }
165:
166: public Dimension minimumSize() {
167: return preferredSize();
168: }
169:
170: private void measure() {
171: if (name == null)
172: return;
173: Font x = getFont();
174: if (x == null)
175: return;
176: FontMetrics fm = getFontMetrics(x);
177: if (fm == null) {
178: Toolkit k = Toolkit.getDefaultToolkit();
179: fm = k.getFontMetrics(x);
180: if (fm == null)
181: return;
182: }
183: hsize = fm.getHeight();
184: ascent = fm.getAscent();
185: wsize = fm.stringWidth(name) /*+10*/; // for bad fonts
186: }
187: }
|