001: package net.xoetrope.awt;
002:
003: import java.awt.Container;
004: import java.awt.Dimension;
005: import java.awt.Font;
006: import java.awt.FontMetrics;
007: import java.awt.Graphics;
008:
009: import net.xoetrope.xui.XMessageBoxSetup;
010: import net.xoetrope.xui.XPage;
011: import javax.swing.SwingUtilities;
012: import net.xoetrope.xui.build.BuildProperties;
013:
014: /**
015: * <p>Creates a simple modal MessageBox with a label for the text
016: * and a close button </p>
017: * <p>Copyright (c) Xoetrope Ltd., 1998-2004<br>
018: * License: see license.txt
019: * $Revision: 1.10 $
020: */
021: public class XMessageBox extends XDialog implements XMessageBoxSetup {
022: private XButton btnClose;
023: private XLabel label;
024: private String message;
025:
026: /**
027: * Constructor creates the close image and the label for displaying the message.
028: */
029: public XMessageBox() {
030: super (true, DEFAULT_PADDING);
031:
032: saveOnClose = false;
033: }
034:
035: /**
036: * Setup the content of the message box
037: * @param title the message box title or caption
038: * @param msg the text of the message
039: * @param size the size of the owner page
040: * @param container container content page
041: */
042: public void setup(String title, String msg, Dimension size,
043: Container container) {
044: try {
045: message = msg;
046: setCaption(title);
047:
048: int x = (getSize().width / 2) - (getSize().width / 2);
049: int y = (getSize().height / 2) - (getSize().height / 2);
050: setLocation(x, y);
051:
052: componentFactory.setParentComponent(contentPanel);
053: btnClose = (XButton) componentFactory.addComponent(
054: XPage.BUTTON, 10, 370, 72, 20, "Close");
055: label = (XLabel) componentFactory.addComponent(XPage.LABEL,
056: 8, 8, 30, 20, msg, null);
057: addMouseHandler(btnClose, "closeDlg");
058:
059: int height = 200;
060: Graphics g = container.getGraphics();
061: FontMetrics fm = null;
062: try {
063: Font f = new Font(null, 0, 12);
064: fm = g.getFontMetrics(f);
065: } catch (Exception ex) {
066: Font f = new Font("arial", 0, 12);
067: fm = g.getFontMetrics(f);
068: }
069: int lines = Math.max(fm.stringWidth(msg) / 380,
070: countLines(msg));
071: height = fm.getHeight() * (2 + lines);
072: g.dispose();
073: g = null;
074: setSize(400, height + 44);
075:
076: if (BuildProperties.BUILD_JDK_118)
077: showDialog(container, null);
078: else {
079: final Container myContainer = container;
080: SwingUtilities.invokeLater(new Runnable() {
081: public void run() {
082: showDialog(myContainer, null);
083: }
084: });
085: }
086: } catch (Exception ex) {
087: ex.printStackTrace();
088: }
089: }
090:
091: private int countLines(String msg) {
092: int pos, count = 0;
093:
094: pos = msg.indexOf(10);
095: while (pos > -1) {
096: count++;
097: pos = msg.indexOf(10, pos + 1);
098: }
099: return count;
100: }
101:
102: /**
103: * Close the dialog.
104: */
105: public void closeDlg() {
106: if (wasMouseClicked()) {
107: returnValue = 1;
108: super .closeDlg();
109: }
110: }
111:
112: /**
113: * Adjust the size and locatio of the close image and the message label.
114: * @param width The new width of the dialog.
115: * @param height The new height of the dialog.
116: */
117: public void setSize(int width, int height) {
118: btnClose.setLocation(width - btnClose.getSize().width - 8,
119: height - btnClose.getSize().height - 8);
120: label.setSize(width - 16, height - btnClose.getSize().height
121: - 24);
122: super.setSize(width, height);
123: }
124: }
|