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.widgets;
023:
024: import java.awt.BorderLayout;
025: import java.awt.Component;
026: import java.awt.Dimension;
027: import java.awt.Point;
028: import java.awt.event.WindowAdapter;
029: import java.awt.event.WindowEvent;
030: import java.util.HashMap;
031: import java.util.Iterator;
032:
033: import javax.swing.BorderFactory;
034: import javax.swing.JButton;
035: import javax.swing.JDialog;
036: import javax.swing.JFrame;
037: import javax.swing.JPanel;
038: import javax.swing.border.Border;
039:
040: import org.beryl.gui.GUIEvent;
041: import org.beryl.gui.GUIEventListener;
042: import org.beryl.gui.GUIException;
043: import org.beryl.gui.GUIUtils;
044: import org.beryl.gui.Widget;
045: import org.beryl.gui.WidgetInfo;
046:
047: public class Dialog extends Widget {
048: protected static WidgetInfo dialogInfo = null;
049: private static final Border windowBorder = BorderFactory
050: .createEmptyBorder(WINDOW_BORDER_SPACING,
051: WINDOW_BORDER_SPACING, WINDOW_BORDER_SPACING,
052: WINDOW_BORDER_SPACING);
053: private JDialog dialog = null;
054: private Panel panel = null;
055: private Button defaultButton = null;
056: private JButton jDefaultButton = null;
057: private boolean isModal = true;
058: private String title = null;
059: private HashMap closeListeners = null;
060: private HashMap openListeners = null;
061: private Point location = null;
062: private String helpid = null;
063: private boolean resizable = true;
064:
065: static {
066: dialogInfo = new WidgetInfo(Dialog.class, widgetInfo);
067: dialogInfo.addProperty("border", "border", BorderFactory
068: .createEmptyBorder());
069: dialogInfo.addProperty("defaultCloseOperation", "enum",
070: new Integer(JFrame.DISPOSE_ON_CLOSE));
071: dialogInfo.addProperty("layout", "layout", new BorderLayout());
072: dialogInfo
073: .addProperty("location", "point", new Point(100, 100));
074: dialogInfo.addProperty("resizable", "bool", Boolean.TRUE);
075: dialogInfo.addProperty("size", "dimension", new Dimension(100,
076: 100));
077: dialogInfo.addProperty("spacing", "int", new Integer(
078: WINDOW_BORDER_SPACING));
079: dialogInfo.addProperty("title", "istring", "");
080: dialogInfo.addEvent("close");
081: dialogInfo.addEvent("open");
082: dialogInfo.removeProperty("enabled");
083: dialogInfo.setSupportsAnchor(false);
084: };
085:
086: public Dialog(Widget parent, String name) throws GUIException {
087: super (parent, name);
088: panel = new Panel(this , null);
089: closeListeners = new HashMap();
090: openListeners = new HashMap();
091: ((JPanel) panel.getWidget()).setBorder(windowBorder);
092: GUIUtils.register(this );
093: addChild(panel);
094: }
095:
096: public Object getProperty(String name) throws GUIException {
097: if (name.equals("default")) {
098: return defaultButton;
099: } else {
100: return super .getProperty(name);
101: }
102: }
103:
104: public void setProperty(String name, Object value)
105: throws GUIException {
106: if ("layout".equals(name) || "border".equals(name)
107: || "spacing".equals(name) || "background".equals(name)) {
108: panel.setProperty(name, value);
109: } else if ("size".equals(name)) {
110: /* Workaround: This makes the content and not the window have the
111: * requested size. Therefore the window content and the window decoration
112: * will be bigger than they would be using the normal setSize() */
113: ((JPanel) panel.getRealWidget())
114: .setPreferredSize((Dimension) value);
115: if (dialog != null)
116: dialog.pack();
117: } else if ("helpid".equals(name)) {
118: helpid = (String) value;
119: } else if ("modal".equals(name)) {
120: isModal = ((Boolean) value).booleanValue();
121: } else if ("visible".equals(name)) {
122: throw new GUIException(
123: "Visible is not a settable property, use Dialog's 'show' method instead");
124: } else if ("location".equals(name)) {
125: location = (Point) value;
126: if (dialog != null)
127: dialog.setLocation((Point) value);
128: } else if ("resizable".equals(name)) {
129: resizable = ((Boolean) value).booleanValue();
130: if (dialog != null)
131: dialog.setResizable(resizable);
132: } else if ("default".equals(name)) {
133: defaultButton = (Button) value;
134: jDefaultButton = (JButton) defaultButton.getRealWidget();
135: if (dialog != null)
136: dialog.getRootPane().setDefaultButton(jDefaultButton);
137: } else if ("title".equals(name)) {
138: title = (String) value;
139: if (dialog != null)
140: dialog.setTitle(title);
141: } else {
142: if (dialog == null)
143: throw new GUIException("Property '" + name
144: + "' is not yet supported by Dialog");
145: super .setProperty(name, value);
146: }
147: }
148:
149: protected void finalize() {
150: GUIUtils.unregister(this );
151: }
152:
153: public void addChild(Widget widget, Object constraint)
154: throws GUIException {
155: panel.addChild(widget, constraint);
156: }
157:
158: public void removeChildWidget(Widget widget) throws GUIException {
159: panel.removeChildWidget(widget);
160: }
161:
162: public void addListener(String event, String name,
163: GUIEventListener listener) throws GUIException {
164: if ("close".equals(event)) {
165: closeListeners.put(name, listener);
166: } else if ("open".equals(event)) {
167: openListeners.put(name, listener);
168: } else {
169: super .addListener(event, name, listener);
170: }
171: }
172:
173: public void hide() {
174: dialog.hide();
175: }
176:
177: public void initDialog(Widget parent) throws GUIException {
178: if (parent == null)
179: dialog = new JDialog(); // this is perfectly valid
180: else if (parent instanceof Dialog)
181: dialog = new JDialog((JDialog) parent.getRealWidget(),
182: isModal);
183: else if (parent instanceof Frame)
184: dialog = new JDialog((JFrame) parent.getRealWidget(),
185: isModal);
186: else if (parent instanceof Wizard)
187: dialog = new JDialog((JFrame) parent.getRealWidget(),
188: isModal);
189: else
190: throw new GUIException(
191: "Invalid parent (no Dialog/Frame/Wizard/null)");
192: }
193:
194: public void show() throws GUIException {
195: if (dialog != null) {
196: JPanel jpanel = (JPanel) panel.getWidget();
197: dialog.getContentPane().add(jpanel);
198: if (jDefaultButton != null)
199: dialog.getRootPane().setDefaultButton(jDefaultButton);
200: if (title != null)
201: dialog.setTitle(title);
202: dialog.pack();
203:
204: /* Workaround for Bug 4102292
205: * http://developer.java.sun.com/developer/bugParade/bugs/4102292.html
206: */
207: if (location == null) {
208: Dimension ss = dialog.getToolkit().getScreenSize();
209: Dimension fs = dialog.getSize();
210: dialog.setLocation((ss.width - fs.width) / 2,
211: (ss.height - fs.height) / 2);
212: } else {
213: dialog.setLocation(location);
214: }
215: dialog.setResizable(resizable);
216:
217: for (Iterator i = closeListeners.keySet().iterator(); i
218: .hasNext();) {
219: final String name = (String) i.next();
220: final GUIEventListener listener = (GUIEventListener) closeListeners
221: .get(name);
222: dialog.addWindowListener(new WindowAdapter() {
223: public void windowClosing(WindowEvent e) {
224: listener.eventOccured(new GUIEvent(Dialog.this ,
225: name, e));
226: }
227: });
228: }
229: for (Iterator i = openListeners.keySet().iterator(); i
230: .hasNext();) {
231: final String name = (String) i.next();
232: final GUIEventListener listener = (GUIEventListener) openListeners
233: .get(name);
234: dialog.addWindowListener(new WindowAdapter() {
235: public void windowOpened(WindowEvent e) {
236: listener.eventOccured(new GUIEvent(Dialog.this ,
237: name, e));
238: }
239: });
240: }
241: } else {
242: throw new GUIException("Initialize dialog first!");
243: }
244: if (helpid != null) {
245: if (helpBroker == null)
246: throw new GUIException(
247: "JavaHelp has not been activated");
248: if (helpid != null)
249: helpBroker.enableHelpKey(dialog.getRootPane(),
250: (String) helpid, null);
251: }
252:
253: dialog.show();
254: }
255:
256: /**
257: * Set the initial focus of the dialog
258: */
259: public void setInitialFocus(final Widget widget)
260: throws GUIException {
261: if (dialog == null)
262: throw new GUIException(
263: "The dialog has not yet been created. Call setInitialFocus() after initDialog()");
264: dialog.addWindowListener(new WindowAdapter() {
265: public void windowOpened(WindowEvent e) {
266: widget.requestFocus();
267: e.getWindow().removeWindowListener(this );
268: }
269: });
270: }
271:
272: public Widget getChild(int index) {
273: return (Widget) panel.getChild(index);
274: }
275:
276: public int getChildIndex(Widget child) {
277: return panel.getChildIndex(child);
278: }
279:
280: public int getChildCount() {
281: return panel.getChildCount();
282: }
283:
284: public void dispose() {
285: dialog.dispose();
286: }
287:
288: public void revalidate() throws GUIException {
289: panel.revalidate();
290: }
291:
292: public Panel getPanel() {
293: return panel;
294: }
295:
296: public Component getWidget() {
297: return dialog;
298: }
299:
300: public WidgetInfo getWidgetInfo() {
301: return dialogInfo;
302: }
303: }
|