KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SDialog


1 /*
2  * $Id: SDialog.java,v 1.11 2005/01/31 10:59:51 oliverscheck Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.wings.plaf.DialogCG;
19 import org.wings.session.SessionManager;
20
21 /**
22  * As opposed to Swing, wingS dialogs are non modal. However, the dismission of
23  * the dialog is propagated by means of ActionEvents. The action command of the
24  * event tells, what kind of user activity caused the dialog to dismiss.
25  *
26  * @author <a HREF="mailto:engels@mercatis.de">Holger Engels</a>
27  * @version $Revision: 1.11 $
28  */

29 public class SDialog extends SForm {
30     private final transient static Log log = LogFactory.getLog(SDialog.class);
31
32     /**
33      * Action command if dialog window was closed
34      */

35     public static final String JavaDoc CLOSE_ACTION = "CLOSE";
36
37     /**
38      * Action command if user hit return
39      */

40     public static final String JavaDoc DEFAULT_ACTION = "DEFAULT";
41
42     /**
43      * The Title of the Dialog Frame
44      */

45     protected String JavaDoc title;
46
47     protected SIcon icon = null;
48
49     private boolean closable = true;
50     private boolean closed = false;
51
52     /**
53      * The parent of the Dialog
54      */

55     protected SRootContainer owner = null;
56
57
58     /**
59      * Creates a Dialog without parent <code>SFrame</code> or <code>SDialog</code>
60      * and without Title
61      */

62     public SDialog() {
63     }
64
65
66     /**
67      * Creates a dialog with the specifed parent <code>SFrame</code> as its owner.
68      *
69      * @param owner the parent <code>SFrame</code>
70      */

71     public SDialog(SFrame owner) {
72         this.owner = owner;
73     }
74
75     /**
76      * Creates a dialog with the specified title and the specified owner frame.
77      *
78      * @param owner the parent <code>SFrame</code>
79      * @param title the <code>String</code> to display as titke
80      */

81     public SDialog(SFrame owner, String JavaDoc title) {
82         this.owner = owner;
83         this.title = title;
84     }
85
86     public void setTitle(String JavaDoc t) {
87         String JavaDoc oldTitle = title;
88         title = t;
89         if ((title == null && oldTitle != null) ||
90                 (title != null && !title.equals(oldTitle)))
91             reload();
92     }
93
94     public String JavaDoc getTitle() {
95         return title;
96     }
97
98
99     public void setIcon(SIcon i) {
100         if (i != icon || i != null && !i.equals(icon)) {
101             icon = i;
102             reload();
103         }
104     }
105
106
107     public SIcon getIcon() {
108         return icon;
109     }
110
111     public void setClosable(boolean v) {
112         boolean old = closable;
113         closable = v;
114         if (old != closable)
115             reload();
116     }
117
118     public boolean isClosable() {
119         return closable;
120     }
121
122     public void setClosed(boolean v) {
123         v &= isClosable();
124         boolean old = closed;
125         closed = v;
126         if (old != closed)
127             reload();
128     }
129
130     public boolean isClosed() {
131         return closed;
132     }
133
134     /**
135      * Removes all <code>SComponents</code> from the pane
136      */

137       public void dispose() {
138         if (visible)
139             hide();
140         removeAll();
141       }
142
143     /**
144      * Remove this dialog from its frame.
145      */

146     public void hide() {
147         log.debug("hide dialog");
148         if (owner != null)
149             owner.popDialog();
150         visible = false;
151     }
152
153     public void setVisible(boolean visible) {
154         if (visible) {
155             if (owner != null) show(owner);
156         } else {
157             if (isVisible()) hide();
158         }
159         super.setVisible(visible);
160     }
161
162     /**
163      * sets the root container in which this dialog is to be displayed.
164      */

165     protected void setFrame(SRootContainer f) {
166         owner = f;
167     }
168
169     /**
170      * shows this dialog in the given SRootContainer. If the component is
171      * not a root container, then the root container the component is in
172      * is used.
173      * If the component is null, the root frame of the session is used. If there
174      * is no root frame in the session, a NullPointerException is thrown (this
175      * should not happen ;-).
176      */

177     public void show(SComponent c) {
178         log.debug("show dialog");
179         if (c == null)
180             c = SessionManager.getSession().getRootFrame();
181
182         SContainer frame = null;
183         if (c instanceof SContainer)
184             frame = (SContainer) c;
185         else
186             frame = c.getParent();
187
188         // find RootContainer
189
while (frame != null && !(frame instanceof SRootContainer)) {
190             frame = frame.getParent();
191         }
192
193         if (frame == null) {
194             frame = SessionManager.getSession().getRootFrame();
195         }
196
197         if (frame == null) {
198             throw new IllegalArgumentException JavaDoc("Component has no root container");
199         }
200         owner = (SRootContainer) frame;
201         owner.pushDialog(this);
202     }
203
204     // LowLevelEventListener interface. Handle own events.
205
public void processLowLevelEvent(String JavaDoc action, String JavaDoc[] values) {
206         processKeyEvents(values);
207
208         // is this a window event?
209
try {
210             switch (new Integer JavaDoc(values[0]).intValue()) {
211                 case org.wings.event.SInternalFrameEvent.INTERNAL_FRAME_CLOSED:
212                     setClosed(true);
213                     actionCommand = CLOSE_ACTION;
214                     break;
215
216                 default:
217                     // form event
218
actionCommand = DEFAULT_ACTION;
219             }
220         } catch (NumberFormatException JavaDoc ex) {
221             // no window event...
222
}
223         SForm.addArmedComponent(this); // trigger later invocation of fire*()
224
}
225
226     public void fireFinalEvents() {
227     }
228
229     public void setCG(DialogCG cg) {
230         super.setCG(cg);
231     }
232 }
233
Popular Tags