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 | */
|
---|
33 | package org.greenstone.gatherer.gui;
|
---|
34 | import java.awt.*;
|
---|
35 | import javax.swing.*;
|
---|
36 | /** 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.
|
---|
37 | * 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. */
|
---|
38 | public class ModalDialog
|
---|
39 | extends JDialog {
|
---|
40 | /** true if this dialog should be modal, ie block user actions to its owner window, false otherwise. */
|
---|
41 | protected boolean modal = false;
|
---|
42 |
|
---|
43 | /** Constructor.
|
---|
44 | */
|
---|
45 | public ModalDialog() {
|
---|
46 | super((Frame)null, "", false);
|
---|
47 |
|
---|
48 | }
|
---|
49 | /** Constructor.
|
---|
50 | * @param parent the Dialog which is the owener of this dialog.
|
---|
51 | */
|
---|
52 | public ModalDialog(Dialog parent) {
|
---|
53 | super(parent, "", false);
|
---|
54 | }
|
---|
55 | /** Constructor.
|
---|
56 | * @param parent the Dialog which is the owener of this dialog.
|
---|
57 | * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
|
---|
58 | */
|
---|
59 | public ModalDialog(Dialog parent, boolean modal) {
|
---|
60 | super(parent, "", false);
|
---|
61 | this.modal = modal;
|
---|
62 |
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Constructor.
|
---|
66 | * @param parent the Dialog which is the owner of this dialog.
|
---|
67 | * @param title the String to use as the title for this dialog.
|
---|
68 | */
|
---|
69 | public ModalDialog(Dialog parent, String title) {
|
---|
70 | super (parent, title, false);
|
---|
71 | this.modal = false;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /** Constructor.
|
---|
75 | * @param parent the Dialog which is the owener of this dialog.
|
---|
76 | * @param title the String to use as the title for this dialog.
|
---|
77 | * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
|
---|
78 | */
|
---|
79 | public ModalDialog(Dialog parent, String title, boolean modal) {
|
---|
80 | super (parent, title, false);
|
---|
81 | this.modal = modal;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Constructor.
|
---|
85 | * @param parent the Frame which is the owener of this dialog.
|
---|
86 | */
|
---|
87 | public ModalDialog(Frame parent) {
|
---|
88 | super(parent, "", false);
|
---|
89 | }
|
---|
90 | /** Constructor.
|
---|
91 | * @param parent the Frame which is the owener of this dialog.
|
---|
92 | * @param title the String to use as the title for this dialog.
|
---|
93 | */
|
---|
94 | public ModalDialog(Frame parent, boolean modal) {
|
---|
95 | super(parent, "", false);
|
---|
96 | this.modal = modal;
|
---|
97 |
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Constructor.
|
---|
101 | * @param parent the Frame which is the owner of this dialog.
|
---|
102 | * @param title the String to use as the title for this dialog.
|
---|
103 | */
|
---|
104 | public ModalDialog(Frame parent, String title) {
|
---|
105 | super (parent, title, false);
|
---|
106 | }
|
---|
107 | /** Constructor.
|
---|
108 | * @param parent the Frame which is the owener of this dialog.
|
---|
109 | * @param title the String to use as the title for this dialog.
|
---|
110 | * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
|
---|
111 | */
|
---|
112 | public ModalDialog(Frame parent, String title, boolean modal) {
|
---|
113 | super (parent, title, false);
|
---|
114 | this.modal = modal;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /** 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.
|
---|
118 | * @param visible true if this dialog should be painted on-screen, false otherwise.
|
---|
119 | */
|
---|
120 | public void setVisible (boolean visible) {
|
---|
121 | getParent ().setEnabled (!(visible && modal));
|
---|
122 | super.setVisible (visible);
|
---|
123 | if (modal && visible)
|
---|
124 | try {
|
---|
125 | if (SwingUtilities.isEventDispatchThread ()) {
|
---|
126 | EventQueue theQueue = getToolkit().getSystemEventQueue();
|
---|
127 | while (isVisible ()) {
|
---|
128 | AWTEvent event = theQueue.getNextEvent();
|
---|
129 | Object src = event.getSource ();
|
---|
130 | if (event instanceof ActiveEvent) {
|
---|
131 | ((ActiveEvent) event).dispatch ();
|
---|
132 | } else if (src instanceof Component) {
|
---|
133 | ((Component) src).dispatchEvent (event);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | } else synchronized (getTreeLock ()) {
|
---|
137 | while (isVisible ())
|
---|
138 | try {
|
---|
139 | getTreeLock().wait();
|
---|
140 | } catch (InterruptedException e) {
|
---|
141 | break;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | } catch (Exception ex) {
|
---|
145 | ex.printStackTrace();
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | /** Overridden method so we can control modality and not rely on the Dialog default.
|
---|
150 | * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
|
---|
151 | */
|
---|
152 | public void setModal (boolean modal) {
|
---|
153 | this.modal = modal;
|
---|
154 | }
|
---|
155 | }
|
---|