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ů.

CoreActionComponent.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 2006-2008 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.actions;
  23. import com.dmdirc.actions.interfaces.ActionComponent;
  24. import com.dmdirc.Channel;
  25. import com.dmdirc.FrameContainer;
  26. import com.dmdirc.Query;
  27. import com.dmdirc.Server;
  28. import com.dmdirc.logger.ErrorLevel;
  29. import com.dmdirc.logger.Logger;
  30. import com.dmdirc.parser.ChannelClientInfo;
  31. import java.awt.Color;
  32. import java.awt.event.KeyEvent;
  33. import java.util.Calendar;
  34. import java.util.GregorianCalendar;
  35. import javax.swing.KeyStroke;
  36. /**
  37. * A CoreActionComponent represents a component of some object that the user can
  38. * use as the subject of a condition within an action.
  39. * @author chris
  40. */
  41. public enum CoreActionComponent implements ActionComponent {
  42. /** Returns the name of the server. */
  43. SERVER_NAME {
  44. /** {@inheritDoc} */
  45. public Object get(final Object argument) { return ((Server) argument).getName(); }
  46. /** {@inheritDoc} */
  47. public Class appliesTo() { return Server.class; }
  48. /** {@inheritDoc} */
  49. public Class getType() { return String.class; }
  50. /** {@inheritDoc} */
  51. public String getName() { return "name"; }
  52. },
  53. /** Returns the network of the server. */
  54. SERVER_NETWORK {
  55. /** {@inheritDoc} */
  56. public Object get(final Object argument) { return ((Server) argument).getNetwork(); }
  57. /** {@inheritDoc} */
  58. public Class appliesTo() { return Server.class; }
  59. /** {@inheritDoc} */
  60. public Class getType() { return String.class; }
  61. /** {@inheritDoc} */
  62. public String getName() { return "network"; }
  63. },
  64. /** Returns the away reason for the server. */
  65. SERVER_MYAWAYREASON {
  66. /** {@inheritDoc} */
  67. public Object get(final Object argument) { return ((Server) argument).getAwayMessage(); }
  68. /** {@inheritDoc} */
  69. public Class appliesTo() { return Server.class; }
  70. /** {@inheritDoc} */
  71. public Class getType() { return String.class; }
  72. /** {@inheritDoc} */
  73. public String getName() { return "away reason"; }
  74. },
  75. /** Returns the channel umodes for the server. */
  76. SERVER_CHANNELUMODES {
  77. /** {@inheritDoc} */
  78. public Object get(final Object argument) { return ((Server) argument).getParser().getPrefixModes(); }
  79. /** {@inheritDoc} */
  80. public Class appliesTo() { return Server.class; }
  81. /** {@inheritDoc} */
  82. public Class getType() { return String.class; }
  83. /** {@inheritDoc} */
  84. public String getName() { return "list of channel usermodes"; }
  85. },
  86. /** Returns the nickname for the server. */
  87. SERVER_MYNICKNAME {
  88. /** {@inheritDoc} */
  89. public Object get(final Object argument) {
  90. final Server server = (Server) argument;
  91. if (server == null || server.getParser() == null || server.getParser().getMyself() == null) {
  92. Logger.appError(ErrorLevel.LOW, "SERVER_MYNICKNAME.get() called with null element",
  93. new UnsupportedOperationException(
  94. server == null ? "Server was null" :
  95. server.getParser() == null ? "Parser was null" :
  96. server.getParser().getMyself() == null ? "Myself was null" :
  97. "Unknown"
  98. ));
  99. return "null";
  100. } else {
  101. return server.getParser().getMyself().getNickname();
  102. }
  103. }
  104. /** {@inheritDoc} */
  105. public Class appliesTo() { return Server.class; }
  106. /** {@inheritDoc} */
  107. public Class getType() { return String.class; }
  108. /** {@inheritDoc} */
  109. public String getName() { return "nickname"; }
  110. },
  111. /** Returns the name of the channel. */
  112. CHANNEL_NAME {
  113. /** {@inheritDoc} */
  114. public Object get(final Object argument) { return ((Channel) argument).getChannelInfo().getName(); }
  115. /** {@inheritDoc} */
  116. public Class appliesTo() { return Channel.class; }
  117. /** {@inheritDoc} */
  118. public Class getType() { return String.class; }
  119. /** {@inheritDoc} */
  120. public String getName() { return "name"; }
  121. },
  122. /** Returns the notification colour of the channel. */
  123. CHANNEL_COLOUR {
  124. /** {@inheritDoc} */
  125. public Object get(final Object argument) { return ((Channel) argument).getNotification(); }
  126. /** {@inheritDoc} */
  127. public Class appliesTo() { return Channel.class; }
  128. /** {@inheritDoc} */
  129. public Class getType() { return Color.class; }
  130. /** {@inheritDoc} */
  131. public String getName() { return "notification colour"; }
  132. },
  133. /** Returns the name of a client. */
  134. USER_NAME {
  135. /** {@inheritDoc} */
  136. public Object get(final Object argument) { return ((ChannelClientInfo) argument).getNickname(); }
  137. /** {@inheritDoc} */
  138. public Class appliesTo() { return ChannelClientInfo.class; }
  139. /** {@inheritDoc} */
  140. public Class getType() { return String.class; }
  141. /** {@inheritDoc} */
  142. public String getName() { return "nickname"; }
  143. },
  144. /** Returns the modes of a client. */
  145. USER_MODES {
  146. /** {@inheritDoc} */
  147. public Object get(final Object argument) { return ((ChannelClientInfo) argument).getChanModeStr(false); }
  148. /** {@inheritDoc} */
  149. public Class appliesTo() { return ChannelClientInfo.class; }
  150. /** {@inheritDoc} */
  151. public Class getType() { return String.class; }
  152. /** {@inheritDoc} */
  153. public String getName() { return "modes"; }
  154. },
  155. /** Returns the host of a client. */
  156. USER_HOST {
  157. /** {@inheritDoc} */
  158. public Object get(final Object argument) { return ((ChannelClientInfo) argument).getClient().getHost(); }
  159. /** {@inheritDoc} */
  160. public Class appliesTo() { return ChannelClientInfo.class; }
  161. /** {@inheritDoc} */
  162. public Class getType() { return String.class; }
  163. /** {@inheritDoc} */
  164. public String getName() { return "host"; }
  165. },
  166. /** Returns the number of common channels the client is on. */
  167. USER_COMCHANS {
  168. /** {@inheritDoc} */
  169. public Object get(final Object argument) { return Integer.valueOf(((ChannelClientInfo) argument).getClient().channelCount()); }
  170. /** {@inheritDoc} */
  171. public Class appliesTo() { return ChannelClientInfo.class; }
  172. /** {@inheritDoc} */
  173. public Class getType() { return Integer.class; }
  174. /** {@inheritDoc} */
  175. public String getName() { return "number of common channels"; }
  176. },
  177. /** Returns the content of a string. */
  178. STRING_STRING {
  179. /** {@inheritDoc} */
  180. public Object get(final Object argument) { return argument; }
  181. /** {@inheritDoc} */
  182. public Class appliesTo() { return String.class; }
  183. /** {@inheritDoc} */
  184. public Class getType() { return String.class; }
  185. /** {@inheritDoc} */
  186. public String getName() { return "content"; }
  187. },
  188. /** Returns the length of a string. */
  189. STRING_LENGTH {
  190. /** {@inheritDoc} */
  191. public Object get(final Object argument) { return ((String) argument).length(); }
  192. /** {@inheritDoc} */
  193. public Class appliesTo() { return String.class; }
  194. /** {@inheritDoc} */
  195. public Class getType() { return Integer.class; }
  196. /** {@inheritDoc} */
  197. public String getName() { return "length"; }
  198. },
  199. /** Returns the size of a string array. */
  200. STRINGARRAY_LENGTH {
  201. /** {@inheritDoc} */
  202. public Object get(final Object argument) { return Integer.valueOf(((String[]) argument).length); }
  203. /** {@inheritDoc} */
  204. public Class appliesTo() { return String[].class; }
  205. /** {@inheritDoc} */
  206. public Class getType() { return Integer.class; }
  207. /** {@inheritDoc} */
  208. public String getName() { return "size"; }
  209. },
  210. /** Returns the readable representation of a date. */
  211. CALENDAR_FULLSTRING {
  212. /** {@inheritDoc} */
  213. public Object get(final Object argument) { return ((GregorianCalendar) argument).getTime().toString(); }
  214. /** {@inheritDoc} */
  215. public Class appliesTo() { return Calendar.class; }
  216. /** {@inheritDoc} */
  217. public Class getType() { return String.class; }
  218. /** {@inheritDoc} */
  219. public String getName() { return "full date"; }
  220. },
  221. /** Returns the name of the key that was pressed. */
  222. KEYEVENT_KEYNAME {
  223. /** {@inheritDoc} */
  224. public Object get(final Object argument) { return KeyEvent.getKeyText(((KeyStroke) argument).getKeyCode()); }
  225. /** {@inheritDoc} */
  226. public Class appliesTo() { return KeyStroke.class; }
  227. /** {@inheritDoc} */
  228. public Class getType() { return String.class; }
  229. /** {@inheritDoc} */
  230. public String getName() { return "key name"; }
  231. },
  232. /** Returns the state of the control key for a key press event. */
  233. KEYEVENT_CTRLSTATE {
  234. /** {@inheritDoc} */
  235. public Object get(final Object argument) {
  236. return Boolean.valueOf((((KeyStroke) argument).getModifiers() & KeyEvent.CTRL_DOWN_MASK) != 0);
  237. }
  238. /** {@inheritDoc} */
  239. public Class appliesTo() { return KeyStroke.class; }
  240. /** {@inheritDoc} */
  241. public Class getType() { return Boolean.class; }
  242. /** {@inheritDoc} */
  243. public String getName() { return "control key state"; }
  244. },
  245. /** Returns the state of the shift key for a key press event. */
  246. KEYEVENT_SHIFTSTATE {
  247. /** {@inheritDoc} */
  248. public Object get(final Object argument) {
  249. return Boolean.valueOf((((KeyStroke) argument).getModifiers() & KeyEvent.SHIFT_DOWN_MASK) != 0);
  250. }
  251. /** {@inheritDoc} */
  252. public Class appliesTo() { return KeyStroke.class; }
  253. /** {@inheritDoc} */
  254. public Class getType() { return Boolean.class; }
  255. /** {@inheritDoc} */
  256. public String getName() { return "shift key state"; }
  257. },
  258. /** Returns the state of the shift key for a key press event. */
  259. KEYEVENT_ALTSTATE {
  260. /** {@inheritDoc} */
  261. public Object get(final Object argument) {
  262. return Boolean.valueOf((((KeyStroke) argument).getModifiers() & KeyEvent.ALT_DOWN_MASK) != 0);
  263. }
  264. /** {@inheritDoc} */
  265. public Class appliesTo() { return KeyStroke.class; }
  266. /** {@inheritDoc} */
  267. public Class getType() { return Boolean.class; }
  268. /** {@inheritDoc} */
  269. public String getName() { return "alt key state"; }
  270. },
  271. /** Returns the host of the query. */
  272. QUERY_HOST {
  273. /** {@inheritDoc} */
  274. public Object get(final Object argument) { return ((Query) argument).getHost(); }
  275. /** {@inheritDoc} */
  276. public Class appliesTo() { return Query.class; }
  277. /** {@inheritDoc} */
  278. public Class getType() { return String.class; }
  279. /** {@inheritDoc} */
  280. public String getName() { return "host"; }
  281. },
  282. /** Returns the host of the query. */
  283. QUERY_NICK {
  284. /** {@inheritDoc} */
  285. public Object get(final Object argument) { return ((Query) argument).toString(); }
  286. /** {@inheritDoc} */
  287. public Class appliesTo() { return Query.class; }
  288. /** {@inheritDoc} */
  289. public Class getType() { return String.class; }
  290. /** {@inheritDoc} */
  291. public String getName() { return "nick"; }
  292. },
  293. /** Returns the notification colour of the query. */
  294. QUERY_COLOUR {
  295. /** {@inheritDoc} */
  296. public Object get(final Object argument) { return ((Query) argument).getNotification(); }
  297. /** {@inheritDoc} */
  298. public Class appliesTo() { return Query.class; }
  299. /** {@inheritDoc} */
  300. public Class getType() { return Color.class; }
  301. /** {@inheritDoc} */
  302. public String getName() { return "notification colour"; }
  303. },
  304. /** Returns the notification colour of the window. */
  305. WINDOW_COLOUR {
  306. /** {@inheritDoc} */
  307. public Object get(final Object argument) { return ((FrameContainer) argument).getNotification(); }
  308. /** {@inheritDoc} */
  309. public Class appliesTo() { return FrameContainer.class; }
  310. /** {@inheritDoc} */
  311. public Class getType() { return Color.class; }
  312. /** {@inheritDoc} */
  313. public String getName() { return "notification colour"; }
  314. };
  315. /** {@inheritDoc} */
  316. public abstract Object get(Object argument);
  317. /** {@inheritDoc} */
  318. public abstract Class appliesTo();
  319. /** {@inheritDoc} */
  320. public abstract Class getType();
  321. /** {@inheritDoc} */
  322. public abstract String getName();
  323. }