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

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