 |
Crossfire JXClient, Trunk
|
Go to the documentation of this file.
23 package com.realtime.crossfire.jxclient.gui.textinput;
34 import java.awt.Color;
35 import java.awt.Dimension;
37 import java.awt.Graphics;
38 import java.awt.Graphics2D;
39 import java.awt.Image;
40 import java.awt.Toolkit;
41 import java.awt.datatransfer.Clipboard;
42 import java.awt.datatransfer.DataFlavor;
43 import java.awt.datatransfer.StringSelection;
44 import java.awt.datatransfer.Transferable;
45 import java.awt.datatransfer.UnsupportedFlavorException;
46 import java.awt.event.KeyEvent;
47 import java.awt.event.MouseEvent;
48 import java.awt.font.FontRenderContext;
49 import java.awt.geom.RectangularShape;
50 import java.io.IOException;
51 import org.jetbrains.annotations.NotNull;
52 import org.jetbrains.annotations.Nullable;
109 private final Clipboard
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
116 private final Clipboard
selection = Toolkit.getDefaultToolkit().getSystemSelection();
147 private final StringBuilder
text;
196 protected GUIText(@NotNull
final CommandCallback commandCallback, @Nullable
final CommandHistory commandHistory, @NotNull
final TooltipManager tooltipManager, @NotNull
final GUIElementListener elementListener, @NotNull
final String
name, @NotNull
final NewCharModel newCharModel, @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, @NotNull
final GuiFactory guiFactory) {
207 this.text =
new StringBuilder(
text);
210 throw new IllegalArgumentException(
"active image size differs from inactive image size");
213 if (
name.equals(
"account_character_create")) {
224 this.text.setLength(0);
225 this.text.append(
text);
235 return text.toString();
240 super.paintComponent(g);
242 final Graphics2D g2 = (Graphics2D)g;
244 for (
int x = 0; x < getWidth(); x += image.getWidth(
null)) {
245 g2.drawImage(image, x, 0,
null);
252 final FontRenderContext fontRenderContext = g2.getFontRenderContext();
253 final RectangularShape rectangle =
font.getStringBounds(tmp, fontRenderContext);
254 y = (int)Math.round(getHeight()-rectangle.getMaxY()-rectangle.getMinY())/2;
256 final String tmpPrefix = tmp.substring(0,
cursor-
offset);
257 final String tmpCursor = tmp.substring(0,
cursor-
offset+1);
258 final RectangularShape rectanglePrefix =
font.getStringBounds(tmpPrefix, fontRenderContext);
259 final RectangularShape rectangleCursor =
font.getStringBounds(tmpCursor, fontRenderContext);
260 final int cursorX1 = (int)Math.round(rectanglePrefix.getWidth());
261 final int cursorX2 = (int)Math.round(rectangleCursor.getWidth());
263 g2.fillRect(
margin+cursorX1, 0, cursorX2-cursorX1, getHeight());
267 g2.drawString(tmp,
margin, y);
272 @SuppressWarnings(
"MethodDoesntCallSuperMethod")
279 @SuppressWarnings(
"MethodDoesntCallSuperMethod")
286 @SuppressWarnings(
"MethodDoesntCallSuperMethod")
300 final String tmpText =
text.substring(
offset);
305 final String
template =
"****************************************************************************************************************************************************************";
306 final String hiddenText =
template.substring(0, Math.min(tmpText.length(),
template.length()));
307 return hiddenText+
" ";
312 super.mouseClicked(e);
318 final int b = e.getButton();
320 case MouseEvent.BUTTON1:
325 case MouseEvent.BUTTON2:
328 case MouseEvent.BUTTON3:
344 switch (e.getKeyCode()) {
345 case KeyEvent.VK_ENTER:
347 final String command =
text.toString();
356 case KeyEvent.VK_BACK_SPACE:
365 case KeyEvent.VK_DELETE:
374 case KeyEvent.VK_KP_LEFT:
375 case KeyEvent.VK_LEFT:
383 case KeyEvent.VK_KP_RIGHT:
384 case KeyEvent.VK_RIGHT:
392 case KeyEvent.VK_KP_UP:
399 case KeyEvent.VK_KP_DOWN:
400 case KeyEvent.VK_DOWN:
406 case KeyEvent.VK_HOME:
414 case KeyEvent.VK_END:
449 final char ch = e.getKeyChar();
450 if (ch != KeyEvent.CHAR_UNDEFINED && ch != (
char)127 && ch >=
' ') {
467 if (commandUp !=
null) {
482 setText(commandDown ==
null ?
"" : commandDown);
512 protected abstract void execute(@NotNull
final String command);
532 if (getGraphics() ==
null) {
534 }
else if (this.cursor <
cursor) {
539 final String tmpCursor = tmp.substring(0,
cursor-
offset+1);
543 final int cursorX = dimension.width;
544 if (cursorX < getWidth()) {
555 }
else if (this.cursor >
cursor) {
577 final Transferable
selection =
new StringSelection(
text.toString());
578 if (this.selection !=
null) {
579 this.selection.setContents(
selection,
null);
588 Transferable content =
null;
592 if (content ==
null) {
595 if (content ==
null) {
601 str = (String)content.getTransferData(DataFlavor.stringFlavor);
602 }
catch (
final IOException|UnsupportedFlavorException ignored) {
613 final String
name =
text.toString();
614 @Nullable
final String
text;
615 if (
name.isEmpty()) {
616 text =
"The character name must not be empty.";
617 }
else if (
name.length() > 255) {
618 text =
"The character name is too long.";
Abstract base class for text input fields.
void updatePlayerName(@NotNull String playerName)
Sets the current player name.
void mouseClicked(@NotNull final MouseEvent e)
Will be called when the user has clicked (pressed+released) this element.
final int margin
The left margin in pixels.
final Clipboard clipboard
The clipboard for cut/copy/paste operations.
static final long serialVersionUID
The serial version UID.
abstract void execute()
Executes the actions associated with this GUI element.
GUIText(@NotNull final CommandCallback commandCallback, @Nullable final CommandHistory commandHistory, @NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final NewCharModel newCharModel, @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, @NotNull final GuiFactory guiFactory)
Creates a new instance.
void setChanged()
Records that the contents have changed and must be repainted.
static final int CTRL
The mask for "ctrl".
int offset
The display offset: this many characters are hidden.
void copy()
Performs a "copy" operation to the system clipboard.
void setChangedListener(@Nullable final GUIElementChangedListener changedListener)
Sets the GUIElementChangedListener to be notified.
String up()
Returns the previous command.
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.
A GUIElement that can be set to active or inactive.
void insertString(@NotNull final String str)
Inserts a string at the cursor position.
void setHideInput(final boolean hideInput)
Enables or disables hidden text.
boolean historyNext()
Activates the next command from the command history.
String down()
Returns the next command.
Represents a pressed or released key.
static final int SCROLL_CHARS
The number of characters to scroll left/right when the cursor would move outside the visible area.
boolean isActive()
Returns whether a GUI element is active.
final Color inactiveColor
The color for rendering displayed text when the element is inactive.
void paste()
Performs a "paste" operation from the system clipboard.
Dimension getPreferredSize()
Utility class for Gui related functions.
final CommandHistory commandHistory
The CommandHistory for this text field.
void updateErrorText()
Updates the newCharModel's error text depending on whether text contains a valid name.
boolean hideInput
If set, hide input; else show input.
void addCommand(@NotNull final String command)
Adds a new command.
final Clipboard selection
The system selection for cut/copy/paste operations.
static final int PRIORITY_CHARACTER_NAME
A priority value for setErrorText(int, String).
final Font font
The font for rendering displayed text.
Dimension getMaximumSize()
void setErrorText(final int priority, @Nullable final String text)
Sets an error text.
void activeChanged()
Will be called whenever the active state has changed.
void markInactivePending()
Marks this GUI element as pending inactive.
static final int MASK
The mask for all used modifiers.
General information for creating new characters.
final GuiFactory guiFactory
The global GuiFactory instance.
final Image activeImage
The element's background image when it is active.
final GUIElementListener elementListener
The GUIElementListener to notify.
int cursor
The cursor location.
Dimension getMinimumSize()
boolean keyPressed(@NotNull final KeyEvent2 e)
Invoked when a key has been pressed.
Interface for classes that may handle "pressed" key events.
Factory for creating Gui instances.
final String name
The name of this element.
final Color activeColor
The color for rendering displayed text when the element is active.
final Image inactiveImage
The element's background image when it is inactive.
Interface that defines callback functions needed by commands.
final Dimension preferredSize
The size of this component.
void setText(@NotNull final String text)
Sets the entered text.
final NewCharModel newCharModel
The NewCharModel that is shown.
void paintComponent(@NotNull final Graphics g)
Manages a list of previously entered commands.
void setCursor(final int cursor)
Sets the cursor position.
String getText()
Returns the entered text.
final CommandCallback commandCallback
The CommandCallback to use.
boolean historyPrev()
Activates the previous command from the command history.
final TooltipManager tooltipManager
The TooltipManager to update.
void insertChar(final char ch)
Inserts a character at the cursor position.
final Object syncCursor
Object used to synchronize on access to text, cursor, and offset.
void setActive(final boolean active)
Sets the active state of a GUI element.
String getDisplayText()
Returns the displayed text.
final StringBuilder text
The entered text.
void setInactiveIfPending()
Unsets the active state of this GUI element if it is pending.
Listener for GUIElement related events.