01: package net.xoetrope.swing;
02:
03: import java.awt.Container;
04: import java.awt.Dimension;
05: import java.awt.Font;
06: import java.awt.FontMetrics;
07: import java.awt.Graphics;
08:
09: import net.xoetrope.xui.XMessageBoxSetup;
10: import net.xoetrope.xui.XPage;
11: import javax.swing.SwingUtilities;
12: import net.xoetrope.xui.build.BuildProperties;
13:
14: /**
15: * <p>Creates a simple modal MessageBox with a label for the text
16: * and a close button </p>
17: * <p>Copyright (c) Xoetrope Ltd., 1998-2004<br>
18: * <p>License: see license.txt</p>
19: * $revision
20: */
21: public class XMessageBox extends XDialog implements XMessageBoxSetup {
22: protected XButton btnClose;
23: protected XLabel label;
24: protected String message;
25:
26: /**
27: * Constructor creates the close image and the label for displaying the message.
28: */
29: public XMessageBox() {
30: super (true, DEFAULT_PADDING);
31: saveOnClose = false;
32: }
33:
34: /**
35: * Setup the content of the message box
36: * @param title the message box title or caption
37: * @param msg the text of the message
38: * @param size the size of the owner page
39: * @param container the content page
40: */
41: public void setup(String title, String msg, Dimension size,
42: Container container) {
43: message = msg;
44: setCaption(title);
45:
46: int x = (getSize().width / 2) - (getSize().width / 2);
47: int y = (getSize().height / 2) - (getSize().height / 2);
48: setLocation(x, y);
49:
50: btnClose = (XButton) componentFactory.addComponent(
51: XPage.BUTTON, 10, 370, 72, 20, "Close");
52: label = (XLabel) componentFactory.addComponent(XPage.LABEL, 2,
53: 2, 30, 20, msg, null);
54: addActionHandler(btnClose, "closeDlg");
55:
56: int height = 200;
57: Graphics g = container.getGraphics();
58: FontMetrics fm = g.getFontMetrics(new Font(null, 0, 12));
59: height = fm.getHeight() * (2 + fm.stringWidth(msg) / 380);
60: g.dispose();
61: g = null;
62: setSize(400, height + 44);
63:
64: if (BuildProperties.BUILD_JDK_118)
65: showDialog(container, null);
66: else {
67: final Container myContainer = container;
68: SwingUtilities.invokeLater(new Runnable() {
69: public void run() {
70: showDialog(myContainer, null);
71: }
72: });
73: }
74: }
75:
76: /**
77: * Close the dialog.
78: */
79: public void closeDlg() {
80: // if ( wasMouseClicked()) {
81: returnValue = 1;
82: super .closeDlg();
83: // }
84: }
85:
86: /**
87: * Adjust the size and locatio of the close image and the message label.
88: * @param width The new width of the dialog.
89: * @param height The new height of the dialog.
90: */
91: public void setSize(int width, int height) {
92: btnClose.setLocation(width - btnClose.getSize().width - 10,
93: height - btnClose.getSize().height - 8);
94: label.setSize(width - 6, height - btnClose.getSize().height
95: - 10);
96: super.setSize(width, height);
97: }
98: }
|