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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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.addons.ui_swing.injection.MainWindow;
  20. import com.dmdirc.config.prefs.CategoryChangeListener;
  21. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  22. import com.dmdirc.config.prefs.PreferencesCategory;
  23. import com.dmdirc.config.prefs.PreferencesDialogModel;
  24. import com.dmdirc.config.prefs.PreferencesInterface;
  25. import com.dmdirc.config.prefs.PreferencesSetting;
  26. import com.dmdirc.config.prefs.PreferencesType;
  27. import com.dmdirc.config.prefs.SettingChangeListener;
  28. import com.dmdirc.events.ClientPrefsOpenedEvent;
  29. import com.dmdirc.events.eventbus.EventBus;
  30. import com.dmdirc.interfaces.config.IdentityController;
  31. import com.dmdirc.plugins.PluginDomain;
  32. import com.dmdirc.plugins.PluginInfo;
  33. import com.dmdirc.ui.messages.ColourManager;
  34. import com.dmdirc.ui.messages.ColourManagerFactory;
  35. import com.dmdirc.util.validators.NumericalValidator;
  36. import com.dmdirc.util.validators.OptionalValidator;
  37. import java.awt.Window;
  38. import java.util.ArrayList;
  39. import java.util.HashMap;
  40. import java.util.LinkedList;
  41. import java.util.List;
  42. import java.util.Map;
  43. import java.util.Queue;
  44. import javax.inject.Inject;
  45. import net.engio.mbassy.listener.Handler;
  46. /**
  47. * Class to manage OSD Windows.
  48. */
  49. public class OsdManager implements CategoryChangeListener, PreferencesInterface,
  50. SettingChangeListener {
  51. /** The frame the OSD will be associated with. */
  52. private final Window mainFrame;
  53. /** List of OSD Windows. */
  54. private final List<OsdWindow> windowList = new ArrayList<>();
  55. /** List of messages to be queued. */
  56. private final Queue<QueuedMessage> windowQueue = new LinkedList<>();
  57. /** This plugin's settings domain. */
  58. private final String domain;
  59. /** Config OSD Window. */
  60. private OsdWindow osdWindow;
  61. /** X-axis position of OSD. */
  62. private int x;
  63. /** Y-axis potion of OSD. */
  64. private int y;
  65. /** Setting objects with registered change listeners. */
  66. private PreferencesSetting fontSizeSetting;
  67. private PreferencesSetting backgroundSetting;
  68. private PreferencesSetting foregroundSetting;
  69. private PreferencesSetting widthSetting;
  70. private PreferencesSetting timeoutSetting;
  71. private PreferencesSetting maxWindowsSetting;
  72. /** This plugin's plugin info. */
  73. private final PluginInfo pluginInfo;
  74. private final EventBus eventBus;
  75. /** The controller to read/write settings with. */
  76. private final IdentityController identityController;
  77. /** The manager to use to parse colours. */
  78. private final ColourManager colourManager;
  79. @Inject
  80. public OsdManager(
  81. @MainWindow final Window mainFrame,
  82. final EventBus eventBus,
  83. final IdentityController identityController,
  84. final ColourManagerFactory colourManagerFactory,
  85. @PluginDomain(OsdPlugin.class) final PluginInfo pluginInfo) {
  86. this.mainFrame = mainFrame;
  87. this.eventBus = eventBus;
  88. this.identityController = identityController;
  89. this.colourManager = colourManagerFactory.getColourManager(identityController.getGlobalConfiguration());
  90. this.pluginInfo = pluginInfo;
  91. this.domain = pluginInfo.getDomain();
  92. }
  93. /**
  94. * Add messages to the queue and call displayWindows.
  95. *
  96. * @param timeout Time message will be displayed
  97. * @param message Message to be displayed.
  98. */
  99. public void showWindow(final int timeout, final String message) {
  100. windowQueue.add(new QueuedMessage(timeout, message));
  101. displayWindows();
  102. }
  103. /**
  104. * Displays as many windows as appropriate.
  105. */
  106. private synchronized void displayWindows() {
  107. final Integer maxWindows = identityController.getGlobalConfiguration()
  108. .getOptionInt(domain, "maxWindows", false);
  109. QueuedMessage nextItem;
  110. while ((maxWindows == null || getWindowCount() < maxWindows)
  111. && (nextItem = windowQueue.poll()) != null) {
  112. displayWindow(nextItem.getTimeout(), nextItem.getMessage());
  113. }
  114. }
  115. /**
  116. * Create a new OSD window with "message".
  117. * <p>
  118. * This method needs to be synchronised to ensure that the window list is not modified in
  119. * between the invocation of
  120. * {@link OsdPolicy#getYPosition(OsdManager, int)} and the point at which
  121. * the {@link OsdWindow} is added to the windowList.
  122. *
  123. * @see OsdPolicy#getYPosition(OsdManager, int)
  124. * @param message Text to display in the OSD window.
  125. */
  126. private synchronized void displayWindow(final int timeout, final String message) {
  127. final OsdPolicy policy = OsdPolicy.valueOf(
  128. identityController.getGlobalConfiguration().getOption(domain, "newbehaviour")
  129. .toUpperCase());
  130. final int startY = identityController.getGlobalConfiguration()
  131. .getOptionInt(domain, "locationY");
  132. windowList.add(UIUtilities.invokeAndWait(() -> new OsdWindow(
  133. mainFrame,
  134. identityController, this, colourManager,
  135. timeout, message, false,
  136. identityController.getGlobalConfiguration().getOptionInt(
  137. domain, "locationX"), policy.getYPosition(this, startY), domain)));
  138. }
  139. /**
  140. * Destroy the given OSD Window and check if the Queue has items, if so Display them.
  141. *
  142. * @param window The window that we are destroying.
  143. */
  144. public synchronized void closeWindow(final OsdWindow window) {
  145. final OsdPolicy policy = OsdPolicy.valueOf(
  146. identityController.getGlobalConfiguration()
  147. .getOption(domain, "newbehaviour").toUpperCase());
  148. int oldY = window.getDesiredY();
  149. final int closedIndex = windowList.indexOf(window);
  150. if (closedIndex == -1) {
  151. return;
  152. }
  153. windowList.remove(window);
  154. UIUtilities.invokeLater(window::dispose);
  155. final List<OsdWindow> newList = getWindowList();
  156. for (OsdWindow otherWindow : newList.subList(closedIndex, newList.size())) {
  157. final int currentY = otherWindow.getDesiredY();
  158. if (policy.changesPosition()) {
  159. otherWindow.setDesiredLocation(otherWindow.getDesiredX(), oldY);
  160. oldY = currentY;
  161. }
  162. }
  163. displayWindows();
  164. }
  165. /**
  166. * Destroy all OSD Windows.
  167. */
  168. public void closeAll() {
  169. getWindowList().forEach(this::closeWindow);
  170. }
  171. /**
  172. * Get the list of current OSDWindows.
  173. *
  174. * @return a List of all currently open OSDWindows.
  175. */
  176. public List<OsdWindow> getWindowList() {
  177. return new ArrayList<>(windowList);
  178. }
  179. /**
  180. * Get the count of open windows.
  181. *
  182. * @return Current number of OSD Windows open.
  183. */
  184. public int getWindowCount() {
  185. return windowList.size();
  186. }
  187. @Handler
  188. public void showConfig(final ClientPrefsOpenedEvent event) {
  189. final PreferencesDialogModel manager = event.getModel();
  190. x = identityController.getGlobalConfiguration()
  191. .getOptionInt(domain, "locationX");
  192. y = identityController.getGlobalConfiguration()
  193. .getOptionInt(domain, "locationY");
  194. final PreferencesCategory category = new PluginPreferencesCategory(
  195. pluginInfo, "OSD",
  196. "General configuration for OSD plugin.", "category-osd");
  197. fontSizeSetting = new PreferencesSetting(PreferencesType.INTEGER,
  198. domain, "fontSize", "Font size", "Changes the font " + "size of the OSD",
  199. manager.getConfigManager(),
  200. manager.getIdentity()).registerChangeListener(this);
  201. backgroundSetting = new PreferencesSetting(PreferencesType.COLOUR,
  202. domain, "bgcolour", "Background colour",
  203. "Background colour for the OSD", manager.getConfigManager(),
  204. manager.getIdentity()).registerChangeListener(this);
  205. foregroundSetting = new PreferencesSetting(PreferencesType.COLOUR,
  206. domain, "fgcolour", "Foreground colour",
  207. "Foreground colour for the OSD", manager.getConfigManager(),
  208. manager.getIdentity()).registerChangeListener(this);
  209. widthSetting = new PreferencesSetting(PreferencesType.INTEGER,
  210. domain, "width", "OSD Width", "Width of the OSD Window",
  211. manager.getConfigManager(), manager.getIdentity())
  212. .registerChangeListener(this);
  213. timeoutSetting = new PreferencesSetting(PreferencesType.OPTIONALINTEGER,
  214. new OptionalValidator(new NumericalValidator(1, Integer.MAX_VALUE)),
  215. domain, "timeout", "Timeout", "Length of time in "
  216. + "seconds before the OSD window closes", manager.getConfigManager(),
  217. manager.getIdentity());
  218. maxWindowsSetting = new PreferencesSetting(PreferencesType.OPTIONALINTEGER,
  219. new OptionalValidator(new NumericalValidator(1, Integer.MAX_VALUE)),
  220. domain, "maxWindows", "Maximum open windows",
  221. "Maximum number of OSD windows that will be displayed at any given time",
  222. manager.getConfigManager(), manager.getIdentity());
  223. category.addSetting(fontSizeSetting);
  224. category.addSetting(backgroundSetting);
  225. category.addSetting(foregroundSetting);
  226. category.addSetting(widthSetting);
  227. category.addSetting(timeoutSetting);
  228. category.addSetting(maxWindowsSetting);
  229. final Map<String, String> posOptions = new HashMap<>();
  230. //Populate policy MULTICHOICE
  231. for (OsdPolicy policy : OsdPolicy.values()) {
  232. posOptions.put(policy.name(), policy.getDescription());
  233. }
  234. category.addSetting(new PreferencesSetting(domain, "newbehaviour",
  235. "New window policy", "What to do when an OSD Window "
  236. + "is opened when there are other, existing windows open",
  237. posOptions, manager.getConfigManager(), manager.getIdentity()));
  238. category.addChangeListener(this);
  239. manager.getCategory("Plugins").addSubCategory(category);
  240. manager.registerSaveListener(this);
  241. }
  242. @Override
  243. public void categorySelected(final PreferencesCategory category) {
  244. osdWindow = new OsdWindow(mainFrame, identityController, this, colourManager,
  245. -1, "Please drag this OSD to position", true, x, y, domain);
  246. osdWindow.setBackgroundColour(backgroundSetting.getValue());
  247. osdWindow.setForegroundColour(foregroundSetting.getValue());
  248. osdWindow.setFontSize(Integer.parseInt(fontSizeSetting.getValue()));
  249. }
  250. @Override
  251. public void categoryDeselected(final PreferencesCategory category) {
  252. x = osdWindow.getLocationOnScreen().x;
  253. y = osdWindow.getLocationOnScreen().y;
  254. osdWindow.dispose();
  255. osdWindow = null;
  256. }
  257. @Override
  258. public void save() {
  259. identityController.getUserSettings().setOption(domain, "locationX", x);
  260. identityController.getUserSettings().setOption(domain, "locationY", y);
  261. }
  262. @Override
  263. public void settingChanged(final PreferencesSetting setting) {
  264. if (osdWindow == null) {
  265. // They've changed categories but are somehow poking settings.
  266. // Ignore the request.
  267. return;
  268. }
  269. if (setting.equals(fontSizeSetting)) {
  270. osdWindow.setFontSize(Integer.parseInt(setting.getValue()));
  271. } else if (setting.equals(backgroundSetting)) {
  272. osdWindow.setBackgroundColour(setting.getValue());
  273. } else if (setting.equals(foregroundSetting)) {
  274. osdWindow.setForegroundColour(setting.getValue());
  275. } else if (setting.equals(widthSetting)) {
  276. int width = 500;
  277. try {
  278. width = Integer.parseInt(setting.getValue());
  279. } catch (NumberFormatException e) {
  280. //Ignore
  281. }
  282. osdWindow.setSize(width, osdWindow.getHeight());
  283. }
  284. }
  285. public void onLoad() {
  286. eventBus.subscribe(this);
  287. }
  288. public void onUnload() {
  289. eventBus.unsubscribe(this);
  290. }
  291. }