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.

OsdPlugin.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.SwingController;
  24. import com.dmdirc.config.prefs.CategoryChangeListener;
  25. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  26. import com.dmdirc.config.prefs.PreferencesCategory;
  27. import com.dmdirc.config.prefs.PreferencesDialogModel;
  28. import com.dmdirc.config.prefs.PreferencesInterface;
  29. import com.dmdirc.config.prefs.PreferencesSetting;
  30. import com.dmdirc.config.prefs.PreferencesType;
  31. import com.dmdirc.config.prefs.SettingChangeListener;
  32. import com.dmdirc.interfaces.CommandController;
  33. import com.dmdirc.interfaces.config.IdentityController;
  34. import com.dmdirc.plugins.Exported;
  35. import com.dmdirc.plugins.PluginInfo;
  36. import com.dmdirc.plugins.implementations.BaseCommandPlugin;
  37. import com.dmdirc.ui.messages.ColourManager;
  38. import com.dmdirc.util.validators.NumericalValidator;
  39. import com.dmdirc.util.validators.OptionalValidator;
  40. import java.awt.Frame;
  41. import java.util.HashMap;
  42. import java.util.Map;
  43. /**
  44. * Allows the user to display on-screen-display messages.
  45. */
  46. public class OsdPlugin extends BaseCommandPlugin implements
  47. CategoryChangeListener, PreferencesInterface, SettingChangeListener {
  48. /** Config OSD Window. */
  49. private OsdWindow osdWindow;
  50. /** The OSD Manager that this plugin is using. */
  51. private final OsdManager osdManager;
  52. /** X-axis position of OSD. */
  53. private int x;
  54. /** Y-axis potion of OSD. */
  55. private int y;
  56. /** Setting objects with registered change listeners. */
  57. private PreferencesSetting fontSizeSetting;
  58. private PreferencesSetting backgroundSetting;
  59. private PreferencesSetting foregroundSetting;
  60. private PreferencesSetting widthSetting;
  61. private PreferencesSetting timeoutSetting;
  62. private PreferencesSetting maxWindowsSetting;
  63. /** This plugin's plugin info. */
  64. private final PluginInfo pluginInfo;
  65. /** The controller to read/write settings with. */
  66. private final IdentityController identityController;
  67. /** The manager to use to parse colours. */
  68. private final ColourManager colourManager;
  69. /** The frame that the OSDs will be associated with. */
  70. private final Frame mainFrame;
  71. /** This plugin's settings domain. */
  72. private final String domain;
  73. /**
  74. * Creates a new instance of this plugin.
  75. *
  76. * @param swingController The controller that owns this plugin.
  77. * @param pluginInfo This plugin's plugin info
  78. * @param identityController The controller to use to read and write settings.
  79. * @param commandController Command controller to register commands
  80. * @param colourManager The manager to use to parse colours.
  81. */
  82. public OsdPlugin(
  83. final SwingController swingController,
  84. final PluginInfo pluginInfo,
  85. final IdentityController identityController,
  86. final CommandController commandController,
  87. final ColourManager colourManager) {
  88. this.pluginInfo = pluginInfo;
  89. this.identityController = identityController;
  90. this.colourManager = colourManager;
  91. this.mainFrame = swingController.getMainFrame();
  92. this.domain = pluginInfo.getDomain();
  93. osdManager = new OsdManager(mainFrame, identityController, colourManager, pluginInfo);
  94. registerCommand(new OsdCommand(commandController, osdManager), OsdCommand.INFO);
  95. }
  96. @Override
  97. public void showConfig(final PreferencesDialogModel manager) {
  98. x = identityController.getGlobalConfiguration()
  99. .getOptionInt(domain, "locationX");
  100. y = identityController.getGlobalConfiguration()
  101. .getOptionInt(domain, "locationY");
  102. final PreferencesCategory category = new PluginPreferencesCategory(
  103. pluginInfo, "OSD",
  104. "General configuration for OSD plugin.", "category-osd");
  105. fontSizeSetting = new PreferencesSetting(PreferencesType.INTEGER,
  106. domain, "fontSize", "Font size", "Changes the font " + "size of the OSD",
  107. manager.getConfigManager(),
  108. manager.getIdentity()).registerChangeListener(this);
  109. backgroundSetting = new PreferencesSetting(PreferencesType.COLOUR,
  110. domain, "bgcolour", "Background colour",
  111. "Background colour for the OSD", manager.getConfigManager(),
  112. manager.getIdentity()).registerChangeListener(this);
  113. foregroundSetting = new PreferencesSetting(PreferencesType.COLOUR,
  114. domain, "fgcolour", "Foreground colour",
  115. "Foreground colour for the OSD", manager.getConfigManager(),
  116. manager.getIdentity()).registerChangeListener(this);
  117. widthSetting = new PreferencesSetting(PreferencesType.INTEGER,
  118. domain, "width", "OSD Width", "Width of the OSD Window",
  119. manager.getConfigManager(), manager.getIdentity())
  120. .registerChangeListener(this);
  121. timeoutSetting = new PreferencesSetting(PreferencesType.OPTIONALINTEGER,
  122. new OptionalValidator(new NumericalValidator(1, Integer.MAX_VALUE)),
  123. domain, "timeout", "Timeout", "Length of time in "
  124. + "seconds before the OSD window closes", manager.getConfigManager(),
  125. manager.getIdentity());
  126. maxWindowsSetting = new PreferencesSetting(PreferencesType.OPTIONALINTEGER,
  127. new OptionalValidator(new NumericalValidator(1, Integer.MAX_VALUE)),
  128. domain, "maxWindows", "Maximum open windows",
  129. "Maximum number of OSD windows that will be displayed at any given time",
  130. manager.getConfigManager(), manager.getIdentity());
  131. category.addSetting(fontSizeSetting);
  132. category.addSetting(backgroundSetting);
  133. category.addSetting(foregroundSetting);
  134. category.addSetting(widthSetting);
  135. category.addSetting(timeoutSetting);
  136. category.addSetting(maxWindowsSetting);
  137. final Map<String, String> posOptions = new HashMap<>();
  138. //Populate policy MULTICHOICE
  139. for (OsdPolicy policy : OsdPolicy.values()) {
  140. posOptions.put(policy.name(), policy.getDescription());
  141. }
  142. category.addSetting(new PreferencesSetting(domain, "newbehaviour",
  143. "New window policy", "What to do when an OSD Window "
  144. + "is opened when there are other, existing windows open",
  145. posOptions, manager.getConfigManager(), manager.getIdentity()));
  146. category.addChangeListener(this);
  147. manager.getCategory("Plugins").addSubCategory(category);
  148. manager.registerSaveListener(this);
  149. }
  150. @Override
  151. public void categorySelected(final PreferencesCategory category) {
  152. osdWindow = new OsdWindow(mainFrame, identityController, osdManager, colourManager,
  153. -1, "Please drag this OSD to position", true, x, y, domain);
  154. osdWindow.setBackgroundColour(backgroundSetting.getValue());
  155. osdWindow.setForegroundColour(foregroundSetting.getValue());
  156. osdWindow.setFontSize(Integer.parseInt(fontSizeSetting.getValue()));
  157. }
  158. @Override
  159. public void categoryDeselected(final PreferencesCategory category) {
  160. x = osdWindow.getLocationOnScreen().x;
  161. y = osdWindow.getLocationOnScreen().y;
  162. osdWindow.dispose();
  163. osdWindow = null;
  164. }
  165. @Override
  166. public void save() {
  167. identityController.getUserSettings().setOption(domain, "locationX", x);
  168. identityController.getUserSettings().setOption(domain, "locationY", y);
  169. }
  170. @Override
  171. public void settingChanged(final PreferencesSetting setting) {
  172. if (osdWindow == null) {
  173. // They've changed categories but are somehow poking settings.
  174. // Ignore the request.
  175. return;
  176. }
  177. if (setting.equals(fontSizeSetting)) {
  178. osdWindow.setFontSize(Integer.parseInt(setting.getValue()));
  179. } else if (setting.equals(backgroundSetting)) {
  180. osdWindow.setBackgroundColour(setting.getValue());
  181. } else if (setting.equals(foregroundSetting)) {
  182. osdWindow.setForegroundColour(setting.getValue());
  183. } else if (setting.equals(widthSetting)) {
  184. int width = 500;
  185. try {
  186. width = Integer.parseInt(setting.getValue());
  187. } catch (NumberFormatException e) {
  188. //Ignore
  189. }
  190. osdWindow.setSize(width, osdWindow.getHeight());
  191. }
  192. }
  193. /**
  194. * Shows an OSD with the specified message, title is ignored, exported method used for
  195. * showNotification.
  196. *
  197. * @param title Ignored
  198. * @param message Message to show
  199. */
  200. @Exported
  201. public void showOSD(final String title, final String message) {
  202. osdManager.showWindow(-1, message);
  203. }
  204. }