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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.addons.ui_swing.components.text.TextLabel;
  24. import java.awt.Window;
  25. import java.awt.event.WindowAdapter;
  26. import java.awt.event.WindowEvent;
  27. import java.util.function.BooleanSupplier;
  28. import javax.swing.JButton;
  29. import javax.swing.WindowConstants;
  30. import net.miginfocom.swing.MigLayout;
  31. /**
  32. * Standard input dialog.
  33. */
  34. public class StandardQuestionDialog extends StandardDialog {
  35. /** Serial version UID. */
  36. private static final long serialVersionUID = 1;
  37. /** Message. */
  38. private final String message;
  39. /** Blurb label. */
  40. private TextLabel blurb;
  41. /** Question result. */
  42. private boolean result;
  43. /** Function to call when the dialog is saved. */
  44. private final BooleanSupplier save;
  45. /** Function to call when the dialog is cancelled. */
  46. private final Runnable cancel;
  47. /**
  48. * Instantiates a new standard input dialog.
  49. *
  50. * @param owner Dialog owner
  51. * @param modal modality type
  52. * @param title Dialog title
  53. * @param message Dialog message
  54. */
  55. public StandardQuestionDialog(
  56. final Window owner, final ModalityType modal, final String title,
  57. final String message,
  58. final BooleanSupplier save) {
  59. this(owner, modal, title, message, save, () -> {});
  60. }
  61. /**
  62. * Instantiates a new standard input dialog.
  63. *
  64. * @param owner Dialog owner
  65. * @param modal modality type
  66. * @param title Dialog title
  67. * @param message Dialog message
  68. */
  69. public StandardQuestionDialog(
  70. final Window owner, final ModalityType modal, final String title,
  71. final String message,
  72. final Runnable save) {
  73. this(owner, modal, title, message, () -> {save.run(); return true; }, () -> {});
  74. }
  75. /**
  76. * Instantiates a new standard input dialog.
  77. *
  78. * @param owner Dialog owner
  79. * @param modal modality type
  80. * @param title Dialog title
  81. * @param message Dialog message
  82. */
  83. public StandardQuestionDialog(
  84. final Window owner, final ModalityType modal, final String title,
  85. final String message,
  86. final BooleanSupplier save,
  87. final Runnable cancel) {
  88. super(owner, modal);
  89. this.message = message;
  90. this.save = save;
  91. this.cancel = cancel;
  92. setTitle(title);
  93. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  94. initComponents();
  95. addListeners();
  96. layoutComponents();
  97. }
  98. /**
  99. * Initialises the components.
  100. */
  101. private void initComponents() {
  102. orderButtons(new JButton(), new JButton());
  103. getOkButton().setText("Yes");
  104. getCancelButton().setText("No");
  105. blurb = new TextLabel(message);
  106. }
  107. /**
  108. * Adds the listeners.
  109. */
  110. private void addListeners() {
  111. getOkButton().addActionListener(e -> {
  112. if (save.getAsBoolean()) {
  113. result = true;
  114. dispose();
  115. }
  116. });
  117. getCancelButton().addActionListener(e -> {
  118. cancel.run();
  119. dispose();
  120. });
  121. addWindowListener(new WindowAdapter() {
  122. @Override
  123. public void windowOpened(final WindowEvent e) {
  124. //Ignore
  125. }
  126. @Override
  127. public void windowClosed(final WindowEvent e) {
  128. if (!result) {
  129. cancel.run();
  130. }
  131. }
  132. });
  133. }
  134. /**
  135. * Lays out the components.
  136. */
  137. private void layoutComponents() {
  138. setLayout(new MigLayout("fill, wrap 1, hidemode 3"));
  139. add(blurb, "growx");
  140. add(getLeftButton(), "split 2, right");
  141. add(getRightButton(), "right");
  142. }
  143. /**
  144. * Returns the result of the question.
  145. *
  146. * @return true iif the user pressed Yes
  147. */
  148. public boolean getResult() {
  149. return result;
  150. }
  151. }