00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.gui.button;
00023
00024 import com.realtime.crossfire.jxclient.gui.commandlist.CommandList;
00025 import com.realtime.crossfire.jxclient.gui.gui.GUIElementListener;
00026 import com.realtime.crossfire.jxclient.gui.gui.GuiUtils;
00027 import com.realtime.crossfire.jxclient.gui.gui.TooltipManager;
00028 import java.awt.Color;
00029 import java.awt.Dimension;
00030 import java.awt.Font;
00031 import java.awt.FontMetrics;
00032 import java.awt.Graphics;
00033 import java.awt.Graphics2D;
00034 import java.awt.Transparency;
00035 import java.awt.font.FontRenderContext;
00036 import java.awt.geom.RectangularShape;
00037 import org.jetbrains.annotations.NotNull;
00038 import org.jetbrains.annotations.Nullable;
00039
00048 public class GUITextButton extends AbstractButton implements GUISelectable {
00049
00053 private static final long serialVersionUID = 1;
00054
00058 @NotNull
00059 private final ButtonImages up;
00060
00064 @NotNull
00065 private final ButtonImages down;
00066
00070 @NotNull
00071 private final String text;
00072
00076 @NotNull
00077 private final Font font;
00078
00082 @NotNull
00083 private final Color color;
00084
00088 @NotNull
00089 private final Color colorSelected;
00090
00094 @NotNull
00095 private final Dimension preferredSize;
00096
00100 private boolean selected;
00101
00117 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) {
00118 super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT, autoRepeat, commandList);
00119 this.colorSelected = colorSelected;
00120 final int preferredHeight = up.getHeight();
00121 if (preferredHeight != down.getHeight()) {
00122 throw new IllegalArgumentException("'up' state height is "+preferredHeight+" but 'down' state height is "+down.getHeight());
00123 }
00124 this.up = up;
00125 this.down = down;
00126 this.text = text;
00127 this.font = font;
00128 this.color = color;
00129 preferredSize = GuiUtils.getTextDimension(text, getFontMetrics(font));
00130 if (preferredSize.height < preferredHeight) {
00131 preferredSize.height = preferredHeight;
00132 }
00133 preferredSize.width += 12;
00134 }
00135
00139 @Override
00140 public void activeChanged() {
00141 setChanged();
00142 }
00143
00147 @Override
00148 public void paintComponent(@NotNull final Graphics g) {
00149 super.paintComponent(g);
00150 final Graphics2D g2 = (Graphics2D)g;
00151 g2.setFont(font);
00152 g2.setColor(selected ? colorSelected : color);
00153 final int width = getWidth();
00154 (isActive() ? down : up).render(g2, width);
00155 final FontRenderContext fontRenderContext = g2.getFontRenderContext();
00156 final RectangularShape rectangle = font.getStringBounds(text, fontRenderContext);
00157 final int x = (int)Math.round((width-rectangle.getWidth())/2);
00158 final FontMetrics fontMetrics = g2.getFontMetrics();
00159 final int y = (int)Math.round(preferredSize.height-rectangle.getHeight())/2+fontMetrics.getAscent();
00160 g2.drawString(text, x, y);
00161 }
00162
00166 @NotNull
00167 @Override
00168 protected Dimension getMinimumSizeInt() {
00169 return new Dimension(preferredSize);
00170 }
00171
00175 @Nullable
00176 @Override
00177 public Dimension getMaximumSize() {
00178 return new Dimension(Integer.MAX_VALUE, preferredSize.height);
00179 }
00180
00184 @Override
00185 public void select(final boolean selected) {
00186 this.selected = selected;
00187 }
00188
00189 }