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

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

Adding file permanently.

  • Property svn:keywords set to Author Date Id Revision
File size: 4.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;
34import java.awt.*;
35import 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. */
37public class ModalDialog
38 extends JDialog {
39 /** true if this dialog should be modal, ie block user actions to its owner window, false otherwise. */
40 protected boolean modal = false;
41
42 /** Constructor.
43 * @param parent the Dialog which is the owener of this dialog.
44 * @param title the String to use as the title for this dialog.
45 */
46 public ModalDialog(Dialog parent, String title) {
47 super (parent, title, false);
48 this.modal = false;
49 }
50
51 /** Constructor.
52 * @param parent the Dialog which is the owener of this dialog.
53 * @param title the String to use as the title for this dialog.
54 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
55 */
56 public ModalDialog(Dialog parent, String title, boolean modal) {
57 super (parent, title, false);
58 this.modal = modal;
59 }
60
61 /** Constructor.
62 * @param parent the Frame which is the owener of this dialog.
63 * @param title the String to use as the title for 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(Frame parent, String title, boolean modal) {
67 super (parent, title, false);
68 this.modal = modal;
69 }
70
71 /** 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.
72 * @param visible true if this dialog should be painted on-screen, false otherwise.
73 */
74 public void setVisible (boolean visible) {
75 getParent ().setEnabled (!(visible && modal));
76 super.setVisible (visible);
77 if (modal && visible)
78 try {
79 if (SwingUtilities.isEventDispatchThread ()) {
80 EventQueue theQueue = getToolkit().getSystemEventQueue();
81 while (isVisible ()) {
82 AWTEvent event = theQueue.getNextEvent();
83 Object src = event.getSource ();
84 if (event instanceof ActiveEvent) {
85 ((ActiveEvent) event).dispatch ();
86 } else if (src instanceof Component) {
87 ((Component) src).dispatchEvent (event);
88 }
89 }
90 } else synchronized (getTreeLock ()) {
91 while (isVisible ())
92 try {
93 getTreeLock().wait();
94 } catch (InterruptedException e) {
95 break;
96 }
97 }
98 } catch (Exception ex) {
99 ex.printStackTrace();
100 }
101 }
102
103 /** Overridden method so we can control modality and not rely on the Dialog default.
104 * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
105 */
106 public void setModal (boolean modal) {
107 this.modal = modal;
108 }
109}
Note: See TracBrowser for help on using the repository browser.