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 7.8KB

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