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.9KB

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