Crossfire JXClient, Trunk  R20561
GUICheckBox.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.misc;
23 
30 import java.awt.Color;
31 import java.awt.Dimension;
32 import java.awt.Font;
33 import java.awt.Graphics;
34 import java.awt.Graphics2D;
35 import java.awt.Image;
36 import java.awt.Transparency;
37 import java.awt.event.MouseEvent;
38 import java.awt.geom.RectangularShape;
39 import java.awt.image.BufferedImage;
40 import org.jetbrains.annotations.NotNull;
41 
46 public class GUICheckBox extends ActivatableGUIElement {
47 
51  private static final long serialVersionUID = 1;
52 
56  @NotNull
57  private final BufferedImage checkedImage;
58 
62  @NotNull
63  private final Image uncheckedImage;
64 
68  @NotNull
69  private final String text;
70 
74  @NotNull
75  private final Font font;
76 
80  @NotNull
81  private final Color color;
82 
86  @NotNull
87  private final CheckBoxOption option;
88 
92  @NotNull
93  private final OptionListener optionListener = this::setChanged;
94 
107  public GUICheckBox(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final BufferedImage checkedImage, @NotNull final BufferedImage uncheckedImage, @NotNull final Font font, @NotNull final Color color, @NotNull final CheckBoxOption option, @NotNull final String text) {
108  super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT);
109  if (checkedImage.getWidth() != uncheckedImage.getWidth()) {
110  throw new IllegalArgumentException("'checked' width is "+checkedImage.getWidth()+" but 'unchecked' width is "+uncheckedImage.getWidth());
111  }
112 
113  this.checkedImage = checkedImage;
114  this.uncheckedImage = uncheckedImage;
115  this.text = text;
116  this.font = font;
117  this.color = color;
118  this.option = option;
119  this.option.addOptionListener(optionListener);
120  }
121 
125  @Override
126  public void dispose() {
127  super.dispose();
128  option.removeOptionListener(optionListener);
129  }
130 
134  @Override
135  public void paintComponent(@NotNull final Graphics g) {
136  super.paintComponent(g);
137  final Graphics2D g2 = (Graphics2D)g;
138  g2.setFont(font);
139  g2.setColor(color);
140  g2.drawImage(option.isChecked() ? checkedImage : uncheckedImage, 0, 0, null);
141  final RectangularShape rectangle = font.getStringBounds(text, g2.getFontRenderContext());
142  final int y = (int)Math.round(getHeight()-rectangle.getMaxY()-rectangle.getMinY())/2;
143  g2.drawString(text, checkedImage.getWidth()+4, y);
144  }
145 
149  @NotNull
150  @Override
151  public Dimension getPreferredSize() {
152  return getMinimumSizeInt();
153  }
154 
158  @NotNull
159  @Override
160  public Dimension getMinimumSize() {
161  return getMinimumSizeInt();
162  }
163 
168  @NotNull
169  private Dimension getMinimumSizeInt() {
170  final Dimension result = GuiUtils.getTextDimension(text, getFontMetrics(font));
171  result.width += checkedImage.getWidth()+4;
172  result.height = checkedImage.getHeight();
173  return result;
174  }
175 
179  @Override
180  protected void activeChanged() {
181  }
182 
186  @Override
187  public void mouseClicked(@NotNull final MouseEvent e) {
188  super.mouseClicked(e);
189  final int b = e.getButton();
190  switch (b) {
191  case MouseEvent.BUTTON1:
192  option.toggleChecked();
193  break;
194 
195  case MouseEvent.BUTTON2:
196  break;
197 
198  case MouseEvent.BUTTON3:
199  break;
200  }
201  }
202 
206  @Override
207  public void mousePressed(@NotNull final MouseEvent e) {
208  super.mousePressed(e);
209  final int b = e.getButton();
210  switch (b) {
211  case MouseEvent.BUTTON1:
212  setActive(true);
213  break;
214 
215  case MouseEvent.BUTTON2:
216  break;
217 
218  case MouseEvent.BUTTON3:
219  break;
220  }
221  }
222 
226  @Override
227  public void execute() {
228  // ignore
229  }
230 
234  @Override
235  public void mouseEntered(@NotNull final MouseEvent e, final boolean debugGui) {
236  if (!hasTooltipText()) { // XXX: properly initialize tooltip text
237  setTooltipText(option.getTooltipText());
238  }
239  super.mouseEntered(e, debugGui);
240  }
241 
242 }
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 mouseEntered(@NotNull final MouseEvent e, final boolean debugGui)
Will be called when the mouse has entered the bounding box of this element.the mouse event relative t...
GUICheckBox(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final BufferedImage checkedImage, @NotNull final BufferedImage uncheckedImage, @NotNull final Font font, @NotNull final Color color, @NotNull final CheckBoxOption option, @NotNull final String text)
Creates a new instance.
static final long serialVersionUID
The serial version UID.
void mouseClicked(@NotNull final MouseEvent e)
Will be called when the user has clicked (pressed+released) this element.This event will be delivered...
final OptionListener optionListener
The OptionListener attached to option.
String getTooltipText()
Returns the tooltip text to explain this option.
Dimension getMinimumSizeInt()
Returns the minimal size needed to display both icon and text.
void setActive(final boolean active)
Sets the active state of a GUI element.
A GUIElement that can be set to active or inactive.
void removeOptionListener(@NotNull final OptionListener listener)
Removes a listener for state changes.
Definition: Option.java:61
final BufferedImage checkedImage
The image for the checked [x] state.
Utility class for Gui related functions.
Definition: GuiUtils.java:35
void setTooltipText(@Nullable final String tooltipText)
Sets the tooltip text to show when the mouse is inside this element.the text to show ornull to disab...
void dispose()
Releases all allocated resources.
Interface for listeners for attribute changes of Options.
final Image uncheckedImage
The image for the unchecked [ ] state.
void paintComponent(@NotNull final Graphics g)
void mousePressed(@NotNull final MouseEvent e)
Will be called when the user has pressed the mouse inside this element.the mouse event relative to th...
boolean hasTooltipText()
Returns whether the tooltip is enabled.whether the tooltip is enabled
final CheckBoxOption option
The option to display.
void addOptionListener(@NotNull final OptionListener listener)
Adds a listener for state changes.
Definition: Option.java:53