Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

WarningDialog.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.ui;
  18. import java.awt.BorderLayout;
  19. import java.awt.Font;
  20. import java.awt.Insets;
  21. import java.awt.Toolkit;
  22. import java.awt.Window;
  23. import java.awt.event.WindowAdapter;
  24. import java.awt.event.WindowEvent;
  25. import java.util.concurrent.Semaphore;
  26. import javax.swing.BorderFactory;
  27. import javax.swing.JButton;
  28. import javax.swing.JDialog;
  29. import javax.swing.JPanel;
  30. import javax.swing.JTextPane;
  31. import javax.swing.SwingUtilities;
  32. import javax.swing.UIManager;
  33. import javax.swing.text.html.HTMLDocument;
  34. import javax.swing.text.html.HTMLEditorKit;
  35. /**
  36. * Simple Dialog to inform the user there are no UI plugins.
  37. */
  38. public class WarningDialog extends JDialog {
  39. /** Dialog heading text. */
  40. public static final String NO_UIS_TITLE = "No UIs Found";
  41. /** Dialog body text. */
  42. public static final String NO_UIS_BODY = "DMDirc cannot find any UI "
  43. + "plugins, which are required for you to use DMDirc. You can "
  44. + "either download a UI plugin or extract one from the jar. DMDirc "
  45. + "will now exit";
  46. /** Alternative Dialog heading text. */
  47. public static final String NO_COMPAT_UIS_TITLE = "No compatible UIs Found";
  48. /** Alternative Dialog body text. */
  49. public static final String NO_COMPAT_UIS_BODY = "DMDirc did not find any "
  50. + "compatible UI plugins, which are required for you to use "
  51. + "DMDirc. The bundled UI plugins have automatically been "
  52. + "extracted from the jar, and your UI has been reset to the "
  53. + "default swing UI. DMDirc will now attempt to restart. If you "
  54. + "are not using the launcher you will need to restart DMDirc "
  55. + "manually.";
  56. /** Another alternative Dialog body text! */
  57. public static final String NO_RECOV_UIS = "DMDirc did not find any "
  58. + "compatible UI plugins, which are required for you to use DMDirc."
  59. + " The bundled UI plugins were automatically extracted from the "
  60. + "jar, but this did not fix the problem. DMDirc is unable to "
  61. + "continue and will now exit.";
  62. /** A version number for this class. */
  63. private static final long serialVersionUID = -528603916540455179L;
  64. /** Create a new NoUIDialog. */
  65. public WarningDialog() {
  66. this(NO_UIS_TITLE, NO_UIS_BODY);
  67. }
  68. /**
  69. * Create a new NoUIDialog.
  70. *
  71. * @param title Title of dialog
  72. * @param body Body of dialog
  73. */
  74. public WarningDialog(final String title, final String body) {
  75. super((Window) null);
  76. setTitle("DMDirc: " + title);
  77. setIconImage(Toolkit.getDefaultToolkit().getImage(
  78. Thread.currentThread().getContextClassLoader()
  79. .getResource("com/dmdirc/res/logo.png")));
  80. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  81. setResizable(false);
  82. final JPanel panel = new JPanel(new BorderLayout(5, 5));
  83. final JButton button = new JButton("OK");
  84. button.addActionListener(e -> dispose());
  85. final JTextPane textArea = new JTextPane(new HTMLDocument());
  86. textArea.setEditorKit(new HTMLEditorKit());
  87. textArea.setOpaque(false);
  88. textArea.setEditable(false);
  89. textArea.setHighlighter(null);
  90. textArea.setMargin(new Insets(5, 5, 5, 5));
  91. final HTMLDocument doc = (HTMLDocument) textArea.getDocument();
  92. final Font font = UIManager.getFont("Label.font");
  93. doc.getStyleSheet().addRule("body "
  94. + "{ font-family: " + font.getFamily() + "; " + "font-size: "
  95. + font.getSize() + "pt; text-align: center; }");
  96. doc.getStyleSheet().addRule("h1 "
  97. + "{ font-family: " + font.getFamily() + "; "
  98. + "font-size: 1.5em; padding: 0; margin: 0}");
  99. doc.getStyleSheet().addRule("p { text-align: justify; }");
  100. textArea.setText("<h1>" + title + "</h1><p>" + body + "</p>");
  101. panel.add(textArea, BorderLayout.CENTER);
  102. panel.add(button, BorderLayout.SOUTH);
  103. panel.setBorder(BorderFactory.createCompoundBorder(
  104. BorderFactory.createEmptyBorder(5, 5, 5, 5),
  105. panel.getBorder()));
  106. add(panel);
  107. }
  108. /**
  109. * Static method to instantiate and display the dialog.
  110. */
  111. public void display() {
  112. SwingUtilities.invokeLater(() -> {
  113. setSize(400, 400);
  114. CoreUIUtils.centreWindow(this);
  115. setVisible(true);
  116. });
  117. }
  118. /**
  119. * Static method to instantiate and display the dialog, blocking until it is closed.
  120. */
  121. public void displayBlocking() {
  122. final Semaphore semaphore = new Semaphore(0);
  123. SwingUtilities.invokeLater(() -> addWindowListener(new WindowAdapter() {
  124. @Override
  125. public void windowClosed(final WindowEvent e) {
  126. semaphore.release();
  127. }
  128. }));
  129. display();
  130. semaphore.acquireUninterruptibly();
  131. }
  132. }