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.

StandardQuestionDialog.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.addons.ui_swing.components.text.TextLabel;
  24. import java.awt.Window;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.ActionListener;
  27. import java.awt.event.WindowAdapter;
  28. import java.awt.event.WindowEvent;
  29. import javax.swing.JButton;
  30. import net.miginfocom.swing.MigLayout;
  31. /**
  32. * Standard input dialog.
  33. */
  34. public abstract class StandardQuestionDialog extends StandardDialog {
  35. /** Blurb label. */
  36. private TextLabel blurb;
  37. /** Message. */
  38. private String message;
  39. /**
  40. * Instantiates a new standard input dialog.
  41. *
  42. * @param owner Dialog owner
  43. * @param modal modality type
  44. * @param title Dialog title
  45. * @param message Dialog message
  46. */
  47. public StandardQuestionDialog(Window owner, ModalityType modal,
  48. final String title, final String message) {
  49. super(owner, modal);
  50. this.message = message;
  51. setTitle(title);
  52. setDefaultCloseOperation(StandardInputDialog.DISPOSE_ON_CLOSE);
  53. initComponents();
  54. addListeners();
  55. layoutComponents();
  56. }
  57. /**
  58. * Called when the dialog's OK button is clicked.
  59. *
  60. * @return whether the dialog can close
  61. */
  62. public abstract boolean save();
  63. /**
  64. * Called when the dialog's cancel button is clicked, or otherwise closed.
  65. */
  66. public abstract void cancelled();
  67. /**
  68. * Initialises the components.
  69. */
  70. private final void initComponents() {
  71. orderButtons(new JButton(), new JButton());
  72. getOkButton().setText("Yes");
  73. getCancelButton().setText("No");
  74. blurb = new TextLabel(message);
  75. }
  76. /**
  77. * Adds the listeners
  78. */
  79. private final void addListeners() {
  80. getOkButton().addActionListener(new ActionListener() {
  81. /** {@inheritDoc} */
  82. @Override
  83. public void actionPerformed(ActionEvent e) {
  84. if (save()) {
  85. dispose();
  86. }
  87. }
  88. });
  89. getCancelButton().addActionListener(new ActionListener() {
  90. /** {@inheritDoc} */
  91. @Override
  92. public void actionPerformed(ActionEvent e) {
  93. cancelled();
  94. dispose();
  95. }
  96. });
  97. addWindowListener(new WindowAdapter() {
  98. /** {@inheritDoc} */
  99. @Override
  100. public void windowOpened(WindowEvent e) {
  101. //Ignore
  102. }
  103. /** {@inheritDoc} */
  104. @Override
  105. public void windowClosed(WindowEvent e) {
  106. cancelled();
  107. }
  108. });
  109. }
  110. /**
  111. * Lays out the components.
  112. */
  113. private final void layoutComponents() {
  114. setLayout(new MigLayout("fill, wrap 1"));
  115. add(blurb, "growx");
  116. add(getLeftButton(), "split 2, right");
  117. add(getRightButton(), "right");
  118. }
  119. }