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

Last change on this file since 6662 was 6662, checked in by jmt12, 20 years ago

Worked around stupid collection title clash warning dialog crashes spooty program on exit

  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 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. */
40public class ModalDialog
41 extends JDialog {
42 /** true if this dialog should be modal, ie block user actions to its owner window, false otherwise. */
43 protected boolean modal = false;
44 /** true if this dialog is currently waiting some thread. */
45 protected boolean waiting = false;
46
47 /** Constructor.
48 */
49 public ModalDialog() {
50 super((Frame)null, "", false);
51 }
52
53 /** Constructor.
54 * @param parent the Dialog which is the owener of this dialog.
55 */
56 public ModalDialog(Dialog parent) {
57 super(parent, "", false);
58 }
59
60 /** Constructor.
61 * @param parent the Dialog which is the owener of this dialog.
62 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
63 */
64 public ModalDialog(Dialog parent, boolean modal) {
65 super(parent, "", false);
66 this.modal = modal;
67 }
68
69 /** Constructor.
70 * @param parent the Dialog which is the owner of this dialog.
71 * @param title the String to use as the title for this dialog.
72 */
73 public ModalDialog(Dialog parent, String title) {
74 super (parent, title, false);
75 this.modal = false;
76 }
77
78 /** Constructor.
79 * @param parent the Dialog which is the owener of this dialog.
80 * @param title the String to use as the title for this dialog.
81 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
82 */
83 public ModalDialog(Dialog parent, String title, boolean modal) {
84 super (parent, title, false);
85 this.modal = modal;
86 }
87
88 /** Constructor.
89 * @param parent the Frame which is the owener of this dialog.
90 */
91 public ModalDialog(Frame parent) {
92 super(parent, "", false);
93 }
94
95 /** Constructor.
96 * @param parent the Frame which is the owener of this dialog.
97 * @param title the String to use as the title for this dialog.
98 */
99 public ModalDialog(Frame parent, boolean modal) {
100 super(parent, "", false);
101 this.modal = modal;
102 }
103
104 /** Constructor.
105 * @param parent the Frame which is the owner of this dialog.
106 * @param title the String to use as the title for this dialog.
107 */
108 public ModalDialog(Frame parent, String title) {
109 super (parent, title, false);
110 }
111
112 /** Constructor.
113 * @param parent the Frame which is the owener of this dialog.
114 * @param title the String to use as the title for this dialog.
115 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
116 */
117 public ModalDialog(Frame parent, String title, boolean modal) {
118 super (parent, title, false);
119 this.modal = modal;
120 }
121
122 public void dispose() {
123 super.dispose();
124 }
125
126 /** Ensures the current dialog is visible. */
127 public void makeVisible() {
128 super.setVisible(true);
129 }
130
131 /** 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.
132 * @param visible true if this dialog should be painted on-screen, false otherwise.
133 */
134 public void setVisible (boolean visible) {
135 if(visible) {
136 Gatherer.current_modal = this;
137 }
138 else {
139 Gatherer.current_modal = null;
140 }
141 // If we are in the AWT Dispatch thread then it is all good.
142 if (SwingUtilities.isEventDispatchThread ()) {
143 getParent ().setEnabled (!(visible && modal));
144 super.setVisible (visible);
145 if (modal && visible) {
146 ///ystem.err.println("is Event Dispatch Thread. Only process certain events.");
147 EventQueue theQueue = getToolkit().getSystemEventQueue();
148 while (isVisible ()) {
149 try {
150 AWTEvent event = theQueue.getNextEvent();
151 Object src = event.getSource ();
152 if (event instanceof ActiveEvent) {
153 System.err.println("ActiveEvent:");
154 ((ActiveEvent) event).dispatch ();
155 } else if (src instanceof Component) {
156 System.err.println("Source is component");
157 ((Component) src).dispatchEvent (event);
158 }
159 else {
160 System.err.println("Event blocked");
161 }
162 }
163 catch(Exception exception) {
164 Gatherer.printStackTrace(exception);
165 }
166 }
167 }
168 }
169 else {
170 try {
171 SwingUtilities.invokeAndWait(new MakeDialogVisibleTask(this, visible));
172 }
173 catch(Exception exception) {
174 Gatherer.printStackTrace(exception);
175 }
176 }
177 }
178
179 private class MakeDialogVisibleTask
180 implements Runnable {
181 private boolean make_visible;
182 private ModalDialog dialog;
183 public MakeDialogVisibleTask(ModalDialog dialog, boolean make_visible) {
184 this.dialog = dialog;
185 this.make_visible = make_visible;
186 }
187 public void run() {
188 // Blocks until the user dismisses the dialog
189 dialog.setVisible(make_visible);
190 }
191 }
192
193 /** Overridden method so we can control modality and not rely on the Dialog default.
194 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
195 */
196 public void setModal (boolean modal) {
197 this.modal = modal;
198 }
199}
Note: See TracBrowser for help on using the repository browser.