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 6.8KB

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