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