您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CoreActionComponent.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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.irc.ChannelClientInfo;
  31. import com.dmdirc.parser.irc.ClientInfo;
  32. import com.dmdirc.ui.messages.Styliser;
  33. import java.awt.Color;
  34. import java.awt.event.KeyEvent;
  35. import java.util.Calendar;
  36. import java.util.GregorianCalendar;
  37. import javax.swing.KeyStroke;
  38. /**
  39. * A CoreActionComponent represents a component of some object that the user can
  40. * use as the subject of a condition within an action.
  41. * @author chris
  42. */
  43. public enum CoreActionComponent implements ActionComponent {
  44. /** Returns the name of the server. */
  45. SERVER_NAME {
  46. /** {@inheritDoc} */
  47. @Override
  48. public Object get(final Object argument) { return ((Server) argument).getName(); }
  49. /** {@inheritDoc} */
  50. @Override
  51. public Class appliesTo() { return Server.class; }
  52. /** {@inheritDoc} */
  53. @Override
  54. public Class getType() { return String.class; }
  55. /** {@inheritDoc} */
  56. @Override
  57. public String getName() { return "name"; }
  58. },
  59. /** Returns the network of the server. */
  60. SERVER_NETWORK {
  61. /** {@inheritDoc} */
  62. @Override
  63. public Object get(final Object argument) { return ((Server) argument).getNetwork(); }
  64. /** {@inheritDoc} */
  65. @Override
  66. public Class appliesTo() { return Server.class; }
  67. /** {@inheritDoc} */
  68. @Override
  69. public Class getType() { return String.class; }
  70. /** {@inheritDoc} */
  71. @Override
  72. public String getName() { return "network"; }
  73. },
  74. /** Returns the away reason for the server. */
  75. SERVER_MYAWAYREASON {
  76. /** {@inheritDoc} */
  77. @Override
  78. public Object get(final Object argument) { return ((Server) argument).getAwayMessage(); }
  79. /** {@inheritDoc} */
  80. @Override
  81. public Class appliesTo() { return Server.class; }
  82. /** {@inheritDoc} */
  83. @Override
  84. public Class getType() { return String.class; }
  85. /** {@inheritDoc} */
  86. @Override
  87. public String getName() { return "away reason"; }
  88. },
  89. /** Returns the channel umodes for the server. */
  90. SERVER_CHANNELUMODES {
  91. /** {@inheritDoc} */
  92. @Override
  93. public Object get(final Object argument) { return ((Server) argument).getParser().getPrefixModes(); }
  94. /** {@inheritDoc} */
  95. @Override
  96. public Class appliesTo() { return Server.class; }
  97. /** {@inheritDoc} */
  98. @Override
  99. public Class getType() { return String.class; }
  100. /** {@inheritDoc} */
  101. @Override
  102. public String getName() { return "list of channel usermodes"; }
  103. },
  104. /** Returns the nickname for the server. */
  105. SERVER_MYNICKNAME {
  106. /** {@inheritDoc} */
  107. @Override
  108. public Object get(final Object argument) {
  109. final Server server = (Server) argument;
  110. if (server == null || server.getParser() == null || server.getParser().getMyself().isFake()) {
  111. Logger.appError(ErrorLevel.LOW, "SERVER_MYNICKNAME.get() called with null element",
  112. new UnsupportedOperationException(
  113. server == null ? "Server was null" :
  114. server.getParser() == null ? "Parser was null" :
  115. server.getParser().getMyself().isFake() ? "Myself was fake" :
  116. "Unknown"
  117. ));
  118. return "null";
  119. } else {
  120. return server.getParser().getMyself().getNickname();
  121. }
  122. }
  123. /** {@inheritDoc} */
  124. @Override
  125. public Class appliesTo() { return Server.class; }
  126. /** {@inheritDoc} */
  127. @Override
  128. public Class getType() { return String.class; }
  129. /** {@inheritDoc} */
  130. @Override
  131. public String getName() { return "nickname"; }
  132. },
  133. /** Returns the name of the channel. */
  134. CHANNEL_NAME {
  135. /** {@inheritDoc} */
  136. @Override
  137. public Object get(final Object argument) { return ((Channel) argument).getChannelInfo().getName(); }
  138. /** {@inheritDoc} */
  139. @Override
  140. public Class appliesTo() { return Channel.class; }
  141. /** {@inheritDoc} */
  142. @Override
  143. public Class getType() { return String.class; }
  144. /** {@inheritDoc} */
  145. @Override
  146. public String getName() { return "name"; }
  147. },
  148. /** Returns the notification colour of the channel. */
  149. CHANNEL_COLOUR {
  150. /** {@inheritDoc} */
  151. @Override
  152. public Object get(final Object argument) { return ((Channel) argument).getNotification(); }
  153. /** {@inheritDoc} */
  154. @Override
  155. public Class appliesTo() { return Channel.class; }
  156. /** {@inheritDoc} */
  157. @Override
  158. public Class getType() { return Color.class; }
  159. /** {@inheritDoc} */
  160. @Override
  161. public String getName() { return "notification colour"; }
  162. },
  163. /** Returns the name of a client. */
  164. CLIENT_NAME {
  165. /** {@inheritDoc} */
  166. @Override
  167. public Object get(final Object argument) { return ((ClientInfo) argument).getNickname(); }
  168. /** {@inheritDoc} */
  169. @Override
  170. public Class appliesTo() { return ClientInfo.class; }
  171. /** {@inheritDoc} */
  172. @Override
  173. public Class getType() { return String.class; }
  174. /** {@inheritDoc} */
  175. @Override
  176. public String getName() { return "nickname"; }
  177. },
  178. /** Returns the host of a client. */
  179. CLIENT_HOST {
  180. /** {@inheritDoc} */
  181. @Override
  182. public Object get(final Object argument) { return ((ClientInfo) argument).getHost(); }
  183. /** {@inheritDoc} */
  184. @Override
  185. public Class appliesTo() { return ClientInfo.class; }
  186. /** {@inheritDoc} */
  187. @Override
  188. public Class getType() { return String.class; }
  189. /** {@inheritDoc} */
  190. @Override
  191. public String getName() { return "host"; }
  192. },
  193. /** Returns the name of a client. */
  194. USER_NAME {
  195. /** {@inheritDoc} */
  196. @Override
  197. public Object get(final Object argument) { return ((ChannelClientInfo) argument).getNickname(); }
  198. /** {@inheritDoc} */
  199. @Override
  200. public Class appliesTo() { return ChannelClientInfo.class; }
  201. /** {@inheritDoc} */
  202. @Override
  203. public Class getType() { return String.class; }
  204. /** {@inheritDoc} */
  205. @Override
  206. public String getName() { return "nickname"; }
  207. },
  208. /** Returns the modes of a client. */
  209. USER_MODES {
  210. /** {@inheritDoc} */
  211. @Override
  212. public Object get(final Object argument) { return ((ChannelClientInfo) argument).getChanModeStr(false); }
  213. /** {@inheritDoc} */
  214. @Override
  215. public Class appliesTo() { return ChannelClientInfo.class; }
  216. /** {@inheritDoc} */
  217. @Override
  218. public Class getType() { return String.class; }
  219. /** {@inheritDoc} */
  220. @Override
  221. public String getName() { return "modes"; }
  222. },
  223. /** Returns the host of a client. */
  224. USER_HOST {
  225. /** {@inheritDoc} */
  226. @Override
  227. public Object get(final Object argument) { return ((ChannelClientInfo) argument).getClient().getHost(); }
  228. /** {@inheritDoc} */
  229. @Override
  230. public Class appliesTo() { return ChannelClientInfo.class; }
  231. /** {@inheritDoc} */
  232. @Override
  233. public Class getType() { return String.class; }
  234. /** {@inheritDoc} */
  235. @Override
  236. public String getName() { return "host"; }
  237. },
  238. /** Returns the number of common channels the client is on. */
  239. USER_COMCHANS {
  240. /** {@inheritDoc} */
  241. @Override
  242. public Object get(final Object argument) { return Integer.valueOf(((ChannelClientInfo) argument).getClient().channelCount()); }
  243. /** {@inheritDoc} */
  244. @Override
  245. public Class appliesTo() { return ChannelClientInfo.class; }
  246. /** {@inheritDoc} */
  247. @Override
  248. public Class getType() { return Integer.class; }
  249. /** {@inheritDoc} */
  250. @Override
  251. public String getName() { return "number of common channels"; }
  252. },
  253. /** Returns the content of a string. */
  254. STRING_STRING {
  255. /** {@inheritDoc} */
  256. @Override
  257. public Object get(final Object argument) { return argument; }
  258. /** {@inheritDoc} */
  259. @Override
  260. public Class appliesTo() { return String.class; }
  261. /** {@inheritDoc} */
  262. @Override
  263. public Class getType() { return String.class; }
  264. /** {@inheritDoc} */
  265. @Override
  266. public String getName() { return "content"; }
  267. },
  268. /** Returns the content of a string, stripped of formatting. */
  269. STRING_STRIPPED {
  270. /** {@inheritDoc} */
  271. @Override
  272. public Object get(final Object argument) { return Styliser.stipControlCodes((String) argument); }
  273. /** {@inheritDoc} */
  274. @Override
  275. public Class appliesTo() { return String.class; }
  276. /** {@inheritDoc} */
  277. @Override
  278. public Class getType() { return String.class; }
  279. /** {@inheritDoc} */
  280. @Override
  281. public String getName() { return "content (without formatting)"; }
  282. },
  283. /** Returns the length of a string. */
  284. STRING_LENGTH {
  285. /** {@inheritDoc} */
  286. @Override
  287. public Object get(final Object argument) { return ((String) argument).length(); }
  288. /** {@inheritDoc} */
  289. @Override
  290. public Class appliesTo() { return String.class; }
  291. /** {@inheritDoc} */
  292. @Override
  293. public Class getType() { return Integer.class; }
  294. /** {@inheritDoc} */
  295. @Override
  296. public String getName() { return "length"; }
  297. },
  298. /** Returns the size of a string array. */
  299. STRINGARRAY_LENGTH {
  300. /** {@inheritDoc} */
  301. @Override
  302. public Object get(final Object argument) { return Integer.valueOf(((String[]) argument).length); }
  303. /** {@inheritDoc} */
  304. @Override
  305. public Class appliesTo() { return String[].class; }
  306. /** {@inheritDoc} */
  307. @Override
  308. public Class getType() { return Integer.class; }
  309. /** {@inheritDoc} */
  310. @Override
  311. public String getName() { return "size"; }
  312. },
  313. /** Returns the readable representation of a date. */
  314. CALENDAR_FULLSTRING {
  315. /** {@inheritDoc} */
  316. @Override
  317. public Object get(final Object argument) { return ((GregorianCalendar) argument).getTime().toString(); }
  318. /** {@inheritDoc} */
  319. @Override
  320. public Class appliesTo() { return Calendar.class; }
  321. /** {@inheritDoc} */
  322. @Override
  323. public Class getType() { return String.class; }
  324. /** {@inheritDoc} */
  325. @Override
  326. public String getName() { return "full date"; }
  327. },
  328. /** Returns the name of the key that was pressed. */
  329. KEYEVENT_KEYNAME {
  330. /** {@inheritDoc} */
  331. @Override
  332. public Object get(final Object argument) { return KeyEvent.getKeyText(((KeyStroke) argument).getKeyCode()); }
  333. /** {@inheritDoc} */
  334. @Override
  335. public Class appliesTo() { return KeyStroke.class; }
  336. /** {@inheritDoc} */
  337. @Override
  338. public Class getType() { return String.class; }
  339. /** {@inheritDoc} */
  340. @Override
  341. public String getName() { return "key name"; }
  342. },
  343. /** Returns the state of the control key for a key press event. */
  344. KEYEVENT_CTRLSTATE {
  345. /** {@inheritDoc} */
  346. @Override
  347. public Object get(final Object argument) {
  348. return Boolean.valueOf((((KeyStroke) argument).getModifiers() & KeyEvent.CTRL_DOWN_MASK) != 0);
  349. }
  350. /** {@inheritDoc} */
  351. @Override
  352. public Class appliesTo() { return KeyStroke.class; }
  353. /** {@inheritDoc} */
  354. @Override
  355. public Class getType() { return Boolean.class; }
  356. /** {@inheritDoc} */
  357. @Override
  358. public String getName() { return "control key state"; }
  359. },
  360. /** Returns the state of the shift key for a key press event. */
  361. KEYEVENT_SHIFTSTATE {
  362. /** {@inheritDoc} */
  363. @Override
  364. public Object get(final Object argument) {
  365. return Boolean.valueOf((((KeyStroke) argument).getModifiers() & KeyEvent.SHIFT_DOWN_MASK) != 0);
  366. }
  367. /** {@inheritDoc} */
  368. @Override
  369. public Class appliesTo() { return KeyStroke.class; }
  370. /** {@inheritDoc} */
  371. @Override
  372. public Class getType() { return Boolean.class; }
  373. /** {@inheritDoc} */
  374. @Override
  375. public String getName() { return "shift key state"; }
  376. },
  377. /** Returns the state of the shift key for a key press event. */
  378. KEYEVENT_ALTSTATE {
  379. /** {@inheritDoc} */
  380. @Override
  381. public Object get(final Object argument) {
  382. return Boolean.valueOf((((KeyStroke) argument).getModifiers() & KeyEvent.ALT_DOWN_MASK) != 0);
  383. }
  384. /** {@inheritDoc} */
  385. @Override
  386. public Class appliesTo() { return KeyStroke.class; }
  387. /** {@inheritDoc} */
  388. @Override
  389. public Class getType() { return Boolean.class; }
  390. /** {@inheritDoc} */
  391. @Override
  392. public String getName() { return "alt key state"; }
  393. },
  394. /** Returns the host of the query. */
  395. QUERY_HOST {
  396. /** {@inheritDoc} */
  397. @Override
  398. public Object get(final Object argument) { return ((Query) argument).getHost(); }
  399. /** {@inheritDoc} */
  400. @Override
  401. public Class appliesTo() { return Query.class; }
  402. /** {@inheritDoc} */
  403. @Override
  404. public Class getType() { return String.class; }
  405. /** {@inheritDoc} */
  406. @Override
  407. public String getName() { return "host"; }
  408. },
  409. /** Returns the host of the query. */
  410. QUERY_NICK {
  411. /** {@inheritDoc} */
  412. @Override
  413. public Object get(final Object argument) { return ((Query) argument).toString(); }
  414. /** {@inheritDoc} */
  415. @Override
  416. public Class appliesTo() { return Query.class; }
  417. /** {@inheritDoc} */
  418. @Override
  419. public Class getType() { return String.class; }
  420. /** {@inheritDoc} */
  421. @Override
  422. public String getName() { return "nick"; }
  423. },
  424. /** Returns the notification colour of the query. */
  425. QUERY_COLOUR {
  426. /** {@inheritDoc} */
  427. @Override
  428. public Object get(final Object argument) { return ((Query) argument).getNotification(); }
  429. /** {@inheritDoc} */
  430. @Override
  431. public Class appliesTo() { return Query.class; }
  432. /** {@inheritDoc} */
  433. @Override
  434. public Class getType() { return Color.class; }
  435. /** {@inheritDoc} */
  436. @Override
  437. public String getName() { return "notification colour"; }
  438. },
  439. /** The name of a window. */
  440. WINDOW_NAME {
  441. /** {@inheritDoc} */
  442. @Override
  443. public Object get(final Object argument) { return ((FrameContainer) argument).toString(); }
  444. /** {@inheritDoc} */
  445. @Override
  446. public Class appliesTo() { return FrameContainer.class; }
  447. /** {@inheritDoc} */
  448. @Override
  449. public Class getType() { return String.class; }
  450. /** {@inheritDoc} */
  451. @Override
  452. public String getName() { return "name"; }
  453. },
  454. /** Returns the notification colour of the window. */
  455. WINDOW_COLOUR {
  456. /** {@inheritDoc} */
  457. @Override
  458. public Object get(final Object argument) { return ((FrameContainer) argument).getNotification(); }
  459. /** {@inheritDoc} */
  460. @Override
  461. public Class appliesTo() { return FrameContainer.class; }
  462. /** {@inheritDoc} */
  463. @Override
  464. public Class getType() { return Color.class; }
  465. /** {@inheritDoc} */
  466. @Override
  467. public String getName() { return "notification colour"; }
  468. };
  469. }