You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StandardDialog.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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.Dimension;
  26. import java.awt.Window;
  27. import java.awt.event.WindowAdapter;
  28. import java.awt.event.WindowEvent;
  29. import javax.swing.JButton;
  30. import javax.swing.JDialog;
  31. /**
  32. * Provides common methods for dialogs.
  33. */
  34. public class StandardDialog extends JDialog {
  35. /** Serial version UID. */
  36. private static final long serialVersionUID = 1;
  37. /** Parent window. */
  38. private Component owner;
  39. /** The OK button for this frame. */
  40. private JButton okButton;
  41. /** The cancel button for this frame. */
  42. private JButton cancelButton;
  43. /**
  44. * Creates a new instance of StandardDialog.
  45. *
  46. * @param owner The frame that owns this dialog
  47. * @param modal Whether to display modally or not
  48. */
  49. public StandardDialog(final Window owner, final ModalityType modal) {
  50. super(owner, modal);
  51. this.owner = owner;
  52. if (owner != null) {
  53. setIconImages(owner.getIconImages());
  54. }
  55. orderButtons(new JButton(), new JButton());
  56. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  57. }
  58. @Override
  59. public void setTitle(final String title) {
  60. super.setTitle("DMDirc: " + title);
  61. }
  62. /**
  63. * Displays the dialog if it is not visible, otherwise requests focus.
  64. */
  65. public void displayOrRequestFocus() {
  66. displayOrRequestFocus(owner);
  67. }
  68. /**
  69. * Displays the dialog if it is not visible, otherwise requests focus. The parent window will
  70. * only be changed if the dialog has not been displayed.
  71. *
  72. * @param parent Parent window
  73. */
  74. public void displayOrRequestFocus(final Component parent) {
  75. if (isVisible()) {
  76. requestFocus();
  77. } else {
  78. display(parent);
  79. }
  80. }
  81. /**
  82. * Displays the dialog centering on the specified window.
  83. *
  84. * @param owner Window to center on
  85. */
  86. public void display(final Component owner) {
  87. this.owner = owner;
  88. display();
  89. }
  90. /**
  91. * Displays the dialog centering on the parent window.
  92. */
  93. public void display() {
  94. if (isVisible()) {
  95. return;
  96. }
  97. addWindowListener(new WindowAdapter() {
  98. @Override
  99. public void windowClosing(final WindowEvent e) {
  100. executeAction(getCancelButton());
  101. }
  102. });
  103. centreOnOwner();
  104. pack();
  105. centreOnOwner();
  106. setVisible(false);
  107. setVisible(true);
  108. }
  109. /**
  110. * Centres this dialog on its owner, or the screen if no owner is present.
  111. */
  112. public void centreOnOwner() {
  113. if (owner == null) {
  114. CoreUIUtils.centreWindow(this);
  115. } else {
  116. setLocationRelativeTo(owner);
  117. }
  118. }
  119. /**
  120. * Returns the window owner for this dialog.
  121. *
  122. * @return Parent window or null
  123. */
  124. public Component getParentWindow() {
  125. return owner;
  126. }
  127. /**
  128. * Sets the specified button up as the OK button.
  129. *
  130. * @param button The target button
  131. */
  132. protected void setOkButton(final JButton button) {
  133. okButton = button;
  134. button.setText("OK");
  135. button.setDefaultCapable(false);
  136. }
  137. /**
  138. * Sets the specified button up as the Cancel button.
  139. *
  140. * @param button The target button
  141. */
  142. protected void setCancelButton(final JButton button) {
  143. cancelButton = button;
  144. button.setText("Cancel");
  145. button.setDefaultCapable(false);
  146. }
  147. /**
  148. * Gets the left hand button for a dialog.
  149. *
  150. * @return left JButton
  151. */
  152. protected JButton getLeftButton() {
  153. if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  154. return getOkButton();
  155. } else {
  156. return getCancelButton();
  157. }
  158. }
  159. /**
  160. * Gets the right hand button for a dialog.
  161. *
  162. * @return right JButton
  163. */
  164. protected JButton getRightButton() {
  165. if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  166. return getCancelButton();
  167. } else {
  168. return getOkButton();
  169. }
  170. }
  171. /**
  172. * Orders the OK and Cancel buttons in an appropriate order for the current operating system.
  173. *
  174. * @param leftButton The left-most button
  175. * @param rightButton The right-most button
  176. */
  177. protected void orderButtons(final JButton leftButton,
  178. final JButton rightButton) {
  179. if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  180. // Windows - put the OK button on the left
  181. setOkButton(leftButton);
  182. setCancelButton(rightButton);
  183. } else {
  184. // Everything else - adhere to usability guidelines and put it on
  185. // the right.
  186. setOkButton(rightButton);
  187. setCancelButton(leftButton);
  188. }
  189. leftButton.setPreferredSize(new Dimension(100, 25));
  190. rightButton.setPreferredSize(new Dimension(100, 25));
  191. leftButton.setMinimumSize(new Dimension(100, 25));
  192. rightButton.setMinimumSize(new Dimension(100, 25));
  193. }
  194. /**
  195. * Retrieves the OK button for this form.
  196. *
  197. * @return The form's OK button
  198. */
  199. public JButton getOkButton() {
  200. return okButton;
  201. }
  202. /**
  203. * Retrieves the Cancel button for this form.
  204. *
  205. * @return The form's cancel button
  206. */
  207. public JButton getCancelButton() {
  208. return cancelButton;
  209. }
  210. /**
  211. * Simulates the user clicking on the specified target button.
  212. *
  213. * @param target The button to use
  214. */
  215. public void executeAction(final JButton target) {
  216. if (target != null && target.isEnabled()) {
  217. target.doClick();
  218. }
  219. }
  220. /**
  221. * This method is called when enter is pressed anywhere in the dialog except on a button. By
  222. * default this method does nothing.
  223. *
  224. * @return Returns true if the key press has been handled and is not be be forwarded on by
  225. * default this is false
  226. */
  227. public boolean enterPressed() {
  228. return false;
  229. }
  230. /**
  231. * This method is called when ctrl + enter is pressed anywhere in the dialog. By default this
  232. * method presses the OK button.
  233. *
  234. * @return Returns true if the key press has been handled and is not be be forwarded on by
  235. * default this is true
  236. */
  237. public boolean ctrlEnterPressed() {
  238. executeAction(getOkButton());
  239. return true;
  240. }
  241. /**
  242. * This method is called when enter is pressed anywhere in the dialog except on a button. By
  243. * default this method presses the cancel button.
  244. *
  245. * @return Returns true if the key press has been handled and is not be be forwarded on by
  246. * default this is true
  247. */
  248. public boolean escapePressed() {
  249. executeAction(getCancelButton());
  250. return true;
  251. }
  252. }