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: DefaultToolTip.java 118 2006-12-12 22:41:48Z ekingulen $
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  
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  *                 ObjectLab is supporing JTreeMap
116  * 
117  * Based in London, we are world leaders in the design and development 
118  * of bespoke applications for the securities financing markets.
119  * 
120  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
121  *           ___  _     _           _   _          _
122  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
123  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
124  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
125  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
126  *                   |__/
127  *
128  *                     www.ObjectLab.co.uk
129  */