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