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.

ToolTipPanel.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  20. import java.awt.Color;
  21. import java.awt.event.MouseEvent;
  22. import java.awt.event.MouseListener;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import javax.swing.BorderFactory;
  26. import javax.swing.JComponent;
  27. import javax.swing.JLabel;
  28. import javax.swing.JLayer;
  29. import javax.swing.JPanel;
  30. import javax.swing.plaf.LayerUI;
  31. import javax.swing.text.SimpleAttributeSet;
  32. import javax.swing.text.StyleConstants;
  33. import net.miginfocom.swing.MigLayout;
  34. /**
  35. * Panel to display tool tips of a component.
  36. */
  37. public class ToolTipPanel extends JPanel implements MouseListener {
  38. /** Serial version UID. */
  39. private static final long serialVersionUID = -8929794537312606692L;
  40. /** Default tool tip. */
  41. private final String defaultHelp;
  42. /** Tool tip display. */
  43. private final TextLabel tooltip;
  44. /** Error icon. */
  45. private final JLabel icon;
  46. /** Whether or not this is a warning. */
  47. private String warningText = null;
  48. /** Map of registered components to their tool tips. */
  49. private final Map<JComponent, String> tooltips;
  50. /**
  51. * Instantiates a new tool tip panel.
  52. *
  53. * @param iconManager Icon Manager
  54. * @param helpText Default help message when idle
  55. */
  56. public ToolTipPanel(final IconManager iconManager, final String helpText) {
  57. super(new MigLayout("hidemode 3"));
  58. defaultHelp = helpText;
  59. tooltips = new HashMap<>();
  60. icon = new JLabel(iconManager.getIcon("warning"));
  61. setBackground(Color.WHITE);
  62. setForeground(Color.BLACK);
  63. setBorder(BorderFactory.createEtchedBorder());
  64. tooltip = new TextLabel();
  65. reset();
  66. add(icon, "aligny top");
  67. add(tooltip, "grow, push");
  68. }
  69. /**
  70. * Resets the content of the tool tip.
  71. */
  72. protected final void reset() {
  73. final SimpleAttributeSet sas = new SimpleAttributeSet();
  74. StyleConstants.setForeground(sas, Color.BLACK);
  75. StyleConstants.setBackground(sas, Color.WHITE);
  76. if (warningText == null || warningText.isEmpty()) {
  77. tooltip.setText(defaultHelp);
  78. icon.setVisible(false);
  79. StyleConstants.setItalic(sas, true);
  80. } else {
  81. icon.setVisible(true);
  82. tooltip.setText(warningText);
  83. }
  84. tooltip.getDocument().setParagraphAttributes(0, tooltip.getDocument().
  85. getLength(), sas, true);
  86. }
  87. /**
  88. * Sets the content of the tool tip area to the specified text.
  89. *
  90. * @param text The text to be displayed
  91. */
  92. protected void setText(final String text) {
  93. if (tooltip == null) {
  94. return;
  95. }
  96. tooltip.setText(text);
  97. if (tooltip.getDocument() == null || text == null) {
  98. return;
  99. }
  100. icon.setVisible(false);
  101. final SimpleAttributeSet sas = new SimpleAttributeSet();
  102. StyleConstants.setItalic(sas, false);
  103. StyleConstants.setForeground(sas, Color.BLACK);
  104. StyleConstants.setBackground(sas, Color.WHITE);
  105. tooltip.getDocument().setParagraphAttributes(0, text.length(), sas,
  106. true);
  107. }
  108. /**
  109. * Sets whether or not this tool tip should be rendered as a warning.
  110. *
  111. * @param warning Warning string, null or empty to reset.
  112. *
  113. * @since 0.6.3
  114. */
  115. public void setWarning(final String warning) {
  116. warningText = warning;
  117. reset();
  118. }
  119. /**
  120. * Registers a component with this tool tip handler.
  121. *
  122. * @param component Component to register
  123. */
  124. public void registerTooltipHandler(final JComponent component) {
  125. registerTooltipHandler(component, component.getToolTipText());
  126. component.setToolTipText(null);
  127. }
  128. /**
  129. * Registers a component with this tool tip handler.
  130. *
  131. * @param component Component to register
  132. * @param tooltipText Tool tip text for the component
  133. */
  134. @SuppressWarnings("unchecked")
  135. public void registerTooltipHandler(final JComponent component,
  136. final String tooltipText) {
  137. if (component == null) {
  138. return;
  139. }
  140. tooltips.put(component, tooltipText);
  141. if (component instanceof JLayer<?>) {
  142. final LayerUI<JComponent> layerUI = new LayerUI<JComponent>() {
  143. private static final long serialVersionUID = -8698248993206174390L;
  144. @Override
  145. protected void processMouseEvent(final MouseEvent e,
  146. final JLayer<? extends JComponent> comp) {
  147. if (e.getID() == MouseEvent.MOUSE_ENTERED) {
  148. setText(tooltips.get(comp));
  149. } else if (e.getID() == MouseEvent.MOUSE_EXITED && comp.
  150. getMousePosition() == null) {
  151. reset();
  152. }
  153. super.processMouseEvent(e, comp);
  154. }
  155. };
  156. UIUtilities.invokeLater(() -> ((JLayer<JComponent>) component).setUI(layerUI));
  157. } else {
  158. component.addMouseListener(this);
  159. }
  160. }
  161. @Override
  162. public void mouseClicked(final MouseEvent e) {
  163. //Not used
  164. }
  165. @Override
  166. public void mousePressed(final MouseEvent e) {
  167. //Not used
  168. }
  169. @Override
  170. public void mouseReleased(final MouseEvent e) {
  171. //Not used
  172. }
  173. @Override
  174. public void mouseEntered(final MouseEvent e) {
  175. if (e.getSource() instanceof JComponent) {
  176. setText(tooltips.get(e.getSource()));
  177. }
  178. }
  179. @Override
  180. public void mouseExited(final MouseEvent e) {
  181. reset();
  182. }
  183. }