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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2006-2015 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.addons.ui_swing.dialogs.channelsetting;
  23. import com.dmdirc.Topic;
  24. import com.dmdirc.addons.ui_swing.UIUtilities;
  25. import com.dmdirc.addons.ui_swing.actions.ReplacePasteAction;
  26. import com.dmdirc.addons.ui_swing.components.IconManager;
  27. import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputHandler;
  28. import com.dmdirc.addons.ui_swing.components.inputfields.TextAreaInputField;
  29. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  30. import com.dmdirc.interfaces.CommandController;
  31. import com.dmdirc.interfaces.GroupChat;
  32. import com.dmdirc.interfaces.GroupChatUser;
  33. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  34. import com.dmdirc.interfaces.ui.InputWindow;
  35. import com.dmdirc.plugins.ServiceManager;
  36. import com.dmdirc.ui.input.TabCompleterUtils;
  37. import com.dmdirc.ui.messages.ColourManagerFactory;
  38. import java.awt.Color;
  39. import java.awt.datatransfer.Clipboard;
  40. import java.awt.event.KeyEvent;
  41. import java.util.Optional;
  42. import javax.swing.JLabel;
  43. import javax.swing.JPanel;
  44. import javax.swing.JScrollPane;
  45. import javax.swing.KeyStroke;
  46. import javax.swing.event.DocumentEvent;
  47. import javax.swing.event.DocumentListener;
  48. import net.miginfocom.swing.MigLayout;
  49. /**
  50. * Class to display a topic to an end user as part of the groupChat settings dialog.
  51. */
  52. public class TopicDisplayPane extends JPanel implements DocumentListener {
  53. /** Serial version UID. */
  54. private static final long serialVersionUID = 1;
  55. /** Parent topic pane. */
  56. private final ChannelSettingsDialog parent;
  57. /** Associated group chat. */
  58. private final GroupChat groupChat;
  59. /** Channel window. */
  60. private final InputWindow channelWindow;
  61. /** the maximum length allowed for a topic. */
  62. private final int topicLengthMax;
  63. /** Clipboard to copy and paste from. */
  64. private final Clipboard clipboard;
  65. /** label showing the number of characters left in a topic. */
  66. private JLabel topicLengthLabel;
  67. /** Topic text entry text area. */
  68. private TextAreaInputField topicText;
  69. /** Topic who. */
  70. private TextLabel topicWho;
  71. /**
  72. * Creates a new topic display panel. This panel shows an editable version of the current topic
  73. * along with relating meta data and validates the length of the new input.
  74. *
  75. * @param groupChat Associated group chat
  76. * @param iconManager Icon manager
  77. * @param serviceManager Service manager
  78. * @param parent Parent settings dialog
  79. * @param channelWindow Channel window
  80. * @param clipboard Clipboard to copy and paste
  81. * @param commandController The controller to use to retrieve command information.
  82. */
  83. public TopicDisplayPane(final GroupChat groupChat, final IconManager iconManager,
  84. final ServiceManager serviceManager, final ChannelSettingsDialog parent,
  85. final InputWindow channelWindow, final Clipboard clipboard,
  86. final CommandController commandController,
  87. final ColourManagerFactory colourManagerFactory,
  88. final TabCompleterUtils tabCompleterUtils) {
  89. this.clipboard = clipboard;
  90. this.groupChat = groupChat;
  91. this.parent = parent;
  92. topicLengthMax = groupChat.getConnection().get().getParser().get().getMaxTopicLength();
  93. this.channelWindow = channelWindow;
  94. initComponents(iconManager, groupChat.getWindowModel().getConfigManager(), serviceManager,
  95. commandController, colourManagerFactory, tabCompleterUtils);
  96. addListeners();
  97. layoutComponents();
  98. setTopic(groupChat.getCurrentTopic());
  99. }
  100. private void initComponents(
  101. final IconManager iconManager,
  102. final AggregateConfigProvider config,
  103. final ServiceManager serviceManager,
  104. final CommandController commandController,
  105. final ColourManagerFactory colourManagerFactory,
  106. final TabCompleterUtils tabCompleterUtils) {
  107. topicLengthLabel = new JLabel();
  108. topicText = new TextAreaInputField(iconManager, colourManagerFactory, config, 100, 4);
  109. topicWho = new TextLabel();
  110. topicWho.setOpaque(false);
  111. topicText.setLineWrap(true);
  112. topicText.setWrapStyleWord(true);
  113. topicText.setRows(5);
  114. topicText.setColumns(30);
  115. final SwingInputHandler handler = new SwingInputHandler(serviceManager, topicText,
  116. commandController,
  117. groupChat.getWindowModel().getInputModel().get().getCommandParser(),
  118. channelWindow.getContainer(), tabCompleterUtils, groupChat.getEventBus());
  119. handler.setTypes(true, false, true, false);
  120. handler.setTabCompleter(groupChat.getWindowModel().getInputModel().get().getTabCompleter());
  121. topicText.getActionMap().put("paste-from-clipboard",
  122. new ReplacePasteAction(clipboard, "(\r\n|\n|\r)", " "));
  123. topicText.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
  124. 0), new TopicEnterAction(parent));
  125. topicText.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
  126. UIUtilities.getCtrlDownMask()), new TopicEnterAction(parent));
  127. UIUtilities.addUndoManager(topicText);
  128. }
  129. /** Adds listeners to the components. */
  130. private void addListeners() {
  131. topicText.getDocument().addDocumentListener(this);
  132. }
  133. /** Lays out the components. */
  134. private void layoutComponents() {
  135. setLayout(new MigLayout("wrap 1, fill, ins 0"));
  136. add(new JScrollPane(topicText), "grow, push");
  137. add(topicLengthLabel, "pushx, growx, pushx");
  138. add(topicWho, "growx, pushx");
  139. }
  140. /**
  141. * Sets the topic for this display panel.
  142. *
  143. * @param topic New topic
  144. */
  145. public void setTopic(final Optional<Topic> topic) {
  146. if (topic.isPresent()) {
  147. topicWho.setText("Topic set by " + topic.get().getClient()
  148. .map(GroupChatUser::getNickname).orElse("Unknown")
  149. + "<br> on " + topic.get().getDate());
  150. topicText.setText(topic.get().getTopic());
  151. } else {
  152. topicWho.setText("No topic set.");
  153. }
  154. }
  155. /**
  156. * Gets the topic text currently being displayed
  157. *
  158. * @return Current topic text
  159. */
  160. public String getTopic() {
  161. return topicText.getText();
  162. }
  163. /** Handles the topic change. */
  164. private void topicChanged() {
  165. if (topicLengthMax == 0) {
  166. topicLengthLabel.setForeground(Color.BLACK);
  167. topicLengthLabel.setText(topicText.getText().length()
  168. + " characters");
  169. } else {
  170. final int charsLeft = topicLengthMax - topicText.getText().length();
  171. if (charsLeft >= 0) {
  172. topicLengthLabel.setForeground(Color.BLACK);
  173. topicLengthLabel.setText(charsLeft + " of " + topicLengthMax
  174. + " available");
  175. } else {
  176. topicLengthLabel.setForeground(Color.RED);
  177. topicLengthLabel.setText(0 + " of " + topicLengthMax
  178. + " available " + (-1 * charsLeft)
  179. + " too many characters");
  180. }
  181. }
  182. }
  183. @Override
  184. public void insertUpdate(final DocumentEvent e) {
  185. topicChanged();
  186. }
  187. @Override
  188. public void removeUpdate(final DocumentEvent e) {
  189. topicChanged();
  190. }
  191. @Override
  192. public void changedUpdate(final DocumentEvent e) {
  193. //Ignore
  194. }
  195. }