Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

WindowStatusPlugin.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.plugins.plugins.windowstatus;
  23. import java.awt.BorderLayout;
  24. import java.awt.Dimension;
  25. import java.util.Hashtable;
  26. import java.util.Properties;
  27. import javax.swing.BorderFactory;
  28. import javax.swing.JInternalFrame;
  29. import javax.swing.JLabel;
  30. import javax.swing.JPanel;
  31. import javax.swing.SwingConstants;
  32. import com.dmdirc.Channel;
  33. import com.dmdirc.Config;
  34. import com.dmdirc.FrameContainer;
  35. import com.dmdirc.Query;
  36. import com.dmdirc.Server;
  37. import com.dmdirc.commandparser.CommandWindow;
  38. import com.dmdirc.actions.ActionType;
  39. import com.dmdirc.actions.CoreActionType;
  40. import com.dmdirc.parser.ChannelClientInfo;
  41. import com.dmdirc.parser.ChannelInfo;
  42. import com.dmdirc.plugins.Plugin;
  43. import com.dmdirc.plugins.EventPlugin;
  44. import com.dmdirc.ui.ChannelFrame;
  45. import com.dmdirc.ui.MainFrame;
  46. import com.dmdirc.ui.QueryFrame;
  47. import com.dmdirc.ui.ServerFrame;
  48. import com.dmdirc.ui.components.PreferencesInterface;
  49. import com.dmdirc.ui.components.PreferencesPanel;
  50. /**
  51. * Displays information related to the current window in the status bar.
  52. *
  53. * @author Shane 'Dataforce' McCormack
  54. * @version $Id: WindowStatusPlugin.java 969 2007-04-30 18:38:20Z ShaneMcC $
  55. */
  56. public final class WindowStatusPlugin extends Plugin implements EventPlugin, PreferencesInterface {
  57. /** What domain do we store all settings in the global config under. */
  58. private static final String MY_DOMAIN = "plugin-Logging";
  59. /** The panel we use in the status bar. */
  60. private final JPanel panel = new JPanel();
  61. /** The label we use to show window status. */
  62. private final JLabel label = new JLabel(" ??? ");
  63. /** Creates a new instance of WindowStatusPlugin. */
  64. public WindowStatusPlugin() {
  65. super();
  66. // panel.setPreferredSize(new Dimension(70, 25));
  67. panel.setLayout(new BorderLayout());
  68. panel.setBorder(BorderFactory.createEtchedBorder());
  69. label.setHorizontalAlignment(SwingConstants.CENTER);
  70. panel.add(label);
  71. }
  72. /**
  73. * Called when the plugin is loaded.
  74. *
  75. * @return false if the plugin can not be loaded
  76. */
  77. public boolean onLoad() {
  78. final Properties config = Config.getConfig();
  79. // Set default options if they don't exist
  80. updateOption(config, "channel.shownone", "true");
  81. updateOption(config, "channel.noneprefix", "None:");
  82. return true;
  83. }
  84. /**
  85. * Called when this plugin is Activated.
  86. */
  87. public void onActivate() {
  88. updateStatus();
  89. }
  90. /**
  91. * Called when this plugin is deactivated.
  92. */
  93. public void onDeactivate() {
  94. MainFrame.getMainFrame().getStatusBar().removeComponent(panel);
  95. }
  96. /**
  97. * Get the plugin version.
  98. *
  99. * @return Plugin Version
  100. */
  101. public String getVersion() { return "0.3"; }
  102. /**
  103. * Get the plugin Author.
  104. *
  105. * @return Author of plugin
  106. */
  107. public String getAuthor() { return "Shane <shane@dmdirc.com>"; }
  108. /**
  109. * Get the plugin Description.
  110. *
  111. * @return Description of plugin
  112. */
  113. public String getDescription() { return "Displays information related to the current window in the status bar."; }
  114. /**
  115. * Get the name of the plugin (used in "Manage Plugins" dialog).
  116. *
  117. * @return Name of plugin
  118. */
  119. public String toString() { return "WindowStatus Plugin"; }
  120. /**
  121. * Process an event of the specified type.
  122. *
  123. * @param type The type of the event to process
  124. * @param format Format of messages that are about to be sent. (May be null)
  125. * @param arguments The arguments for the event
  126. */
  127. public void processEvent(final ActionType type, final StringBuffer format, final Object... arguments) {
  128. if (type instanceof CoreActionType) {
  129. final CoreActionType thisType = (CoreActionType)type;
  130. switch (thisType) {
  131. case CLIENT_FRAME_CHANGED:
  132. updateStatus((FrameContainer)arguments[0]);
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. }
  139. /**
  140. * Update the window status using the current active window.
  141. */
  142. public void updateStatus() {
  143. MainFrame.getMainFrame().getStatusBar().addComponent(panel);
  144. JInternalFrame active = MainFrame.getMainFrame().getActiveFrame();
  145. if (active instanceof CommandWindow) {
  146. FrameContainer activeFrame = ((CommandWindow)active).getContainer();
  147. updateStatus(activeFrame);
  148. }
  149. }
  150. /**
  151. * Update the window status using a given FrameContainer as the active frame.
  152. *
  153. * @param current Window to use when adding status.
  154. */
  155. public void updateStatus(final FrameContainer current) {
  156. if (current == null) { return; }
  157. StringBuffer textString = new StringBuffer("");
  158. if (current instanceof Server) {
  159. Server frame = (Server)current;
  160. textString.append(frame.getName());
  161. } else if (current instanceof Channel) {
  162. Channel frame = (Channel)current;
  163. ChannelInfo chan = frame.getChannelInfo();
  164. Hashtable<Integer,String> names = new Hashtable<Integer,String>();
  165. Hashtable<Integer,Integer> types = new Hashtable<Integer,Integer>();
  166. textString.append(chan.getName());
  167. textString.append(" - Nicks: "+chan.getUserCount()+" (");
  168. for (ChannelClientInfo client : chan.getChannelClients()) {
  169. Integer im = client.getImportantModeValue();
  170. if (!names.containsKey(im)) {
  171. String mode = client.getImportantModePrefix();
  172. if (mode.equals("")) {
  173. if (Config.getOptionBool(MY_DOMAIN, "channel.shownone")) {
  174. if (Config.hasOption(MY_DOMAIN, "channel.noneprefix")) {
  175. mode = Config.getOption(MY_DOMAIN, "channel.noneprefix");
  176. } else {
  177. mode = "None:";
  178. }
  179. } else {
  180. continue;
  181. }
  182. }
  183. names.put(im, mode);
  184. }
  185. Integer count = types.get(im);
  186. if (count == null) {
  187. count = new Integer(1);
  188. } else {
  189. count = count+1;
  190. }
  191. types.put(im, count);
  192. }
  193. boolean isFirst = true;
  194. for (Integer modeval : types.keySet()) {
  195. int count = types.get(modeval);
  196. if (isFirst) { isFirst = false; }
  197. else { textString.append(" "); }
  198. textString.append(names.get(modeval)+count);
  199. }
  200. textString.append(")");
  201. } else if (current instanceof Query) {
  202. Query frame = (Query)current;
  203. textString.append(frame.getHost());
  204. }
  205. label.setText(" "+textString.toString()+" ");
  206. }
  207. /**
  208. * Called to see if the plugin has configuration options (via dialog).
  209. *
  210. * @return true if the plugin has configuration options via a dialog.
  211. */
  212. public boolean isConfigurable() { return true; }
  213. /**
  214. * Called to show the Configuration dialog of the plugin if appropriate.
  215. */
  216. public void showConfig() {
  217. final PreferencesPanel preferencesPanel = new PreferencesPanel(this, "Window Status Plugin - Config");
  218. preferencesPanel.addCategory("Channel", "Configuration for Window Status plugin when showing a channel window.");
  219. preferencesPanel.addCheckboxOption("Channel", "channel.shownone", "Show 'none' count: ", "Should the count for uses with no state be shown?", Config.getOptionBool(MY_DOMAIN, "channel.shownone"));
  220. preferencesPanel.addTextfieldOption("Channel", "channel.noneprefix", "Prefix used before 'none' count: ", "The Prefix to use when showing the 'none' count", Config.getOption(MY_DOMAIN, "channel.noneprefix"));
  221. preferencesPanel.display();
  222. }
  223. /**
  224. * Get the name of the domain we store all settings in the global config under.
  225. *
  226. * @return the plugins domain
  227. */
  228. protected static String getDomain() { return MY_DOMAIN; }
  229. /**
  230. * Copy the new vaule of an option to the global config.
  231. *
  232. * @param properties Source of option value
  233. * @param name name of option
  234. * @param defaultValue value to set if source doesn't contain a value.
  235. * if this is null, value will not be changed.
  236. */
  237. protected void updateOption(final Properties properties, final String name, final String defaultValue) {
  238. String value = null;
  239. // Get the value from the properties file if one is given
  240. // if one isn't given we will just use the defaultValue and set that
  241. if (properties != null) {
  242. if (properties == Config.getConfig()) {
  243. // If this properties file is the global config, then
  244. // we need to prepend our domain name to it.
  245. value = properties.getProperty(MY_DOMAIN + "." + name);
  246. } else {
  247. // Otherwise we do not.
  248. value = properties.getProperty(name);
  249. }
  250. }
  251. // Check if the Properties contains the value we want
  252. if (value != null) {
  253. // It does, so update the global config
  254. Config.setOption(MY_DOMAIN, name, value);
  255. } else {
  256. // It does not, so check if we have a default value
  257. if (defaultValue != null) {
  258. // We do, use that instead.
  259. Config.setOption(MY_DOMAIN, name, defaultValue);
  260. }
  261. }
  262. }
  263. /**
  264. * Called when the preferences dialog is closed.
  265. *
  266. * @param properties user preferences
  267. */
  268. public void configClosed(final Properties properties) {
  269. // Update Config options
  270. updateOption(properties, "channel.shownone", null);
  271. updateOption(properties, "channel.noneprefix", "");
  272. // Update Status bar
  273. updateStatus();
  274. }
  275. }