PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/edu/uncc/parsets/parsets/Tooltip.java

https://code.google.com/p/parsets/
Java | 85 lines | 43 code | 14 blank | 28 comment | 8 complexity | b55e410d2952f72d218d549b43160783 MD5 | raw file
 1package edu.uncc.parsets.parsets;
 2
 3import java.awt.Color;
 4import java.awt.Font;
 5import java.awt.FontMetrics;
 6import java.awt.Graphics2D;
 7
 8/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 9 * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
10 *                     and others (see Authors.txt for full list)
11 * All rights reserved.
12 * 
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 * 
16 *    * Redistributions of source code must retain the above copyright
17 *      notice, this list of conditions and the following disclaimer.
18 *    * Redistributions in binary form must reproduce the above copyright
19 *      notice, this list of conditions and the following disclaimer in the
20 *      documentation and/or other materials provided with the distribution.
21 *    * Neither the name of UNC Charlotte nor the names of its contributors
22 *      may be used to endorse or promote products derived from this software
23 *      without specific prior written permission.
24 *      
25 * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
26 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
29 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
36
37public class Tooltip {
38
39	private int x;
40	private int y;
41	private String text;
42	
43	public Tooltip(String text, int x, int y) {
44		this.text = text;
45		this.x = x;
46		this.y = y;
47	}
48	
49	public void paint(Graphics2D g, Font tooltipFont, FontMetrics tooltipFontMetrics, int width) {
50		
51		if (text == null)
52			return;
53		
54		g.setColor(new Color(.8f, .8f, .8f, .8f));
55
56		String tok[] = text.split("\n");
57		int maxWidth = 0;
58		for (int i=0; i<tok.length; i++) {
59			if (tooltipFontMetrics.stringWidth(tok[i]) > maxWidth) 
60				maxWidth = tooltipFontMetrics.stringWidth(tok[i]);
61		}	
62		
63		if (x + maxWidth > width) 
64			x -= maxWidth;
65		
66		if (y - 13 < 0) 
67			y += 13;
68
69		g.fillRect(x, y + 10, maxWidth + 5, tooltipFontMetrics.getHeight()*tok.length + 3);
70		
71		g.setFont(tooltipFont);
72		g.setColor(Color.BLACK);
73		if (text.length() > 0) {
74			for (int i = 0; i < tok.length; i++) {
75				g.drawString(tok[i], x + 2, y + tooltipFontMetrics.getHeight()*(i+1) + 8);
76			}
77		}
78	}
79	
80	public void newValues(String newText, int newX, int newY) {
81		text = newText;
82		x = newX;
83		y = newY;
84	}
85}