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.

WarningDialog.java 6.0KB

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