Crossfire JXClient, Trunk  R20561
GUITextButton.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.button;
23 
28 import java.awt.Color;
29 import java.awt.Dimension;
30 import java.awt.Font;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
33 import java.awt.Graphics2D;
34 import java.awt.Transparency;
35 import java.awt.font.FontRenderContext;
36 import java.awt.geom.RectangularShape;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
39 
48 public class GUITextButton extends AbstractButton implements GUISelectable {
49 
53  private static final long serialVersionUID = 1;
54 
58  @NotNull
59  private final ButtonImages up;
60 
64  @NotNull
65  private final ButtonImages down;
66 
70  @NotNull
71  private final String text;
72 
76  @NotNull
77  private final Font font;
78 
82  @NotNull
83  private final Color color;
84 
88  @NotNull
89  private final Color colorSelected;
90 
94  @NotNull
95  private final Dimension preferredSize;
96 
100  private boolean selected;
101 
117  public GUITextButton(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final ButtonImages up, @NotNull final ButtonImages down, @NotNull final String text, @NotNull final Font font, @NotNull final Color color, @NotNull final Color colorSelected, final boolean autoRepeat, @NotNull final CommandList commandList) {
118  super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT, autoRepeat, commandList);
119  this.colorSelected = colorSelected;
120  final int preferredHeight = up.getHeight();
121  if (preferredHeight != down.getHeight()) {
122  throw new IllegalArgumentException("'up' state height is "+preferredHeight+" but 'down' state height is "+down.getHeight());
123  }
124  this.up = up;
125  this.down = down;
126  this.text = text;
127  this.font = font;
128  this.color = color;
129  preferredSize = GuiUtils.getTextDimension(text, getFontMetrics(font));
130  if (preferredSize.height < preferredHeight) {
131  preferredSize.height = preferredHeight;
132  }
133  preferredSize.width += 12;
134  }
135 
139  @Override
140  public void activeChanged() {
141  setChanged();
142  }
143 
147  @Override
148  public void paintComponent(@NotNull final Graphics g) {
149  super.paintComponent(g);
150  final Graphics2D g2 = (Graphics2D)g;
151  g2.setFont(font);
152  g2.setColor(selected ? colorSelected : color);
153  final int width = getWidth();
154  (isActive() ? down : up).render(g2, width);
155  final FontRenderContext fontRenderContext = g2.getFontRenderContext();
156  final RectangularShape rectangle = font.getStringBounds(text, fontRenderContext);
157  final int x = (int)Math.round((width-rectangle.getWidth())/2);
158  final FontMetrics fontMetrics = g2.getFontMetrics();
159  final int y = (int)Math.round(preferredSize.height-rectangle.getHeight())/2+fontMetrics.getAscent();
160  g2.drawString(text, x, y);
161  }
162 
166  @NotNull
167  @Override
168  protected Dimension getMinimumSizeInt() {
169  return new Dimension(preferredSize);
170  }
171 
175  @Nullable
176  @Override
177  public Dimension getMaximumSize() {
178  return new Dimension(Integer.MAX_VALUE, preferredSize.height);
179  }
180 
184  @Override
185  public void select(final boolean selected) {
186  this.selected = selected;
187  }
188 
189 }
final TooltipManager tooltipManager
The TooltipManager to update.
final GUIElementListener elementListener
The GUIElementListener to notify.
static Dimension getTextDimension(@NotNull final String text, @NotNull final FontMetrics fontMetrics)
Returns the extents of a string when rendered in a given Font on this component.
Definition: GuiUtils.java:51
void setChanged()
Records that the contents have changed and must be repainted.
final Dimension preferredSize
The preferred size of this component.
A set of images to form a button image.
final ButtonImages up
The images comprising the "up" button state.
final Color colorSelected
The text color when selected.
boolean selected
Whether the element is currently selected.
final CommandList commandList
The commands to execute when the button is elected.
Utility class for Gui related functions.
Definition: GuiUtils.java:35
GUITextButton(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final ButtonImages up, @NotNull final ButtonImages down, @NotNull final String text, @NotNull final Font font, @NotNull final Color color, @NotNull final Color colorSelected, final boolean autoRepeat, @NotNull final CommandList commandList)
Creates a new instance.
void select(final boolean selected)
Selects or deselects the element.whether the element should be selected
final boolean autoRepeat
Whether this button should autorepeat.
final ButtonImages down
The images comprising the "down" button state.
static final long serialVersionUID
The serial version UID.
boolean isActive()
Returns whether a GUI element is active.