XTextFieldPeer(TextField target) { super(target); text = target.getText(); xtext = new XAWTTextField(text, this, target.getParent()); xtext.getDocument().addDocumentListener(xtext); xtext.setCursor(target.getCursor()); XToolkit.specialPeerMap.put(xtext, this); initTextField(); setText(target.getText()); if (target.echoCharIsSet()) { setEchoChar(target.getEchoChar()); } else setEchoChar((char) 0); int start = target.getSelectionStart(); int end = target.getSelectionEnd(); // Fix for 5100200 // Restoring Motif behaviour // Since the end position of the selected text can be greater than the length of the text, // so we should set caret to max position of the text setCaretPosition(Math.min(end, text.length())); if (end > start) { // Should be called after setText() and setCaretPosition() select(start, end); } setEditable(target.isEditable()); // After this line we should not change the component's text firstChangeSkipped = true; AWTAccessor.getComponentAccessor().setPeer(xtext, this); }
void initialize() { int start, end; TextField txt = (TextField) target; setText(txt.getText()); if (txt.echoCharIsSet()) { setEchoChar(txt.getEchoChar()); } start = txt.getSelectionStart(); end = txt.getSelectionEnd(); if (end > start) { select(start, end); } else { setCaretPosition(start); } if (!target.isBackgroundSet()) { // This is a way to set the background color of the TextArea // without calling setBackground - go through native C code setTargetBackground(SystemColor.text); } if (!target.isForegroundSet()) { target.setForeground(SystemColor.textText); } setEditable(txt.isEditable()); // oldSelectionStart = -1; // accessibility support // oldSelectionEnd = -1; // accessibility support super.initialize(); }
int pos2x(int pos) { TextField txt = (TextField) target; FontMetrics fm = getFontMetrics(txt.getFont()); int x = MARGIN, widths[] = fm.getWidths(); String text = txt.getText(); char echoChar = txt.getEchoChar(); if (echoChar == 0) { for (int i = 0; i < pos; i++) { x += widths[text.charAt(i)]; } } else { x += widths[echoChar] * pos; } return x; }
public void print(Graphics g) { TextField txt = (TextField) target; Dimension d = txt.size(); int w = d.width - (2 * BORDER); int h = d.height - (2 * BORDER); Color bg = txt.getBackground(); Color fg = txt.getForeground(); Color highlight = bg.brighter(); String text = txt.getText(); int moved = 0; int selStart = 0; int selEnd = 0; g.setFont(txt.getFont()); g.setColor(txt.isEditable() ? highlight : bg); g.fillRect(BORDER, BORDER, w, h); g.setColor(bg); // g.drawRect(0, 0, d.width-1, d.height-1); draw3DRect(g, bg, 1, 1, d.width - 3, d.height - 3, false); if (text != null) { g.clipRect(BORDER, MARGIN, w, d.height - (2 * MARGIN)); FontMetrics fm = g.getFontMetrics(); w = d.width - BORDER; h = d.height - (2 * MARGIN); int xs = pos2x(selStart) - moved; int xe = pos2x(selEnd) - moved; if ((xs < MARGIN) && (xe > w)) { g.setColor(highlight); g.fillRect(BORDER, MARGIN, w - BORDER, h); } else { g.setColor(bg); // g.fillRect(BORDER, MARGIN, w - BORDER, h); if ((xs >= MARGIN) && (xs <= w)) { g.setColor(highlight); // selected text if (xe > w) { g.fillRect(xs, MARGIN, w - xs, h); } else if (xs == xe) { // g.fillRect(xs, MARGIN, 1, h); } else { g.fillRect(xs, MARGIN, xe - xs, h); } } else if ((xe >= MARGIN) && (xe <= w)) { g.setColor(highlight); g.fillRect(BORDER, MARGIN, xe - BORDER, h); } } g.setColor(fg); int x = MARGIN - moved; char echoChar = txt.getEchoChar(); if (echoChar == 0) { g.drawString(text, x, BORDER + MARGIN + fm.getMaxAscent()); } else { char data[] = new char[text.length()]; for (int i = 0; i < data.length; i++) { data[i] = echoChar; } g.drawChars(data, 0, data.length, x, BORDER + MARGIN + fm.getMaxAscent()); } } target.print(g); }