001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.wings.plaf.DialogCG;
018: import org.wings.plaf.FrameCG;
019:
020: /**
021: * Top-level window with a title and a border that is typically used to take
022: * some form of input from the user. <p/> As opposed to Swing, wingS dialogs are
023: * non modal. However, the dismission of the dialog is propagated by means of
024: * ActionEvents. The action command of the event tells, what kind of user
025: * activity caused the dialog to dismiss.
026: *
027: * @author <a href="mailto:engels@mercatis.de">Holger Engels</a>
028: * @author <a href="mailto:Roman.Raedle@uni-konstanz.de">Roman Rädle</a>
029: */
030: public class SDialog extends SWindow {
031: private final transient static Log log = LogFactory
032: .getLog(SDialog.class);
033:
034: /**
035: * Action command if dialog window was closed
036: */
037: public static final String CLOSE_ACTION = "CLOSE";
038:
039: /**
040: * Action command if user hit return
041: */
042: public static final String DEFAULT_ACTION = "DEFAULT";
043:
044: /**
045: * The Title of the Dialog Frame
046: */
047: protected String title;
048:
049: protected SIcon icon = null;
050:
051: protected boolean modal = false;
052:
053: protected boolean draggable = true;
054:
055: protected int x = -1;
056: protected int y = -1;
057:
058: private boolean closable = true;
059: private boolean closed = false;
060:
061: /**
062: * Creates a Dialog without parent <code>SFrame</code> or
063: * <code>SDialog</code> and without Title
064: */
065: public SDialog() {
066: }
067:
068: /**
069: * Creates a dialog with the specifed parent <code>SFrame</code> as its
070: * owner.
071: *
072: * @param owner
073: * the parent <code>SFrame</code>
074: */
075: public SDialog(SFrame owner) {
076: this (owner, null, false);
077: }
078:
079: /**
080: * Creates a modal or non-modal dialog without a title and with the
081: * specified owner <code>Frame</code>. If <code>owner</code> is
082: * <code>null</code>, a shared, hidden frame will be set as the owner of
083: * the dialog.
084: * <p>
085: * This constructor sets the component's locale property to the value
086: * returned by <code>JComponent.getDefaultLocale</code>.
087: *
088: * @param owner
089: * the <code>Frame</code> from which the dialog is displayed
090: * @param modal
091: * true for a modal dialog, false for one that allows others
092: * windows to be active at the same time returns true.
093: */
094: public SDialog(SFrame owner, boolean modal) {
095: this (owner, null, modal);
096: }
097:
098: /**
099: * Creates a dialog with the specified title and the specified owner frame.
100: *
101: * @param owner
102: * the parent <code>SFrame</code>
103: * @param title
104: * the <code>String</code> to display as titke
105: */
106: public SDialog(SFrame owner, String title) {
107: this (owner, title, false);
108: }
109:
110: /**
111: * Creates a modal or non-modal dialog with the specified title and the
112: * specified owner <code>Frame</code>. If <code>owner</code> is
113: * <code>null</code>, a shared, hidden frame will be set as the owner of
114: * this dialog. All constructors defer to this one.
115: * <p>
116: * NOTE: Any popup components (<code>JComboBox</code>,
117: * <code>JPopupMenu</code>, <code>JMenuBar</code>) created within a
118: * modal dialog will be forced to be lightweight.
119: * <p>
120: * This constructor sets the component's locale property to the value
121: * returned by <code>JComponent.getDefaultLocale</code>.
122: *
123: * @param owner
124: * the <code>Frame</code> from which the dialog is displayed
125: * @param title
126: * the <code>String</code> to display in the dialog's title bar
127: * @param modal
128: * true for a modal dialog, false for one that allows other
129: * windows to be active at the same time returns true.
130: */
131: public SDialog(SFrame owner, String title, boolean modal) {
132: this .owner = owner;
133: this .title = title;
134: this .modal = modal;
135: }
136:
137: public int getX() {
138: return x;
139: }
140:
141: public void setX(int x) {
142: int oldX = x;
143: this .x = x;
144: if (oldX != x)
145: reload();
146: }
147:
148: public int getY() {
149: return y;
150: }
151:
152: public void setY(int y) {
153: int oldY = y;
154: this .y = y;
155: if (oldY != y)
156: reload();
157: }
158:
159: /**
160: * Sets the title of the dialog.
161: *
162: * @param t
163: * the title displayed in the dialog's border; a null value
164: * results in an empty title
165: */
166: public void setTitle(String t) {
167: String oldTitle = title;
168: title = t;
169: if ((title == null && oldTitle != null)
170: || (title != null && !title.equals(oldTitle)))
171: reload();
172: }
173:
174: /**
175: * Gets the title of the dialog. The title is displayed in the dialog's
176: * border.
177: *
178: * @return the title of this dialog window. The title may be null.
179: */
180: public String getTitle() {
181: return title;
182: }
183:
184: public void setIcon(SIcon i) {
185: if (i != icon || i != null && !i.equals(icon)) {
186: icon = i;
187: reload();
188: }
189: }
190:
191: public SIcon getIcon() {
192: return icon;
193: }
194:
195: public boolean isModal() {
196: return modal;
197: }
198:
199: public void setModal(boolean modal) {
200: this .modal = modal;
201: }
202:
203: public boolean isDraggable() {
204: return draggable;
205: }
206:
207: public void setDraggable(boolean draggable) {
208: this .draggable = draggable;
209: }
210:
211: public void hide() {
212: super .hide();
213: }
214:
215: public void setClosable(boolean v) {
216: boolean old = closable;
217: closable = v;
218: if (old != closable)
219: reload();
220: }
221:
222: public boolean isClosable() {
223: return closable;
224: }
225:
226: // public void setClosed(boolean v) {
227: // v &= isClosable();
228: // boolean old = closed;
229: // closed = v;
230: // if (old != closed)
231: // reload();
232: // }
233: //
234: // public boolean isClosed() {
235: // return closed;
236: // }
237:
238: // LowLevelEventListener interface. Handle own events.
239: public void processLowLevelEvent(String action, String[] values) {
240: processKeyEvents(values);
241: if (action.endsWith("_keystroke"))
242: return;
243:
244: if (action.indexOf("_") > -1) {
245: String[] actions = action.split("_");
246: if ("xy".equals(actions[1])) {
247: String[] coords = values[0].split(",");
248:
249: x = Integer.parseInt(coords[0]);
250: y = Integer.parseInt(coords[1]);
251: return;
252: }
253: }
254:
255: // is this a window event?
256: try {
257: switch (new Integer(values[0]).intValue()) {
258: case org.wings.event.SInternalFrameEvent.INTERNAL_FRAME_CLOSED:
259: hide();
260: actionCommand = CLOSE_ACTION;
261: break;
262: default:
263: // form event
264: actionCommand = DEFAULT_ACTION;
265: }
266: } catch (NumberFormatException ex) {
267: // no window event...
268: }
269: SForm.addArmedComponent(this ); // trigger later invocation of fire*()
270: }
271:
272: public void setCG(DialogCG cg) {
273: super.setCG(cg);
274: }
275: }
|