1 19 20 21 package org.netbeans.core.windows.services; 22 23 24 import java.awt.Component ; 25 import java.awt.Dialog ; 26 import java.awt.EventQueue ; 27 import java.awt.Frame ; 28 import java.awt.KeyboardFocusManager ; 29 import java.awt.Window ; 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 ; 36 import java.util.Collections ; 37 import java.util.List ; 38 39 40 46 public class DialogDisplayerImpl extends DialogDisplayer { 47 48 private static List <Runnable > run = Collections.synchronizedList(new ArrayList <Runnable >()); 49 50 51 private Object testResult; 52 53 54 public DialogDisplayerImpl() { 55 this (null); 56 } 57 58 DialogDisplayerImpl (Object testResult) { 59 this.testResult = testResult; 60 } 61 62 63 public static void runDelayed() { 64 List <Runnable > local = run; 65 run = null; 66 if (local == null) { 67 return; 68 } 69 70 assert EventQueue.isDispatchThread(); 71 for (Runnable r : local) { 72 r.run(); 73 } 74 } 75 76 77 78 public Dialog createDialog (final DialogDescriptor d) { 79 return Mutex.EVENT.readAccess (new Mutex.Action<Dialog > () { 80 public Dialog run () { 81 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 w = KeyboardFocusManager.getCurrentKeyboardFocusManager ().getActiveWindow (); 92 if (!(w instanceof NbPresenter) || !w.isVisible()) { 93 w = WindowManager.getDefault ().getMainWindow (); 95 } else if (w instanceof NbPresenter && ((NbPresenter) w).isLeaf ()) { 96 w = WindowManager.getDefault ().getMainWindow (); 97 } 98 if (w instanceof Dialog ) { 99 NbDialog dlg = new NbDialog(d, (Dialog ) w); 100 dlg.requestFocusInWindow (); 101 return dlg; 102 } else { 103 Frame f = w instanceof Frame ? (Frame ) w : WindowManager.getDefault ().getMainWindow (); 104 NbDialog dlg = new NbDialog(d, f); 105 dlg.requestFocusInWindow (); 106 return dlg; 107 } 108 } 109 } 110 }); 111 } 112 113 117 public Object notify (NotifyDescriptor descriptor) { 118 return notify(descriptor, false); 119 } 120 121 126 private Object notify (final NotifyDescriptor descriptor, final boolean noParent) { 127 class AWTQuery implements Runnable { 128 public Object 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 descriptor.setValue (testResult); 149 return; 150 } 151 152 Component focusOwner = null; 153 Component comp = org.openide.windows.TopComponent.getRegistry ().getActivated (); 154 Component win = comp; 155 while ((win != null) && (!(win instanceof Window ))) win = win.getParent (); 156 if (win != null) focusOwner = ((Window )win).getFocusOwner (); 157 158 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 w = KeyboardFocusManager.getCurrentKeyboardFocusManager ().getActiveWindow (); 171 if (w instanceof NbPresenter && ((NbPresenter) w).isLeaf ()) { 172 w = WindowManager.getDefault ().getMainWindow (); 173 } 174 Frame f = w instanceof Frame ? (Frame ) 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 f = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow() 182 instanceof Frame ? 183 (Frame ) 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 if ("true".equals(System.getProperty("javahelp.ignore.modality"))) { presenter.getRootPane().putClientProperty ("javahelp.ignore.modality", "true"); System.setProperty("javahelp.ignore.modality", "false"); } 198 199 presenter.getRootPane().requestDefaultFocus(); 201 presenter.setVisible(true); 202 203 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 ex) { 225 } 227 228 if (query.running) { 229 while (query.result == null) { 230 try { 231 query.wait (); 232 } catch (InterruptedException ex) { 233 } 235 } 236 return query.result; 237 } else { 238 return NotifyDescriptor.CLOSED_OPTION; 239 } 240 } 241 } 242 243 246 public void notifyLater(final NotifyDescriptor descriptor) { 247 class R implements Runnable { 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 <Runnable > 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 |