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

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