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.

NickList.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2006-2014 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.components;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.addons.ui_swing.EDTInvocation;
  25. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  26. import com.dmdirc.addons.ui_swing.UIUtilities;
  27. import com.dmdirc.addons.ui_swing.components.frames.ChannelFrame;
  28. import com.dmdirc.addons.ui_swing.components.renderers.NicklistRenderer;
  29. import com.dmdirc.addons.ui_swing.textpane.ClickType;
  30. import com.dmdirc.addons.ui_swing.textpane.ClickTypeValue;
  31. import com.dmdirc.config.ConfigBinding;
  32. import com.dmdirc.events.NickListClientAddedEvent;
  33. import com.dmdirc.events.NickListClientRemovedEvent;
  34. import com.dmdirc.events.NickListClientsChangedEvent;
  35. import com.dmdirc.events.NickListUpdatedEvent;
  36. import com.dmdirc.interfaces.GroupChatUser;
  37. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  38. import com.dmdirc.ui.messages.ColourManager;
  39. import com.dmdirc.ui.messages.ColourManagerFactory;
  40. import java.awt.Dimension;
  41. import java.awt.Font;
  42. import java.awt.Point;
  43. import java.awt.event.MouseEvent;
  44. import java.awt.event.MouseListener;
  45. import java.util.List;
  46. import javax.swing.JList;
  47. import javax.swing.JScrollPane;
  48. import javax.swing.ListSelectionModel;
  49. import net.engio.mbassy.listener.Handler;
  50. /**
  51. * Nicklist class.
  52. */
  53. public class NickList extends JScrollPane implements MouseListener {
  54. /** A version number for this class. */
  55. private static final long serialVersionUID = 10;
  56. /** Nick list. */
  57. private final JList<GroupChatUser> nickList;
  58. /** Parent frame. */
  59. private final ChannelFrame frame;
  60. /** The colour manager to use for this nicklist. */
  61. private final ColourManager colourManager;
  62. /** Nick list model. */
  63. private final NicklistListModel nicklistModel;
  64. /**
  65. * Creates a nicklist.
  66. *
  67. * @param frame Frame
  68. * @param config Config
  69. */
  70. public NickList(final ChannelFrame frame, final AggregateConfigProvider config,
  71. final ColourManagerFactory colourManagerFactory) {
  72. this.frame = frame;
  73. this.colourManager = colourManagerFactory.getColourManager(config);
  74. nickList = new JList<>();
  75. nickList.setCellRenderer(new NicklistRenderer(nickList.getCellRenderer(), config,
  76. nickList, colourManager));
  77. nickList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  78. nickList.addMouseListener(this);
  79. nicklistModel = new NicklistListModel(config);
  80. nickList.setModel(nicklistModel);
  81. setViewportView(nickList);
  82. final int splitPanePosition = config.getOptionInt("ui", "channelSplitPanePosition");
  83. setPreferredSize(new Dimension(splitPanePosition, 0));
  84. setMinimumSize(new Dimension(75, 0));
  85. nicklistModel.replace(((Channel) frame.getContainer()).getUsers());
  86. frame.getContainer().getEventBus().subscribe(this);
  87. config.getBinder().bind(this, NickList.class);
  88. }
  89. @Override
  90. public void mouseClicked(final MouseEvent e) {
  91. processMouseEvent(e);
  92. }
  93. @Override
  94. public void mousePressed(final MouseEvent e) {
  95. processMouseEvent(e);
  96. }
  97. @Override
  98. public void mouseReleased(final MouseEvent e) {
  99. processMouseEvent(e);
  100. }
  101. @Override
  102. public void mouseEntered(final MouseEvent e) {
  103. //Ignore
  104. }
  105. @Override
  106. public void mouseExited(final MouseEvent e) {
  107. //Ignore
  108. }
  109. @Override
  110. public void processMouseEvent(final MouseEvent e) {
  111. if (!e.isPopupTrigger()
  112. || e.getSource() != nickList
  113. || nickList.getMousePosition() == null) {
  114. return;
  115. }
  116. if (checkCursorInSelectedCell() || selectNickUnderCursor()) {
  117. final List<GroupChatUser> values = nickList.getSelectedValuesList();
  118. final StringBuilder builder = new StringBuilder();
  119. for (GroupChatUser value : values) {
  120. if (builder.length() > 0) {
  121. builder.append('\n');
  122. }
  123. builder.append(value.getNickname());
  124. }
  125. frame.showPopupMenu(new ClickTypeValue(ClickType.NICKNAME,
  126. builder.toString()), new Point(e.getXOnScreen(),
  127. e.getYOnScreen()));
  128. } else {
  129. nickList.clearSelection();
  130. }
  131. super.processMouseEvent(e);
  132. }
  133. /**
  134. * Checks whether the mouse cursor is currently over a cell in the nicklist which has been
  135. * previously selected.
  136. *
  137. * @return True if the cursor is over a selected cell, false otherwise
  138. */
  139. private boolean checkCursorInSelectedCell() {
  140. boolean showMenu = false;
  141. final Point mousePos = nickList.getMousePosition();
  142. if (mousePos != null) {
  143. for (int i = 0; i < nickList.getModel().getSize(); i++) {
  144. if (nickList.getCellBounds(i, i) != null && nickList.
  145. getCellBounds(i, i).
  146. contains(mousePos) && nickList.isSelectedIndex(i)) {
  147. showMenu = true;
  148. break;
  149. }
  150. }
  151. }
  152. return showMenu;
  153. }
  154. /**
  155. * If the mouse cursor is over a nick list cell, sets that cell to be selected and returns true.
  156. * If the mouse is not over any cell, the selection is unchanged and the method returns false.
  157. *
  158. * @return True if an item was selected
  159. */
  160. private boolean selectNickUnderCursor() {
  161. boolean suceeded = false;
  162. final Point mousePos = nickList.getMousePosition();
  163. if (mousePos != null) {
  164. for (int i = 0; i < nickList.getModel().getSize(); i++) {
  165. if (nickList.getCellBounds(i, i) != null && nickList.
  166. getCellBounds(i, i).
  167. contains(mousePos)) {
  168. nickList.setSelectedIndex(i);
  169. suceeded = true;
  170. break;
  171. }
  172. }
  173. }
  174. return suceeded;
  175. }
  176. @ConfigBinding(domain = "ui", key = "textPaneFontName", invocation = EDTInvocation.class)
  177. public void handleFontName(final String value) {
  178. nickList.setFont(new Font(value, Font.PLAIN, getFont().getSize()));
  179. nickList.repaint();
  180. }
  181. @ConfigBinding(domain = "ui", key = "nicklistbackgroundcolour",
  182. fallbacks = {"ui", "backgroundcolour"}, invocation = EDTInvocation.class)
  183. public void handleBackgroundColour(final String value) {
  184. nickList.setBackground(UIUtilities.convertColour(
  185. colourManager.getColourFromString(value, null)));
  186. nickList.repaint();
  187. }
  188. @ConfigBinding(domain = "ui", key = "nicklistforegroundcolour",
  189. fallbacks = {"ui", "foregroundcolour"}, invocation = EDTInvocation.class)
  190. public void handleForegroundColour(final String value) {
  191. nickList.setForeground(UIUtilities.convertColour(
  192. colourManager.getColourFromString(value, null)));
  193. nickList.repaint();
  194. }
  195. @Handler(invocation = EdtHandlerInvocation.class)
  196. public void handleClientsChanged(final NickListClientsChangedEvent event) {
  197. nicklistModel.replace(event.getUsers());
  198. }
  199. @Handler(invocation = EdtHandlerInvocation.class)
  200. public void handleNickListUpdated(final NickListUpdatedEvent event) {
  201. nicklistModel.sort();
  202. repaint();
  203. }
  204. @Handler(invocation = EdtHandlerInvocation.class)
  205. public void handleClientAdded(final NickListClientAddedEvent event) {
  206. nicklistModel.add(event.getUser());
  207. }
  208. @Handler(invocation = EdtHandlerInvocation.class)
  209. public void handleClientRemoved(final NickListClientRemovedEvent event) {
  210. nicklistModel.remove(event.getUser());
  211. }
  212. }