[5304] | 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 | /** true if this dialog is currently waiting some thread. */
|
---|
| 43 | protected boolean waiting = false;
|
---|
| 44 | /** Constructor.
|
---|
| 45 | */
|
---|
| 46 | public ModalDialog() {
|
---|
| 47 | super((Frame)null, "", false);
|
---|
| 48 |
|
---|
| 49 | }
|
---|
| 50 | /** Constructor.
|
---|
| 51 | * @param parent the Dialog which is the owener of this dialog.
|
---|
| 52 | */
|
---|
| 53 | public ModalDialog(Dialog parent) {
|
---|
| 54 | super(parent, "", false);
|
---|
| 55 | }
|
---|
| 56 | /** Constructor.
|
---|
| 57 | * @param parent the Dialog which is the owener of this dialog.
|
---|
| 58 | * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
|
---|
| 59 | */
|
---|
| 60 | public ModalDialog(Dialog parent, boolean modal) {
|
---|
| 61 | super(parent, "", false);
|
---|
| 62 | this.modal = modal;
|
---|
| 63 |
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /** Constructor.
|
---|
| 67 | * @param parent the Dialog which is the owner of this dialog.
|
---|
| 68 | * @param title the String to use as the title for this dialog.
|
---|
| 69 | */
|
---|
| 70 | public ModalDialog(Dialog parent, String title) {
|
---|
| 71 | super (parent, title, false);
|
---|
| 72 | this.modal = false;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /** Constructor.
|
---|
| 76 | * @param parent the Dialog which is the owener of this dialog.
|
---|
| 77 | * @param title the String to use as the title for this dialog.
|
---|
| 78 | * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
|
---|
| 79 | */
|
---|
| 80 | public ModalDialog(Dialog parent, String title, boolean modal) {
|
---|
| 81 | super (parent, title, false);
|
---|
| 82 | this.modal = modal;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /** Constructor.
|
---|
| 86 | * @param parent the Frame which is the owener of this dialog.
|
---|
| 87 | */
|
---|
| 88 | public ModalDialog(Frame parent) {
|
---|
| 89 | super(parent, "", false);
|
---|
| 90 | }
|
---|
| 91 | /** Constructor.
|
---|
| 92 | * @param parent the Frame which is the owener of this dialog.
|
---|
| 93 | * @param title the String to use as the title for this dialog.
|
---|
| 94 | */
|
---|
| 95 | public ModalDialog(Frame parent, boolean modal) {
|
---|
| 96 | super(parent, "", false);
|
---|
| 97 | this.modal = modal;
|
---|
| 98 |
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /** Constructor.
|
---|
| 102 | * @param parent the Frame which is the owner of this dialog.
|
---|
| 103 | * @param title the String to use as the title for this dialog.
|
---|
| 104 | */
|
---|
| 105 | public ModalDialog(Frame parent, String title) {
|
---|
| 106 | super (parent, title, false);
|
---|
| 107 | }
|
---|
| 108 | /** Constructor.
|
---|
| 109 | * @param parent the Frame which is the owener of this dialog.
|
---|
| 110 | * @param title the String to use as the title for this dialog.
|
---|
| 111 | * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
|
---|
| 112 | */
|
---|
| 113 | public ModalDialog(Frame parent, String title, boolean modal) {
|
---|
| 114 | super (parent, title, false);
|
---|
| 115 | this.modal = modal;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /** 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.
|
---|
| 119 | * @param visible true if this dialog should be painted on-screen, false otherwise.
|
---|
| 120 | */
|
---|
| 121 | public void setVisible (boolean visible) {
|
---|
| 122 | ///ystem.err.println("setVisible(" + visible + ")");
|
---|
| 123 | getParent ().setEnabled (!(visible && modal));
|
---|
| 124 | super.setVisible (visible);
|
---|
| 125 | if (modal && visible) {
|
---|
| 126 | try {
|
---|
| 127 | if (SwingUtilities.isEventDispatchThread ()) {
|
---|
| 128 | ///ystem.err.println("is Event Dispatch Thread.");
|
---|
| 129 | EventQueue theQueue = getToolkit().getSystemEventQueue();
|
---|
| 130 | while (isVisible ()) {
|
---|
| 131 | AWTEvent event = theQueue.getNextEvent();
|
---|
| 132 | Object src = event.getSource ();
|
---|
| 133 | if (event instanceof ActiveEvent) {
|
---|
| 134 | ((ActiveEvent) event).dispatch ();
|
---|
| 135 | } else if (src instanceof Component) {
|
---|
| 136 | ((Component) src).dispatchEvent (event);
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | ///ystem.err.println("No longer visible - AWT");
|
---|
| 140 | } else synchronized (getTreeLock ()) {
|
---|
| 141 | ///ystem.err.println("is other Thread.");
|
---|
| 142 | while (isVisible ()) {
|
---|
| 143 | try {
|
---|
| 144 | waiting = true;
|
---|
| 145 | getTreeLock().wait();
|
---|
| 146 | waiting = false;
|
---|
| 147 | } catch (InterruptedException e) {
|
---|
| 148 | ///ystem.err.println("Interrupted!!!");
|
---|
| 149 | break;
|
---|
| 150 | }
|
---|
| 151 | ///ystem.err.println("No longer visible - Other");
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | } catch (Exception ex) {
|
---|
| 155 | ex.printStackTrace();
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | else if(modal && !visible && waiting) {
|
---|
| 159 | ///ystem.err.println("Tree lock is: " + getTreeLock());
|
---|
| 160 | synchronized(getTreeLock()) {
|
---|
| 161 | ///ystem.err.println("Notify!");
|
---|
| 162 | getTreeLock().notify();
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /** Overridden method so we can control modality and not rely on the Dialog default.
|
---|
| 168 | * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
|
---|
| 169 | */
|
---|
| 170 | public void setModal (boolean modal) {
|
---|
| 171 | this.modal = modal;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|