22 package com.realtime.crossfire.jxclient.gui.textinput;
30 import java.awt.Color;
31 import java.awt.Dimension;
33 import java.awt.Graphics;
34 import java.awt.Graphics2D;
35 import java.awt.Image;
36 import java.awt.Toolkit;
37 import java.awt.Transparency;
38 import java.awt.datatransfer.Clipboard;
39 import java.awt.datatransfer.DataFlavor;
40 import java.awt.datatransfer.Transferable;
41 import java.awt.datatransfer.UnsupportedFlavorException;
42 import java.awt.event.KeyEvent;
43 import java.awt.event.MouseEvent;
44 import java.awt.font.FontRenderContext;
45 import java.awt.geom.RectangularShape;
46 import java.io.IOException;
47 import org.jetbrains.annotations.NotNull;
48 import org.jetbrains.annotations.Nullable;
99 private final Clipboard
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
105 private final Clipboard
selection = Toolkit.getDefaultToolkit().getSystemSelection();
136 private final StringBuilder
text;
183 protected GUIText(@NotNull
final CommandCallback commandCallback, @Nullable
final CommandHistory commandHistory, @NotNull
final TooltipManager tooltipManager, @NotNull
final GUIElementListener elementListener, @NotNull
final String
name, @NotNull
final Image activeImage, @NotNull
final Image inactiveImage, @NotNull
final Font font, @NotNull
final Color inactiveColor, @NotNull
final Color activeColor,
final int margin, @NotNull
final String text) {
184 super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT);
193 this.text =
new StringBuilder(text);
194 preferredSize =
new Dimension(activeImage.getWidth(null), activeImage.getHeight(null));
195 if (!preferredSize.equals(
new Dimension(inactiveImage.getWidth(null), inactiveImage.getHeight(null)))) {
196 throw new IllegalArgumentException(
"active image size differs from inactive image size");
205 public void setText(@NotNull
final String text) {
206 this.text.setLength(0);
207 this.text.append(text);
217 return text.toString();
225 super.paintComponent(g);
227 final Graphics2D g2 = (Graphics2D)g;
228 g2.drawImage(
isActive() ? activeImage : inactiveImage, 0, 0, null);
234 final FontRenderContext fontRenderContext = g2.getFontRenderContext();
235 final RectangularShape rectangle = font.getStringBounds(tmp, fontRenderContext);
236 y = (int)Math.round(getHeight()-rectangle.getMaxY()-rectangle.getMinY())/2;
238 final String tmpPrefix = tmp.substring(0, cursor-offset);
239 final String tmpCursor = tmp.substring(0, cursor-offset+1);
240 final RectangularShape rectanglePrefix = font.getStringBounds(tmpPrefix, fontRenderContext);
241 final RectangularShape rectangleCursor = font.getStringBounds(tmpCursor, fontRenderContext);
242 final int cursorX1 = (int)Math.round(rectanglePrefix.getWidth());
243 final int cursorX2 = (int)Math.round(rectangleCursor.getWidth());
244 g2.setColor(inactiveColor);
245 g2.fillRect(margin+cursorX1, 0, cursorX2-cursorX1, getHeight());
248 g2.setColor(
isActive() ? activeColor : inactiveColor);
249 g2.drawString(tmp, margin, y);
258 return new Dimension(preferredSize);
267 return new Dimension(preferredSize);
276 return new Dimension(preferredSize);
288 final String tmpText = text.substring(offset);
293 final String
template =
"****************************************************************************************************************************************************************";
294 final String hiddenText =
template.substring(0, Math.min(tmpText.length(),
template.length()));
295 return hiddenText+
" ";
303 super.mouseClicked(e);
304 final int b = e.getButton();
306 case MouseEvent.BUTTON1:
311 case MouseEvent.BUTTON2:
314 case MouseEvent.BUTTON3:
337 switch (e.getKeyCode()) {
338 case KeyEvent.VK_ENTER:
340 final String command = text.toString();
343 if (!hideInput && commandHistory != null) {
349 case KeyEvent.VK_BACK_SPACE:
352 text.delete(cursor-1, cursor);
358 case KeyEvent.VK_DELETE:
360 if (cursor < text.length()) {
361 text.delete(cursor, cursor+1);
367 case KeyEvent.VK_KP_LEFT:
368 case KeyEvent.VK_LEFT:
376 case KeyEvent.VK_KP_RIGHT:
377 case KeyEvent.VK_RIGHT:
379 if (cursor < text.length()) {
385 case KeyEvent.VK_KP_UP:
392 case KeyEvent.VK_KP_DOWN:
393 case KeyEvent.VK_DOWN:
399 case KeyEvent.VK_HOME:
407 case KeyEvent.VK_END:
409 if (cursor < text.length()) {
435 final char ch = e.getKeyChar();
436 if (ch != KeyEvent.CHAR_UNDEFINED && ch != (
char)127 && ch >=
' ') {
449 if (commandHistory == null) {
452 final String commandUp = commandHistory.
up();
453 if (commandUp != null) {
464 if (commandHistory == null) {
467 final String commandDown = commandHistory.
down();
468 setText(commandDown == null ?
"" : commandDown);
486 text.insert(cursor, ch);
497 text.insert(cursor, str);
506 protected abstract void execute(@NotNull
final String command);
513 if (this.hideInput != hideInput) {
525 if (getGraphics() == null) {
527 }
else if (this.cursor < cursor) {
532 final String tmpCursor = tmp.substring(0, cursor-offset+1);
537 final int cursorX = dimension.width;
538 if (cursorX < getWidth()) {
542 if (offset+SCROLL_CHARS > cursor) {
549 }
else if (this.cursor > cursor) {
552 while (cursor < offset) {
553 if (offset <= SCROLL_CHARS) {
571 Transferable content = null;
572 if (selection != null) {
573 content = selection.getContents(
this);
575 if (content == null) {
576 content = clipboard.getContents(
this);
578 if (content == null) {
584 str = (String)content.getTransferData(DataFlavor.stringFlavor);
585 }
catch (
final IOException|UnsupportedFlavorException ignored) {
Listener for GUIElement related events.
boolean hideInput
If set, hide input; else show input.
boolean keyReleased(@NotNull final KeyEvent e)
Invoked when a key has been released.the key event for the key whether the key event has been consume...
GUIText(@NotNull final CommandCallback commandCallback, @Nullable final CommandHistory commandHistory, @NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final Image activeImage, @NotNull final Image inactiveImage, @NotNull final Font font, @NotNull final Color inactiveColor, @NotNull final Color activeColor, final int margin, @NotNull final String text)
Creates a new instance.
String up()
Returns the previous command.
int cursor
The cursor location.
final TooltipManager tooltipManager
The TooltipManager to update.
String down()
Returns the next command.
Dimension getPreferredSize()
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.
final CommandHistory commandHistory
The CommandHistory for this text field.
void paintComponent(@NotNull final Graphics g)
final Font font
The font for rendering displayed text.
void setChanged()
Records that the contents have changed and must be repainted.
Dimension getMaximumSize()
int offset
The display offset: this many characters are hidden.
Interface for listeners for keyboard input.
final Clipboard clipboard
The clipboard for cut/copy/paste operations.
final Object syncCursor
Object used to synchronize on access to text, cursor, and offset.
void insertChar(final char ch)
Inserts a character at the cursor position.
void insertString(@NotNull final String str)
Inserts a string at the cursor position.
void setActive(final boolean active)
Sets the active state of a GUI element.
final Dimension preferredSize
The size of this component.
Represents a pressed or released key.
boolean keyPressed(@NotNull final KeyEvent2 e)
Invoked when a key has been pressed.
void updatePlayerName(@NotNull String playerName)
Sets the current player name.
boolean historyNext()
Activates the next command from the command history.
void paste()
Performs a "paste" operation from the system clipboard.
final StringBuilder text
The entered text.
final Color activeColor
The color for rendering displayed text when the element is active.
void setCursor(final int cursor)
Sets the cursor position.
String getText()
Returns the entered text.
final String name
The name of this element.
final Color inactiveColor
The color for rendering displayed text when the element is inactive.
void markInactivePending()
Marks this GUI element as pending inactive.
void mouseClicked(@NotNull final MouseEvent e)
Will be called when the user has clicked (pressed+released) this element.This event will be delivered...
A GUIElement that can be set to active or inactive.
static final int SCROLL_CHARS
The number of characters to scroll left/right when the cursor would move outside of the visible area...
static final long serialVersionUID
The serial version UID.
void setInactiveIfPending()
Unsets the active state of this GUI element if is is pending.
Utility class for Gui related functions.
Interface that defines callback functions needed by commands.
void addCommand(@NotNull final String command)
Adds a new command.
String getDisplayText()
Returns the displayed text.
final Image activeImage
The element's background image when it is active.
final Image inactiveImage
The element's background image when it is inactive.
Dimension getMinimumSize()
static final int CTRL
The mask for "ctrl".
Manages a list of previously entered commands.
final int margin
The left margin in pixels.
void setText(@NotNull final String text)
Sets the entered text.
final CommandCallback commandCallback
The CommandCallback to use.
boolean historyPrev()
Activates the previous command from the command history.
final Clipboard selection
The system selection for cut/copy/paste operations.
Abstract base class for text input fields.
boolean isActive()
Returns whether a GUI element is active.
static final int MASK
The mask for all used modifiers.
void setHideInput(final boolean hideInput)
Enables or disables hidden text.