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.

TypingLabel.java 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.EdtHandlerInvocation;
  19. import com.dmdirc.addons.ui_swing.UIUtilities;
  20. import com.dmdirc.config.binding.ConfigBinding;
  21. import com.dmdirc.events.FrameClosingEvent;
  22. import com.dmdirc.events.FrameComponentAddedEvent;
  23. import com.dmdirc.events.FrameComponentRemovedEvent;
  24. import com.dmdirc.interfaces.WindowModel;
  25. import com.dmdirc.ui.core.components.WindowComponent;
  26. import javax.swing.JLabel;
  27. import net.engio.mbassy.listener.Handler;
  28. /**
  29. * Simple panel to show when a user is typing.
  30. */
  31. public class TypingLabel extends JLabel {
  32. /** A version number for this class. */
  33. private static final long serialVersionUID = 2;
  34. /** Parent frame container. */
  35. private final WindowModel container;
  36. /** Whether or not to show the typing indicator. */
  37. private boolean useTypingIndicator;
  38. /**
  39. * Creates a new typing label for the specified container.
  40. *
  41. * @param container Parent frame container
  42. */
  43. public TypingLabel(final WindowModel container) {
  44. super("[Typing...]");
  45. this.container = container;
  46. setVisible(false);
  47. container.getEventBus().subscribe(this);
  48. container.getConfigManager().getBinder().bind(this, TypingLabel.class);
  49. if (container.getComponents().contains(WindowComponent.TYPING_INDICATOR.getIdentifier())) {
  50. setVisible(true);
  51. }
  52. }
  53. @ConfigBinding(domain = "ui", key = "typingindicator")
  54. public void handleTypingIndicator(final String value) {
  55. useTypingIndicator = Boolean.valueOf(value);
  56. if (!useTypingIndicator) {
  57. UIUtilities.invokeLater(() -> setVisible(false));
  58. }
  59. }
  60. @Handler(invocation = EdtHandlerInvocation.class)
  61. public void componentAdded(final FrameComponentAddedEvent event) {
  62. if (event.getContainer().equals(container) &&
  63. WindowComponent.TYPING_INDICATOR.getIdentifier().equals(event.getComponent()) &&
  64. useTypingIndicator) {
  65. setVisible(true);
  66. }
  67. }
  68. @Handler(invocation = EdtHandlerInvocation.class)
  69. public void componentRemoved(final FrameComponentRemovedEvent event) {
  70. if (event.getContainer().equals(container) &&
  71. WindowComponent.TYPING_INDICATOR.getIdentifier().equals(event.getComponent()) &&
  72. useTypingIndicator) {
  73. setVisible(false);
  74. }
  75. }
  76. @Handler(invocation = EdtHandlerInvocation.class)
  77. public void windowClosing(final FrameClosingEvent event) {
  78. if (event.getSource().equals(container)) {
  79. container.getConfigManager().getBinder().unbind(this);
  80. container.getEventBus().unsubscribe(this);
  81. }
  82. }
  83. }