001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui;
023:
024: import java.awt.Dimension;
025:
026: import javax.swing.ImageIcon;
027:
028: import org.beryl.gui.model.MapDataModel;
029: import org.beryl.gui.widgets.Button;
030: import org.beryl.gui.widgets.Dialog;
031: import org.beryl.gui.widgets.Panel;
032: import org.beryl.gui.widgets.TextPane;
033:
034: /**
035: * General purpose message dialog
036: */
037: public class MessageDialog extends Controller {
038: private static ImageIcon less = null;
039: private static ImageIcon more = null;
040: private static ImageIcon error = null;
041: private static ImageIcon warning = null;
042: private static ImageIcon info = null;
043: private MapDataModel dataModel = null;
044: private Dialog dialog = null;
045: private Panel containerPanel = null;
046: private Button switchButton = null;
047: private Dimension size = null;
048: private Dimension bigSize = null;
049: private boolean expanded = false;
050:
051: /* Message types */
052: public static final int INFORMATION_MESSAGE = 1;
053: public static final int WARNING_MESSAGE = 2;
054: public static final int ERROR_MESSAGE = 3;
055:
056: public MessageDialog(Widget parent, String title, String message)
057: throws GUIException {
058: initialize(parent, INFORMATION_MESSAGE, title, message, null);
059: }
060:
061: public MessageDialog(Widget parent, int type, String title,
062: String message, String details) throws GUIException {
063: initialize(parent, type, title, message, details);
064: }
065:
066: public MessageDialog(Exception exception) {
067: this (null, exception);
068: }
069:
070: public MessageDialog(Widget parent, Exception exception) {
071: try {
072: log.error("An exception occured", exception);
073: String exceptionString = GUIUtils
074: .getStringForThrowable(exception);
075: initialize(parent, ERROR_MESSAGE,
076: getString("xmlgui.messagedialog.exception.title"),
077: getString("xmlgui.messagedialog.exception"),
078: exceptionString);
079: } catch (Exception exception2) {
080: log
081: .error(
082: "However, due to some strange error, the XML GUI could not create an error dialog - the exception keeping the XML GUI from displaying the exception was ",
083: exception2);
084: System.exit(-1);
085: }
086: }
087:
088: private void initialize(Widget parent, int type, String title,
089: String message, String details) throws GUIException {
090: dataModel = new MapDataModel();
091:
092: size = new Dimension(500, 100);
093: bigSize = new Dimension(500, 200);
094:
095: if (less == null) {
096: less = ImageIconFactory.getIcon("less");
097: more = ImageIconFactory.getIcon("more");
098: error = ImageIconFactory.getIcon("error");
099: warning = ImageIconFactory.getIcon("warning");
100: info = ImageIconFactory.getIcon("info");
101: }
102:
103: dialog = constructDialog("MessageDialog", dataModel);
104: TextPane textPane = (TextPane) dialog.getWidget("MessagePane");
105: switchButton = (Button) dialog.getWidget("SwitchButton");
106: containerPanel = (Panel) dialog.getWidget("ContainerPanel");
107:
108: if (details == null) {
109: switchButton.setEnabled(false);
110: } else {
111: TextPane detailPane = (TextPane) dialog
112: .getWidget("DetailPane");
113: detailPane.addText(details);
114: }
115:
116: switch (type) {
117: case INFORMATION_MESSAGE:
118: textPane.addIcon(info);
119: break;
120: case WARNING_MESSAGE:
121: textPane.addIcon(warning);
122: break;
123: case ERROR_MESSAGE:
124: textPane.addIcon(error);
125: break;
126: default:
127: throw new GUIException("Unknown message type");
128: }
129:
130: textPane.addText(" " + message);
131:
132: dialog.removeChildWidget(containerPanel);
133:
134: dialog.initDialog(parent);
135: dialog.setProperty("title", title);
136: dialog.setProperty("size", size);
137: dialog.show();
138: }
139:
140: public void eventOccured(GUIEvent event) {
141: try {
142: if (event.getName().equals("switch")) {
143: expanded = !expanded;
144:
145: switchButton.setProperty("text",
146: getString("xmlgui.messagedialog."
147: + (expanded ? "less" : "more")));
148: switchButton
149: .setProperty("icon", expanded ? less : more);
150:
151: if (expanded) {
152: dialog.addChild(containerPanel,
153: AnchorFactory.constraints.rc(2, 1));
154: dialog.setProperty("size", bigSize);
155: } else {
156: dialog.removeChildWidget(containerPanel);
157: dialog.setProperty("size", size);
158: }
159: dialog.revalidate();
160: ((javax.swing.JDialog) dialog.getWidget()).pack();
161: } else if (event.getName().equals("close")) {
162: dialog.dispose();
163: }
164: } catch (Exception e) {
165: log.error("Error while processing a message dialog event",
166: e);
167: }
168: }
169: }
|