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.

TopicDisplayPane.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.dialogs.channelsetting;
  18. import com.dmdirc.Topic;
  19. import com.dmdirc.addons.ui_swing.UIUtilities;
  20. import com.dmdirc.addons.ui_swing.actions.ReplacePasteAction;
  21. import com.dmdirc.addons.ui_swing.components.IconManager;
  22. import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputHandler;
  23. import com.dmdirc.addons.ui_swing.components.inputfields.TextAreaInputField;
  24. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  25. import com.dmdirc.interfaces.CommandController;
  26. import com.dmdirc.interfaces.GroupChat;
  27. import com.dmdirc.interfaces.GroupChatUser;
  28. import com.dmdirc.interfaces.WindowModel;
  29. import com.dmdirc.config.provider.AggregateConfigProvider;
  30. import com.dmdirc.plugins.ServiceManager;
  31. import com.dmdirc.ui.input.TabCompleterUtils;
  32. import com.dmdirc.ui.messages.ColourManagerFactory;
  33. import java.awt.Color;
  34. import java.awt.datatransfer.Clipboard;
  35. import java.awt.event.KeyEvent;
  36. import java.util.Optional;
  37. import javax.swing.JLabel;
  38. import javax.swing.JPanel;
  39. import javax.swing.JScrollPane;
  40. import javax.swing.KeyStroke;
  41. import javax.swing.event.DocumentEvent;
  42. import javax.swing.event.DocumentListener;
  43. import net.miginfocom.swing.MigLayout;
  44. /**
  45. * Class to display a topic to an end user as part of the groupChat settings dialog.
  46. */
  47. public class TopicDisplayPane extends JPanel implements DocumentListener {
  48. /** Serial version UID. */
  49. private static final long serialVersionUID = 1;
  50. /** Parent topic pane. */
  51. private final ChannelSettingsDialog parent;
  52. /** Associated group chat. */
  53. private final GroupChat groupChat;
  54. /** Channel window. */
  55. private final WindowModel channelWindow;
  56. /** the maximum length allowed for a topic. */
  57. private final int topicLengthMax;
  58. /** Clipboard to copy and paste from. */
  59. private final Clipboard clipboard;
  60. /** label showing the number of characters left in a topic. */
  61. private JLabel topicLengthLabel;
  62. /** Topic text entry text area. */
  63. private TextAreaInputField topicText;
  64. /** Topic who. */
  65. private TextLabel topicWho;
  66. /**
  67. * Creates a new topic display panel. This panel shows an editable version of the current topic
  68. * along with relating meta data and validates the length of the new input.
  69. *
  70. * @param groupChat Associated group chat
  71. * @param iconManager Icon manager
  72. * @param serviceManager Service manager
  73. * @param parent Parent settings dialog
  74. * @param channelWindow Channel window
  75. * @param clipboard Clipboard to copy and paste
  76. * @param commandController The controller to use to retrieve command information.
  77. */
  78. public TopicDisplayPane(final GroupChat groupChat, final IconManager iconManager,
  79. final ServiceManager serviceManager, final ChannelSettingsDialog parent,
  80. final WindowModel channelWindow, final Clipboard clipboard,
  81. final CommandController commandController,
  82. final ColourManagerFactory colourManagerFactory,
  83. final TabCompleterUtils tabCompleterUtils) {
  84. this.clipboard = clipboard;
  85. this.groupChat = groupChat;
  86. this.parent = parent;
  87. topicLengthMax = groupChat.getConnection().get().getParser().get().getMaxTopicLength();
  88. this.channelWindow = channelWindow;
  89. initComponents(iconManager, groupChat.getWindowModel().getConfigManager(), serviceManager,
  90. commandController, colourManagerFactory, tabCompleterUtils);
  91. addListeners();
  92. layoutComponents();
  93. setTopic(groupChat.getCurrentTopic());
  94. }
  95. private void initComponents(
  96. final IconManager iconManager,
  97. final AggregateConfigProvider config,
  98. final ServiceManager serviceManager,
  99. final CommandController commandController,
  100. final ColourManagerFactory colourManagerFactory,
  101. final TabCompleterUtils tabCompleterUtils) {
  102. topicLengthLabel = new JLabel();
  103. topicText = new TextAreaInputField(iconManager, colourManagerFactory, config, 100, 4);
  104. topicWho = new TextLabel();
  105. topicWho.setOpaque(false);
  106. topicText.setLineWrap(true);
  107. topicText.setWrapStyleWord(true);
  108. topicText.setRows(5);
  109. topicText.setColumns(30);
  110. final SwingInputHandler handler = new SwingInputHandler(serviceManager, topicText,
  111. commandController,
  112. groupChat.getWindowModel().getInputModel().get().getCommandParser(),
  113. channelWindow, tabCompleterUtils, groupChat.getEventBus());
  114. handler.setTypes(true, false, true, false);
  115. handler.setTabCompleter(groupChat.getWindowModel().getInputModel().get().getTabCompleter());
  116. topicText.getActionMap().put("paste-from-clipboard",
  117. new ReplacePasteAction(clipboard, "(\r\n|\n|\r)", " "));
  118. topicText.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
  119. 0), new TopicEnterAction(parent));
  120. topicText.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
  121. UIUtilities.getCtrlDownMask()), new TopicEnterAction(parent));
  122. UIUtilities.addUndoManager(topicText);
  123. }
  124. /** Adds listeners to the components. */
  125. private void addListeners() {
  126. topicText.getDocument().addDocumentListener(this);
  127. }
  128. /** Lays out the components. */
  129. private void layoutComponents() {
  130. setLayout(new MigLayout("wrap 1, fill, ins 0"));
  131. add(new JScrollPane(topicText), "grow, push");
  132. add(topicLengthLabel, "pushx, growx, pushx");
  133. add(topicWho, "growx, pushx");
  134. }
  135. /**
  136. * Sets the topic for this display panel.
  137. *
  138. * @param topic New topic
  139. */
  140. public void setTopic(final Optional<Topic> topic) {
  141. if (topic.isPresent()) {
  142. topicWho.setText("Topic set by " + topic.get().getClient()
  143. .map(GroupChatUser::getNickname).orElse("Unknown")
  144. + "<br> on " + topic.get().getDate());
  145. topicText.setText(topic.get().getTopic());
  146. } else {
  147. topicWho.setText("No topic set.");
  148. }
  149. }
  150. /**
  151. * Gets the topic text currently being displayed
  152. *
  153. * @return Current topic text
  154. */
  155. public String getTopic() {
  156. return topicText.getText();
  157. }
  158. /** Handles the topic change. */
  159. private void topicChanged() {
  160. if (topicLengthMax == 0) {
  161. topicLengthLabel.setForeground(Color.BLACK);
  162. topicLengthLabel.setText(topicText.getText().length()
  163. + " characters");
  164. } else {
  165. final int charsLeft = topicLengthMax - topicText.getText().length();
  166. if (charsLeft >= 0) {
  167. topicLengthLabel.setForeground(Color.BLACK);
  168. topicLengthLabel.setText(charsLeft + " of " + topicLengthMax
  169. + " available");
  170. } else {
  171. topicLengthLabel.setForeground(Color.RED);
  172. topicLengthLabel.setText(0 + " of " + topicLengthMax
  173. + " available " + (-1 * charsLeft)
  174. + " too many characters");
  175. }
  176. }
  177. }
  178. @Override
  179. public void insertUpdate(final DocumentEvent e) {
  180. topicChanged();
  181. }
  182. @Override
  183. public void removeUpdate(final DocumentEvent e) {
  184. topicChanged();
  185. }
  186. @Override
  187. public void changedUpdate(final DocumentEvent e) {
  188. //Ignore
  189. }
  190. }