source: trunk/gli/src/org/greenstone/gatherer/gui/ModalDialog.java@ 7314

Last change on this file since 7314 was 7314, checked in by kjdon, 20 years ago

added veronikas changes for the feedback stuff

  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1/*
2 * ModalDialog
3 *
4 * GLI 2.3c
5 *
6 * 30May2003
7 *
8 *#########################################################################
9 *
10 * A component of the Gatherer application, part of the Greenstone digital
11 * library suite from the New Zealand Digital Library Project at the
12 * University of Waikato, New Zealand.
13 *
14 * Author: John Thompson, Greenstone Digital Library, University of Waikato
15 *
16 * Copyright (C) 1999 New Zealand Digital Library Project
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 *########################################################################
32 */
33package org.greenstone.gatherer.gui;
34
35import java.awt.*;
36import javax.swing.*;
37import org.greenstone.gatherer.Gatherer;
38/** An extension of the JDialog that overrides the JVM's typical modal behaviour. This typical behaviour is that when a modal dialog is opened, all other windows cease to respond to user events until the modal dialog is disposed. However this prevents us opening the help documents property whenever a modal dialog is open. Thus we override the modal behaviour so that only the owner frame or dialog is blocked.
39 * Note that because we always call the super constructor with modal set to false, this should be made visible with setVisible(true) rather than show() which will return straight away. */
40// feedback note: veronika had changed all the super constructor calls to
41// use modal instead of false - not sure if this is needed so I have not
42// put that in. --kjdon
43public class ModalDialog
44 extends JDialog {
45 /** true if this dialog should be modal, ie block user actions to its owner window, false otherwise. */
46 protected boolean modal = false;
47 /** true if this dialog is currently waiting some thread. */
48 protected boolean waiting = false;
49
50 /** Constructor.
51 */
52 public ModalDialog() {
53 super((Frame)null, "", false);
54 }
55
56 /** Constructor.
57 * @param parent the Dialog which is the owener of this dialog.
58 */
59 public ModalDialog(Dialog parent) {
60 super(parent, "", false);
61 }
62
63 /** Constructor.
64 * @param parent the Dialog which is the owener of this dialog.
65 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
66 */
67 public ModalDialog(Dialog parent, boolean modal) {
68 super(parent, "", false);
69 this.modal = modal;
70 }
71
72 /** Constructor.
73 * @param parent the Dialog which is the owner of this dialog.
74 * @param title the String to use as the title for this dialog.
75 */
76 public ModalDialog(Dialog parent, String title) {
77 super (parent, title, false);
78 this.modal = false;
79 }
80
81 /** Constructor.
82 * @param parent the Dialog which is the owener of this dialog.
83 * @param title the String to use as the title for this dialog.
84 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
85 */
86 public ModalDialog(Dialog parent, String title, boolean modal) {
87 super (parent, title, false);
88 this.modal = modal;
89 }
90
91 /** Constructor.
92 * @param parent the Frame which is the owener of this dialog.
93 */
94 public ModalDialog(Frame parent) {
95 super(parent, "", false);
96 }
97
98 /** Constructor.
99 * @param parent the Frame which is the owener of this dialog.
100 * @param modal whether this dialog is modal or not
101 */
102 public ModalDialog(Frame parent, boolean modal) {
103 super(parent, "", false);
104 this.modal = modal;
105 }
106
107 /** Constructor.
108 * @param parent the Frame which is the owner of this dialog.
109 * @param title the String to use as the title for this dialog.
110 */
111 public ModalDialog(Frame parent, String title) {
112 super (parent, title, false);
113 }
114
115 /** Constructor.
116 * @param parent the Frame which is the owener of this dialog.
117 * @param title the String to use as the title for this dialog.
118 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
119 */
120 public ModalDialog(Frame parent, String title, boolean modal) {
121 super (parent, title, false);
122 this.modal = modal;
123 }
124
125 public void dispose() {
126 super.dispose();
127 }
128
129 /** Ensures the current dialog is visible. */
130 public void makeVisible() {
131 super.setVisible(true);
132 }
133
134 /** The set visible method is overriden to provide modal functionality. It essentially hijacks control of the event dispatch thread while the dialog is open, only allowing non-user events to be passed to the parent dialog. Furthermore it only has this effect within the current AWT component tree by utilitizing the TreeLock.
135 * @param visible true if this dialog should be painted on-screen, false otherwise.
136 */
137 public void setVisible (boolean visible) {
138 if(visible) {
139 Gatherer.current_modal = this;
140 }
141 else {
142 Gatherer.current_modal = null;
143 }
144 // If we are in the AWT Dispatch thread then it is all good.
145 if (SwingUtilities.isEventDispatchThread ()) {
146 getParent ().setEnabled (!(visible && modal));
147 super.setVisible (visible);
148 if (modal && visible) {
149 ///ystem.err.println("is Event Dispatch Thread. Only process certain events.");
150 EventQueue theQueue = getToolkit().getSystemEventQueue();
151 while (isVisible ()) {
152 try {
153 AWTEvent event = theQueue.getNextEvent();
154 Object src = event.getSource ();
155 if (event instanceof ActiveEvent) {
156 ///ystem.err.println("ActiveEvent:");
157 ((ActiveEvent) event).dispatch ();
158 } else if (src instanceof Component) {
159 ///ystem.err.println("Source is component");
160 ((Component) src).dispatchEvent (event);
161 }
162 else {
163 ///ystem.err.println("Event blocked");
164 }
165 }
166 catch(Exception exception) {
167 Gatherer.printStackTrace(exception);
168 }
169 }
170 }
171 }
172 else {
173 try {
174 SwingUtilities.invokeAndWait(new MakeDialogVisibleTask(this, visible));
175 }
176 catch(Exception exception) {
177 Gatherer.printStackTrace(exception);
178 }
179 }
180 }
181
182 private class MakeDialogVisibleTask
183 implements Runnable {
184 private boolean make_visible;
185 private ModalDialog dialog;
186 public MakeDialogVisibleTask(ModalDialog dialog, boolean make_visible) {
187 this.dialog = dialog;
188 this.make_visible = make_visible;
189 }
190 public void run() {
191 // Blocks until the user dismisses the dialog
192 dialog.setVisible(make_visible);
193 }
194 }
195
196 /** Overridden method so we can control modality and not rely on the Dialog default.
197 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
198 */
199 public void setModal (boolean modal) {
200 this.modal = modal;
201 }
202}
Note: See TracBrowser for help on using the repository browser.