1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package net.sf.jtreemap.swing;
34
35 import java.awt.Color;
36 import java.awt.Dimension;
37 import java.awt.Font;
38 import java.awt.Graphics;
39
40 import javax.swing.JToolTip;
41
42 /**
43 * Default ToolTip for the jTreeMap.
44 *
45 * @author Laurent DUTHEIL
46 */
47 public class DefaultToolTip extends JToolTip {
48 private static final int TOOLTIP_OFFSET = 5;
49
50 private static final int DEFAULT_VALUE_SIZE = 10;
51
52 private static final int DEFAULT_LABEL_SIZE = 12;
53
54 private static final long serialVersionUID = -2492627777999093973L;
55
56 private JTreeMap jTreeMap;
57
58 private Font labelFont;
59
60 private Font valueFont;
61
62 private String weightPrefix;
63
64 private String valuePrefix;
65
66 private boolean showWeight;
67
68 /**
69 * Constructor.
70 *
71 * @param jTreeMap
72 * the jTreeMap who display the tooltip
73 */
74 public DefaultToolTip(final JTreeMap jTreeMap, final String weightPrefix, final String valuePrefix, final boolean showWeight) {
75 this.jTreeMap = jTreeMap;
76 this.weightPrefix = weightPrefix;
77 this.valuePrefix = valuePrefix;
78 this.showWeight = showWeight;
79 this.labelFont = new Font("Default", Font.BOLD, DEFAULT_LABEL_SIZE);
80 this.valueFont = new Font("Default", Font.PLAIN, DEFAULT_VALUE_SIZE);
81
82 final int width = 160;
83 final int height = getFontMetrics(this.labelFont).getHeight() + getFontMetrics(this.valueFont).getHeight();
84
85 final Dimension size = new Dimension(width, height);
86 this.setSize(size);
87 this.setPreferredSize(size);
88 }
89
90 @Override
91 public void paint(final Graphics g) {
92 if (this.jTreeMap.getActiveLeaf() != null) {
93 Graphics g2D = (Graphics) g;
94 g2D.setColor(Color.YELLOW);
95 g2D.fill3DRect(0, 0, this.getWidth(), this.getHeight(), true);
96 g2D.setColor(Color.black);
97 g2D.setFont(this.labelFont);
98 g2D.drawString(this.jTreeMap.getActiveLeaf().getLabel(), TOOLTIP_OFFSET, g2D.getFontMetrics(this.labelFont).getAscent());
99 g2D.setFont(this.valueFont);
100 String toDraw = this.jTreeMap.getActiveLeaf().getLabelValue();
101 if (valuePrefix != null) {
102 toDraw = valuePrefix + " " + toDraw;
103 }
104 if (showWeight) {
105 toDraw = this.jTreeMap.getActiveLeaf().getWeight() + ", " + toDraw;
106 }
107 if (weightPrefix != null && showWeight) {
108 toDraw = weightPrefix + " " + toDraw;
109 }
110 g2D.drawString(toDraw, TOOLTIP_OFFSET, this.getHeight() - TOOLTIP_OFFSET);
111 }
112 }
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129