Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AwayLabel.java 4.0KB

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