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); }