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.

CoreActionComponent.java 20KB

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