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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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.addons.ui_swing.components;
  23. import com.dmdirc.addons.ui_swing.components.inputfields.TextPaneInputField;
  24. import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputHandler;
  25. import com.dmdirc.Channel;
  26. import com.dmdirc.Topic;
  27. import com.dmdirc.addons.ui_swing.SwingController;
  28. import com.dmdirc.addons.ui_swing.UIUtilities;
  29. import com.dmdirc.addons.ui_swing.actions.ReplacePasteAction;
  30. import com.dmdirc.addons.ui_swing.components.frames.ChannelFrame;
  31. import com.dmdirc.addons.ui_swing.components.text.WrapEditorKit;
  32. import com.dmdirc.config.IdentityManager;
  33. import com.dmdirc.interfaces.ConfigChangeListener;
  34. import com.dmdirc.interfaces.TopicChangeListener;
  35. import com.dmdirc.parser.common.ChannelJoinRequest;
  36. import com.dmdirc.ui.IconManager;
  37. import com.dmdirc.ui.messages.ColourManager;
  38. import com.dmdirc.ui.messages.Styliser;
  39. import java.awt.Color;
  40. import java.awt.event.ActionEvent;
  41. import java.awt.event.ActionListener;
  42. import java.awt.event.MouseEvent;
  43. import java.awt.event.MouseListener;
  44. import javax.swing.AbstractAction;
  45. import javax.swing.JButton;
  46. import javax.swing.JComponent;
  47. import javax.swing.JLabel;
  48. import javax.swing.JScrollPane;
  49. import javax.swing.KeyStroke;
  50. import javax.swing.SwingUtilities;
  51. import javax.swing.event.DocumentEvent;
  52. import javax.swing.event.DocumentListener;
  53. import javax.swing.event.HyperlinkEvent;
  54. import javax.swing.event.HyperlinkListener;
  55. import javax.swing.text.DefaultStyledDocument;
  56. import javax.swing.text.SimpleAttributeSet;
  57. import javax.swing.text.StyleConstants;
  58. import javax.swing.text.StyledDocument;
  59. import net.miginfocom.swing.MigLayout;
  60. /**
  61. * Component to show and edit topics for a channel.
  62. */
  63. public class TopicBar extends JComponent implements ActionListener,
  64. ConfigChangeListener, HyperlinkListener, MouseListener,
  65. DocumentListener, TopicChangeListener {
  66. /**
  67. * A version number for this class. It should be changed whenever the class
  68. * structure is changed (or anything else that would prevent serialized
  69. * objects being unserialized with the new class).
  70. */
  71. private static final long serialVersionUID = 1;
  72. /** Topic text. */
  73. private final TextPaneInputField topicText;
  74. /** Edit button. */
  75. private final JButton topicEdit;
  76. /** Cancel button. */
  77. private final JButton topicCancel;
  78. /** Associated channel. */
  79. private final Channel channel;
  80. /** Controller. */
  81. private final SwingController controller;
  82. /** the maximum length allowed for a topic. */
  83. private final int topicLengthMax;
  84. /** Empty Attribute set. */
  85. private SimpleAttributeSet as;
  86. /** Foreground Colour. */
  87. private Color foregroundColour;
  88. /** Background Colour. */
  89. private Color backgroundColour;
  90. /** Error icon. */
  91. private final JLabel errorIcon;
  92. /**
  93. * Instantiates a new topic bar.
  94. *
  95. * @param channelFrame Parent channel frame
  96. */
  97. public TopicBar(final ChannelFrame channelFrame) {
  98. super();
  99. this.channel = (Channel) channelFrame.getContainer();
  100. controller = channelFrame.getController();
  101. topicText = new TextPaneInputField();
  102. topicLengthMax = channel.getMaxTopicLength();
  103. errorIcon =
  104. new JLabel(IconManager.getIconManager().getIcon("input-error"));
  105. topicText.setEditorKit(new WrapEditorKit(channel.getConfigManager()
  106. .getOptionBool(controller.getDomain(), "showfulltopic")));
  107. ((DefaultStyledDocument) topicText.getDocument()).setDocumentFilter(
  108. new NewlinesDocumentFilter());
  109. topicText.getActionMap().put("paste-from-clipboard",
  110. new ReplacePasteAction("(\r\n|\n|\r)", " "));
  111. topicEdit = new ImageButton("edit", IconManager.getIconManager().
  112. getIcon("edit-inactive"), IconManager.getIconManager().
  113. getIcon("edit"));
  114. topicCancel = new ImageButton("cancel", IconManager.getIconManager().
  115. getIcon("close"), IconManager.getIconManager().
  116. getIcon("close-active"));
  117. final SwingInputHandler handler = new SwingInputHandler(topicText,
  118. channelFrame.getContainer().getCommandParser(), channelFrame);
  119. handler.setTypes(true, false, true, false);
  120. handler.setTabCompleter(channel.getTabCompleter());
  121. final JScrollPane sp = new JScrollPane(topicText);
  122. sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  123. sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
  124. setLayout(new MigLayout("fillx, ins 0, hidemode 3"));
  125. add(sp, "growx, pushx");
  126. add(errorIcon, "");
  127. add(topicCancel, "");
  128. add(topicEdit, "");
  129. //Fix broken layout manager
  130. invalidate();
  131. validate();
  132. invalidate();
  133. channel.addTopicChangeListener(this);
  134. topicText.addActionListener(this);
  135. topicEdit.addActionListener(this);
  136. topicCancel.addActionListener(this);
  137. topicText.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),
  138. "enterButton");
  139. topicText.getActionMap().put("enterButton", new AbstractAction(
  140. "enterButton") {
  141. /**
  142. * A version number for this class. It should be changed whenever the class
  143. * structure is changed (or anything else that would prevent serialized
  144. * objects being unserialized with the new class).
  145. */
  146. private static final long serialVersionUID = 1;
  147. /** {@inheritDoc} */
  148. @Override
  149. public void actionPerformed(final ActionEvent e) {
  150. commitTopicEdit();
  151. }
  152. });
  153. topicText.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"),
  154. "escapeButton");
  155. topicText.getActionMap().put("escapeButton", new AbstractAction(
  156. "escapeButton") {
  157. private static final long serialVersionUID = 1;
  158. /** {@inheritDoc} */
  159. @Override
  160. public void actionPerformed(final ActionEvent e) {
  161. cancelTopicEdit();
  162. }
  163. });
  164. topicText.addHyperlinkListener(this);
  165. topicText.addMouseListener(this);
  166. topicText.getDocument().addDocumentListener(this);
  167. IdentityManager.getGlobalConfig().addChangeListener(
  168. "ui", "backgroundcolour", this);
  169. IdentityManager.getGlobalConfig().addChangeListener(
  170. "ui", "foregroundcolour", this);
  171. IdentityManager.getGlobalConfig().addChangeListener(
  172. "ui", "inputbackgroundcolour", this);
  173. IdentityManager.getGlobalConfig().addChangeListener(
  174. "ui", "inputforegroundcolour", this);
  175. IdentityManager.getGlobalConfig().addChangeListener(
  176. controller.getDomain(), "showfulltopic", this);
  177. IdentityManager.getGlobalConfig().addChangeListener(
  178. controller.getDomain(), "hideEmptyTopicBar", this);
  179. topicText.setFocusable(false);
  180. topicText.setEditable(false);
  181. topicCancel.setVisible(false);
  182. setColours();
  183. topicChanged(channel, channel.getCurrentTopic());
  184. }
  185. /** {@inheritDoc} */
  186. @Override
  187. public final void topicChanged(final Channel channel, final Topic topic) {
  188. UIUtilities.invokeLater(new Runnable() {
  189. /** {@inheritDoc} */
  190. @Override
  191. public void run() {
  192. if (topicText.isEditable()) {
  193. return;
  194. }
  195. topicText.setText("");
  196. if (channel.getCurrentTopic() != null) {
  197. channel.getStyliser().addStyledString(
  198. (StyledDocument) topicText.getDocument(),
  199. new String[]{Styliser.CODE_HEXCOLOUR
  200. + ColourManager.getHex(foregroundColour)
  201. + channel.getCurrentTopic().getTopic(), },
  202. as);
  203. }
  204. if (channel.getConfigManager().getOptionBool(controller.
  205. getDomain(),
  206. "hideEmptyTopicBar")) {
  207. setVisible(topicText.getDocument().getLength() != 0);
  208. }
  209. topicText.setCaretPosition(0);
  210. validateTopic();
  211. setVisible(false);
  212. setVisible(true);
  213. }
  214. });
  215. }
  216. /**
  217. * {@inheritDoc}
  218. *
  219. * @param e Action event
  220. */
  221. @Override
  222. public void actionPerformed(final ActionEvent e) {
  223. if (!channel.isOnChannel()) {
  224. return;
  225. }
  226. if (e.getSource() == topicEdit) {
  227. if (topicText.isEditable()) {
  228. commitTopicEdit();
  229. } else {
  230. setupTopicEdit();
  231. }
  232. } else if (e.getSource() == topicCancel) {
  233. cancelTopicEdit();
  234. }
  235. }
  236. /**
  237. * Commits a topic edit to the parent channel.
  238. */
  239. private void commitTopicEdit() {
  240. if ((channel.getCurrentTopic() == null
  241. && !topicText.getText().isEmpty())
  242. || (channel.getCurrentTopic() != null
  243. && !channel.getCurrentTopic().getTopic()
  244. .equals(topicText.getText()))) {
  245. channel.setTopic(topicText.getText());
  246. }
  247. ((ChannelFrame) controller.getWindowFactory().getSwingWindow(channel))
  248. .getInputField().requestFocusInWindow();
  249. topicChanged(channel, null);
  250. topicText.setFocusable(false);
  251. topicText.setEditable(false);
  252. topicCancel.setVisible(false);
  253. }
  254. /**
  255. * Sets the topic ready to be edited, changing attributes and focus.
  256. */
  257. private void setupTopicEdit() {
  258. topicText.setVisible(false);
  259. topicText.setText("");
  260. if (channel.getCurrentTopic() != null) {
  261. topicText.setText(channel.getCurrentTopic().getTopic());
  262. }
  263. applyAttributes();
  264. topicText.setCaretPosition(0);
  265. topicText.setFocusable(true);
  266. topicText.setEditable(true);
  267. topicText.setVisible(true);
  268. topicText.requestFocusInWindow();
  269. topicCancel.setVisible(true);
  270. }
  271. /**
  272. * Cancels a topic edit, resetting focus and button states.
  273. */
  274. private void cancelTopicEdit() {
  275. topicText.setFocusable(false);
  276. topicText.setEditable(false);
  277. topicCancel.setVisible(false);
  278. ((ChannelFrame) controller.getWindowFactory().getSwingWindow(channel))
  279. .getInputField().requestFocusInWindow();
  280. topicChanged(channel, null);
  281. }
  282. /** {@inheritDoc} */
  283. @Override
  284. public void hyperlinkUpdate(final HyperlinkEvent e) {
  285. if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
  286. final String url = e.getDescription();
  287. if (url == null) {
  288. return;
  289. }
  290. if (url.charAt(0) == '#') {
  291. channel.getServer().join(new ChannelJoinRequest(url));
  292. } else if (url.contains("://")) {
  293. controller.getURLHandler().launchApp(e.getDescription());
  294. } else {
  295. channel.getServer().getQuery(url).activateFrame();
  296. }
  297. }
  298. }
  299. /**
  300. * Load and set colours.
  301. */
  302. private void setColours() {
  303. backgroundColour = channel.getConfigManager().getOptionColour(
  304. "ui", "inputbackgroundcolour", "ui", "backgroundcolour");
  305. foregroundColour = channel.getConfigManager().getOptionColour(
  306. "ui", "inputforegroundcolour", "ui", "foregroundcolour");
  307. setBackground(backgroundColour);
  308. setForeground(foregroundColour);
  309. setDisabledTextColour(foregroundColour);
  310. setCaretColor(foregroundColour);
  311. setAttributes();
  312. }
  313. /**
  314. * Sets sensible attributes.
  315. */
  316. private void setAttributes() {
  317. as = new SimpleAttributeSet();
  318. StyleConstants.setFontFamily(as, topicText.getFont().getFamily());
  319. StyleConstants.setFontSize(as, topicText.getFont().getSize());
  320. StyleConstants.setBackground(as, backgroundColour);
  321. StyleConstants.setForeground(as, foregroundColour);
  322. StyleConstants.setUnderline(as, false);
  323. StyleConstants.setBold(as, false);
  324. StyleConstants.setItalic(as, false);
  325. }
  326. /**
  327. * Applies predefined attributes to the topic bar.
  328. */
  329. private void applyAttributes() {
  330. setAttributes();
  331. ((DefaultStyledDocument) topicText.getDocument())
  332. .setCharacterAttributes(0, Integer.MAX_VALUE, as, true);
  333. }
  334. /**
  335. * Sets the caret position in this topic bar.
  336. *
  337. * @param position New position
  338. */
  339. public void setCaretPosition(final int position) {
  340. UIUtilities.invokeLater(new Runnable() {
  341. /** {@inheritDoc} */
  342. @Override
  343. public void run() {
  344. topicText.setCaretPosition(position);
  345. }
  346. });
  347. }
  348. /**
  349. * Sets the caret colour to the specified coloour.
  350. *
  351. * @param optionColour Colour for the caret
  352. */
  353. public void setCaretColor(final Color optionColour) {
  354. UIUtilities.invokeLater(new Runnable() {
  355. /** {@inheritDoc} */
  356. @Override
  357. public void run() {
  358. topicText.setCaretColor(optionColour);
  359. }
  360. });
  361. }
  362. /**
  363. * Sets the foreground colour to the specified coloour.
  364. *
  365. * @param optionColour Colour for the foreground
  366. */
  367. @Override
  368. public void setForeground(final Color optionColour) {
  369. UIUtilities.invokeLater(new Runnable() {
  370. /** {@inheritDoc} */
  371. @Override
  372. public void run() {
  373. topicText.setForeground(optionColour);
  374. }
  375. });
  376. }
  377. /**
  378. * Sets the disabled text colour to the specified coloour.
  379. *
  380. * @param optionColour Colour for the disabled text
  381. */
  382. public void setDisabledTextColour(final Color optionColour) {
  383. UIUtilities.invokeLater(new Runnable() {
  384. /** {@inheritDoc} */
  385. @Override
  386. public void run() {
  387. topicText.setDisabledTextColor(optionColour);
  388. }
  389. });
  390. }
  391. /**
  392. * Sets the background colour to the specified coloour.
  393. *
  394. * @param optionColour Colour for the caret
  395. */
  396. @Override
  397. public void setBackground(final Color optionColour) {
  398. UIUtilities.invokeLater(new Runnable() {
  399. /** {@inheritDoc} */
  400. @Override
  401. public void run() {
  402. topicText.setBackground(optionColour);
  403. }
  404. });
  405. }
  406. /** {@inheritDoc} */
  407. @Override
  408. public void configChanged(final String domain, final String key) {
  409. if ("showfulltopic".equals(key)) {
  410. topicText.setEditorKit(new WrapEditorKit(channel.getConfigManager()
  411. .getOptionBool(controller.getDomain(), "showfulltopic")));
  412. ((DefaultStyledDocument) topicText.getDocument()).setDocumentFilter(
  413. new NewlinesDocumentFilter());
  414. topicChanged(channel, null);
  415. }
  416. setColours();
  417. if ("hideEmptyTopicBar".equals(key)) {
  418. setVisible(true);
  419. if (channel.getConfigManager().getOptionBool(controller.getDomain(),
  420. "hideEmptyTopicBar")) {
  421. setVisible(topicText.getDocument().getLength() != 0);
  422. }
  423. }
  424. }
  425. /**
  426. * Closes this topic bar.
  427. */
  428. public void close() {
  429. channel.removeTopicChangeListener(this);
  430. }
  431. /**
  432. * Validates the topic text and shows errors as appropriate.
  433. */
  434. public void validateTopic() {
  435. UIUtilities.invokeLater(new Runnable() {
  436. /** {@inheritDoc} */
  437. @Override
  438. public void run() {
  439. if (topicText.isEditable()) {
  440. final int charsLeft = topicLengthMax - topicText.getText().
  441. length();
  442. if (charsLeft < 0) {
  443. errorIcon.setVisible(true);
  444. errorIcon.setToolTipText("Topic too long: " + topicText.
  445. getText().length() + " of " + topicLengthMax);
  446. } else {
  447. errorIcon.setVisible(false);
  448. errorIcon.setToolTipText(null);
  449. }
  450. } else {
  451. errorIcon.setVisible(false);
  452. }
  453. }
  454. });
  455. }
  456. /**
  457. * {@inheritDoc}
  458. *
  459. * @param e Mouse event
  460. */
  461. @Override
  462. public void mouseClicked(final MouseEvent e) {
  463. if (e.getClickCount() == 2 && !topicText.isEditable()) {
  464. topicEdit.doClick();
  465. }
  466. }
  467. /**
  468. * {@inheritDoc}
  469. *
  470. * @param e Mouse event
  471. */
  472. @Override
  473. public void mousePressed(final MouseEvent e) {
  474. //Ignore
  475. }
  476. /**
  477. * {@inheritDoc}
  478. *
  479. * @param e Mouse event
  480. */
  481. @Override
  482. public void mouseReleased(final MouseEvent e) {
  483. //Ignore
  484. }
  485. /**
  486. * {@inheritDoc}
  487. *
  488. * @param e Mouse event
  489. */
  490. @Override
  491. public void mouseEntered(final MouseEvent e) {
  492. //Ignore
  493. }
  494. /**
  495. * {@inheritDoc}
  496. *
  497. * @param e Mouse event
  498. */
  499. @Override
  500. public void mouseExited(final MouseEvent e) {
  501. //Ignore
  502. }
  503. /** {@inheritDoc} */
  504. @Override
  505. public void insertUpdate(final DocumentEvent e) {
  506. validateTopic();
  507. if (topicText.isEditable()) {
  508. SwingUtilities.invokeLater(new Runnable() {
  509. @Override
  510. public void run() {
  511. applyAttributes();
  512. }
  513. });
  514. }
  515. }
  516. /** {@inheritDoc} */
  517. @Override
  518. public void removeUpdate(final DocumentEvent e) {
  519. validateTopic();
  520. }
  521. /** {@inheritDoc} */
  522. @Override
  523. public void changedUpdate(final DocumentEvent e) {
  524. validateTopic();
  525. }
  526. }