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