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

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