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.

NicklistRenderer.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.renderers;
  18. import com.dmdirc.addons.ui_swing.EDTInvocation;
  19. import com.dmdirc.addons.ui_swing.UIUtilities;
  20. import com.dmdirc.config.binding.ConfigBinding;
  21. import com.dmdirc.events.DisplayProperty;
  22. import com.dmdirc.interfaces.GroupChatUser;
  23. import com.dmdirc.config.provider.AggregateConfigProvider;
  24. import com.dmdirc.ui.messages.ColourManager;
  25. import java.awt.Color;
  26. import javax.swing.JLabel;
  27. import javax.swing.JList;
  28. import javax.swing.ListCellRenderer;
  29. /** Renders the nicklist. */
  30. public final class NicklistRenderer extends DMDircListCellRenderer<GroupChatUser> {
  31. /** Config manager. */
  32. private final AggregateConfigProvider config;
  33. /** Nicklist alternate background colour. */
  34. private Color altBackgroundColour;
  35. /** Show nick colours. */
  36. private boolean showColours;
  37. /** The list that we're using for the nicklist. */
  38. private final JList<GroupChatUser> nicklist;
  39. /** Colour manager to use to resolve colours. */
  40. private final ColourManager colourManager;
  41. /**
  42. * Creates a new instance of NicklistRenderer.
  43. *
  44. * @param config ConfigManager for the associated channel
  45. * @param nicklist The nicklist that we're rendering for.
  46. * @param colourManager Colour manager to use to resolve colours.
  47. */
  48. public NicklistRenderer(final ListCellRenderer<? super GroupChatUser> renderer,
  49. final AggregateConfigProvider config,
  50. final JList<GroupChatUser> nicklist,
  51. final ColourManager colourManager) {
  52. super(renderer);
  53. this.config = config;
  54. this.nicklist = nicklist;
  55. this.colourManager = colourManager;
  56. config.getBinder().bind(this, NicklistRenderer.class);
  57. }
  58. @Override
  59. protected void renderValue(final JLabel label, final GroupChatUser value, final int index,
  60. final boolean isSelected, final boolean hasFocus) {
  61. if (!isSelected && (index & 1) == 1) {
  62. label.setBackground(altBackgroundColour);
  63. }
  64. if (showColours) {
  65. value.getDisplayProperty(DisplayProperty.FOREGROUND_COLOUR).ifPresent(
  66. c -> label.setForeground(UIUtilities.convertColour(c)));
  67. value.getDisplayProperty(DisplayProperty.BACKGROUND_COLOUR).ifPresent(
  68. c -> label.setForeground(UIUtilities.convertColour(c)));
  69. }
  70. label.setText(value.getImportantMode() + value.getNickname());
  71. }
  72. @ConfigBinding(domain = "ui", key = "shownickcoloursinnicklist",
  73. invocation = EDTInvocation.class)
  74. public void handleShowColoursInNickList(final String value) {
  75. showColours = config.getOptionBool("ui", "shownickcoloursinnicklist");
  76. nicklist.repaint();
  77. }
  78. @ConfigBinding(domain = "ui", key = "nickListAltBackgroundColour",
  79. fallbacks = {"ui", "nicklistbackgroundcolour", "ui", "backgroundcolour"},
  80. invocation = EDTInvocation.class)
  81. public void handleColours(final String value) {
  82. altBackgroundColour = UIUtilities.convertColour(
  83. colourManager.getColourFromString(value, null));
  84. nicklist.repaint();
  85. }
  86. }