package sun.awt.motif;
import java.awt.*;
import java.awt.peer.*;
import java.awt.datatransfer.*;
import java.awt.event.ActionEvent;
import java.awt.event.TextEvent;
import java.awt.im.InputMethodRequests;
public class MTextFieldPeer extends MComponentPeer implements TextFieldPeer {
native void pCreate(MComponentPeer parent);
private boolean firstChangeSkipped;
private static native void initIDs();
static {
initIDs();
}
void create(MComponentPeer parent) {
firstChangeSkipped = false;
pCreate(parent);
}
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()) {
setTargetBackground(SystemColor.text);
}
if (!target.isForegroundSet()) {
target.setForeground(SystemColor.textText);
}
setEditable(txt.isEditable());
super.initialize();
}
public MTextFieldPeer(TextField target) {
super(target);
}
public void setEditable(boolean editable) {
pSetEditable(editable);
setBackground(target.getBackground());
}
public native void pSetEditable(boolean editable);
public native void select(int selStart, int selEnd);
public native int getSelectionStart();
public native int getSelectionEnd();
public native void setText(String l);
public native void insertReplaceText(String l);
public native void preDispose();
public native String getText();
public native void setEchoChar(char c);
public native void setFont(Font f);
public native void setCaretPosition(int pos);
public native int getCaretPosition();
private static final int padding = 16;
public Dimension getMinimumSize() {
FontMetrics fm = getFontMetrics(target.getFont());
return new Dimension(fm.stringWidth(((TextField)target).getText())+20,
fm.getMaxDescent() + fm.getMaxAscent() + padding);
}
public Dimension getPreferredSize(int cols) {
return getMinimumSize(cols);
}
public Dimension getMinimumSize(int cols) {
FontMetrics fm = getFontMetrics(target.getFont());
return new Dimension(fm.charWidth('0') * cols + 20,
fm.getMaxDescent() + fm.getMaxAscent() + padding);
}
public boolean isFocusable() {
return true;
}
public void action(final long when, final int modifiers) {
MToolkit.executeOnEventHandlerThread(target, new Runnable() {
public void run() {
postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,
((TextField)target).getText(), when,
modifiers));
}
});
}
protected void disposeImpl() {
preDispose();
super.disposeImpl();
}
public void valueChanged() {
postEvent(new TextEvent(target, TextEvent.TEXT_VALUE_CHANGED));
}
public void pasteFromClipboard() {
Clipboard clipboard = target.getToolkit().getSystemClipboard();
Transferable content = clipboard.getContents(this);
if (content != null) {
try {
String data = (String)(content.getTransferData(DataFlavor.stringFlavor));
insertReplaceText(data);
} catch (Exception e) {
}
}
}
public final static int BORDER = 2;
public final static int MARGIN = 4;
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);
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);
if ((xs >= MARGIN) && (xs <= w)) {
g.setColor(highlight);
if (xe > w) {
g.fillRect(xs, MARGIN, w - xs, h);
} else if (xs == xe) {
} 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);
}
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 setEchoCharacter(char c) {
setEchoChar(c);
}
public Dimension minimumSize() {
return getMinimumSize();
}
public Dimension minimumSize(int cols) {
return getMinimumSize(cols);
}
public Dimension preferredSize(int cols) {
return getPreferredSize(cols);
}
void pShow(){
super.pShow();
notifyTextComponentChange(true);
}
void pHide(){
notifyTextComponentChange(false);
super.pHide();
}
void pDispose(){
notifyTextComponentChange(false);
super.pDispose();
}
public InputMethodRequests getInputMethodRequests() {
return null;
}
public int getIndexAtPoint(int x, int y) { return -1; }
public Rectangle getCharacterBounds(int i) { return null; }
public long filterEvents(long mask) { return 0; }
}