KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > services > DialogDisplayerImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.core.windows.services;
22
23
24 import java.awt.Component JavaDoc;
25 import java.awt.Dialog JavaDoc;
26 import java.awt.EventQueue JavaDoc;
27 import java.awt.Frame JavaDoc;
28 import java.awt.KeyboardFocusManager JavaDoc;
29 import java.awt.Window JavaDoc;
30 import org.openide.DialogDescriptor;
31 import org.openide.DialogDisplayer;
32 import org.openide.NotifyDescriptor;
33 import org.openide.util.Mutex;
34 import org.openide.windows.WindowManager;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.List JavaDoc;
38
39
40 // Extracted from core/NbTopManager.
41
/**
42  * Implementation of <code>org.openide.DialogDisplayer</code>.
43  *
44  * @author Jesse Glick
45  */

46 public class DialogDisplayerImpl extends DialogDisplayer {
47     /** delayed runnables */
48     private static List JavaDoc<Runnable JavaDoc> run = Collections.synchronizedList(new ArrayList JavaDoc<Runnable JavaDoc>());
49     
50     /** non-null if we are running in unit test and should no show any dialogs */
51     private Object JavaDoc testResult;
52     
53     /** Creates a new instance of DialogDisplayerImpl */
54     public DialogDisplayerImpl() {
55         this (null);
56     }
57     
58     DialogDisplayerImpl (Object JavaDoc testResult) {
59         this.testResult = testResult;
60     }
61     
62     /* Runs list of tasks gathered from notifyLater calls */
63     public static void runDelayed() {
64         List JavaDoc<Runnable JavaDoc> local = run;
65         run = null;
66         if (local == null) {
67             return;
68         }
69         
70         assert EventQueue.isDispatchThread();
71         for (Runnable JavaDoc r : local) {
72             r.run();
73         }
74     }
75     
76
77     /** Creates new dialog. */
78     public Dialog JavaDoc createDialog (final DialogDescriptor d) {
79         return Mutex.EVENT.readAccess (new Mutex.Action<Dialog JavaDoc> () {
80             public Dialog JavaDoc run () {
81                 // if a modal dialog active use it as parent
82
// otherwise use the main window
83
if (NbPresenter.currentModalDialog != null) {
84                     if (NbPresenter.currentModalDialog.isLeaf ()) {
85                         return new NbDialog(d, WindowManager.getDefault ().getMainWindow ());
86                     } else {
87                         return new NbDialog(d, NbPresenter.currentModalDialog);
88                     }
89                 }
90                 else {
91                     Window JavaDoc w = KeyboardFocusManager.getCurrentKeyboardFocusManager ().getActiveWindow ();
92                     if (!(w instanceof NbPresenter) || !w.isVisible()) {
93                         // don't set non-ide window as parent
94
w = WindowManager.getDefault ().getMainWindow ();
95                     } else if (w instanceof NbPresenter && ((NbPresenter) w).isLeaf ()) {
96                         w = WindowManager.getDefault ().getMainWindow ();
97                     }
98                     if (w instanceof Dialog JavaDoc) {
99                         NbDialog dlg = new NbDialog(d, (Dialog JavaDoc) w);
100                         dlg.requestFocusInWindow ();
101                         return dlg;
102                     } else {
103                         Frame JavaDoc f = w instanceof Frame JavaDoc ? (Frame JavaDoc) w : WindowManager.getDefault ().getMainWindow ();
104                         NbDialog dlg = new NbDialog(d, f);
105                         dlg.requestFocusInWindow ();
106                         return dlg;
107                     }
108                 }
109             }
110         });
111     }
112     
113     /** Notifies user by a dialog.
114      * @param descriptor description that contains needed informations
115      * @return the option that has been choosen in the notification.
116      */

117     public Object JavaDoc notify (NotifyDescriptor descriptor) {
118         return notify(descriptor, false);
119     }
120
121     /** Notifies user by a dialog.
122      * @param descriptor description that contains needed informations
123      * @param noParent don't set any window as parent of dialog, if flag is true
124      * @return the option that has been choosen in the notification.
125      */

126     private Object JavaDoc notify (final NotifyDescriptor descriptor, final boolean noParent) {
127         class AWTQuery implements Runnable JavaDoc {
128             public Object JavaDoc result;
129             public boolean running;
130         
131             public void run () {
132                 synchronized (this) {
133                     notify ();
134                     running = true;
135                 }
136                 
137                 showDialog ();
138
139                 synchronized (this) {
140                     this.result = descriptor.getValue();
141                     notifyAll ();
142                 }
143             }
144             
145             public void showDialog () {
146                 if (testResult != null) {
147                     // running in Unit test
148
descriptor.setValue (testResult);
149                     return;
150                 }
151                 
152                 Component JavaDoc focusOwner = null;
153                 Component JavaDoc comp = org.openide.windows.TopComponent.getRegistry ().getActivated ();
154                 Component JavaDoc win = comp;
155                 while ((win != null) && (!(win instanceof Window JavaDoc))) win = win.getParent ();
156                 if (win != null) focusOwner = ((Window JavaDoc)win).getFocusOwner ();
157
158                 // if a modal dialog is active use it as parent
159
// otherwise use the main window
160

161                 NbPresenter presenter = null;
162                 if (descriptor instanceof DialogDescriptor) {
163                     if (NbPresenter.currentModalDialog != null) {
164                         if (NbPresenter.currentModalDialog.isLeaf ()) {
165                             presenter = new NbDialog((DialogDescriptor) descriptor, WindowManager.getDefault ().getMainWindow ());
166                         } else {
167                             presenter = new NbDialog((DialogDescriptor) descriptor, NbPresenter.currentModalDialog);
168                         }
169                     } else {
170                         Window JavaDoc w = KeyboardFocusManager.getCurrentKeyboardFocusManager ().getActiveWindow ();
171                         if (w instanceof NbPresenter && ((NbPresenter) w).isLeaf ()) {
172                             w = WindowManager.getDefault ().getMainWindow ();
173                         }
174                         Frame JavaDoc f = w instanceof Frame JavaDoc ? (Frame JavaDoc) w : WindowManager.getDefault().getMainWindow();
175                         presenter = new NbDialog((DialogDescriptor) descriptor, f);
176                     }
177                 } else {
178                     if (NbPresenter.currentModalDialog != null) {
179                         presenter = new NbPresenter(descriptor, NbPresenter.currentModalDialog, true);
180                     } else {
181                         Frame JavaDoc f = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow()
182                             instanceof Frame JavaDoc ?
183                             (Frame JavaDoc) KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow()
184                             : WindowManager.getDefault().getMainWindow();
185                         
186                         if (noParent) {
187                             f = null;
188                         }
189                         presenter = new NbPresenter(descriptor, f, true);
190                     }
191                 }
192                 
193                 //#47150 - horrible hack for vcs module
194
if ("true".equals(System.getProperty("javahelp.ignore.modality"))) { //NOI18N
195
presenter.getRootPane().putClientProperty ("javahelp.ignore.modality", "true"); //NOI18N
196
System.setProperty("javahelp.ignore.modality", "false"); //NOI18N
197
}
198
199                 //Bugfix #8551
200
presenter.getRootPane().requestDefaultFocus();
201                 presenter.setVisible(true);
202
203                 // dialog is gone, restore the focus
204

205                 if (focusOwner != null) {
206                     win.requestFocus ();
207                     comp.requestFocus ();
208                     focusOwner.requestFocus ();
209                 }
210             }
211         }
212         
213         AWTQuery query = new AWTQuery ();
214         
215         if (javax.swing.SwingUtilities.isEventDispatchThread ()) {
216             query.showDialog ();
217             return descriptor.getValue ();
218         }
219         
220         synchronized (query) {
221             javax.swing.SwingUtilities.invokeLater (query);
222             try {
223                 query.wait (10000);
224             } catch (InterruptedException JavaDoc ex) {
225                 // ok, should not happen and does not matter
226
}
227             
228             if (query.running) {
229                 while (query.result == null) {
230                     try {
231                         query.wait ();
232                     } catch (InterruptedException JavaDoc ex) {
233                         // one more round
234
}
235                 }
236                 return query.result;
237             } else {
238                 return NotifyDescriptor.CLOSED_OPTION;
239             }
240         }
241     }
242
243     /* Schedules notification for specific later time if called before
244      * <code>runDelayed</code>, otherwise works as superclass method.
245      */

246     public void notifyLater(final NotifyDescriptor descriptor) {
247         class R implements Runnable JavaDoc {
248             public boolean noParent;
249             
250             public void run() {
251                 DialogDisplayerImpl.this.notify(descriptor, noParent);
252             }
253         }
254         R r = new R();
255         
256         List JavaDoc<Runnable JavaDoc> local = run;
257         if (local != null) {
258             r.noParent = true;
259             local.add(r);
260         } else {
261             EventQueue.invokeLater(r);
262         }
263     }
264 }
265
Popular Tags