jntextfield.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 439 行
JAVA
439 行
/*
* $Id: JNTextField.java,v 1.1 2004/02/28 09:20:51 epr Exp $
*/
package org.jnode.wt.components;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.awt.peer.TextFieldPeer;
import java.util.ArrayList;
import org.jnode.wt.events.JNodeKeyEvent;
/**
* Creation date: (12/13/03 12:26:18 AM)
* @author: Kishore
*/
public class JNTextField extends JNTextComponent implements TextFieldPeer
{
public static int INSETS_WIDTH = 10;
public static int INSETS_HEIGHT = 3;
protected BufferedImage bi = null;
protected Graphics big = null;
protected int biwidth = -1;
protected int biheight = -1;
/*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*/
protected StringBuffer sb = new StringBuffer();
protected ArrayList charsWidthArray = new ArrayList();
protected boolean currentMode = true;
/* CURSOR ALWAYS SITS 'BEFORE' THE INDEX'ED CHARECTER */
protected int cursorLocationOnScreen = 1;
protected int cursorIndexInArray = 0;
public static final boolean MODE_NORMAL = true;
public static final boolean MODE_INSERT = false;
protected static final int LEFT = 3;
protected static final int RIGHT = 4;
private int visibleTextWidth = -1;
//private int visibleTextHeight = -1;
// This is only temp adjustment to call paint
private int _LCI = 0;
private int _RCI = 0;
/**
* KTextField constructor comment.
*/
public JNTextField() {
this("");
}
/**
* KTextField constructor comment.
*/
public JNTextField(String s) {
this(s, -1);
}
/**
* KTextField constructor comment.
*/
public JNTextField(String s, int columns) {
super();
// Util.printTodo("KTextField: constructor() code for Columns parameter not yet implemented");
// Variable initialize,
// setText( s );
init();
// height is set to -1 as for TextField HEIGHT is always the height of Font.
setSize(70, -1);
addString(s);
}
protected void _add_Char(char c, int index) {
int cwidth = getFontMetrics().charWidth(c);
/* If the index is greater than the size of arr,sb just append */
if (index >= sb.length()) {
sb.append(c);
charsWidthArray.add(new Integer(cwidth));
} else {
// add each char to StringBuffer.
sb.insert(index, c);
// add each char length to charWidthArray.
charsWidthArray.add(index, new Integer(cwidth));
}
_moveCursor(RIGHT);
}
protected void _Backspace_Char(int index) {
if (index < 0) {
cursorIndexInArray = 0;
return;
}
_moveCursor(LEFT);
sb.deleteCharAt(index);
charsWidthArray.remove(index);
}
protected void _Delete_Char(int index) {
if (index < 0) {
cursorIndexInArray = 0;
return;
}
if (index >= sb.length()) {
return;
}
sb.deleteCharAt(index);
charsWidthArray.remove(index);
}
private int _get_LCI(int rci) {
/* calcluate RCI based on visibleTextWidth. */
int twidth = 0;
int charw = 0;
int i = rci;
while (true) {
charw = ((Integer) charsWidthArray.get(i)).intValue();
twidth = twidth + charw;
if (twidth > visibleTextWidth) {
break;
}
i--;
if (i < 0) break; // this condition should never occur.
}
return i;
}
protected String _getVisibleString() {
// _LCI = 0; _RCI = sb.length();
// String r = sb.substring( _LCI, _RCI );
//System.out.println( _LCI + " JNTextFeild _getVisibleString() LCI");
String r = sb.substring(_LCI, sb.length());
return r;
}
protected void _insert_Char(char c, int index) {
// REmove the char first
sb.deleteCharAt(index);
charsWidthArray.remove(index);
// add the char next
sb.insert(index, c);
charsWidthArray.add(index, new Integer(c));
}
private void _moveCursor(int b) {
if (b == RIGHT) {
// add cursor char width to cursor location.
int cw = ((Integer) charsWidthArray.get(cursorIndexInArray)).intValue();
int tcl = cursorLocationOnScreen + cw;
if (tcl > visibleTextWidth) {
_RCI = cursorIndexInArray;
_LCI = _get_LCI(_RCI);
} else {
cursorLocationOnScreen = tcl;
}
// .. //
++cursorIndexInArray;
} else if (b == LEFT) {
if (cursorIndexInArray > 0) //!= 0)
{
// .. //
--cursorIndexInArray;
int cw = ((Integer) charsWidthArray.get(cursorIndexInArray)).intValue();
int tcl = cursorLocationOnScreen - cw;
if (tcl < 0) {
_LCI = cursorIndexInArray;
// _RCI = _get_RCI( _LCI );
} else {
// else no change for _LCI
// add cursor char width to cursor location.
cursorLocationOnScreen = tcl;
}
}
}
}
protected void _processkeytyped(JNodeKeyEvent e) {
/* STEP :1 FIRST OF ALLLL, CHECK WHETHER MODE IS CHANGED */
if (e.getKeyCode() == KeyEvent.VK_INSERT) {
currentMode = !currentMode;
return; // END.
}
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
_Backspace_Char(cursorIndexInArray - 1);
} else if (e.getKeyCode() == KeyEvent.VK_DELETE) {
_Delete_Char(cursorIndexInArray);
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
_moveCursor(LEFT);
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
_moveCursor(RIGHT);
} else {
char c = e.getKeyChar();
/* STEP :2 find whether the typed key is a char or integer. */
if (Character.isLetterOrDigit(c) || Character.isWhitespace(c)) {
if (currentMode == MODE_NORMAL) {
_add_Char(c, cursorIndexInArray);
} else if (currentMode == MODE_INSERT) {
_insert_Char(c, cursorIndexInArray);
}
}
}
repaint();
}
protected void addString(String s) {
for (int i = 0; i < s.length(); i++) {
_add_Char(s.charAt(i), cursorIndexInArray);
}
repaint();
}
protected void createBufferedImage(int w, int h) {
//if((this.getWidth() == w) &&(this.getHeight() == height) ) return;
/* Create bufferedImage of this size */
bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
big = bi.getGraphics();
big.setColor(this.getBackground()); // white.
big.fillRect(0, 0, w, h);
/* Draw BorderLines. */
/* // white, right and bottom.
big.drawLine(w,0, w,h);
big.drawLine(0,h, w,h);*/
// darkgray top and left
big.setColor(Color.darkGray);
big.drawLine(0, 0, w - 2, 0);
big.drawLine(0, 0, 0, h - 2);
// lightgray right-1 and bottom-1
big.setColor(Color.lightGray);
big.drawLine(w - 2, 1, w - 2, h - 2);
big.drawLine(1, h - 2, w - 2, h - 2);
// black top-1 and left-1
big.setColor(this.getForeground()); // black.
big.drawLine(1, 1, w - 3, 1);
big.drawLine(1, 1, 1, h - 3);
}
private void init() {
sb = new StringBuffer("");
charsWidthArray = new ArrayList(5); // This is only initializing
this.setBackground(Color.white);
this.setForeground(Color.black);
}
public void internallyPaint(Graphics g) {
/* and finally call repaint(). */
// add this char to BufferedImage.
big.setColor(this.getBackground());
// big.fillRect(0,0,this.getWidth(), this.getHeight());
big.fillRect(2, 2, this.getWidth() - 4, this.getHeight() - 4);
big.setColor(this.getForeground());
big.drawString(_getVisibleString(), 2, this.getHeight() - 6);
// draw Cursor.
big.setColor(Color.red);
int xc = cursorLocationOnScreen + 1;
big.drawLine(xc, 3, xc, this.getHeight() - 4);
g.drawImage(bi, 0, 0, null);
}
public void processKeyEvent(JNodeKeyEvent event) {
// if(event.getID() == JNodeKeyEvent.KEY_TYPED)
if (event.getID() == JNodeKeyEvent.KEY_PRESSED) {
// System.out.println("VVVVVV key pressed "+ event.getID());
_processkeytyped(event);
} else if (event.getID() == JNodeKeyEvent.KEY_RELEASED) {
// System.out.println("^^^^^^ key released "+ event.getID());
} else if (event.getID() == JNodeKeyEvent.KEY_TYPED) {
// System.out.println("++++++ key typed "+ event.getID());
// _keytyped( event );
}
//event.consume();
}
public void setSize(int w, int h) {
int wid = w;
// For TextFeild height is always the height of STring.
int string_height = getFontMetrics().getHeight();
//- int textfeild_height = string_height + (this.getInsets(). * 2);
int textfeild_height = string_height + (INSETS_HEIGHT * 2);
int hei = textfeild_height;
// visibleTextWidth = w - (INSETS_WIDTH * 2);
visibleTextWidth = w - 4;
/* Create bufferedImage of this size */
createBufferedImage(wid, hei);
super.setSize(wid, hei);
}
public void setText(String str) {
//System.out.println("TODO::JNTextFeild setText not implemented "+ str);
addString(str);
}
public int getCaretPosition() {
//TODO: implement it
return 0;
}
public Dimension getMinimumSize(int len) {
//TODO: implement it
return null;
}
public Dimension getPreferredSize(int len) {
//TODO: implement it
return null;
}
public int getSelectionEnd() {
//TODO: implement it
return 0;
}
public int getSelectionStart() {
//TODO: implement it
return 0;
}
public String getText() {
//TODO: implement it
return null;
}
public Dimension minimumSize(int len) {
//TODO: implement it
return null;
}
public Dimension preferredSize(int len) {
//TODO: implement it
return null;
}
public void select(int start_pos, int end_pos) {
//TODO: implement it
}
public void setCaretPosition(int pos) {
//TODO: implement it
}
public void setEchoChar(char echo_char) {
//TODO: implement it
}
public void setEchoCharacter(char echo_char) {
//TODO: implement it
}
public void setEditable(boolean editable) {
//TODO: implement it
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?