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.";