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.

AwayLabel.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2014 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.FrameContainer;
  24. import com.dmdirc.addons.ui_swing.UIUtilities;
  25. import com.dmdirc.interfaces.AwayStateListener;
  26. import com.dmdirc.interfaces.FrameCloseListener;
  27. import com.dmdirc.interfaces.config.ConfigChangeListener;
  28. import javax.swing.JLabel;
  29. /**
  30. * Simple panel to show when a user is away or not.
  31. */
  32. public class AwayLabel extends JLabel implements ConfigChangeListener,
  33. AwayStateListener, FrameCloseListener {
  34. /**
  35. * A version number for this class. It should be changed whenever the class structure is changed
  36. * (or anything else that would prevent serialized objects being unserialized with the new
  37. * class).
  38. */
  39. private static final long serialVersionUID = 2;
  40. /** awayindicator string for compiler optimisation. */
  41. private static final String AWAY_INDICATOR = "awayindicator";
  42. /** Away indicator. */
  43. private boolean useAwayIndicator;
  44. /** Parent frame container. */
  45. private final FrameContainer container;
  46. /**
  47. * Creates a new away label for the specified container.
  48. *
  49. * @param container Parent frame container
  50. */
  51. public AwayLabel(final FrameContainer container) {
  52. super("(away)");
  53. this.container = container;
  54. container.getConfigManager().addChangeListener("ui", AWAY_INDICATOR,
  55. this);
  56. setVisible(false);
  57. useAwayIndicator = container.getConfigManager().getOptionBool("ui",
  58. AWAY_INDICATOR);
  59. if (container.getConnection() != null) {
  60. setVisible(container.getConnection().isAway());
  61. container.getConnection().addAwayStateListener(this);
  62. }
  63. container.addCloseListener(this);
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public void configChanged(final String domain, final String key) {
  68. useAwayIndicator = container.getConfigManager()
  69. .getOptionBool("ui", AWAY_INDICATOR);
  70. if (!useAwayIndicator) {
  71. UIUtilities.invokeLater(new Runnable() {
  72. /** {@inheritDoc} */
  73. @Override
  74. public void run() {
  75. setVisible(false);
  76. }
  77. });
  78. }
  79. }
  80. /** {@inheritDoc} */
  81. @Override
  82. public void onAway(final String reason) {
  83. UIUtilities.invokeLater(new Runnable() {
  84. /** {@inheritDoc} */
  85. @Override
  86. public void run() {
  87. if (useAwayIndicator) {
  88. setVisible(true);
  89. }
  90. }
  91. });
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public void onBack() {
  96. UIUtilities.invokeLater(new Runnable() {
  97. /** {@inheritDoc} */
  98. @Override
  99. public void run() {
  100. if (useAwayIndicator) {
  101. setVisible(false);
  102. }
  103. }
  104. });
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public void windowClosing(final FrameContainer window) {
  109. if (container != null && container.getConnection() != null) {
  110. container.getConnection().removeAwayStateListener(this);
  111. }
  112. }
  113. }