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.

OsdWindow.java 9.5KB

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