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