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

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