Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

StandardMessageDialog.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2006-2011 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. * Abstract standard dialog for showing messages.
  33. */
  34. public class StandardMessageDialog extends StandardDialog {
  35. /**
  36. * A version number for this class. It should be changed whenever the class
  37. * structure is changed (or anything else that would prevent serialized
  38. * objects being unserialized with the new class).
  39. */
  40. private static final long serialVersionUID = 1;
  41. /** Message. */
  42. private final String message;
  43. /** Blurb label. */
  44. private TextLabel blurb;
  45. /**
  46. * Instantiates a new standard input dialog.
  47. *
  48. * @param owner Dialog owner
  49. * @param modal modality type
  50. * @param title Dialog title
  51. * @param message Dialog message
  52. */
  53. public StandardMessageDialog(final Window owner, final ModalityType modal,
  54. final String title, final String message) {
  55. super(owner, modal);
  56. this.message = message;
  57. setTitle(title);
  58. setDefaultCloseOperation(StandardInputDialog.DISPOSE_ON_CLOSE);
  59. initComponents();
  60. addListeners();
  61. layoutComponents();
  62. }
  63. /**
  64. * Called when the dialog's OK button is clicked.
  65. *
  66. * @return whether the dialog can close
  67. */
  68. public boolean save() {
  69. return true;
  70. }
  71. /**
  72. * Called when the dialog's cancel button is clicked, or otherwise closed.
  73. */
  74. public void cancelled() {
  75. //Do nothing
  76. }
  77. /**
  78. * Initialises the components.
  79. */
  80. private void initComponents() {
  81. orderButtons(new JButton(), new JButton());
  82. getOkButton().setText("OK");
  83. blurb = new TextLabel(message);
  84. }
  85. /**
  86. * Adds the listeners.
  87. */
  88. private void addListeners() {
  89. final ActionListener listener = new ActionListener() {
  90. /** {@inheritDoc} */
  91. @Override
  92. public void actionPerformed(final ActionEvent e) {
  93. if (save()) {
  94. dispose();
  95. }
  96. }
  97. };
  98. getCancelButton().addActionListener(listener);
  99. getOkButton().addActionListener(listener);
  100. addWindowListener(new WindowAdapter() {
  101. /** {@inheritDoc} */
  102. @Override
  103. public void windowOpened(final WindowEvent e) {
  104. //Ignore
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public void windowClosed(final WindowEvent e) {
  109. cancelled();
  110. }
  111. });
  112. }
  113. /**
  114. * Lays out the components.
  115. */
  116. private void layoutComponents() {
  117. setLayout(new MigLayout("fill, wrap 1, hidemode 3, wmax min(80sp, 500),"
  118. + " wmin min(80sp, 500), pack"));
  119. add(blurb, "growx");
  120. add(getOkButton(), "right");
  121. }
  122. }