Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

StandardDialog.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.dialogs;
  23. import com.dmdirc.ui.CoreUIUtils;
  24. import java.awt.Component;
  25. import java.awt.Dialog;
  26. import java.awt.Dimension;
  27. import java.awt.Frame;
  28. import java.awt.Window;
  29. import java.awt.event.WindowAdapter;
  30. import java.awt.event.WindowEvent;
  31. import java.util.concurrent.Semaphore;
  32. import javax.swing.JButton;
  33. import javax.swing.JDialog;
  34. import javax.swing.SwingUtilities;
  35. /**
  36. * Provides common methods for dialogs.
  37. */
  38. public class StandardDialog extends JDialog {
  39. /**
  40. * A version number for this class. It should be changed whenever the class
  41. * structure is changed (or anything else that would prevent serialized
  42. * objects being unserialized with the new class).
  43. */
  44. private static final long serialVersionUID = 1;
  45. /** The OK button for this frame. */
  46. private JButton okButton;
  47. /** The cancel button for this frame. */
  48. private JButton cancelButton;
  49. /** Parent window. */
  50. private Window owner;
  51. /**
  52. * Creates a new instance of StandardDialog.
  53. * @param owner The frame that owns this dialog
  54. * @param modal Whether to display modally or not
  55. */
  56. public StandardDialog(final Frame owner, final boolean modal) {
  57. super(owner, modal);
  58. this.owner = owner;
  59. if (owner != null) {
  60. setIconImages(owner.getIconImages());
  61. }
  62. orderButtons(new JButton(), new JButton());
  63. }
  64. /**
  65. * Creates a new instance of StandardDialog.
  66. * @param owner The frame that owns this dialog
  67. * @param modal Whether to display modally or not
  68. */
  69. public StandardDialog(final Window owner, final ModalityType modal) {
  70. super(owner, modal);
  71. this.owner = owner;
  72. if (owner != null) {
  73. setIconImages(owner.getIconImages());
  74. }
  75. orderButtons(new JButton(), new JButton());
  76. }
  77. /**
  78. * Creates a new instance of StandardDialog.
  79. * @param owner The frame that owns this dialog
  80. * @param modal Whether to display modally or not
  81. */
  82. public StandardDialog(final Dialog owner, final boolean modal) {
  83. super(owner, modal);
  84. this.owner = owner;
  85. if (owner != null) {
  86. setIconImages(owner.getIconImages());
  87. }
  88. orderButtons(new JButton(), new JButton());
  89. }
  90. /** {@inheritDoc} */
  91. @Override
  92. public void setTitle(final String title) {
  93. super.setTitle("DMDirc: " + title);
  94. }
  95. /**
  96. * Displays the dialog centering on the parent window.
  97. */
  98. public void display() {
  99. display(owner);
  100. }
  101. /**
  102. * Displays the dialog centering on the specified window.
  103. *
  104. * @param owner Window to center on
  105. */
  106. public void display(final Component owner) {
  107. addWindowListener(new WindowAdapter() {
  108. /** {@inheritDoc} */
  109. @Override
  110. public void windowClosing(final WindowEvent e) {
  111. executeAction(getCancelButton());
  112. }
  113. });
  114. centreOnOwner();
  115. pack();
  116. centreOnOwner();
  117. setVisible(false);
  118. setVisible(true);
  119. }
  120. /**
  121. * Displays the dialog centering on the parent window, blocking until
  122. * complete.
  123. */
  124. public void displayBlocking() {
  125. displayBlocking(owner);
  126. }
  127. /**
  128. * Displays the dialog centering on the specified window, blocking until
  129. * complete.
  130. *
  131. * @param owner Window to center on
  132. */
  133. public void displayBlocking(final Component owner) {
  134. if (SwingUtilities.isEventDispatchThread()) {
  135. throw new IllegalStateException("Unable to display blocking dialog"
  136. + " in the EDT.");
  137. }
  138. final Semaphore semaphore = new Semaphore(0);
  139. SwingUtilities.invokeLater(new Runnable() {
  140. /** {@inheritDoc} */
  141. @Override
  142. public void run() {
  143. display(owner);
  144. addWindowListener(new WindowAdapter() {
  145. @Override
  146. public void windowClosed(final WindowEvent e) {
  147. semaphore.release();
  148. }
  149. });
  150. }
  151. });
  152. semaphore.acquireUninterruptibly();
  153. }
  154. /**
  155. * Centres this dialog on its owner, or the screen if no owner is present.
  156. */
  157. public void centreOnOwner() {
  158. if (owner == null) {
  159. CoreUIUtils.centreWindow(this);
  160. } else {
  161. setLocationRelativeTo(owner);
  162. }
  163. }
  164. /**
  165. * Returns the window owner for this dialog.
  166. *
  167. * @return Parent window or null
  168. */
  169. public Window getParentWindow() {
  170. return owner;
  171. }
  172. /**
  173. * Sets the specified button up as the OK button.
  174. * @param button The target button
  175. */
  176. protected void setOkButton(final JButton button) {
  177. okButton = button;
  178. button.setText("OK");
  179. button.setDefaultCapable(false);
  180. }
  181. /**
  182. * Sets the specified button up as the Cancel button.
  183. * @param button The target button
  184. */
  185. protected void setCancelButton(final JButton button) {
  186. cancelButton = button;
  187. button.setText("Cancel");
  188. button.setDefaultCapable(false);
  189. }
  190. /**
  191. * Gets the left hand button for a dialog.
  192. * @return left JButton
  193. */
  194. protected final JButton getLeftButton() {
  195. if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  196. return getOkButton();
  197. } else {
  198. return getCancelButton();
  199. }
  200. }
  201. /**
  202. * Gets the right hand button for a dialog.
  203. * @return right JButton
  204. */
  205. protected final JButton getRightButton() {
  206. if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  207. return getCancelButton();
  208. } else {
  209. return getOkButton();
  210. }
  211. }
  212. /**
  213. * Orders the OK and Cancel buttons in an appropriate order for the current
  214. * operating system.
  215. * @param leftButton The left-most button
  216. * @param rightButton The right-most button
  217. */
  218. protected final void orderButtons(final JButton leftButton,
  219. final JButton rightButton) {
  220. if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  221. // Windows - put the OK button on the left
  222. setOkButton(leftButton);
  223. setCancelButton(rightButton);
  224. } else {
  225. // Everything else - adhere to usability guidelines and put it on
  226. // the right.
  227. setOkButton(rightButton);
  228. setCancelButton(leftButton);
  229. }
  230. leftButton.setPreferredSize(new Dimension(100, 25));
  231. rightButton.setPreferredSize(new Dimension(100, 25));
  232. leftButton.setMinimumSize(new Dimension(100, 25));
  233. rightButton.setMinimumSize(new Dimension(100, 25));
  234. }
  235. /**
  236. * Retrieves the OK button for this form.
  237. * @return The form's OK button
  238. */
  239. public final JButton getOkButton() {
  240. return okButton;
  241. }
  242. /**
  243. * Retrieves the Cancel button for this form.
  244. * @return The form's cancel button
  245. */
  246. public final JButton getCancelButton() {
  247. return cancelButton;
  248. }
  249. /**
  250. * Simulates the user clicking on the specified target button.
  251. * @param target The button to use
  252. */
  253. public final void executeAction(final JButton target) {
  254. if (target != null && target.isEnabled()) {
  255. target.doClick();
  256. }
  257. }
  258. /**
  259. * This method is called when enter is pressed anywhere in the dialog
  260. * except on a button. By default this method does nothing.
  261. *
  262. * @return Returns true if the key press has been handled and is not be
  263. * be forwarded on by default this is false
  264. */
  265. public boolean enterPressed() {
  266. return false;
  267. }
  268. /**
  269. * This method is called when ctrl + enter is pressed anywhere in the
  270. * dialog. By default this method presses the OK button.
  271. *
  272. * @return Returns true if the key press has been handled and is not be
  273. * be forwarded on by default this is true
  274. */
  275. public boolean ctrlEnterPressed() {
  276. executeAction(getOkButton());
  277. return true;
  278. }
  279. /**
  280. * This method is called when enter is pressed anywhere in the dialog
  281. * except on a button. By default this method presses the cancel button.
  282. *
  283. * @return Returns true if the key press has been handled and is not be
  284. * be forwarded on by default this is true
  285. */
  286. public boolean escapePressed() {
  287. executeAction(getCancelButton());
  288. return true;
  289. }
  290. }