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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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. /**
  46. * A version number for this class. It should be changed whenever the
  47. * class structure is changed (or anything else that would prevent
  48. * serialized objects being unserialized with the new class).
  49. */
  50. private static final long serialVersionUID = -8929794537312606692L;
  51. /** Default tool tip. */
  52. private final String defaultHelp;
  53. /** Tool tip display. */
  54. private final TextLabel tooltip;
  55. /** Error icon. */
  56. private final JLabel icon;
  57. /** Whether or not this is a warning. */
  58. private String warningText = null;
  59. /** Map of registered components to their tool tips. */
  60. private final Map<JComponent, String> tooltips;
  61. /**
  62. * Instantiates a new tool tip panel.
  63. *
  64. * @param helpText Default help message when idle
  65. */
  66. public ToolTipPanel(final String helpText) {
  67. super(new MigLayout("hidemode 3"));
  68. defaultHelp = helpText;
  69. this.tooltips = new HashMap<JComponent, String>();
  70. this.icon = new JLabel(IconManager.getIconManager().getIcon("warning"));
  71. setBackground(Color.WHITE);
  72. setForeground(Color.BLACK);
  73. setBorder(BorderFactory.createEtchedBorder());
  74. tooltip = new TextLabel();
  75. reset();
  76. add(icon, "aligny top");
  77. add(tooltip, "grow, push");
  78. }
  79. /**
  80. * Resets the content of the tool tip.
  81. */
  82. protected final void reset() {
  83. final SimpleAttributeSet sas = new SimpleAttributeSet();
  84. StyleConstants.setForeground(sas, Color.BLACK);
  85. StyleConstants.setBackground(sas, Color.WHITE);
  86. if (warningText == null || warningText.isEmpty()) {
  87. tooltip.setText(defaultHelp);
  88. icon.setVisible(false);
  89. StyleConstants.setItalic(sas, true);
  90. } else {
  91. icon.setVisible(true);
  92. tooltip.setText(warningText);
  93. }
  94. tooltip.getDocument().setParagraphAttributes(0, tooltip.getDocument().
  95. getLength(), sas, true);
  96. }
  97. /**
  98. * Sets the content of the tool tip area to the specified text.
  99. *
  100. * @param text The text to be displayed
  101. */
  102. protected void setText(final String text) {
  103. if (tooltip == null) {
  104. return;
  105. }
  106. tooltip.setText(text);
  107. if (tooltip.getDocument() == null || text == null) {
  108. return;
  109. }
  110. icon.setVisible(false);
  111. final SimpleAttributeSet sas = new SimpleAttributeSet();
  112. StyleConstants.setItalic(sas, false);
  113. StyleConstants.setForeground(sas, Color.BLACK);
  114. StyleConstants.setBackground(sas, Color.WHITE);
  115. tooltip.getDocument().setParagraphAttributes(0, text.length(), sas,
  116. true);
  117. }
  118. /**
  119. * Sets whether or not this tool tip should be rendered as a warning.
  120. *
  121. * @param warning Warning string, null or empty to reset.
  122. * @since 0.6.3
  123. */
  124. public void setWarning(final String warning) {
  125. warningText = warning;
  126. reset();
  127. }
  128. /**
  129. * Registers a component with this tool tip handler.
  130. *
  131. * @param component Component to register
  132. */
  133. public void registerTooltipHandler(final JComponent component) {
  134. registerTooltipHandler(component, component.getToolTipText());
  135. component.setToolTipText(null);
  136. }
  137. /**
  138. * Registers a component with this tool tip handler.
  139. *
  140. * @param component Component to register
  141. * @param tooltipText Tool tip text for the component
  142. */
  143. @SuppressWarnings("unchecked")
  144. public void registerTooltipHandler(final JComponent component,
  145. final String tooltipText) {
  146. if (component == null) {
  147. return;
  148. }
  149. tooltips.put(component, tooltipText);
  150. if (component instanceof JXLayer<?>) {
  151. final LayerUI<JComponent> layerUI =
  152. new AbstractLayerUI<JComponent>() {
  153. private static final long serialVersionUID =
  154. -8698248993206174390L;
  155. /** {@inheritDoc} */
  156. @Override
  157. protected void processMouseEvent(final MouseEvent e,
  158. final JXLayer<? extends JComponent> comp) {
  159. if (e.getID() == MouseEvent.MOUSE_ENTERED) {
  160. setText(tooltips.get(comp));
  161. } else if (e.getID() == MouseEvent.MOUSE_EXITED && comp.
  162. getMousePosition() == null) {
  163. reset();
  164. }
  165. super.processMouseEvent(e, comp);
  166. }
  167. };
  168. UIUtilities.invokeLater(new Runnable() {
  169. /** {@inheritDoc} */
  170. @Override
  171. public void run() {
  172. ((JXLayer<JComponent>) component).setUI(layerUI);
  173. }
  174. });
  175. } else {
  176. component.addMouseListener(this);
  177. }
  178. }
  179. /**
  180. * {@inheritDoc}
  181. *
  182. * @param e Mouse event
  183. */
  184. @Override
  185. public void mouseClicked(final MouseEvent e) {
  186. //Not used
  187. }
  188. /**
  189. * {@inheritDoc}
  190. *
  191. * @param e Mouse event
  192. */
  193. @Override
  194. public void mousePressed(final MouseEvent e) {
  195. //Not used
  196. }
  197. /**
  198. * {@inheritDoc}
  199. *
  200. * @param e Mouse event
  201. */
  202. @Override
  203. public void mouseReleased(final MouseEvent e) {
  204. //Not used
  205. }
  206. /**
  207. * {@inheritDoc}
  208. *
  209. * @param e Mouse event
  210. */
  211. @Override
  212. public void mouseEntered(final MouseEvent e) {
  213. if (e.getSource() instanceof JComponent) {
  214. setText(tooltips.get((JComponent) e.getSource()));
  215. }
  216. }
  217. /**
  218. * {@inheritDoc}
  219. *
  220. * @param e Mouse event
  221. */
  222. @Override
  223. public void mouseExited(final MouseEvent e) {
  224. reset();
  225. }
  226. }