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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.components;
  24. import com.dmdirc.addons.ui_swing.components.frames.ChannelFrame;
  25. import com.dmdirc.addons.ui_swing.components.renderers.NicklistRenderer;
  26. import com.dmdirc.addons.ui_swing.textpane.ClickType;
  27. import com.dmdirc.config.ConfigManager;
  28. import com.dmdirc.interfaces.ConfigChangeListener;
  29. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  30. import java.awt.Dimension;
  31. import java.awt.Point;
  32. import java.awt.event.MouseEvent;
  33. import java.awt.event.MouseListener;
  34. import java.util.Collection;
  35. import javax.swing.JList;
  36. import javax.swing.JScrollPane;
  37. import javax.swing.ListSelectionModel;
  38. import javax.swing.SwingUtilities;
  39. /**
  40. * Nicklist class.
  41. */
  42. public class NickList extends JScrollPane implements ConfigChangeListener,
  43. MouseListener {
  44. /**
  45. * A version number for this class. It should be changed whenever the class
  46. * structure is changed (or anything else that would prevent serialized
  47. * objects being unserialized with the new class).
  48. */
  49. private static final long serialVersionUID = 10;
  50. /** Nick list. */
  51. private JList nickList;
  52. /** Parent frame. */
  53. private final ChannelFrame frame;
  54. /** Config. */
  55. private final ConfigManager config;
  56. /** Nick list model. */
  57. private final NicklistListModel nicklistModel;
  58. /**
  59. * Creates a nicklist.
  60. *
  61. * @param frame Frame
  62. * @param config Config
  63. */
  64. public NickList(final ChannelFrame frame, final ConfigManager config) {
  65. super();
  66. this.frame = frame;
  67. this.config = config;
  68. nickList = new JList();
  69. nickList.setBackground(config.getOptionColour(
  70. "ui", "nicklistbackgroundcolour",
  71. "ui", "backgroundcolour"));
  72. nickList.setForeground(config.getOptionColour(
  73. "ui", "nicklistforegroundcolour",
  74. "ui", "foregroundcolour"));
  75. config.addChangeListener("ui", "nicklistforegroundcolour", this);
  76. config.addChangeListener("ui", "foregroundcolour", this);
  77. config.addChangeListener("ui", "nicklistbackgroundcolour", this);
  78. config.addChangeListener("ui", "backgroundcolour", this);
  79. config.addChangeListener("ui", "nickListAltBackgroundColour", this);
  80. nickList.setCellRenderer(new NicklistRenderer(config, nickList));
  81. nickList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  82. nickList.addMouseListener(this);
  83. nicklistModel = new NicklistListModel(config);
  84. nickList.setModel(nicklistModel);
  85. setViewportView(nickList);
  86. final int splitPanePosition = config.getOptionInt("ui",
  87. "channelSplitPanePosition");
  88. setPreferredSize(new Dimension(splitPanePosition, 0));
  89. setMinimumSize(new Dimension(75, 0));
  90. }
  91. /**
  92. * {@inheritDoc}
  93. *
  94. * @param e Mouse event
  95. */
  96. @Override
  97. public void mouseClicked(final MouseEvent e) {
  98. processMouseEvent(e);
  99. }
  100. /**
  101. * {@inheritDoc}
  102. *
  103. * @param e Mouse event
  104. */
  105. @Override
  106. public void mousePressed(final MouseEvent e) {
  107. processMouseEvent(e);
  108. }
  109. /**
  110. * {@inheritDoc}
  111. *
  112. * @param e Mouse event
  113. */
  114. @Override
  115. public void mouseReleased(final MouseEvent e) {
  116. processMouseEvent(e);
  117. }
  118. /**
  119. * {@inheritDoc}
  120. *
  121. * @param e Mouse event
  122. */
  123. @Override
  124. public void mouseEntered(MouseEvent e) {
  125. //Ignore
  126. }
  127. /**
  128. * {@inheritDoc}
  129. *
  130. * @param e Mouse event
  131. */
  132. @Override
  133. public void mouseExited(final MouseEvent e) {
  134. //Ignore
  135. }
  136. /**
  137. * Processes every mouse button event to check for a popup trigger.
  138. *
  139. * @param e mouse event
  140. */
  141. @Override
  142. public void processMouseEvent(final MouseEvent e) {
  143. if (e.getSource() == nickList && nickList.getMousePosition() != null &&
  144. getMousePosition() != null) {
  145. if (checkCursorInSelectedCell() || selectNickUnderCursor()) {
  146. if (e.isPopupTrigger()) {
  147. frame.showPopupMenu(ClickType.NICKNAME, new Point(e.
  148. getXOnScreen(), e.getYOnScreen()),
  149. ((ChannelClientInfo) nickList.getSelectedValue()).
  150. getClient().getNickname());
  151. }
  152. } else {
  153. nickList.clearSelection();
  154. }
  155. }
  156. super.processMouseEvent(e);
  157. }
  158. /**
  159. * Checks whether the mouse cursor is currently over a cell in the nicklist
  160. * which has been previously selected.
  161. *
  162. * @return True if the cursor is over a selected cell, false otherwise
  163. */
  164. private boolean checkCursorInSelectedCell() {
  165. boolean showMenu = false;
  166. final Point mousePos = nickList.getMousePosition();
  167. if (mousePos != null) {
  168. for (int i = 0; i < nickList.getModel().getSize(); i++) {
  169. if (nickList.getCellBounds(i, i) != null && nickList.
  170. getCellBounds(i, i).
  171. contains(mousePos) && nickList.isSelectedIndex(i)) {
  172. showMenu = true;
  173. break;
  174. }
  175. }
  176. }
  177. return showMenu;
  178. }
  179. /**
  180. * If the mouse cursor is over a nicklist cell, sets that cell to be
  181. * selected and returns true. If the mouse is not over any cell, the
  182. * selection is unchanged and the method returns false.
  183. *
  184. * @return True if an item was selected
  185. */
  186. private boolean selectNickUnderCursor() {
  187. boolean suceeded = false;
  188. final Point mousePos = nickList.getMousePosition();
  189. if (mousePos != null) {
  190. for (int i = 0; i < nickList.getModel().getSize(); i++) {
  191. if (nickList.getCellBounds(i, i) != null && nickList.
  192. getCellBounds(i, i).
  193. contains(mousePos)) {
  194. nickList.setSelectedIndex(i);
  195. suceeded = true;
  196. break;
  197. }
  198. }
  199. }
  200. return suceeded;
  201. }
  202. /** {@inheritDoc} */
  203. @Override
  204. public void configChanged(final String domain, final String key) {
  205. if ("nickListAltBackgroundColour".equals(key) ||
  206. "nicklistbackgroundcolour".equals(key) ||
  207. "backgroundcolour".equals(key) ||
  208. "nicklistforegroundcolour".equals(key) ||
  209. "foregroundcolour".equals(key)) {
  210. nickList.setBackground(config.getOptionColour(
  211. "ui", "nicklistbackgroundcolour",
  212. "ui", "backgroundcolour"));
  213. nickList.setForeground(config.getOptionColour(
  214. "ui", "nicklistforegroundcolour",
  215. "ui", "foregroundcolour"));
  216. nickList.repaint();
  217. }
  218. nickList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  219. }
  220. /**
  221. * Updates the names in this nicklist,
  222. *
  223. * @param clients Clients to show
  224. */
  225. public void updateNames(final Collection<ChannelClientInfo> clients) {
  226. SwingUtilities.invokeLater(new Runnable() {
  227. /** {@inheritDoc} */
  228. @Override
  229. public void run() {
  230. nicklistModel.replace(clients);
  231. }
  232. });
  233. }
  234. /**
  235. * Updates the order of this nicklist.
  236. */
  237. public void updateNames() {
  238. SwingUtilities.invokeLater(new Runnable() {
  239. /** {@inheritDoc} */
  240. @Override
  241. public void run() {
  242. nicklistModel.sort();
  243. }
  244. });
  245. }
  246. /**
  247. * Adds a client to this nicklist.
  248. *
  249. * @param client Client to add
  250. */
  251. public void addName(final ChannelClientInfo client) {
  252. SwingUtilities.invokeLater(new Runnable() {
  253. /** {@inheritDoc} */
  254. @Override
  255. public void run() {
  256. nicklistModel.add(client);
  257. }
  258. });
  259. }
  260. /**
  261. * Removes a client from the nicklist.
  262. *
  263. * @param client Client to remove
  264. */
  265. public void removeName(final ChannelClientInfo client) {
  266. SwingUtilities.invokeLater(new Runnable() {
  267. /** {@inheritDoc} */
  268. @Override
  269. public void run() {
  270. nicklistModel.remove(client);
  271. }
  272. });
  273. }
  274. }