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.

TopicHistoryPane.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.addons.ui_swing.dialogs.channelsetting;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.Channel;
  25. import com.dmdirc.Topic;
  26. import com.dmdirc.util.ListenerList;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.util.Collections;
  30. import java.util.List;
  31. import javax.swing.JPanel;
  32. import javax.swing.JScrollPane;
  33. import javax.swing.ListSelectionModel;
  34. import javax.swing.event.ListSelectionEvent;
  35. import javax.swing.event.ListSelectionListener;
  36. import net.miginfocom.swing.MigLayout;
  37. /**
  38. * Topic history panel.
  39. */
  40. public class TopicHistoryPane extends JPanel implements ListSelectionListener {
  41. /**
  42. * A version number for this class. It should be changed whenever the class
  43. * structure is changed (or anything else that would prevent serialized
  44. * objects being unserialized with the new class).
  45. */
  46. private static final long serialVersionUID = 1;
  47. /** The table used to list previous topics. */
  48. private final TopicTable topicHistory = new TopicTable();
  49. /** The scrollpane for the list panel. */
  50. private final JScrollPane scrollPane = new JScrollPane(topicHistory,
  51. JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  52. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  53. /** Listener list. */
  54. private final ListenerList listeners = new ListenerList();
  55. /** Selected topic. */
  56. private int selectedTopic = -1;
  57. /**
  58. * Instantiates a new topic history pane.
  59. *
  60. * @param channel Parent channel
  61. */
  62. public TopicHistoryPane(final Channel channel) {
  63. topicHistory.getSelectionModel().addListSelectionListener(this);
  64. topicHistory.getSelectionModel().setSelectionMode(
  65. ListSelectionModel.SINGLE_SELECTION);
  66. final List<Topic> topics = channel.getTopics();
  67. Collections.reverse(topics);
  68. scrollPane.getVerticalScrollBar().setUnitIncrement(15);
  69. for (Topic topic : topics) {
  70. topicHistory.getModel().addRow(new Object[]{new TopicLabel(topic),});
  71. }
  72. topicHistory.getSelectionModel().setSelectionInterval(0, 0);
  73. setLayout(new MigLayout("fill, ins 0"));
  74. add(scrollPane, "hmin 50, hmax 200, wmax 450");
  75. this.setOpaque(UIUtilities.getTabbedPaneOpaque());
  76. }
  77. /**
  78. * Returns the select historical topic.
  79. *
  80. * @return Selected topic
  81. */
  82. public Topic getSelectedTopic() {
  83. if (topicHistory.getSelectedRow() == -1) {
  84. return null;
  85. }
  86. final Object topicLabel = topicHistory.getValueAt(topicHistory.
  87. getSelectedRow(), 0);
  88. if (topicLabel instanceof TopicLabel) {
  89. return ((TopicLabel) topicLabel).getTopic();
  90. }
  91. return null;
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public void valueChanged(final ListSelectionEvent e) {
  96. if (!e.getValueIsAdjusting()) {
  97. if (topicHistory.getSelectedRow() == -1) {
  98. topicHistory.getSelectionModel().setSelectionInterval(
  99. selectedTopic, selectedTopic);
  100. return;
  101. }
  102. selectedTopic = topicHistory.getSelectedRow();
  103. fireActionEvent();
  104. }
  105. }
  106. /**
  107. * Adds an action listener to this object.
  108. *
  109. * @param listener Listener to add
  110. */
  111. public void addActionListener(final ActionListener listener) {
  112. listeners.add(ActionListener.class, listener);
  113. }
  114. /**
  115. * Removes an action listener from this object.
  116. *
  117. * @param listener Listener to remove
  118. */
  119. public void removeActionListener(final ActionListener listener) {
  120. listeners.remove(ActionListener.class, listener);
  121. }
  122. /**
  123. * Fires a new action event.
  124. */
  125. private void fireActionEvent() {
  126. for (ActionListener listener : listeners.get(ActionListener.class)) {
  127. listener.actionPerformed(new ActionEvent(this,
  128. ActionEvent.ACTION_PERFORMED, ""));
  129. }
  130. }
  131. }