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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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.MainFrame;
  24. import com.dmdirc.addons.ui_swing.UIUtilities;
  25. import com.dmdirc.interfaces.config.IdentityController;
  26. import com.dmdirc.ui.Colour;
  27. import com.dmdirc.ui.messages.ColourManager;
  28. import java.awt.Color;
  29. import java.awt.Point;
  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 mainFrame The window to associate with.
  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 MainFrame mainFrame,
  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(mainFrame, false);
  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. /** {@inheritDoc} */
  132. @Override
  133. public void run() {
  134. osdManager.closeWindow(OsdWindow.this);
  135. }
  136. }, this.timeout * 1000);
  137. }
  138. }
  139. }
  140. /**
  141. * {@inheritDoc}
  142. *
  143. * @param e Mouse event
  144. */
  145. @Override
  146. public void mouseClicked(final MouseEvent e) {
  147. if (!config) {
  148. osdManager.closeWindow(this);
  149. }
  150. }
  151. /**
  152. * {@inheritDoc}
  153. *
  154. * @param e Mouse event
  155. */
  156. @Override
  157. public void mousePressed(final MouseEvent e) {
  158. if (config) {
  159. startX = e.getPoint().x;
  160. startY = e.getPoint().y;
  161. }
  162. }
  163. /**
  164. * {@inheritDoc}
  165. *
  166. * @param e Mouse event
  167. */
  168. @Override
  169. public void mouseReleased(final MouseEvent e) {
  170. // Do nothing
  171. }
  172. /**
  173. * {@inheritDoc}
  174. *
  175. * @param e Mouse event
  176. */
  177. @Override
  178. public void mouseEntered(final MouseEvent e) {
  179. // Do nothing
  180. }
  181. /**
  182. * {@inheritDoc}
  183. *
  184. * @param e Mouse event
  185. */
  186. @Override
  187. public void mouseExited(final MouseEvent e) {
  188. // Do nothing
  189. }
  190. /**
  191. * {@inheritDoc}
  192. *
  193. * @param e Mouse event
  194. */
  195. @Override
  196. public void mouseDragged(final MouseEvent e) {
  197. final Point p = e.getLocationOnScreen();
  198. p.translate(-1 * startX, -1 * startY);
  199. setLocation(p);
  200. }
  201. /**
  202. * {@inheritDoc}
  203. *
  204. * @param e Mouse event
  205. */
  206. @Override
  207. public void mouseMoved(final MouseEvent e) {
  208. // Do nothing
  209. }
  210. /**
  211. * Sets the font size that this OSD uses.
  212. *
  213. * @param size The new size of the font
  214. */
  215. public void setFontSize(final int size) {
  216. label.setFont(label.getFont().deriveFont((float) size));
  217. }
  218. /**
  219. * Sets the background colour for this OSD.
  220. *
  221. * @param colour The background colour to use
  222. */
  223. public void setBackgroundColour(final String colour) {
  224. panel.setBackground(UIUtilities.convertColour(colourManager.getColourFromString(colour,
  225. Colour.WHITE)));
  226. }
  227. /**
  228. * Sets the foreground colour for this OSD.
  229. *
  230. * @param colour The foreground colour to use
  231. */
  232. public void setForegroundColour(final String colour) {
  233. label.setForeground(UIUtilities.convertColour(colourManager.getColourFromString(colour,
  234. Colour.WHITE)));
  235. }
  236. /** {@inheritDoc} */
  237. @Override
  238. public void setVisible(final boolean b) {
  239. super.setVisible(b);
  240. if (b) {
  241. transferFocusBackward();
  242. }
  243. }
  244. /**
  245. * Retrieves the desired x offset of this OSD window.
  246. *
  247. * @since 0.6.3
  248. * @see #setDesiredLocation(int, int)
  249. * @return The desired offset of this window
  250. */
  251. public int getDesiredX() {
  252. return desiredX;
  253. }
  254. /**
  255. * Retrieves the desired y offset of this OSD window.
  256. *
  257. * @since 0.6.3
  258. * @see #setDesiredLocation(int, int)
  259. * @return The desired offset of this window
  260. */
  261. public int getDesiredY() {
  262. return desiredY;
  263. }
  264. /**
  265. * Sets the desired location of this OSD window, and queues an event to move the window to the
  266. * desired location at some point in the future.
  267. * <p>
  268. * This method WILL NOT alter the location immediately, but will schedule an event in the AWT
  269. * event despatch thread which will be executed in the future.
  270. * <p>
  271. * This method will immediately update the values returned by the {@link #getDesiredX()} and
  272. * {@link #getDesiredY()} methods, but the {@link #getX()} and {@link #getY()} methods will
  273. * continue to reflect the actual location of the window.
  274. * <p>
  275. * This method is thread safe.
  276. *
  277. * @param x The desired x offset of this window
  278. * @param y The desired y offset of this window
  279. *
  280. * @since 0.6.3
  281. */
  282. public void setDesiredLocation(final int x, final int y) {
  283. this.desiredX = x;
  284. this.desiredY = y;
  285. UIUtilities.invokeLater(new Runnable() {
  286. /** {@inheritDoc} */
  287. @Override
  288. public void run() {
  289. setLocation(getDesiredX(), getDesiredY());
  290. }
  291. });
  292. }
  293. /** {@inheritDoc} */
  294. @Override
  295. public String toString() {
  296. return label.getText();
  297. }
  298. }