📄 lwtextfield.java
字号:
/**
* Caption: Zaval Light-Weight Visual Components Library
* $Revision: 2.79 $
* $Date: 2003/08/22 11:24:16 $
*
* @author: Andrei Vishnevsky
* @version: 3.50
*
* Zaval Light-Weight Visual Components Library (LwVCL) is a pure Java
* alternative to humble AWT-based and SWING-based GUI interfaces for
* wide ranges of platforms, including J2SE, PersonalJava and J2ME.
*
* Designed as light-weight but, alternatively to Swing, built separately
* from AWT (not on top of the java.awt library like Swing), the LwVCL is
* the good alternative to highly performant, memory-efficient, flexible
* GUI solution for embedded, stand-alone and applet applications.
*
* For more info on this product read Zaval Light-Weight Visual Components Library Tutorial
* (It comes within this package).
* The latest product version is always available from the product's homepage:
* http://www.zaval.org/products/lwvcl/
* and from the SourceForge:
* http://sourceforge.net/projects/zaval0003/
*
* Contacts:
* Support : support@zaval.org
* Change Requests : change-request@zaval.org
* Feedback : feedback@zaval.org
* Other : info@zaval.org
*
* Copyright (C) 2001-2003 Zaval Creative Engineering Group (http://www.zaval.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* (version 2) as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package org.zaval.lw;import java.awt.*;import java.awt.event.*;import org.zaval.lw.event.*;import org.zaval.misc.*;import org.zaval.misc.event.*;import org.zaval.data.*;/** * This class is a text field component. The library provides several text renders implementations * that can be used as a face view of the component: * <ul> * <li> * LwAdvTextRender. This text render is used as default text render. * </li> * <li>LwTextRender. This text render is base for LwAdvTextRender render.</li> * <li> * LwPasswordText. This text render is used for password text fields. * The sample below shows the render usage: * <pre> * ... * LwTextFiled tf = new LwTextFiled("Password text"); * tf.getViewMan(true).setView(new LwPasswordText(tf.getText())); * ... * </pre> * </li> * </ul> * <p> * The list below describes some features of the component: * <ul> * <li> * Use following code to organize single line text field component with a fixed number * of characters * <pre> * ... * // Creates single line text field component, where it is possible to * // enter at most 10 characters. * LwTextFiled tf = new LwTextFiled("", 10); * ... * </pre> * </li> * <li> * Use <code>setPSByRowsCols</code> method to define preferred size of the component * basing on the number of rows and columns. The method calculates and sets preferred size of * the text field component according to row size and column size. Draw attention that * if the font has been redefined for the text render, than it is necessary to call * the method again to recalculate his preferred size. * </li> * <li> * It is possible to redefined text cursor view by <code>setCursorView</code> method. * </li> * <li> * It is possible to define another pos controller by <code>setPosController</code> * method to control text cursor position. * </li> * <li> * Use <code>setEditable</code> method to define if a text can be edited or not. * </li> * <li> * Use <code>getSelectedText</code> method to get selected text. * </li> * </ul> * <p> * The component overrides the <code>getOrigin</code> method to organize scrolling of the * content. The scrolling mechanism works right if the component will be inserted * into a LwScrollPan component. * <p> * To listen when the cursor position has been changed use the pos controller as follow: * <pre> * public class Sample * implements PosListener * { * ... * public void init() * { * ... * LwTextFiled tf = new LwTextFiled("Text"); * tf.getPosController().addPosListener(this); * ... * } * public void posChanged(PosEvent e) { * System.out.println("The old cursor location is - " + e.getPrevOffset()); * } * } * </pre> */public class LwTextFieldextends LwLabelimplements LwKeyListener, LwMouseListener, LwMouseMotionListener, LwFocusListener, Cursorable, PosInfo, PosListener, ScrollObj, TxtSelectionInfo{ private Color selectColor = LwToolkit.darkBlue; private LwView curView; private PosController cur; private ScrollMan man; private int cx = -1, cy = -1, dx = 0, dy = 0; private int startLine = -1, startCol, startOff, endLine, endCol, endOff; private boolean isEditableVal = true; /** * Constructs a text filed component with no text. */ public LwTextField() { this(""); } /** * Constructs a text field component with the specified text. The methods sets LwAdvTextRender * as the face view and sets org.zaval.data.Text as the render target. * @param <code>s</code> the specified text. */ public LwTextField(String s) { this(new Text(s)); } /** * Constructs a single line text field component with the specified text and * the maximal number of columns. * @param <code>s</code> the specified text. * @param <code>maxCol</code> the specified maximal number of columns. */ public LwTextField(String s, int maxCol) { this (new SingleLineTxt(s, maxCol)); if (maxCol > 0) setPSByRowsCols(-1, maxCol); } /** * Constructs a text field component with the specified text model. * @param <code>model</code> the specified text model. */ public LwTextField(TextModel model) { super (model); setPosController(new PosController(this)); setCursorView(LwManager.getView("txt.cur")); getViewMan(true).setBorder(LwManager.getView("br.sunken")); } public /*C#override*/ boolean canHaveFocus() { return true; } /** * Gets the pos controller that manages the text cursor position. * @return a pos controller. */ public PosController getPosController() { return cur; } /** * Sets the specified pos controller to manage the text cursor position. * @param <code>p</code> the specified pos controller. */ public void setPosController(PosController p) { if (cur != p) { if (cur != null) cur.removePosListener(this); cur = p; cur.setPosInfo(this); cur.addPosListener(this); invalidate(); } } /** * Gets the cursor view that is used to paint the text cursor. * @return a cursor view. */ public LwView getCursorView() { return curView; } /** * Sets the specified cursor view to render the text cursor. * @param <code>v</code> the specified cursor view. */ public void setCursorView(LwView v) { if (v != curView) { curView = v; repaint(); } } /** * Sets the preferred size for the text field component that conforms to the * specified number of rows and columns. * @param <code>r</code> the specified number of rows. * @param <code>c</code> the specified number of columns. */ public void setPSByRowsCols(int r, int c) { LwTextRender tr = getTextRender(); int w = (c > 0)? (tr.stringWidth("W") * c):psWidth; int h = (r > 0)? (r * tr.getLineHeight() + (r - 1) * tr.getLineIndent()):psHeight; setPSSize(w, h); } /** * Paints this component. The method initiates painting of the cursor caret by calling * <code>drawCursor</code> method. * @param <code>g</code> the graphics context to be used for painting. */ public /*C#override*/ void paint(Graphics g) { super.paint(g); drawCursor(g); } /** * Sets the specified mode for the text field component. The component is editable if * it is possible to edit a text. * @param <code>b</code> the specified mode. If the mode is <code>true</code> than the component * is editable; otherwise not-editable. */ public void setEditable (boolean b) { if (b != isEditableVal) isEditableVal = b; } /** * Checks if the text field component is editable. * @return <code>true</code> if the text field is editable; <code>false</code> * otherwise. */ public boolean isEditable() { return isEditableVal; } public void keyPressed(LwKeyEvent e) { if (!isFiltered(e)) handleKey(e); } public void keyTyped(LwKeyEvent e) { if ((e.getMask()&InputEvent.CTRL_MASK) == 0) { char ch = (char)e.getKeyChar(); switch (e.getKeyCode()) { case KeyEvent.VK_ENTER : if (getTextModel() instanceof SingleLineTxt) return; else ch = '\n'; break; case KeyEvent.VK_ESCAPE : case KeyEvent.VK_TAB : case KeyEvent.VK_BACK_SPACE : return; } if (hasSelection()) { Point p = getSelectionOffsets(); remove(p.x, p.y - p.x); stopSelection(); } write(cur.getOffset(), String.valueOf(ch)); } } public void focusGained (LwFocusEvent e) { if (cur.getOffset() < 0) cur.setOffset(0); } public void focusLost (LwFocusEvent e) {} public void keyReleased (LwKeyEvent e) {} public void mouseClicked (LwMouseEvent e) {} public void mouseEntered (LwMouseEvent e) {} public void mouseExited (LwMouseEvent e) {} public void startDragged (LwMouseMotionEvent e) { startSelection(); } public void endDragged (LwMouseMotionEvent e) {} public void mouseReleased(LwMouseEvent e) {} public void mouseMoved (LwMouseMotionEvent e) {} public void mouseDragged (LwMouseMotionEvent e) { Point p = getTextRowColAt(getTextRender(), e.getX() - dx, e.getY() - dy); if (p != null) cur.setRowCol(p.x, p.y); } public void mousePressed (LwMouseEvent e) { if (LwToolkit.isActionMask(e.getMask())) { if ((e.getMask() & KeyEvent.SHIFT_MASK) > 0) startSelection(); else stopSelection(); Insets i = getInsets(); Point p = getTextRowColAt(getTextRender(), e.getX() - dx - i.left, e.getY() - dy - i.top); if (p != null) cur.setRowCol(p.x, p.y); } } /** * Gets the cursor type for the specified location. The method returns Cursor.TEXT_CURSOR * cursor type. * @param <code>x</code> the x coordinate. * @param <code>y</code> the y coordinate. * @return a cursor type. */ public int getCursorType (int x, int y) { return Cursor.TEXT_CURSOR; } public int getLineSize(int i) { return getTextModel().getLine(i).length() + 1; } public int getLines() { return getTextModel().getSize(); } public int getMaxOffset() { return getTextModel().getTextLength(); } public /*C#override*/ void setText(String s) { cur.setOffset(0); if (man != null) man.scrollObjMoved(0, 0); else setSOLocation(0, 0); super.setText(s); } public /*C#virtual*/ void posChanged(PosEvent e) { cur.validate(); if (cur.getOffset() >=0) { LwTextRender r = getTextRender(); Point p = getTextLocationAt(r, cur); Insets i = getInsets(); cx = p.x + i.left; cy = p.y + i.top; Point o = LwToolkit.calcOrigin(cx, cy, curView.getPreferredSize().width, r.getLineHeight(), this); if (man != null) man.scrollObjMoved(o.x, o.y); else setSOLocation(o.x, o.y); boolean isSelStarted = isSelectionStarted(); if (e.getPrevLine() >= 0 && !isSelStarted) { int minUpdatedLine = Math.min(e.getPrevLine(), cur.getCurrentLine()); int maxUpdatedLine = Math.max(e.getPrevLine(), cur.getCurrentLine()); int lh = r.getLineHeight(), li = r.getLineIndent(); int y1 = lh * minUpdatedLine + minUpdatedLine * li + i.top + dy; int y2 = lh * maxUpdatedLine + maxUpdatedLine * li + i.top + dy; repaint (i.left, y1, width, y2 - y1 + r.getLineHeight()) ; } else { if (isSelStarted) { endLine = cur.getCurrentLine(); endCol = cur.getCurrentCol(); endOff = cur.getOffset(); } repaint(); } } } /** * Returns an origin of the component. The method is overrided with the component * to offset a content of the text field depending on the cursor position. * @return an origin of the component. */ public /*C#override*/ Point getOrigin() { return new Point(dx, dy); } public /*C#override*/ Dimension getPreferredSize() { Dimension d1 = super.getPreferredSize(); Dimension d2 = curView.getPreferredSize(); d1.width += d2.width; return d1; } public Point getSOLocation () { return getOrigin(); } public void setSOLocation (int x, int y) { if (x != dx || y != dy) { dx = x; dy = y; repaint(); } } public Dimension getSOSize() { return getPreferredSize(); } public void setScrollMan (ScrollMan m) { man = m; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -