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:
031: import javax.swing.BorderFactory;
032: import javax.swing.ImageIcon;
033: import javax.swing.JButton;
034: import javax.swing.JFrame;
035: import javax.swing.JMenuBar;
036: import javax.swing.JPanel;
037: import javax.swing.border.Border;
038:
039: import org.beryl.gui.GUIEvent;
040: import org.beryl.gui.GUIEventListener;
041: import org.beryl.gui.GUIException;
042: import org.beryl.gui.GUIUtils;
043: import org.beryl.gui.ImageIconFactory;
044: import org.beryl.gui.Widget;
045: import org.beryl.gui.WidgetInfo;
046:
047: /**
048: * Frame class
049: *
050: * Has an 11px border by default. Can be overwritten
051: * using 'spacing'
052: */
053:
054: public class Frame extends Widget {
055: private static final Border windowBorder = BorderFactory
056: .createEmptyBorder(WINDOW_BORDER_SPACING,
057: WINDOW_BORDER_SPACING, WINDOW_BORDER_SPACING,
058: WINDOW_BORDER_SPACING);
059: protected static WidgetInfo frameInfo = null;
060: private JFrame frame = null;
061: private Panel panel = null;
062: private Button defaultButton = null;
063: private boolean visible = false;
064: private boolean locationHasBeenSet = false;
065: private boolean isConstructed = false;
066:
067: static {
068: frameInfo = new WidgetInfo(Frame.class, widgetInfo);
069: frameInfo.addProperty("border", "border", BorderFactory
070: .createEmptyBorder());
071: frameInfo.addProperty("defaultCloseOperation", "enum",
072: new Integer(JFrame.DISPOSE_ON_CLOSE));
073: frameInfo.addProperty("layout", "layout", new BorderLayout());
074: frameInfo.addProperty("location", "point", new Point(100, 100));
075: frameInfo.addProperty("resizable", "bool", Boolean.TRUE);
076: frameInfo.addProperty("spacing", "int", new Integer(
077: WINDOW_BORDER_SPACING));
078: try {
079: frameInfo.addProperty("iconImage", "icon", ImageIconFactory
080: .getIcon("broken"));
081: } catch (Exception e) {
082: throw new RuntimeException(e);
083: }
084: frameInfo.addProperty("size", "dimension", new Dimension(100,
085: 100));
086: frameInfo.addProperty("title", "istring", "");
087: frameInfo.addProperty("visible", "bool", Boolean.FALSE);
088: frameInfo.addEvent("close");
089: frameInfo.removeProperty("enabled");
090: frameInfo.setSupportsAnchor(false);
091: };
092:
093: public Frame(Widget parent, String name) throws GUIException {
094: super (parent, name);
095: frame = new JFrame();
096: panel = new Panel(this , null);
097: addChild(panel);
098: JPanel jpanel = (JPanel) panel.getWidget();
099: jpanel.setBorder(windowBorder);
100: frame.getContentPane().add(jpanel);
101: GUIUtils.register(this );
102: }
103:
104: public Object getProperty(String name) throws GUIException {
105: if (name.equals("default")) {
106: return defaultButton;
107: } else {
108: return super .getProperty(name);
109: }
110: }
111:
112: public void setProperty(String name, Object value)
113: throws GUIException {
114: if ("layout".equals(name) || "border".equals(name)
115: || "spacing".equals(name) || "background".equals(name)) {
116: panel.setProperty(name, value);
117: } else if ("helpid".equals(name)) {
118: if (helpBroker == null)
119: throw new GUIException(
120: "JavaHelp has not been activated");
121: if (value != null)
122: helpBroker.enableHelpKey(frame.getRootPane(),
123: (String) value, null);
124: } else if ("iconImage".equals(name)) {
125: frame.setIconImage(((ImageIcon) value).getImage());
126: } else if ("size".equals(name)) {
127: /* Workaround: This makes the content and not the window have the
128: * requested size. Therefore the window content and the window decoration
129: * will be bigger than they would be using the normal setSize() */
130: ((JPanel) panel.getRealWidget())
131: .setPreferredSize((Dimension) value);
132: frame.pack();
133: } else if ("realSize".equals(name)) {
134: frame.setSize((Dimension) value);
135: frame.show();
136: } else if ("location".equals(name)) {
137: frame.setLocation((Point) value);
138: locationHasBeenSet = true;
139: } else if ("default".equals(name)) {
140: defaultButton = (Button) value;
141:
142: JButton jbutton = null;
143: if (value != null) {
144: jbutton = (JButton) ((Button) value).getRealWidget();
145: }
146: frame.getRootPane().setDefaultButton(jbutton);
147: } else if ("visible".equals(name)) {
148: visible = ((Boolean) value).booleanValue();
149:
150: if (isConstructed)
151: frame.setVisible(visible);
152: } else {
153: super .setProperty(name, value);
154: }
155: }
156:
157: public Widget getChild(int index) {
158: return (Widget) panel.getChild(index);
159: }
160:
161: public int getChildIndex(Widget child) {
162: return panel.getChildIndex(child);
163: }
164:
165: public int getChildCount() {
166: return panel.getChildCount();
167: }
168:
169: protected void finalize() {
170: GUIUtils.unregister(this );
171: }
172:
173: public void addListener(String event, final String name,
174: final GUIEventListener listener) throws GUIException {
175: if ("close".equals(event)) {
176: frame.addWindowListener(new WindowAdapter() {
177: public void windowClosing(WindowEvent e) {
178: listener.eventOccured(new GUIEvent(Frame.this ,
179: name, e));
180: }
181: });
182: } else {
183: super .addListener(event, name, listener);
184: }
185: }
186:
187: public void addChild(Widget widget, Object constraint)
188: throws GUIException {
189: if (widget instanceof MenuBar) {
190: frame.setJMenuBar((JMenuBar) ((MenuBar) widget)
191: .getRealWidget());
192: panel.addChild(widget);
193: } else {
194: panel.addChild(widget, constraint);
195: }
196: }
197:
198: public void removeChildWidget(Widget widget) throws GUIException {
199: if (widget instanceof MenuBar) {
200: frame.setJMenuBar(null);
201: super .removeChildWidget(widget);
202: } else {
203: panel.removeChildWidget(widget);
204: }
205: }
206:
207: public void hide() {
208: frame.hide();
209: }
210:
211: public void show() {
212: /* Workaround for Bug 4102292
213: * http://developer.java.sun.com/developer/bugParade/bugs/4102292.html
214: */
215: if (!locationHasBeenSet) {
216: Dimension ss = frame.getToolkit().getScreenSize();
217: Dimension fs = frame.getSize();
218: frame.setLocation((ss.width - fs.width) / 2,
219: (ss.height - fs.height) / 2);
220: }
221: frame.show();
222: }
223:
224: /**
225: * Set the initial focus of the frame
226: */
227: public void setInitialFocus(final Widget widget) {
228: frame.addWindowListener(new WindowAdapter() {
229: public void windowOpened(WindowEvent e) {
230: widget.requestFocus();
231: e.getWindow().removeWindowListener(this );
232: }
233: });
234: }
235:
236: public void dispose() {
237: frame.dispose();
238: }
239:
240: public void finalizeConstruction() throws GUIException {
241: isConstructed = true;
242: if (visible)
243: show();
244: }
245:
246: public void revalidate() throws GUIException {
247: panel.revalidate();
248: }
249:
250: public Panel getPanel() {
251: return panel;
252: }
253:
254: public boolean isVisible() {
255: return frame.isVisible();
256: }
257:
258: public Component getWidget() {
259: return frame;
260: }
261:
262: public WidgetInfo getWidgetInfo() {
263: return frameInfo;
264: }
265: }
|