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.

OsdManager.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.plugins.PluginInfo;
  26. import com.dmdirc.ui.messages.ColourManager;
  27. import java.awt.Window;
  28. import java.util.ArrayList;
  29. import java.util.LinkedList;
  30. import java.util.List;
  31. import java.util.Queue;
  32. /**
  33. * Class to manage OSD Windows.
  34. */
  35. public class OsdManager {
  36. /** The frame the OSD will be associated with. */
  37. private final Window mainFrame;
  38. /** The controller to read/write settings with. */
  39. private final IdentityController identityController;
  40. /** The colour manager to use to parse colours. */
  41. private final ColourManager colourManager;
  42. /** List of OSD Windows. */
  43. private final List<OsdWindow> windowList = new ArrayList<>();
  44. /** List of messages to be queued. */
  45. private final Queue<QueuedMessage> windowQueue = new LinkedList<>();
  46. /** This plugin's settings domain. */
  47. private final String domain;
  48. public OsdManager(final Window mainFrame, final IdentityController identityController,
  49. final ColourManager colourManager, final PluginInfo pluginInfo) {
  50. this.mainFrame = mainFrame;
  51. this.identityController = identityController;
  52. this.colourManager = colourManager;
  53. this.domain = pluginInfo.getDomain();
  54. }
  55. /**
  56. * Add messages to the queue and call displayWindows.
  57. *
  58. * @param timeout Time message will be displayed
  59. * @param message Message to be displayed.
  60. */
  61. public void showWindow(final int timeout, final String message) {
  62. windowQueue.add(new QueuedMessage(timeout, message));
  63. displayWindows();
  64. }
  65. /**
  66. * Displays as many windows as appropriate.
  67. */
  68. private synchronized void displayWindows() {
  69. final Integer maxWindows = identityController.getGlobalConfiguration()
  70. .getOptionInt(domain, "maxWindows", false);
  71. QueuedMessage nextItem;
  72. while ((maxWindows == null || getWindowCount() < maxWindows)
  73. && (nextItem = windowQueue.poll()) != null) {
  74. displayWindow(nextItem.getTimeout(), nextItem.getMessage());
  75. }
  76. }
  77. /**
  78. * Create a new OSD window with "message".
  79. * <p>
  80. * This method needs to be synchronised to ensure that the window list is not modified in
  81. * between the invocation of
  82. * {@link OsdPolicy#getYPosition(OsdManager, int)} and the point at which
  83. * the {@link OsdWindow} is added to the windowList.
  84. *
  85. * @see OsdPolicy#getYPosition(OsdManager, int)
  86. * @param message Text to display in the OSD window.
  87. */
  88. private synchronized void displayWindow(final int timeout, final String message) {
  89. final OsdPolicy policy = OsdPolicy.valueOf(identityController.getGlobalConfiguration()
  90. .getOption(domain, "newbehaviour").toUpperCase());
  91. final int startY = identityController.getGlobalConfiguration()
  92. .getOptionInt(domain, "locationY");
  93. windowList.add(UIUtilities.invokeAndWait(() -> new OsdWindow(
  94. mainFrame,
  95. identityController, this, colourManager,
  96. timeout, message, false,
  97. identityController.getGlobalConfiguration().getOptionInt(
  98. domain, "locationX"), policy.getYPosition(this, startY), domain)));
  99. }
  100. /**
  101. * Destroy the given OSD Window and check if the Queue has items, if so Display them.
  102. *
  103. * @param window The window that we are destroying.
  104. */
  105. public synchronized void closeWindow(final OsdWindow window) {
  106. final OsdPolicy policy = OsdPolicy.valueOf(
  107. identityController.getGlobalConfiguration()
  108. .getOption(domain, "newbehaviour").toUpperCase());
  109. int oldY = window.getDesiredY();
  110. final int closedIndex = windowList.indexOf(window);
  111. if (closedIndex == -1) {
  112. return;
  113. }
  114. windowList.remove(window);
  115. UIUtilities.invokeLater(window::dispose);
  116. final List<OsdWindow> newList = getWindowList();
  117. for (OsdWindow otherWindow : newList.subList(closedIndex, newList.size())) {
  118. final int currentY = otherWindow.getDesiredY();
  119. if (policy.changesPosition()) {
  120. otherWindow.setDesiredLocation(otherWindow.getDesiredX(), oldY);
  121. oldY = currentY;
  122. }
  123. }
  124. displayWindows();
  125. }
  126. /**
  127. * Destroy all OSD Windows.
  128. */
  129. public void closeAll() {
  130. getWindowList().forEach(this::closeWindow);
  131. }
  132. /**
  133. * Get the list of current OSDWindows.
  134. *
  135. * @return a List of all currently open OSDWindows.
  136. */
  137. public List<OsdWindow> getWindowList() {
  138. return new ArrayList<>(windowList);
  139. }
  140. /**
  141. * Get the count of open windows.
  142. *
  143. * @return Current number of OSD Windows open.
  144. */
  145. public int getWindowCount() {
  146. return windowList.size();
  147. }
  148. }