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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.osd;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.interfaces.config.IdentityController;
  20. import com.dmdirc.util.colours.Colour;
  21. import com.dmdirc.ui.messages.ColourManager;
  22. import java.awt.Color;
  23. import java.awt.Point;
  24. import java.awt.Window;
  25. import java.awt.event.MouseEvent;
  26. import java.awt.event.MouseListener;
  27. import java.awt.event.MouseMotionListener;
  28. import java.util.Timer;
  29. import java.util.TimerTask;
  30. import javax.swing.JDialog;
  31. import javax.swing.JLabel;
  32. import javax.swing.JPanel;
  33. import javax.swing.SwingConstants;
  34. import javax.swing.WindowConstants;
  35. import javax.swing.border.LineBorder;
  36. import net.miginfocom.swing.MigLayout;
  37. /**
  38. * The OSD Window is an always-on-top window designed to convey information about events to the
  39. * user.
  40. */
  41. public class OsdWindow extends JDialog implements MouseListener, MouseMotionListener {
  42. /** A version number for this class. */
  43. private static final long serialVersionUID = 2;
  44. /** The OSD Manager that owns this window. */
  45. private final OsdManager osdManager;
  46. /** The manager to use to parse colours. */
  47. private final ColourManager colourManager;
  48. /** OSD Label. */
  49. private final JLabel label;
  50. /** OSD Panel. */
  51. private final JPanel panel;
  52. /** Starting positions of the mouse. */
  53. private int startX;
  54. private int startY;
  55. /** Desired position. */
  56. private volatile int desiredX;
  57. private volatile int desiredY;
  58. /** Is this a config instance? */
  59. private final boolean config;
  60. /** Timeout before the windows are automatically closed */
  61. private final Integer timeout;
  62. /**
  63. * Creates a new instance of OsdWindow.
  64. *
  65. * @param mainWindow The frame that parents this window.
  66. * @param identityController The controller to read/write settings with.
  67. * @param colourManager The manager to use to parse colours.
  68. * @param timeout Timeout period for the window. Set to -1 to use value from config
  69. * @param text The text to be displayed in the OSD window
  70. * @param config Is the window being configured (should it timeout and allow itself
  71. * to be moved)
  72. * @param x The x-axis position for the OSD Window
  73. * @param y The y-axis position for the OSD window
  74. * @param osdManager The manager that owns this OSD Window
  75. * @param domain This plugin's settings domain
  76. */
  77. public OsdWindow(
  78. final Window mainWindow,
  79. final IdentityController identityController,
  80. final OsdManager osdManager, final ColourManager colourManager,
  81. final int timeout, final String text, final boolean config, final int x,
  82. final int y, final String domain) {
  83. super(mainWindow, ModalityType.MODELESS);
  84. this.colourManager = colourManager;
  85. this.config = config;
  86. this.osdManager = osdManager;
  87. if (timeout < 0) {
  88. this.timeout = identityController.getGlobalConfiguration().getOptionInt(domain,
  89. "timeout", false);
  90. } else {
  91. this.timeout = timeout;
  92. }
  93. setFocusableWindowState(false);
  94. setAlwaysOnTop(true);
  95. setResizable(false);
  96. setUndecorated(true);
  97. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  98. desiredX = x;
  99. desiredY = y;
  100. setLocation(x, y);
  101. panel = new JPanel();
  102. panel.setBorder(new LineBorder(Color.BLACK));
  103. panel.setBackground(UIUtilities.convertColour(
  104. colourManager.getColourFromString(identityController.getGlobalConfiguration()
  105. .getOptionString(domain, "bgcolour"), null)));
  106. final int width = identityController.getGlobalConfiguration().getOptionInt(domain, "width");
  107. setContentPane(panel);
  108. setLayout(new MigLayout("wmin " + width + ", wmax " + width + ", ins rel, fill"));
  109. label = new JLabel(text);
  110. label.setForeground(UIUtilities.convertColour(
  111. colourManager.getColourFromString(identityController.getGlobalConfiguration()
  112. .getOptionString(domain, "fgcolour"), null)));
  113. label.setFont(label.getFont().deriveFont((float) identityController
  114. .getGlobalConfiguration().getOptionInt(domain, "fontSize")));
  115. label.setHorizontalAlignment(SwingConstants.CENTER);
  116. add(label, "alignx center, hmin " + label.getFont().getSize());
  117. setVisible(true);
  118. pack();
  119. if (config) {
  120. addMouseMotionListener(this);
  121. addMouseListener(this);
  122. } else {
  123. addMouseListener(this);
  124. if (this.timeout != null && this.timeout > 0) {
  125. new Timer("OSD Display Timer").schedule(new TimerTask() {
  126. @Override
  127. public void run() {
  128. osdManager.closeWindow(OsdWindow.this);
  129. }
  130. }, this.timeout * 1000);
  131. }
  132. }
  133. }
  134. @Override
  135. public void mouseClicked(final MouseEvent e) {
  136. if (!config) {
  137. osdManager.closeWindow(this);
  138. }
  139. }
  140. @Override
  141. public void mousePressed(final MouseEvent e) {
  142. if (config) {
  143. startX = e.getPoint().x;
  144. startY = e.getPoint().y;
  145. }
  146. }
  147. @Override
  148. public void mouseReleased(final MouseEvent e) {
  149. // Do nothing
  150. }
  151. @Override
  152. public void mouseEntered(final MouseEvent e) {
  153. // Do nothing
  154. }
  155. @Override
  156. public void mouseExited(final MouseEvent e) {
  157. // Do nothing
  158. }
  159. @Override
  160. public void mouseDragged(final MouseEvent e) {
  161. final Point p = e.getLocationOnScreen();
  162. p.translate(-1 * startX, -1 * startY);
  163. setLocation(p);
  164. }
  165. @Override
  166. public void mouseMoved(final MouseEvent e) {
  167. // Do nothing
  168. }
  169. /**
  170. * Sets the font size that this OSD uses.
  171. *
  172. * @param size The new size of the font
  173. */
  174. public void setFontSize(final int size) {
  175. label.setFont(label.getFont().deriveFont((float) size));
  176. }
  177. /**
  178. * Sets the background colour for this OSD.
  179. *
  180. * @param colour The background colour to use
  181. */
  182. public void setBackgroundColour(final String colour) {
  183. panel.setBackground(UIUtilities.convertColour(colourManager.getColourFromString(colour,
  184. Colour.WHITE)));
  185. }
  186. /**
  187. * Sets the foreground colour for this OSD.
  188. *
  189. * @param colour The foreground colour to use
  190. */
  191. public void setForegroundColour(final String colour) {
  192. label.setForeground(UIUtilities.convertColour(colourManager.getColourFromString(colour,
  193. Colour.WHITE)));
  194. }
  195. @Override
  196. public void setVisible(final boolean b) {
  197. super.setVisible(b);
  198. if (b) {
  199. transferFocusBackward();
  200. }
  201. }
  202. /**
  203. * Retrieves the desired x offset of this OSD window.
  204. *
  205. * @since 0.6.3
  206. * @see #setDesiredLocation(int, int)
  207. * @return The desired offset of this window
  208. */
  209. public int getDesiredX() {
  210. return desiredX;
  211. }
  212. /**
  213. * Retrieves the desired y offset of this OSD window.
  214. *
  215. * @since 0.6.3
  216. * @see #setDesiredLocation(int, int)
  217. * @return The desired offset of this window
  218. */
  219. public int getDesiredY() {
  220. return desiredY;
  221. }
  222. /**
  223. * Sets the desired location of this OSD window, and queues an event to move the window to the
  224. * desired location at some point in the future.
  225. * <p>
  226. * This method WILL NOT alter the location immediately, but will schedule an event in the AWT
  227. * event despatch thread which will be executed in the future.
  228. * <p>
  229. * This method will immediately update the values returned by the {@link #getDesiredX()} and
  230. * {@link #getDesiredY()} methods, but the {@link #getX()} and {@link #getY()} methods will
  231. * continue to reflect the actual location of the window.
  232. * <p>
  233. * This method is thread safe.
  234. *
  235. * @param x The desired x offset of this window
  236. * @param y The desired y offset of this window
  237. *
  238. * @since 0.6.3
  239. */
  240. public void setDesiredLocation(final int x, final int y) {
  241. this.desiredX = x;
  242. this.desiredY = y;
  243. UIUtilities.invokeLater(() -> setLocation(getDesiredX(), getDesiredY()));
  244. }
  245. @Override
  246. public String toString() {
  247. return label.getText();
  248. }
  249. }