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.dialog;
042:
043: import java.awt.*;
044: import java.util.*;
045:
046: import org.zaval.awt.*;
047:
048: public class MessageBox2 extends Dialog implements LayoutManager {
049: private StaticImage icon = null;
050: private Vector buttons = null;
051: private ResultField text;
052: private Color storedColor = null;
053:
054: private Vector listeners = new Vector();
055: private Button pressedButton = null;
056:
057: private static final int INDENT = 10;
058: private static final int ICON_TEXT_GAP = 5;
059: private static final int w = 4, h = 3, maxlinecount = 15;
060:
061: public ResultField getTextContainer() {
062: return text;
063: }
064:
065: public MessageBox2(Frame parent) {
066: super (parent, false);
067: setLayout(this );
068: setTitle("Message");
069: text = new ResultField();
070: TextAlignArea area = text.getAlignArea();
071: if (area != null) {
072: area.setMultiLine(true);
073: area.setAlign(Align.TLEFT);
074: area.setInsets(new Insets(10, 0, 10, 0));
075: }
076: add(text);
077: }
078:
079: public void show() {
080: pack();
081: Dimension d1 = preferredSize();
082: Dimension d2 = Toolkit.getDefaultToolkit().getScreenSize();
083: resize(d1.width, d1.height);
084: move((d2.width - d1.width) / 2, (d2.height - d1.height) / 2);
085: setResizable(false);
086: super .show();
087: }
088:
089: public void setBackground(Color c) //// new
090: {
091: if (storedColor != null)
092: c = storedColor;
093: storedColor = c;
094: if (text != null)
095: text.setBackground(c);
096: if (icon != null)
097: icon.setBackground(c);
098: for (int i = 0; buttons != null && i < buttons.size(); i++)
099: ((Component) buttons.elementAt(i)).setBackground(c);
100: super .setBackground(c);
101: }
102:
103: public void setForeground(Color c) {
104: super .setForeground(c);
105: if (text != null)
106: text.setForeground(c);
107: if (icon != null)
108: icon.setForeground(c);
109: for (int i = 0; i < buttons.size(); i++)
110: ((Component) buttons.elementAt(i)).setForeground(c);
111: }
112:
113: public void setButtons(Vector b) {
114: if (buttons != null)
115: for (int i = 0; i < buttons.size(); i++)
116: remove((Component) buttons.elementAt(i));
117: buttons = b;
118: for (int i = 0; i < buttons.size(); i++)
119: add((Component) buttons.elementAt(i));
120: invalidate();
121: validate();
122: }
123:
124: public Vector getButtons() {
125: return buttons;
126: }
127:
128: public void setButtons(String[] sa) {
129: int j;
130: Vector z = new Vector(sa.length);
131: for (j = 0; j < sa.length; ++j) {
132: Button b = new Button(sa[j]);
133: z.addElement(b);
134: }
135: setButtons(z);
136: }
137:
138: public void setButtons(String one) {
139: Vector z = new Vector();
140: z.addElement(new Button(one));
141: setButtons(z);
142: }
143:
144: public void setIcon(Image icon1) {
145: if (icon != null)
146: remove(icon);
147: if (icon1 == null)
148: return;
149: icon = new StaticImage(icon1);
150: add(icon);
151: }
152:
153: public void setText(String str) {
154: text.setText(str);
155: }
156:
157: public String getText() {
158: return text.getText();
159: }
160:
161: public void addListener(Component ls) {
162: listeners.addElement(ls);
163: }
164:
165: public void removeListener(Component ls) {
166: listeners.removeElement(ls);
167: }
168:
169: public boolean action(Event e, Object o) {
170: if (e.target instanceof Button) {
171: pressedButton = (Button) e.target;
172: Enumeration els = listeners.elements();
173: while (els.hasMoreElements()) {
174: Component listener = (Component) els.nextElement();
175: listener.postEvent(new Event(this , Event.ACTION_EVENT,
176: (Button) e.target));
177: }
178: hide();
179: return true;
180: }
181: return super .action(e, o);
182: }
183:
184: private void setDefFontMetrics() {
185: if (text != null
186: && text.getAlignArea().getFontMetrics() == null) {
187: Font font = getFont();
188: if (font == null)
189: return;
190: FontMetrics fm = getFontMetrics(font);
191: if (fm == null)
192: return;
193: text.getAlignArea().setFontMetrics(fm);
194: }
195: }
196:
197: private Dimension getIconSize() {
198: if (icon == null)
199: return new Dimension(0, 0);
200: return icon.preferredSize();
201: }
202:
203: private Dimension getTextSize(int maxwidth) {
204: if (text == null)
205: return new Dimension(0, 0);
206: setDefFontMetrics();
207:
208: int prefwidth = 0;
209: int prefheight = 0;
210: TextAlignArea align = text.getAlignArea();
211: if (align != null && align.getFontMetrics() != null) {
212: FontMetrics fm = align.getFontMetrics();
213: String[] str = TextAlignArea.breakString(align.getText(),
214: fm, maxwidth);
215: for (int i = 0; i < str.length; i++)
216: prefwidth = Math.max(fm.stringWidth(str[i]), prefwidth);
217: prefheight = fm.getHeight()
218: * Math.min(maxlinecount, str.length);
219: Insets ins = align.getInsets();
220: prefwidth += ins.left + ins.right;
221: prefheight += ins.top + ins.bottom;
222: }
223: return new Dimension(prefwidth, prefheight);
224: }
225:
226: private Button getButton(int z) {
227: return (Button) buttons.elementAt(z);
228: }
229:
230: private Dimension getButtonsSize() {
231: if (buttons == null || buttons.size() == 0)
232: return new Dimension(0, 0);
233: int h = 23, w = 48; // minimum height and width
234: for (int i = 0; i < buttons.size(); i++) {
235: Component z = getButton(i);
236: Dimension d = getButton(i).preferredSize();
237: h = Math.max(d.height, h);
238: w = Math.max(d.width, w);
239: }
240: return new Dimension(w, h);
241: }
242:
243: private Rectangle getPosition(Rectangle area, Dimension size) {
244: int w, h;
245: w = Math.max(0, (area.width - size.width) / 2);
246: h = Math.max(0, (area.height - size.height) / 2);
247: return new Rectangle(area.x + w, area.y + h,
248: area.width - w * 2, area.height - h * 2);
249: }
250:
251: // LayoutManager implementation
252: public void addLayoutComponent(String s, Component component) {
253: }
254:
255: public void removeLayoutComponent(Component component) {
256: }
257:
258: public Dimension preferredLayoutSize(Container parent) {
259: Insets ins = insets();
260: Dimension d;
261: Dimension d2 = Toolkit.getDefaultToolkit().getScreenSize();
262: d = getTextSize(d2.width / 2);
263: int prefwidth = d.width;
264: int prefheight = d.height;
265:
266: d = getIconSize();
267: if (d.width != 0)
268: d.width += ICON_TEXT_GAP;
269: prefwidth += d.width;
270: prefheight = Math.max(d.height, prefheight);
271:
272: d = getButtonsSize();
273: prefwidth = Math.max(prefwidth, (d.width + w) * buttons.size()
274: + w);
275: prefheight += d.height + h * 2 + INDENT;
276: if (prefwidth < d2.width / 4)
277: prefwidth = d2.width / 4;
278: Dimension d111 = new Dimension(prefwidth + ins.left + ins.right
279: + 2 * INDENT, prefheight + ins.top + ins.bottom + 2
280: * INDENT);
281: return d111;
282:
283: }
284:
285: public Dimension minimumLayoutSize(Container parent) {
286: return preferredLayoutSize(parent);
287: }
288:
289: public void layoutContainer(Container parent) {
290: Dimension size = parent.size();
291: Insets inss = insets();
292: int insl = inss.left + INDENT;
293: int inst = inss.top + INDENT;
294: int width = size.width - (insl + inss.right + INDENT);
295: int height = size.height - (inst + inss.bottom + INDENT);
296: Dimension isize = getIconSize();
297: Dimension tsize = getTextSize(width - isize.width
298: - ICON_TEXT_GAP);
299: Dimension bsize = getButtonsSize();
300: height -= bsize.height + h * 2;
301: height = Math.max(height, Math.max(isize.height, tsize.height));
302: if (icon != null) {
303: Dimension d = icon.preferredSize();
304: Rectangle r = getPosition(new Rectangle(insl, inst,
305: isize.width, height), d);
306: icon.move(r.x, r.y);
307: icon.resize(r.width, r.height);
308: }
309: if (text != null) {
310: int addIc = isize.width;
311: if (isize.width > 0)
312: addIc += ICON_TEXT_GAP;
313: if (width - tsize.width > 3 * addIc)
314: addIc = 0;
315: else
316: addIc -= (width - tsize.width - addIc) / 2;
317: Rectangle r = getPosition(new Rectangle(insl + addIc, inst,
318: width - addIc, height), tsize);
319: text.move(r.x, r.y);
320: text.resize(r.width, r.height);
321: }
322: int x, y;
323: y = height + inst + h;
324: x = (width - (bsize.width + w) * buttons.size() - w) / 2 + w
325: + insl;
326: for (int i = 0; i < buttons.size(); i++) {
327: getButton(i).move(x, y);
328: getButton(i).resize(bsize);
329: x += bsize.width + w;
330: }
331: }
332:
333: public boolean handleEvent(Event e) {
334: if (e.id == Event.WINDOW_DESTROY) {
335: hide();
336: return true;
337: }
338: return super .handleEvent(e);
339: }
340:
341: public Button getPressedButton() {
342: return pressedButton;
343: }
344:
345: public void init() {
346: }
347:
348: /*public void setModal( boolean b )
349: {
350: }*/
351:
352: public boolean keyDown(Event e, int key) {
353: if (e.target instanceof Button && key == Event.ENTER) {
354: Window wnd = null;
355: Component p = this ;
356: while (p != null && !(p instanceof Window))
357: p = p.getParent();
358: if (p == null)
359: return false;
360: wnd = (Window) p;
361: p.action(e, null);
362: return true;
363: }
364: return false;
365: }
366: }
|