/*
Eteria IRC Client, an RFC 1459 compliant client program written in Java.
Copyright (C) 2000 Javier Kohen <jkohen at tough.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package ar.com.jkohen.awt;
import java.awt.*;
import ar.com.jkohen.irc.MircMessage;
public class NickInfoPopup extends Canvas
{
private final int H_GAP = 2;
private final int V_GAP = 2;
// Just guessing here.
public static int IMAGE_WIDTH = 160;
public static int IMAGE_HEIGHT = 14;
private String nick;
private boolean show = true;
private Image image;
private Graphics img_gfx;
private Rectangle bounds;
private boolean rendered;
public NickInfoPopup()
{
this.setBackground(Color.white);
this.bounds = new Rectangle(IMAGE_WIDTH, 0);
}
public String getNick()
{
return(nick);
}
public void setNick(String n)
{
this.nick = n;
setVisible(true);
}
private String getInfoString(String nick)
{
String line = "";
int age = NickInfo.getAge(nick);
char sex = NickInfo.nickToChar(nick);
String location = NickInfo.getLocation(nick);
String description = NickInfo.getDescription(nick);
if (description == null)
line = (age > 0 ? String.valueOf(age) : " ") + " " + sex + " " + (location != null ? location : "");
else
line = description;
line = MircMessage.filterMircAttributes(line);
return(line);
}
private Color getInfoColor(String nick)
{
return(NickInfo.nickToColor(nick));
}
public void setVisible(boolean b)
{
this.show = b;
if (show)
{
show(nick);
}
else
{
this.nick = null;
if (img_gfx != null)
{
this.image = null;
this.img_gfx.dispose();
}
}
}
private void show(String n)
{
if (nick == null || nick.equals(""))
return;
// Render the info off-screen.
if (image == null)
{
Font f = new Font(getFont().getName(), Font.PLAIN, getFont().getSize() - 1);
FontMetrics fm = getFontMetrics(f);
IMAGE_HEIGHT = fm.getLeading() + fm.getAscent() + fm.getDescent() + V_GAP * 2;
image = createImage(IMAGE_WIDTH, IMAGE_HEIGHT);
img_gfx = image.getGraphics();
img_gfx.setFont(f);
if (NewGraphics2D.hasRenderingHints)
NewGraphics2D.setRendering(img_gfx);
}
this.nick = n;
this.show = true;
render(img_gfx);
rendered = false;
}
private void render(Graphics g)
{
FontMetrics fm = g.getFontMetrics();
int text_max_height = fm.getLeading() + fm.getAscent() + fm.getDescent();
int text_baseline = fm.getLeading() + fm.getAscent();
bounds.height = text_max_height + V_GAP * 2;
String infos = getInfoString(nick);
Color col1 = getInfoColor(nick);
int rd = col1.getRed();
int gr = col1.getGreen();
int bl = col1.getBlue();
int a = 128;
Color col2 = new Color((rd + a > 255 ? 255 : rd + a), (gr + a > 255 ? 255 : gr + a), (bl + a > 255 ? 255 : bl + a));
// Clear background.
g.setColor(Color.black);
g.fillRect(2, 2, IMAGE_WIDTH - 2, IMAGE_HEIGHT - 2);
g.setColor(col1);
g.fillRect(0, 0, IMAGE_WIDTH - 1, IMAGE_HEIGHT - 1);
g.setColor(col2);
g.fillRect(1, 1, IMAGE_WIDTH - 3, IMAGE_HEIGHT - 3);
// Render the text.
g.setColor(Color.black);
g.drawString(infos, H_GAP, text_baseline + V_GAP);
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
// Copy the info from the off-line buffer.
if (show && image != null)
{
Dimension parent_size = getParent().getSize();
bounds.x = Math.min(bounds.x, parent_size.width - bounds.width);
bounds.x = Math.max(bounds.x, 0);
bounds.y -= bounds.height;
bounds.y = Math.min(bounds.y, parent_size.height - bounds.height);
bounds.y = Math.max(bounds.y, 0);
g.drawImage(image, bounds.x, bounds.y, this);
}
}
public void setFont(Font f)
{
if (img_gfx != null)
img_gfx.setFont(new Font(f.getName(), Font.PLAIN, f.getSize() - 1));
}
public void setLocation(int x, int y)
{
bounds.setLocation(x, y);
}
public void setLocation(Point p)
{
bounds.setLocation(p);
}
public Point getLocation()
{
return(new Point(bounds.x, bounds.y));
}
public Dimension getSize()
{
return new Dimension(IMAGE_WIDTH, IMAGE_HEIGHT);
}
public Dimension getPreferredSize()
{
// We don't want to be drawn by the AWT.
return new Dimension(0, 0);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
}