View Javadoc
1   /*
2    * ObjectLab, http://www.objectlab.co.uk/open is supporting JTreeMap.
3    *
4    * Based in London, we are world leaders in the design and development
5    * of bespoke applications for the securities financing markets.
6    *
7    * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8    *           ___  _     _           _   _          _
9    *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
10   *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
11   *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
12   *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13   *                   |__/
14   *
15   *                     www.ObjectLab.co.uk
16   *
17   * $Id$
18   *
19   * Copyright 2006 the original author or authors.
20   *
21   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22   * use this file except in compliance with the License. You may obtain a copy of
23   * the License at
24   *
25   * http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing, software
28   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30   * License for the specific language governing permissions and limitations under
31   * the License.
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      private static final int DEFAULT_VALUE_SIZE = 10;
50      private static final int DEFAULT_LABEL_SIZE = 12;
51      private static final long serialVersionUID = -2492627777999093973L;
52  
53      private final JTreeMap jTreeMap;
54      private final Font labelFont;
55      private final Font valueFont;
56      private final String weightPrefix;
57      private final String valuePrefix;
58      private final boolean showWeight;
59  
60      /**
61       * Constructor.
62       *
63       * @param jTreeMap
64       *            the jTreeMap who display the tooltip
65       */
66      public DefaultToolTip(final JTreeMap jTreeMap, final String weightPrefix, final String valuePrefix, final boolean showWeight) {
67          this.jTreeMap = jTreeMap;
68          this.weightPrefix = weightPrefix;
69          this.valuePrefix = valuePrefix;
70          this.showWeight = showWeight;
71          this.labelFont = new Font("Default", Font.BOLD, DEFAULT_LABEL_SIZE);
72          this.valueFont = new Font("Default", Font.PLAIN, DEFAULT_VALUE_SIZE);
73  
74          final int width = 160;
75          final int height = getFontMetrics(this.labelFont).getHeight() + getFontMetrics(this.valueFont).getHeight();
76  
77          final Dimension size = new Dimension(width, height);
78          this.setSize(size);
79          this.setPreferredSize(size);
80      }
81  
82      @Override
83      public void paint(final Graphics g) {
84          if (this.jTreeMap.getActiveLeaf() != null) {
85              final Graphics g2D = g;
86              g2D.setColor(Color.YELLOW);
87              g2D.fill3DRect(0, 0, this.getWidth(), this.getHeight(), true);
88              g2D.setColor(Color.black);
89              g2D.setFont(this.labelFont);
90              g2D.drawString(this.jTreeMap.getActiveLeaf().getLabel(), TOOLTIP_OFFSET, g2D.getFontMetrics(this.labelFont).getAscent());
91              g2D.setFont(this.valueFont);
92              String toDraw = this.jTreeMap.getActiveLeaf().getLabelValue();
93              if (valuePrefix != null) {
94                  toDraw = valuePrefix + " " + toDraw;
95              }
96              if (showWeight) {
97                  toDraw = this.jTreeMap.getActiveLeaf().getWeight() + ", " + toDraw;
98              }
99              if (weightPrefix != null && showWeight) {
100                 toDraw = weightPrefix + " " + toDraw;
101             }
102             g2D.drawString(toDraw, TOOLTIP_OFFSET, this.getHeight() - TOOLTIP_OFFSET);
103         }
104     }
105 }
106 /*
107  *                 ObjectLab is supporing JTreeMap
108  *
109  * Based in London, we are world leaders in the design and development
110  * of bespoke applications for the securities financing markets.
111  *
112  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
113  *           ___  _     _           _   _          _
114  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
115  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
116  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
117  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
118  *                   |__/
119  *
120  *                     www.ObjectLab.co.uk
121  */