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.

StatusbarPanel.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.statusbar;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.interfaces.ui.StatusBarComponent;
  20. import java.awt.Color;
  21. import java.awt.event.MouseListener;
  22. import javax.swing.JComponent;
  23. import javax.swing.JPanel;
  24. import javax.swing.UIManager;
  25. import javax.swing.border.Border;
  26. import javax.swing.border.EtchedBorder;
  27. import net.miginfocom.swing.MigLayout;
  28. /**
  29. * A panel shown in the status bar which displays a {@link StatusbarPopupWindow} when the user
  30. * mouses over it.
  31. *
  32. * @param <T> Type of component used to trigger this panel
  33. *
  34. * @since 0.6.3m1
  35. */
  36. public abstract class StatusbarPanel<T extends JComponent> extends JPanel
  37. implements StatusBarComponent, MouseListener {
  38. /** The label we use to show information. */
  39. protected final T label;
  40. /** A version number for this class. */
  41. private static final long serialVersionUID = 2;
  42. /** The popup window we're using to show extra info. */
  43. private StatusbarPopupWindow dialog;
  44. /** Non selected border. */
  45. protected final Border nonSelectedBorder;
  46. /** Selected border. */
  47. protected final Border selectedBorder;
  48. /**
  49. * Creates a new {@link StatusbarPanel}, using the specified label.
  50. *
  51. * @param label The component to be displayed in the status bar
  52. */
  53. public StatusbarPanel(final T label) {
  54. this(label, new EtchedBorder(), new SidelessEtchedBorder(
  55. SidelessEtchedBorder.Side.TOP));
  56. }
  57. /**
  58. * Creates a new {@link StatusbarPanel}.
  59. *
  60. * @param label The component to be displayed in the status bar
  61. * @param nonSelectedBorder The border for when the panel is unselected
  62. * @param selectedBorder The border for when for the panel is selected
  63. */
  64. public StatusbarPanel(final T label, final Border nonSelectedBorder,
  65. final Border selectedBorder) {
  66. this.label = label;
  67. this.nonSelectedBorder = nonSelectedBorder;
  68. this.selectedBorder = selectedBorder;
  69. setBorder(nonSelectedBorder);
  70. setLayout(new MigLayout("ins 0 rel 0 rel, aligny center"));
  71. add(label);
  72. addMouseListener(this);
  73. }
  74. /**
  75. * Returns the component associated with this panel.
  76. *
  77. * @return Assocaited component
  78. */
  79. public T getComponent() {
  80. return label;
  81. }
  82. /**
  83. * Closes and reopens the dialog to update information and border positions.
  84. */
  85. public final void refreshDialog() {
  86. UIUtilities.invokeLater(() -> {
  87. synchronized (this) {
  88. if (dialog != null) {
  89. closeDialog();
  90. openDialog();
  91. }
  92. }
  93. });
  94. }
  95. /**
  96. * Opens the information dialog.
  97. */
  98. protected void openDialog() {
  99. synchronized (this) {
  100. if (dialog == null) {
  101. setBackground(getPopupBackground());
  102. setForeground(getPopupForeground());
  103. setBorder(selectedBorder);
  104. dialog = getWindow();
  105. dialog.setVisible(true);
  106. }
  107. }
  108. }
  109. /**
  110. * Closes the information dialog.
  111. */
  112. protected void closeDialog() {
  113. synchronized (this) {
  114. if (dialog != null) {
  115. setBackground(null);
  116. setForeground(null);
  117. setBorder(nonSelectedBorder);
  118. dialog.setVisible(false);
  119. dialog.dispose();
  120. dialog = null;
  121. }
  122. }
  123. }
  124. /**
  125. * Checks if this dialog is open.
  126. *
  127. * @return is the dialog open
  128. */
  129. protected final boolean isDialogOpen() {
  130. synchronized (this) {
  131. return dialog != null && dialog.isVisible();
  132. }
  133. }
  134. /**
  135. * Returns the dialog being shown by this panel.
  136. *
  137. * @return Visible dialog, or null if not visible
  138. */
  139. protected final StatusbarPopupWindow getDialog() {
  140. return dialog;
  141. }
  142. /**
  143. * Returns the colour to be used for the panel's foreground.
  144. *
  145. * @return Foreground colour
  146. */
  147. protected Color getPopupForeground() {
  148. return UIManager.getColor("ToolTip.foreground");
  149. }
  150. /**
  151. * Returns the colour to be used for the panel's background.
  152. *
  153. * @return Background colour
  154. */
  155. protected Color getPopupBackground() {
  156. return UIManager.getColor("ToolTip.background");
  157. }
  158. /**
  159. * Retrieves the implementation of {@link StatusbarPopupWindow} that should be shown by this
  160. * panel when the user mouses over it.
  161. *
  162. * @return A concrete {@link StatusbarPopupWindow} implementation to use
  163. */
  164. protected abstract StatusbarPopupWindow getWindow();
  165. }