Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ToolTipPanel.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2006-2013 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<JComponent, String>();
  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. * @since 0.6.3
  120. */
  121. public void setWarning(final String warning) {
  122. warningText = warning;
  123. reset();
  124. }
  125. /**
  126. * Registers a component with this tool tip handler.
  127. *
  128. * @param component Component to register
  129. */
  130. public void registerTooltipHandler(final JComponent component) {
  131. registerTooltipHandler(component, component.getToolTipText());
  132. component.setToolTipText(null);
  133. }
  134. /**
  135. * Registers a component with this tool tip handler.
  136. *
  137. * @param component Component to register
  138. * @param tooltipText Tool tip text for the component
  139. */
  140. @SuppressWarnings("unchecked")
  141. public void registerTooltipHandler(final JComponent component,
  142. final String tooltipText) {
  143. if (component == null) {
  144. return;
  145. }
  146. tooltips.put(component, tooltipText);
  147. if (component instanceof JXLayer<?>) {
  148. final LayerUI<JComponent> layerUI =
  149. new AbstractLayerUI<JComponent>() {
  150. private static final long serialVersionUID =
  151. -8698248993206174390L;
  152. /** {@inheritDoc} */
  153. @Override
  154. protected void processMouseEvent(final MouseEvent e,
  155. final JXLayer<? extends JComponent> comp) {
  156. if (e.getID() == MouseEvent.MOUSE_ENTERED) {
  157. setText(tooltips.get(comp));
  158. } else if (e.getID() == MouseEvent.MOUSE_EXITED && comp.
  159. getMousePosition() == null) {
  160. reset();
  161. }
  162. super.processMouseEvent(e, comp);
  163. }
  164. };
  165. UIUtilities.invokeLater(new Runnable() {
  166. /** {@inheritDoc} */
  167. @Override
  168. public void run() {
  169. ((JXLayer<JComponent>) component).setUI(layerUI);
  170. }
  171. });
  172. } else {
  173. component.addMouseListener(this);
  174. }
  175. }
  176. /**
  177. * {@inheritDoc}
  178. *
  179. * @param e Mouse event
  180. */
  181. @Override
  182. public void mouseClicked(final MouseEvent e) {
  183. //Not used
  184. }
  185. /**
  186. * {@inheritDoc}
  187. *
  188. * @param e Mouse event
  189. */
  190. @Override
  191. public void mousePressed(final MouseEvent e) {
  192. //Not used
  193. }
  194. /**
  195. * {@inheritDoc}
  196. *
  197. * @param e Mouse event
  198. */
  199. @Override
  200. public void mouseReleased(final MouseEvent e) {
  201. //Not used
  202. }
  203. /**
  204. * {@inheritDoc}
  205. *
  206. * @param e Mouse event
  207. */
  208. @Override
  209. public void mouseEntered(final MouseEvent e) {
  210. if (e.getSource() instanceof JComponent) {
  211. setText(tooltips.get(e.getSource()));
  212. }
  213. }
  214. /**
  215. * {@inheritDoc}
  216. *
  217. * @param e Mouse event
  218. */
  219. @Override
  220. public void mouseExited(final MouseEvent e) {
  221. reset();
  222. }
  223. }