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

Last change on this file since 6091 was 6091, checked in by jmt12, 21 years ago

Tried to stop our modal and java's modal dialogs fighting, but it didn't help

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 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.*;
37/** 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.
38 * 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. */
39public class ModalDialog
40 extends JDialog {
41 /** true if this dialog should be modal, ie block user actions to its owner window, false otherwise. */
42 protected boolean modal = false;
43 /** true if this dialog is currently waiting some thread. */
44 protected boolean waiting = false;
45
46 /** Constructor.
47 */
48 public ModalDialog() {
49 super((Frame)null, "", false);
50 }
51
52 /** Constructor.
53 * @param parent the Dialog which is the owener of this dialog.
54 */
55 public ModalDialog(Dialog parent) {
56 super(parent, "", false);
57 }
58
59 /** Constructor.
60 * @param parent the Dialog which is the owener of this dialog.
61 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
62 */
63 public ModalDialog(Dialog parent, boolean modal) {
64 super(parent, "", false);
65 this.modal = modal;
66 }
67
68 /** Constructor.
69 * @param parent the Dialog which is the owner of this dialog.
70 * @param title the String to use as the title for this dialog.
71 */
72 public ModalDialog(Dialog parent, String title) {
73 super (parent, title, false);
74 this.modal = false;
75 }
76
77 /** Constructor.
78 * @param parent the Dialog which is the owener of this dialog.
79 * @param title the String to use as the title for this dialog.
80 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
81 */
82 public ModalDialog(Dialog parent, String title, boolean modal) {
83 super (parent, title, false);
84 this.modal = modal;
85 }
86
87 /** Constructor.
88 * @param parent the Frame which is the owener of this dialog.
89 */
90 public ModalDialog(Frame parent) {
91 super(parent, "", false);
92 }
93
94 /** Constructor.
95 * @param parent the Frame which is the owener of this dialog.
96 * @param title the String to use as the title for this dialog.
97 */
98 public ModalDialog(Frame parent, boolean modal) {
99 super(parent, "", false);
100 this.modal = modal;
101
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 /** 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.
123 * @param visible true if this dialog should be painted on-screen, false otherwise.
124 */
125 public void setVisible (boolean visible) {
126 ///ystem.err.println("setVisible(" + visible + ")");
127 //getParent ().setEnabled (!(visible && modal));
128 super.setVisible (visible);
129 if (modal && visible) {
130 try {
131 if (SwingUtilities.isEventDispatchThread ()) {
132 ///ystem.err.println("is Event Dispatch Thread. Only process certain events.");
133 EventQueue theQueue = getToolkit().getSystemEventQueue();
134 while (isVisible ()) {
135 AWTEvent event = theQueue.getNextEvent();
136 Object src = event.getSource ();
137 if (event instanceof ActiveEvent) {
138 ((ActiveEvent) event).dispatch ();
139 } else if (src instanceof Component) {
140 ((Component) src).dispatchEvent (event);
141 }
142 }
143 ///ystem.err.println("No longer visible - AWT");
144 } else synchronized (getTreeLock ()) {
145 ///ystem.err.println("is other Thread. Block all events.");
146 while (isVisible ()) {
147 try {
148 waiting = true;
149 getTreeLock().wait();
150 waiting = false;
151 } catch (InterruptedException e) {
152 ///ystem.err.println("Interrupted!!!");
153 break;
154 }
155 }
156 ///ystem.err.println("No longer visible - Other");
157 }
158 } catch (Exception ex) {
159 ex.printStackTrace();
160 }
161 }
162 else if(modal && !visible && waiting) {
163 ///ystem.err.println("Tree lock is: " + getTreeLock());
164 synchronized(getTreeLock()) {
165 ///ystem.err.println("Notify!");
166 getTreeLock().notify();
167 }
168 }
169 else if(modal && !waiting) {
170 ///ystem.err.println("Modal Dialog is not currently waiting.");
171 }
172 else if(!modal) {
173 ///ystem.err.println("Dialog is not modal.");
174 }
175 }
176
177 /** Overridden method so we can control modality and not rely on the Dialog default.
178 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
179 */
180 public void setModal (boolean modal) {
181 this.modal = modal;
182 }
183}
Note: See TracBrowser for help on using the repository browser.