Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package com.dmdirc.addons.ui_swing.components;
  2. /*
  3. *
  4. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. import com.dmdirc.addons.ui_swing.UIUtilities;
  25. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  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.JPanel;
  34. import javax.swing.text.SimpleAttributeSet;
  35. import javax.swing.text.StyleConstants;
  36. import net.miginfocom.swing.MigLayout;
  37. import org.jdesktop.jxlayer.JXLayer;
  38. import org.jdesktop.jxlayer.plaf.AbstractLayerUI;
  39. import org.jdesktop.jxlayer.plaf.LayerUI;
  40. /**
  41. * Panel to display toolstips of a component.
  42. */
  43. public class ToolTipPanel extends JPanel implements MouseListener {
  44. /**
  45. * A version number for this class. It should be changed whenever the
  46. * class structure is changed (or anything else that would prevent
  47. * serialized objects being unserialized with the new class).
  48. */
  49. private static final long serialVersionUID = -8929794537312606692L;
  50. /** Default tooltip. */
  51. private final String defaultHelp;
  52. /** Tooltip display. */
  53. private TextLabel tooltip;
  54. /** Map of registered components to their tooltips. */
  55. private final Map<JComponent, String> tooltips;
  56. /**
  57. * Instantiates a new tooltip panel.
  58. *
  59. * @param defaultHelp Default help message when idle
  60. */
  61. public ToolTipPanel(final String defaultHelp) {
  62. super(new MigLayout());
  63. this.defaultHelp = defaultHelp;
  64. this.tooltips = new HashMap<JComponent, String>();
  65. setBackground(Color.WHITE);
  66. setBorder(BorderFactory.createEtchedBorder());
  67. tooltip = new TextLabel();
  68. reset();
  69. add(tooltip, "grow, push");
  70. }
  71. /**
  72. * Resets the content of the tooltip.
  73. */
  74. protected void reset() {
  75. tooltip.setText(defaultHelp);
  76. SimpleAttributeSet sas = new SimpleAttributeSet();
  77. StyleConstants.setItalic(sas, true);
  78. tooltip.getDocument().setParagraphAttributes(0, defaultHelp.length(),
  79. sas, true);
  80. }
  81. /**
  82. * Sets the content of the tooltip area to the specified text.
  83. *
  84. * @param text The text to be displayed
  85. */
  86. protected void setText(final String text) {
  87. if (tooltip == null) {
  88. return;
  89. }
  90. tooltip.setText(text);
  91. if (tooltip.getDocument() == null || text == null) {
  92. return;
  93. }
  94. SimpleAttributeSet sas = new SimpleAttributeSet();
  95. StyleConstants.setItalic(sas, false);
  96. tooltip.getDocument().setParagraphAttributes(0, text.length(), sas, true);
  97. }
  98. /**
  99. * Registers a component with this tooltip handler.
  100. *
  101. * @param component Component to register
  102. */
  103. public void registerTooltipHandler(final JComponent component) {
  104. registerTooltipHandler(component, component.getToolTipText());
  105. component.setToolTipText(null);
  106. }
  107. /**
  108. * Registers a component with this tooltip handler.
  109. *
  110. * @param component Component to register
  111. * @param tooltipText Tooltip text for the component
  112. */
  113. @SuppressWarnings("unchecked")
  114. public void registerTooltipHandler(final JComponent component,
  115. final String tooltipText) {
  116. tooltips.put(component, tooltipText);
  117. if (component instanceof JXLayer) {
  118. final LayerUI<JComponent> layerUI = new AbstractLayerUI<JComponent>() {
  119. private static final long serialVersionUID =
  120. -8698248993206174390L;
  121. /** {@inheritDoc} */
  122. @Override
  123. protected void processMouseEvent(MouseEvent e,
  124. JXLayer<? extends JComponent> comp) {
  125. if (e.getID() == MouseEvent.MOUSE_ENTERED) {
  126. setText(tooltips.get(comp));
  127. } else if (e.getID() == MouseEvent.MOUSE_EXITED && comp.
  128. getMousePosition() == null) {
  129. reset();
  130. }
  131. super.processMouseEvent(e, comp);
  132. }
  133. };
  134. UIUtilities.invokeLater(new Runnable() {
  135. /** {@inheritDoc} */
  136. @Override
  137. public void run() {
  138. ((JXLayer<JComponent>) component).setUI(layerUI);
  139. }
  140. });
  141. } else {
  142. component.addMouseListener(this);
  143. }
  144. }
  145. /**
  146. * {@inheritDoc}
  147. *
  148. * @param e Mouse event
  149. */
  150. @Override
  151. public void mouseClicked(final MouseEvent e) {
  152. //Not used
  153. }
  154. /**
  155. * {@inheritDoc}
  156. *
  157. * @param e Mouse event
  158. */
  159. @Override
  160. public void mousePressed(final MouseEvent e) {
  161. //Not used
  162. }
  163. /**
  164. * {@inheritDoc}
  165. *
  166. * @param e Mouse event
  167. */
  168. @Override
  169. public void mouseReleased(final MouseEvent e) {
  170. //Not used
  171. }
  172. /**
  173. * {@inheritDoc}
  174. *
  175. * @param e Mouse event
  176. */
  177. @Override
  178. public void mouseEntered(final MouseEvent e) {
  179. if (e.getSource() instanceof JComponent) {
  180. setText(tooltips.get(e.getSource()));
  181. }
  182. }
  183. /**
  184. * {@inheritDoc}
  185. *
  186. * @param e Mouse event
  187. */
  188. @Override
  189. public void mouseExited(final MouseEvent e) {
  190. reset();
  191. }
  192. }