Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AwayLabel.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2006-2015 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.addons.ui_swing.EDTInvocation;
  24. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  25. import com.dmdirc.config.ConfigBinding;
  26. import com.dmdirc.events.FrameClosingEvent;
  27. import com.dmdirc.events.ServerAwayEvent;
  28. import com.dmdirc.events.ServerBackEvent;
  29. import com.dmdirc.interfaces.Connection;
  30. import com.dmdirc.interfaces.WindowModel;
  31. import javax.swing.JLabel;
  32. import net.engio.mbassy.listener.Handler;
  33. import net.engio.mbassy.listener.Invoke;
  34. import static com.google.common.base.Preconditions.checkNotNull;
  35. /**
  36. * Simple panel to show when a user is away or not.
  37. */
  38. public class AwayLabel extends JLabel {
  39. /** A version number for this class. */
  40. private static final long serialVersionUID = 2;
  41. /** Away indicator. */
  42. private boolean useAwayIndicator;
  43. /** Parent frame container. */
  44. private final WindowModel container;
  45. /**
  46. * Creates a new away label for the specified container.
  47. *
  48. * @param container Parent frame container
  49. */
  50. public AwayLabel(final WindowModel container) {
  51. super("(away)");
  52. this.container = checkNotNull(container);
  53. container.getConfigManager().getBinder().bind(this, AwayLabel.class);
  54. container.getEventBus().subscribe(this);
  55. setVisible(false);
  56. container.getConnection().map(Connection::isAway).ifPresent(this::updateVisibility);
  57. }
  58. @ConfigBinding(domain = "ui", key = "awayindicator", invocation = EDTInvocation.class)
  59. public void handleAwayIndicator(final String value) {
  60. useAwayIndicator = Boolean.valueOf(value);
  61. container.getConnection().map(Connection::isAway).ifPresent(this::updateVisibility);
  62. }
  63. @Handler(delivery = Invoke.Asynchronously, invocation = EdtHandlerInvocation.class)
  64. public void handleAway(final ServerAwayEvent event) {
  65. container.getConnection().map(Connection::isAway).ifPresent(this::updateVisibility);
  66. }
  67. @Handler(delivery = Invoke.Asynchronously, invocation = EdtHandlerInvocation.class)
  68. public void handleBack(final ServerBackEvent event) {
  69. container.getConnection().filter(c -> c.equals(event.getConnection()))
  70. .map(Connection::isAway).ifPresent(this::updateVisibility);
  71. }
  72. private void updateVisibility(final boolean away) {
  73. setVisible(useAwayIndicator && away);
  74. }
  75. @Handler(invocation = EdtHandlerInvocation.class)
  76. public void windowClosing(final FrameClosingEvent event) {
  77. if (event.getSource().equals(container)) {
  78. container.getConfigManager().getBinder().unbind(this);
  79. container.getEventBus().unsubscribe(this);
  80. }
  81. }
  82. }