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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * Copyright (c) 2006-2014 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.actions;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.FrameContainer;
  25. import com.dmdirc.Query;
  26. import com.dmdirc.interfaces.Connection;
  27. import com.dmdirc.interfaces.actions.ActionComponent;
  28. import com.dmdirc.interfaces.config.ConfigProvider;
  29. import com.dmdirc.interfaces.ui.Window;
  30. import com.dmdirc.logger.ErrorLevel;
  31. import com.dmdirc.logger.Logger;
  32. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  33. import com.dmdirc.parser.interfaces.ClientInfo;
  34. import com.dmdirc.ui.Colour;
  35. import com.dmdirc.ui.messages.Styliser;
  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. */
  44. public enum CoreActionComponent implements ActionComponent {
  45. /** Returns the name of the server. */
  46. SERVER_NAME {
  47. /** {@inheritDoc} */
  48. @Override
  49. public Object get(final Object arg) { return ((Connection) arg).getAddress(); }
  50. /** {@inheritDoc} */
  51. @Override
  52. public Class<?> appliesTo() { return Connection.class; }
  53. /** {@inheritDoc} */
  54. @Override
  55. public Class<?> getType() { return String.class; }
  56. /** {@inheritDoc} */
  57. @Override
  58. public String getName() { return "name"; }
  59. },
  60. /** Returns the network of the server. */
  61. SERVER_NETWORK {
  62. /** {@inheritDoc} */
  63. @Override
  64. @ComponentOptions(requireConnected = true)
  65. public Object get(final Object arg) { return ((Connection) arg).getNetwork(); }
  66. /** {@inheritDoc} */
  67. @Override
  68. public Class<?> appliesTo() { return Connection.class; }
  69. /** {@inheritDoc} */
  70. @Override
  71. public Class<?> getType() { return String.class; }
  72. /** {@inheritDoc} */
  73. @Override
  74. public String getName() { return "network"; }
  75. },
  76. /**
  77. * Returns the protocol of the server.
  78. *
  79. * @since 0.6.4
  80. */
  81. SERVER_PROTOCOL {
  82. /** {@inheritDoc} */
  83. @Override
  84. public Object get(final Object arg) { return ((Connection) arg).getProtocol(); }
  85. /** {@inheritDoc} */
  86. @Override
  87. public Class<?> appliesTo() { return Connection.class; }
  88. /** {@inheritDoc} */
  89. @Override
  90. public Class<?> getType() { return String.class; }
  91. /** {@inheritDoc} */
  92. @Override
  93. public String getName() { return "protocol"; }
  94. },
  95. /** Returns the away reason for the server. */
  96. SERVER_MYAWAYREASON {
  97. /** {@inheritDoc} */
  98. @Override
  99. public Object get(final Object arg) { return ((Connection) arg).getAwayMessage(); }
  100. /** {@inheritDoc} */
  101. @Override
  102. public Class<?> appliesTo() { return Connection.class; }
  103. /** {@inheritDoc} */
  104. @Override
  105. public Class<?> getType() { return String.class; }
  106. /** {@inheritDoc} */
  107. @Override
  108. public String getName() { return "away reason"; }
  109. },
  110. /** Returns the channel umodes for the server. */
  111. SERVER_CHANNELUMODES {
  112. /** {@inheritDoc} */
  113. @Override
  114. @ComponentOptions(requireConnected = true)
  115. public Object get(final Object arg) { return ((Connection) arg).getParser().getChannelUserModes(); }
  116. /** {@inheritDoc} */
  117. @Override
  118. public Class<?> appliesTo() { return Connection.class; }
  119. /** {@inheritDoc} */
  120. @Override
  121. public Class<?> getType() { return String.class; }
  122. /** {@inheritDoc} */
  123. @Override
  124. public String getName() { return "list of channel usermodes"; }
  125. },
  126. /** Returns the nickname for the server. */
  127. SERVER_MYNICKNAME {
  128. /** {@inheritDoc} */
  129. @Override
  130. @ComponentOptions(requireConnected = true)
  131. public Object get(final Object arg) {
  132. final Connection server = (Connection) arg;
  133. if (server == null || server.getParser() == null) {
  134. Logger.appError(ErrorLevel.LOW, "SERVER_MYNICKNAME.get() called with null element",
  135. new UnsupportedOperationException(
  136. server == null ? "Server was null"
  137. : server.getParser() == null ? "Parser was null"
  138. : "Unknown"
  139. ));
  140. return "null";
  141. } else {
  142. return server.getParser().getLocalClient().getNickname();
  143. }
  144. }
  145. /** {@inheritDoc} */
  146. @Override
  147. public Class<?> appliesTo() { return Connection.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 arg) { return ((Connection) arg).getProfile(); }
  160. /** {@inheritDoc} */
  161. @Override
  162. public Class<?> appliesTo() { return Connection.class; }
  163. /** {@inheritDoc} */
  164. @Override
  165. public Class<?> getType() { return ConfigProvider.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 arg) { return ((Channel) arg).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 arg) { return ((Channel) arg).getNotification(); }
  190. /** {@inheritDoc} */
  191. @Override
  192. public Class<?> appliesTo() { return Channel.class; }
  193. /** {@inheritDoc} */
  194. @Override
  195. public Class<?> getType() { return Colour.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 arg) { return ((ClientInfo) arg).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 arg) { return ((ClientInfo) arg).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 arg) { return ((ChannelClientInfo) arg).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 arg) { return ((ChannelClientInfo) arg).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 arg) { return ((ChannelClientInfo) arg).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 arg) { return Integer.valueOf(((ChannelClientInfo) arg).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 arg) { return arg; }
  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 arg) { return Styliser.stipControlCodes((String) arg); }
  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 arg) { return ((String) arg).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 arg) { return Integer.valueOf(((String[]) arg).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 arg) { return ((GregorianCalendar) arg).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 arg) { return KeyEvent.getKeyText(((KeyStroke) arg).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 arg) {
  385. return Boolean.valueOf((((KeyStroke) arg).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 arg) {
  402. return Boolean.valueOf((((KeyStroke) arg).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 arg) {
  419. return Boolean.valueOf((((KeyStroke) arg).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 arg) { return ((Query) arg).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 arg) { return ((Query) arg).getName(); }
  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 arg) { return ((Query) arg).getNotification(); }
  466. /** {@inheritDoc} */
  467. @Override
  468. public Class<?> appliesTo() { return Query.class; }
  469. /** {@inheritDoc} */
  470. @Override
  471. public Class<?> getType() { return Colour.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 arg) { return ((FrameContainer) arg).getName(); }
  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 arg) { return ((FrameContainer) arg).getNotification(); }
  496. /** {@inheritDoc} */
  497. @Override
  498. public Class<?> appliesTo() { return FrameContainer.class; }
  499. /** {@inheritDoc} */
  500. @Override
  501. public Class<?> getType() { return Colour.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 arg) {
  515. return ((Window) arg).getContainer().getConnection();
  516. }
  517. /** {@inheritDoc} */
  518. @Override
  519. public Class<?> appliesTo() { return Window.class; }
  520. /** {@inheritDoc} */
  521. @Override
  522. public Class<?> getType() { return Connection.class; }
  523. /** {@inheritDoc} */
  524. @Override
  525. public String getName() { return "server"; }
  526. },
  527. /** Returns the name of an identity. */
  528. IDENTITY_NAME {
  529. /** {@inheritDoc} */
  530. @Override
  531. public Object get(final Object arg) { return ((ConfigProvider) arg).getName(); }
  532. /** {@inheritDoc} */
  533. @Override
  534. public Class<?> appliesTo() { return ConfigProvider.class; }
  535. /** {@inheritDoc} */
  536. @Override
  537. public Class<?> getType() { return String.class; }
  538. /** {@inheritDoc} */
  539. @Override
  540. public String getName() { return "name"; }
  541. },
  542. /** Returns the value of an integer. */
  543. INTEGER_VALUE {
  544. /** {@inheritDoc} */
  545. @Override
  546. public Object get(final Object arg) { return (Integer) arg; }
  547. /** {@inheritDoc} */
  548. @Override
  549. public Class<?> appliesTo() { return Integer.class; }
  550. /** {@inheritDoc} */
  551. @Override
  552. public Class<?> getType() { return Integer.class; }
  553. /** {@inheritDoc} */
  554. @Override
  555. public String getName() { return "value"; }
  556. };
  557. }