📄 lwtextfield.java
字号:
/** * Tests if the scroll component performs scrolling by changing it location or view. * The method is overrided with the component to point move a content of the text field, * the method always returns <code>true</code>. * @return <code>true</code> if the scroll component organizes scrolling by moving * its view; otherwise <code>false</code>. */ public boolean moveContent () { return true; } public Point getStartSelection() { if (isSelectionStarted()) return (startOff < endOff)?new Point(startLine, startCol) :new Point(endLine, endCol); else return new Point(-1, -1); } public Point getEndSelection() { if (isSelectionStarted()) return (startOff < endOff)?new Point(endLine, endCol) :new Point(startLine, startCol); else return new Point(-1, -1); } /** * Tests if the a text part has been selected. * @return <code>true</code> if the text part has been selected; <code>false</code> otherwise. */ public boolean hasSelection() { return isSelectionStarted() && startOff != cur.getOffset(); } /** * Gets the start and last selection offsets. * @return the start and last selection offsets. The result is represented with * java.awt.Point class where <code>x</code> field corresponds to start selection offset and * <code>y</code> field corresponds to last selection offset. */ public Point getSelectionOffsets() { if (isSelectionStarted()) return (startOff < endOff)?new Point(startOff, endOff) :new Point(endOff, startOff); else return new Point(-1, -1); } public String getSelectedText() { return hasSelection()?getSubString(getTextModel(), getStartSelection(), getEndSelection()):null; } public Color getSelectColor () { return selectColor; } /** * Sets the specified color to render selected text. * @param <code>c</code> the specified color. */ public void setSelectColor (Color c) { if (!c.equals(selectColor)) { selectColor = c; if (hasSelection()) repaint(); } } public /*C#override*/ void setEnabled (boolean b) { stopSelection(); super.setEnabled(b); } /** * Selects the specified by the start and end offsets the text part. * @param <code>startOffset</code> the specified start offset. * @param <code>endOffset</code> the specified end offset. */ public /*C#virtual*/ void select (int startOffset, int endOffset) { if (endOffset <= startOffset || startOffset < 0 || endOffset > cur.getMaxOffset()) throw new IllegalArgumentException(); stopSelection(); if (startOff != startOffset || endOffset != endOff) { startOff = startOffset; Point p1 = PosController.getPointByOffset(startOffset, cur); startLine = p1.x; startCol = p1.y; endOff = endOffset; Point p2 = PosController.getPointByOffset(endOffset, cur); endLine = p2.x; endCol = p2.y; repaint(); } } /** * The method is used to paint the text cursor using the cursor view. * @param <code>g</code> the graphics context to be used for painting. */ protected /*C#virtual*/ void drawCursor(Graphics g) { if (isEditableVal && hasFocus() && cur.getOffset() >= 0) curView.paint(g, cx, cy, curView.getPreferredSize().width, getTextRender().getLineHeight() - 1, this); } protected /*C#override*/ void recalc() { super.recalc(); stopSelection(); if (cur.getOffset() >= 0) { cur.validate(); int row = cur.getCurrentLine(); int col = cur.getCurrentCol (); TextModel text = getTextModel(); if (row >= text.getSize() || (row > 0 && col > text.getLine(row).length())) cur.setOffset(0); } } /** * Handles the specified key event. * @param <code>e</code> the specified key event to be handle. * @return <code>true</code> if the key event has been handled with the method; otherwise * <code>false</code>. */ protected /*C#virtual*/ boolean handleKey(LwKeyEvent e) { int mask = e.getMask(); boolean isControlDown = (mask & KeyEvent.CTRL_MASK ) > 0; boolean isShiftDown = (mask & KeyEvent.SHIFT_MASK) > 0; if (isShiftDown) startSelection(); switch (e.getKeyCode()) { case KeyEvent.VK_DOWN : cur.seekLineTo(PosController.DOWN); break; case KeyEvent.VK_UP : cur.seekLineTo(PosController.UP); break; case KeyEvent.VK_RIGHT : { if (isControlDown) { Point p = findNextWord(getTextModel(), cur.getCurrentLine(), cur.getCurrentCol(), 1); if (p != null) cur.setRowCol(p.x, p.y); } else cur.seek(1); } break; case KeyEvent.VK_LEFT : { if (isControlDown) { Point p = findNextWord(getTextModel(), cur.getCurrentLine(), cur.getCurrentCol(), -1); if (p != null) cur.setRowCol(p.x, p.y); } else cur.seek(-1); } break; case KeyEvent.VK_END : { if (isControlDown) cur.seekLineTo(PosController.DOWN, cur.getPosInfo().getLines() - cur.getCurrentLine() - 1); else cur.seekLineTo(PosController.END); } break; case KeyEvent.VK_HOME : { if (isControlDown) cur.seekLineTo(PosController.UP, cur.getCurrentLine()); else cur.seekLineTo(PosController.BEG); } break; case KeyEvent.VK_PAGE_DOWN : cur.seekLineTo(PosController.DOWN, pageSize()); break; case KeyEvent.VK_PAGE_UP : cur.seekLineTo(PosController.UP, pageSize()); break; case KeyEvent.VK_DELETE : { if (hasSelection()) { if (isShiftDown) LwClipboardMan.manager.put(getSelectedText()); Point p = getSelectionOffsets(); remove(p.x, p.y - p.x); } else remove(cur.getOffset(), 1); } break; case KeyEvent.VK_BACK_SPACE: if (!hasSelection()) remove(cur.getOffset()-1, 1); else { Point p = getSelectionOffsets(); remove(p.x, p.y - p.x); } break; case KeyEvent.VK_INSERT : { if (hasSelection() && isControlDown) { LwClipboardMan.manager.put(getSelectedText()); return true; } else if (isShiftDown) { String s = (String)LwClipboardMan.manager.get(); if (s != null) { if (hasSelection()) { Point p = getSelectionOffsets(); remove(p.x, p.y - p.x); } write(cur.getOffset(), s); } } } break; default: return false; } if (!isShiftDown) stopSelection(); return true; } /** * Returns <code>true</code> if the key event should be handle next, returns * <code>false</code> if the handling process has to be terminated. The method * is called before any other event handler will be executed. * @param <code>e</code> the specified key event. * @return <code>true</code> if the key event should be handled. */ protected /*C#virtual*/ boolean isFiltered(LwKeyEvent e) { int code = e.getKeyCode(); return code == KeyEvent.VK_SHIFT || code == KeyEvent.VK_CONTROL || code == KeyEvent.VK_TAB || code == KeyEvent.VK_ALT || (e.getMask() & KeyEvent.ALT_MASK) > 0; } protected /*C#override*/ LwTextRender makeTextRender(TextModel t) { return new LwAdvTextRender(t); } /** * Removes a part of the text starting from the given position and with the specified size. * @param <code>pos</code> the given position. * @param <code>size</code> the specified removed part size. */ protected /*C#virtual*/ void remove(int pos, int size) { if (isEditableVal) { int max = cur.getMaxOffset(); int pl = getLines(); if (pos >= 0 && (pos + size) <= max) { int old = cur.getOffset(); cur.setOffset(pos); getTextModel().remove(pos, size); if (man != null) { Dimension d = getSOSize(); man.scrollObjResized(d.width, d.height); } if (getLines() != pl || old == pos) repaint(); } } } /** * Inserts the specified text at the given position. * @param <code>pos</code> the given position. * @param <code>s</code> the specified text to be inserted. */ protected /*C#virtual*/ void write(int pos, String s) { if (isEditableVal) { int old = cur.getOffset(); int pl = getLines(); getTextModel().write(s, pos); if (man != null) { Dimension d = getSOSize(); man.scrollObjResized(d.width, d.height); } cur.seek (s.length()); if (getLines() != pl || cur.getOffset() == old) repaint(); } } /** * Gets the page size. * @return a page size. */ protected /*C#virtual*/ int pageSize() { Dimension d = getSize(); Insets ins = getInsets(); d.height -= (ins.top + ins.bottom); LwTextRender render = getTextRender(); int indent = render.getLineIndent(); int height = render.getLineHeight(); return (d.height + indent)/(height + indent) + (((d.height + indent)%(height + indent)>indent)?1:0); } /** * Clear the current selection. */ protected void stopSelection() { if (startLine >= 0) { startLine = -1; repaint(); } } private boolean isSelectionStarted() { return startLine != -1; } private void startSelection() { if (!isSelectionStarted()) { startLine = cur.getCurrentLine(); startCol = cur.getCurrentCol(); startOff = cur.getOffset(); } } /** * Calculates and gets a pixel location for the specified text render and the text * cursor position. * @param <code>render</code> the specified text render. * @param <code>pos</code> the pos controller that defines the text position. * @return a pixel location. */ public static Point getTextLocationAt(LwTextRender render, PosController pos) { if (pos.getOffset() < 0) return null; int cl = pos.getCurrentLine(); return new Point(render.substrWidth(render.getLine(cl), 0, pos.getCurrentCol()), cl * (render.getLineHeight() + render.getLineIndent())); } /** * Calculates and gets a text position for the specified text render and the location. * The result is represented with java.awt.Point class where <code>x</code> field defines * a row and <code>y</code> field defines a column. * @param <code>render</code> the specified text render. * @param <code>x</code> the x coordinate of the location. * @param <code>y</code> the y coordinate of the location. * @return a text position. */ public static Point getTextRowColAt(LwTextRender render, int x, int y) { int size = render.getTextModel().getSize(); if (size == 0) return null; int lineHeight = render.getLineHeight(); int lineIndent = render.getLineIndent(); int lineNumber = (y<0)?0:(y + lineIndent)/(lineHeight + lineIndent) + ((y + lineIndent)%(lineHeight + lineIndent)>lineIndent?1:0) - 1; if (lineNumber >= size) return new Point (size - 1, render.getLine(size - 1).length()); else if (lineNumber < 0) return new Point(); if (x < 0) return new Point(lineNumber, 0); int x1 = 0, x2 = 0; String s = render.getLine(lineNumber); for(int c = 0; c < s.length(); c++) { x1 = x2; x2 = render.substrWidth(s, 0, c + 1); if (x >= x1 && x < x2) return new Point(lineNumber, c); } return new Point (lineNumber, s.length()); } /** * Finds starting from the specified line and column next word in the given text model and returns * the found word location. The result is represented with java.awt.Point object where * <code>x</code> field defines a row text position and <code>y</code> field defines a * column text position. * @param <code>t</code> the specified text model. * @param <code>line</code> the specified starting line. * @param <code>col</code> the specified starting column. * @param <code>d</code> the specified searching direction. If the value is <code>1</code> * than the method looks for a next word location, if the value is * <code>-1</code> than the method looks for a previous word location. * @return a word location. */ public static Point findNextWord(TextModel t, int line, int col, int d) { if (line < 0 || line >= t.getSize()) return null; String ln = t.getLine(line); col += d; if (col < 0 && line > 0) return new Point (line - 1, t.getLine(line - 1).length()); else if (col > ln.length() && line < t.getSize() - 1) return new Point (line + 1, 0); char[] buf = ln.toCharArray(); boolean b = false; int j = col; for (; j >= 0 && j < buf.length; j += d) { if (b) { if (d > 0) { if (Character.isLetter(buf[j])) return new Point (line, j); } else if (!Character.isLetter(buf[j])) return new Point (line, j + 1); } else { if (d > 0) b = !Character.isLetter(buf[j]); else b = Character.isLetter(buf[j]); } } return (d > 0?new Point(line, buf.length):new Point (line, 0)); } /** * Returns a substring of the specified text model, at the given start and last position. * The positions are represented with java.awt.Point object where * <code>x</code> field defines a row and <code>y</code> field defines a * column. * @param <code>t</code> the specified text model. * @param <code>start</code> the specified start position. * @param <code>end</code> the specified last position. * @return a substring. */ public static String getSubString(TextModel t, Point start, Point end) { StringBuffer res = new StringBuffer(); for (int i=start.x; i<end.x + 1; i++) { String ln = t.getLine(i); if (i != start.x) res.append ('\n'); if (i == start.x) ln = ln.substring(start.y); if (i == end.x ) ln = ln.substring(0, end.y - ((start.x == end.x)?start.y:0)); res.append (ln); } return res.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -