Dialog.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /* Dialog.java -- An AWT dialog box
  2. Copyright (C) 1999, 2000, 2001, 2002, 2005, 2006
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.awt;
  33. import java.awt.peer.DialogPeer;
  34. import javax.accessibility.AccessibleContext;
  35. import javax.accessibility.AccessibleRole;
  36. import javax.accessibility.AccessibleState;
  37. import javax.accessibility.AccessibleStateSet;
  38. /**
  39. * <code>Dialog</code> provides a top-level window normally used to receive
  40. * user input in applications.
  41. * <p>
  42. * A dialog always has another top-level window as owner and is only visible
  43. * if this owner is visible to the user. The default layout of dialogs is the
  44. * <code>BorderLayout</code>. Dialogs can be modal (blocks user input to other
  45. * components) or non-modal (user input in other components are allowed).
  46. * </p>
  47. *
  48. * @author Aaron M. Renn (arenn@urbanophile.com)
  49. * @author Tom Tromey (tromey@redhat.com)
  50. */
  51. public class Dialog extends Window
  52. {
  53. public enum ModalExclusionType
  54. {
  55. APPLICATION_EXCLUDE,
  56. NO_EXCLUDE,
  57. TOOLKIT_EXCLUDE
  58. }
  59. public enum ModalityType
  60. {
  61. APPLICATION_MODAL,
  62. DOCUMENT_MODAL,
  63. MODELESS,
  64. TOOLKIT_MODAL
  65. }
  66. // Serialization constant
  67. private static final long serialVersionUID = 5920926903803293709L;
  68. /**
  69. * @serial Indicates whether or not this dialog box is modal.
  70. */
  71. private boolean modal;
  72. /**
  73. * @serial Indicates whether or not this dialog box is resizable.
  74. */
  75. private boolean resizable = true;
  76. /**
  77. * @serial The title string for this dialog box, which can be
  78. * <code>null</code>.
  79. */
  80. private String title;
  81. /**
  82. * This field indicates whether the dialog is undecorated or not.
  83. */
  84. private boolean undecorated = false;
  85. /**
  86. * Indicates that we are blocked for modality in show
  87. */
  88. private boolean blocked = false;
  89. /**
  90. * Secondary EventQueue to handle AWT events while we are blocked for
  91. * modality in show.
  92. */
  93. private EventQueue eq2 = null;
  94. /**
  95. * The number used to generate the name returned by getName.
  96. */
  97. private static transient long next_dialog_number;
  98. /**
  99. * Initializes a new instance of <code>Dialog</code> with the specified
  100. * parent, that is resizable and not modal, and which has no title.
  101. *
  102. * @param parent The parent frame of this dialog box.
  103. * @exception IllegalArgumentException If the owner's GraphicsConfiguration
  104. * is not from a screen device, or if owner is null. This exception is
  105. * always thrown when GraphicsEnvironment.isHeadless() returns true.
  106. */
  107. public Dialog(Frame parent)
  108. {
  109. this(parent, "", false);
  110. }
  111. /**
  112. * Initializes a new instance of <code>Dialog</code> with the specified
  113. * parent and modality, that is resizable and which has no title.
  114. *
  115. * @param parent The parent frame of this dialog box.
  116. * @param modal <code>true</code> if this dialog box is modal,
  117. * <code>false</code> otherwise.
  118. *
  119. * @exception IllegalArgumentException If the owner's GraphicsConfiguration
  120. * is not from a screen device, or if owner is null. This exception is
  121. * always thrown when GraphicsEnvironment.isHeadless() returns true.
  122. */
  123. public Dialog(Frame parent, boolean modal)
  124. {
  125. this(parent, "", modal);
  126. }
  127. /**
  128. * Initializes a new instance of <code>Dialog</code> with the specified
  129. * parent, that is resizable and not modal, and which has the specified
  130. * title.
  131. *
  132. * @param parent The parent frame of this dialog box.
  133. * @param title The title string for this dialog box.
  134. *
  135. * @exception IllegalArgumentException If the owner's GraphicsConfiguration
  136. * is not from a screen device, or if owner is null. This exceptionnis
  137. * always thrown when GraphicsEnvironment.isHeadless() returns true.
  138. */
  139. public Dialog(Frame parent, String title)
  140. {
  141. this(parent, title, false);
  142. }
  143. /**
  144. * Initializes a new instance of <code>Dialog</code> with the specified,
  145. * parent, title, and modality, that is resizable.
  146. *
  147. * @param parent The parent frame of this dialog box.
  148. * @param title The title string for this dialog box.
  149. * @param modal <code>true</code> if this dialog box is modal,
  150. * <code>false</code> otherwise.
  151. *
  152. * @exception IllegalArgumentException If owner is null or
  153. * GraphicsEnvironment.isHeadless() returns true.
  154. */
  155. public Dialog(Frame parent, String title, boolean modal)
  156. {
  157. this(parent, title, modal, parent.getGraphicsConfiguration());
  158. }
  159. /**
  160. * Initializes a new instance of <code>Dialog</code> with the specified,
  161. * parent, title, modality and <code>GraphicsConfiguration</code>, that is
  162. * resizable.
  163. *
  164. * @param parent The parent frame of this dialog box.
  165. * @param title The title string for this dialog box.
  166. * @param modal <code>true</code> if this dialog box is modal,
  167. * <code>false</code> otherwise.
  168. * @param gc The <code>GraphicsConfiguration</code> object to use. If
  169. * <code>null</code> the <code>GraphicsConfiguration</code> of the target
  170. * frame is used.
  171. *
  172. * @exception IllegalArgumentException If owner is null, the
  173. * GraphicsConfiguration is not a screen device or
  174. * GraphicsEnvironment.isHeadless() returns true.
  175. * @since 1.4
  176. */
  177. public Dialog(Frame parent, String title, boolean modal,
  178. GraphicsConfiguration gc)
  179. {
  180. super(parent, (gc == null) ? parent.getGraphicsConfiguration() : gc);
  181. // A null title is equivalent to an empty title
  182. this.title = (title != null) ? title : "";
  183. this.modal = modal;
  184. visible = false;
  185. setLayout(new BorderLayout());
  186. setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  187. }
  188. /**
  189. * Initializes a new instance of <code>Dialog</code> with the specified,
  190. * parent, that is resizable.
  191. *
  192. * @param owner The parent frame of this dialog box.
  193. *
  194. * @exception IllegalArgumentException If parent is null. This exception is
  195. * always thrown when GraphicsEnvironment.isHeadless() returns true.
  196. *
  197. * @since 1.2
  198. */
  199. public Dialog(Dialog owner)
  200. {
  201. this(owner, "", false, owner.getGraphicsConfiguration());
  202. }
  203. /**
  204. * Initializes a new instance of <code>Dialog</code> with the specified,
  205. * parent and title, that is resizable.
  206. *
  207. * @param owner The parent frame of this dialog box.
  208. * @param title The title string for this dialog box.
  209. *
  210. * @exception IllegalArgumentException If parent is null. This exception is
  211. * always thrown when GraphicsEnvironment.isHeadless() returns
  212. * true.
  213. * @since 1.2
  214. */
  215. public Dialog(Dialog owner, String title)
  216. {
  217. this(owner, title, false, owner.getGraphicsConfiguration());
  218. }
  219. /**
  220. * Initializes a new instance of <code>Dialog</code> with the specified,
  221. * parent, title and modality, that is resizable.
  222. *
  223. * @param owner The parent frame of this dialog box.
  224. * @param title The title string for this dialog box.
  225. * @param modal <code>true</code> if this dialog box is modal,
  226. * <code>false</code> otherwise.
  227. *
  228. * @exception IllegalArgumentException If parent is null. This exception is
  229. * always thrown when GraphicsEnvironment.isHeadless() returns true.
  230. * @since 1.2
  231. */
  232. public Dialog(Dialog owner, String title, boolean modal)
  233. {
  234. this(owner, title, modal, owner.getGraphicsConfiguration());
  235. }
  236. /**
  237. * Initializes a new instance of <code>Dialog</code> with the specified,
  238. * parent, title, modality and <code>GraphicsConfiguration</code>, that is
  239. * resizable.
  240. *
  241. * @param parent The parent frame of this dialog box.
  242. * @param title The title string for this dialog box.
  243. * @param modal <code>true</code> if this dialog box is modal,
  244. * <code>false</code> otherwise.
  245. * @param gc The <code>GraphicsConfiguration</code> object to use. If
  246. * <code>null</code> the <code>GraphicsConfiguration</code> of the target
  247. * frame is used.
  248. *
  249. * @exception IllegalArgumentException If parent is null, the
  250. * GraphicsConfiguration is not a screen device or
  251. * GraphicsEnvironment.isHeadless() returns true.
  252. *
  253. * @since 1.4
  254. */
  255. public Dialog(Dialog parent, String title, boolean modal,
  256. GraphicsConfiguration gc)
  257. {
  258. super(parent, (gc == null) ? parent.getGraphicsConfiguration() : gc);
  259. // A null title is equivalent to an empty title
  260. this.title = (title != null) ? title : "";
  261. this.modal = modal;
  262. visible = false;
  263. setLayout(new BorderLayout());
  264. setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  265. }
  266. /**
  267. * Returns the title of this dialog box.
  268. *
  269. * @return The title of this dialog box.
  270. */
  271. public String getTitle()
  272. {
  273. return title;
  274. }
  275. /**
  276. * Sets the title of this dialog box to the specified string.
  277. *
  278. * @param title the new title. If <code>null</code> an empty
  279. * title will be set.
  280. */
  281. public synchronized void setTitle(String title)
  282. {
  283. // A null title is equivalent to an empty title
  284. this.title = (title != null) ? title : "";
  285. if (peer != null)
  286. {
  287. DialogPeer d = (DialogPeer) peer;
  288. d.setTitle(title);
  289. }
  290. }
  291. /**
  292. * Tests whether or not this dialog box is modal.
  293. *
  294. * @return <code>true</code> if this dialog box is modal, <code>false</code>
  295. * otherwise.
  296. */
  297. public boolean isModal()
  298. {
  299. return modal;
  300. }
  301. /**
  302. * Changes the modality of this dialog box. This can only be done before the
  303. * peer is created.
  304. *
  305. * @param modal <code>true</code> to make this dialog box modal,
  306. * <code>false</code> to make it non-modal.
  307. */
  308. public void setModal(boolean modal)
  309. {
  310. this.modal = modal;
  311. }
  312. /**
  313. * Tests whether or not this dialog box is resizable.
  314. *
  315. * @return <code>true</code> if this dialog is resizable,
  316. * <code>false</code> otherwise.
  317. */
  318. public boolean isResizable()
  319. {
  320. return resizable;
  321. }
  322. /**
  323. * Changes the resizability of this dialog box.
  324. *
  325. * @param resizable <code>true</code> to make this dialog resizable,
  326. * <code>false</code> to make it non-resizable.
  327. */
  328. public synchronized void setResizable(boolean resizable)
  329. {
  330. this.resizable = resizable;
  331. if (peer != null)
  332. {
  333. DialogPeer d = (DialogPeer) peer;
  334. d.setResizable(resizable);
  335. }
  336. }
  337. /**
  338. * Creates this object's native peer.
  339. */
  340. public synchronized void addNotify()
  341. {
  342. if (peer == null)
  343. peer = getToolkit().createDialog(this);
  344. super.addNotify();
  345. }
  346. /**
  347. * Makes this dialog visible and brings it to the front. If the dialog is
  348. * modal and is not already visible, this call will not return until the
  349. * dialog is hidden by someone calling hide or dispose. If this is the event
  350. * dispatching thread we must ensure that another event thread runs while the
  351. * one which invoked this method is blocked.
  352. *
  353. * @deprecated Use {@link Component#setVisible(boolean)} instead.
  354. */
  355. public synchronized void show()
  356. {
  357. super.show();
  358. if (isModal())
  359. {
  360. // If already shown (and blocked) just return
  361. if (blocked)
  362. return;
  363. /*
  364. * If show is called in the dispatch thread for a modal dialog it will
  365. * block so we must run another thread so the events keep being
  366. * dispatched.
  367. */
  368. if (EventQueue.isDispatchThread())
  369. {
  370. EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
  371. eq2 = new EventQueue();
  372. eq.push(eq2);
  373. }
  374. try
  375. {
  376. blocked = true;
  377. wait();
  378. blocked = false;
  379. }
  380. catch (InterruptedException e)
  381. {
  382. blocked = false;
  383. }
  384. if (eq2 != null)
  385. {
  386. eq2.pop();
  387. eq2 = null;
  388. }
  389. }
  390. }
  391. /**
  392. * Hides the Dialog and then causes show() to return if it is currently
  393. * blocked.
  394. *
  395. * @deprecated Use {@link Component#setVisible(boolean)} instead.
  396. */
  397. public synchronized void hide()
  398. {
  399. if (blocked)
  400. {
  401. notifyAll();
  402. }
  403. super.hide();
  404. }
  405. /**
  406. * Disposes the Dialog and then causes show() to return if it is currently
  407. * blocked.
  408. */
  409. public synchronized void dispose()
  410. {
  411. if (blocked)
  412. {
  413. notifyAll();
  414. }
  415. super.dispose();
  416. }
  417. /**
  418. * Returns a debugging string for this component.
  419. *
  420. * @return A debugging string for this component.
  421. */
  422. protected String paramString()
  423. {
  424. return "title+" + title + ",modal=" + modal + ",resizable=" + resizable
  425. + "," + super.paramString();
  426. }
  427. /**
  428. * Returns whether this frame is undecorated or not.
  429. *
  430. * @return <code>true</code> if this dialog is undecorated,
  431. * <code>false</code> otherwise.
  432. *
  433. * @since 1.4
  434. */
  435. public boolean isUndecorated()
  436. {
  437. return undecorated;
  438. }
  439. /**
  440. * Disables or enables decorations for this frame. This method can only be
  441. * called while the frame is not displayable.
  442. *
  443. * @param undecorated <code>true</code> to disable dialog decorations,
  444. * <code>false</code> otherwise.
  445. *
  446. * @exception IllegalComponentStateException If this frame is displayable.
  447. * @since 1.4
  448. */
  449. public void setUndecorated(boolean undecorated)
  450. {
  451. if (isDisplayable())
  452. throw new IllegalComponentStateException();
  453. this.undecorated = undecorated;
  454. }
  455. /**
  456. * Accessibility support for <code>Dialog</code>.
  457. */
  458. protected class AccessibleAWTDialog
  459. extends AccessibleAWTWindow
  460. {
  461. private static final long serialVersionUID = 4837230331833941201L;
  462. /**
  463. * Gets the role of this object.
  464. * @return AccessibleRole.DIALOG
  465. */
  466. public AccessibleRole getAccessibleRole()
  467. {
  468. return AccessibleRole.DIALOG;
  469. }
  470. /**
  471. * Gets the state set of this object.
  472. * @return The current state of this dialog.
  473. */
  474. public AccessibleStateSet getAccessibleStateSet()
  475. {
  476. AccessibleStateSet states = super.getAccessibleStateSet();
  477. if (isResizable())
  478. states.add(AccessibleState.RESIZABLE);
  479. if (isModal())
  480. states.add(AccessibleState.MODAL);
  481. return states;
  482. }
  483. }
  484. /**
  485. * Gets the AccessibleContext associated with this <code>Dialog</code>. The
  486. * context is created, if necessary.
  487. *
  488. * @return the associated context
  489. */
  490. public AccessibleContext getAccessibleContext()
  491. {
  492. /* Create the context if this is the first request */
  493. if (accessibleContext == null)
  494. accessibleContext = new AccessibleAWTDialog();
  495. return accessibleContext;
  496. }
  497. /**
  498. * Generate a unique name for this <code>Dialog</code>.
  499. *
  500. * @return A unique name for this <code>Dialog</code>.
  501. */
  502. String generateName()
  503. {
  504. return "dialog" + getUniqueLong();
  505. }
  506. private static synchronized long getUniqueLong()
  507. {
  508. return next_dialog_number++;
  509. }
  510. }