Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/greenstone/gatherer/gui/ModalDialog.java


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  
35  import java.awt.*;
36  import 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. */
39  public 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           ///ystem.err.println("ActiveEvent:");
139           ((ActiveEvent) event).dispatch ();
140       } else if (src instanceof Component) {
141           ///ystem.err.println("Source is component");
142           ((Component) src).dispatchEvent (event);
143       }
144       else {
145           ///ystem.err.println("Event blocked");
146       }
147         }
148         ///ystem.err.println("No longer visible - AWT");
149                 } else synchronized (getTreeLock ()) {
150         ///ystem.err.println("is other Thread. Block all events.");
151         while (isVisible ()) {
152       try {
153           waiting = true;
154           getTreeLock().wait();
155           waiting = false;
156       } catch (InterruptedException e) {
157           ///ystem.err.println("Interrupted!!!");
158           break;
159       }
160         }
161         ///ystem.err.println("No longer visible - Other");
162     }
163             } catch (Exception ex) { 
164     ex.printStackTrace(); 
165       }
166   }
167   else if(modal && !visible && waiting) {
168       ///ystem.err.println("Hiding dialog. Tree lock is: " + getTreeLock());
169       synchronized(getTreeLock()) {
170     ///ystem.err.println("Notify!");
171     getTreeLock().notify();
172       }
173   }
174   else if(modal && !waiting) {
175       ///ystem.err.println("Modal Dialog is not currently waiting.");
176   }
177   else if(!modal) {
178       ///ystem.err.println("Dialog is not modal.");
179   }
180     }
181     
182     /** Overridden method so we can control modality and not rely on the Dialog default.
183      * @param modal true if this dialog should be modal, ie block user actions to its owner window, false otherwise.
184      */
185     public void setModal (boolean modal) {
186   this.modal = modal;
187     }
188 }