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

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