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

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