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

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

Heres a bunch of other changed files. If it wasn't a Friday afternoon I might be bothered finding out what I actually changed in them. Such changes include: a new option or three on preferences, a bug fix for the GDM classes, several changes to CDM to allow for G2.39 configuration files, a fix to Codec to allow for quotes in format strings and more work on CommandTokenizer to allow for stupid, stupid, stupid collectionextra's starting with speech marks then a new line. Plus other stuff. And things. Peace Out.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 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 Gatherer.current_modal = this;
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 Gatherer.current_modal = this;
60 }
61
62 /** Constructor.
63 * @param parent the Dialog which is the owener of this dialog.
64 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
65 */
66 public ModalDialog(Dialog parent, boolean modal) {
67 super(parent, "", false);
68 this.modal = modal;
69 Gatherer.current_modal = this;
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 Gatherer.current_modal = this;
80 }
81
82 /** Constructor.
83 * @param parent the Dialog which is the owener of this dialog.
84 * @param title the String to use as the title for this dialog.
85 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
86 */
87 public ModalDialog(Dialog parent, String title, boolean modal) {
88 super (parent, title, false);
89 this.modal = modal;
90 Gatherer.current_modal = this;
91 }
92
93 /** Constructor.
94 * @param parent the Frame which is the owener of this dialog.
95 */
96 public ModalDialog(Frame parent) {
97 super(parent, "", false);
98 Gatherer.current_modal = this;
99 }
100
101 /** Constructor.
102 * @param parent the Frame which is the owener of this dialog.
103 * @param title the String to use as the title for this dialog.
104 */
105 public ModalDialog(Frame parent, boolean modal) {
106 super(parent, "", false);
107 this.modal = modal;
108 Gatherer.current_modal = this;
109 }
110
111 /** Constructor.
112 * @param parent the Frame which is the owner of this dialog.
113 * @param title the String to use as the title for this dialog.
114 */
115 public ModalDialog(Frame parent, String title) {
116 super (parent, title, false);
117 Gatherer.current_modal = this;
118 }
119
120 /** Constructor.
121 * @param parent the Frame which is the owener of this dialog.
122 * @param title the String to use as the title for this dialog.
123 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
124 */
125 public ModalDialog(Frame parent, String title, boolean modal) {
126 super (parent, title, false);
127 this.modal = modal;
128 Gatherer.current_modal = this;
129 }
130
131 public void dispose() {
132 Gatherer.current_modal = null;
133 super.dispose();
134 }
135
136 /** 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.
137 * @param visible true if this dialog should be painted on-screen, false otherwise.
138 */
139 public void setVisible (boolean visible) {
140 ///ystem.err.println("setVisible(" + visible + ")");
141 getParent ().setEnabled (!(visible && modal));
142 super.setVisible (visible);
143 if (modal && visible) {
144 try {
145 if (SwingUtilities.isEventDispatchThread ()) {
146 ///ystem.err.println("is Event Dispatch Thread. Only process certain events.");
147 EventQueue theQueue = getToolkit().getSystemEventQueue();
148 while (isVisible ()) {
149 AWTEvent event = theQueue.getNextEvent();
150 Object src = event.getSource ();
151 if (event instanceof ActiveEvent) {
152 ///ystem.err.println("ActiveEvent:");
153 ((ActiveEvent) event).dispatch ();
154 } else if (src instanceof Component) {
155 ///ystem.err.println("Source is component");
156 ((Component) src).dispatchEvent (event);
157 }
158 else {
159 ///ystem.err.println("Event blocked");
160 }
161 }
162 ///ystem.err.println("No longer visible - AWT");
163 } else synchronized (getTreeLock ()) {
164 ///ystem.err.println("is other Thread. Block all events.");
165 while (isVisible ()) {
166 try {
167 waiting = true;
168 getTreeLock().wait();
169 waiting = false;
170 } catch (InterruptedException e) {
171 ///ystem.err.println("Interrupted!!!");
172 break;
173 }
174 }
175 ///ystem.err.println("No longer visible - Other");
176 }
177 } catch (Exception ex) {
178 ex.printStackTrace();
179 }
180 }
181 else if(modal && !visible && waiting) {
182 ///ystem.err.println("Hiding dialog. Tree lock is: " + getTreeLock());
183 synchronized(getTreeLock()) {
184 ///ystem.err.println("Notify!");
185 getTreeLock().notify();
186 }
187 }
188 else if(modal && !waiting) {
189 ///ystem.err.println("Modal Dialog is not currently waiting.");
190 }
191 else if(!modal) {
192 ///ystem.err.println("Dialog is not modal.");
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.