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

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