Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CoreActionComponent.java 18KB

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