📄 textarea.java
字号:
}public void mouseReleased( MouseEvent e) { redirectMouseEvent( e);}void newline() { String s = ""; TextBuffer tb = getCursorLine(); if ( tb.len > tCursor.index ) { int rl = tb.len - tCursor.index; s = tb.getString( tCursor.index, rl); tb.remove( tCursor.index, rl); } TextBuffer tbNew = insertLine( s, tCursor.yindex+1); tbNew.copyLevelFrom( tb); updateScrolls(); setCursorPos( tbNew.getLevel(), tCursor.yindex+1, true, true); repaintRows( tCursor.yindex-1, getVisibleRows() );}void pageDown( boolean extend) { int vr = getVisibleRows(); int newY = Math.min( tCursor.yindex + vr, rows.size() - 1); if ( extend) updateSel( tCursor.index, newY, true); else setCursorPos( tCursor.index, newY, true, true);}void pageUp( boolean extend) { int vr = getVisibleRows(); int newY = Math.max( tCursor.yindex - vr, 0); if ( extend) updateSel( tCursor.index, newY, true); else setCursorPos( tCursor.index, newY, true, true);}public void paint( Graphics g) { repaintRows( g, first, rows.size()-first ); this.kaffePaintBorder( g);}void paintInactiveCursor() { if ( rgr != null) paintInactiveCursor( rgr);}void paintInactiveCursor( Graphics g) { g.setColor( Defaults.TextCursorInactiveClr ); tCursor.blank( g, xOffs, getRowYPos( tCursor.yindex) );}void repaintCursor() { repaintCursor( rgr);}void repaintCursor( Graphics g) { if ( g != null ) { if ( AWTEvent.keyTgt == this) tCursor.paint( g, xOffs, getRowYPos( tCursor.yindex) ); else paintInactiveCursor( g); }}void repaintLine( Graphics g, int row, int startX, TextBuffer tb) { int x0, w; int d = BORDER_WIDTH; if ( g == null ) return; if ( tb == null ) tb = (TextBuffer)rows.elementAt( row); int ss = selXStart( row); int se = selXEnd( row, tb); int y0 = d + (row - first) * rowHeight; if ( ss == se ) { x0 = (startX == 0) ? 0 : tb.getPos( startX) + xOffs; w = this.width - x0; g.setColor( this.bgClr ); g.fillRect( x0, y0, w-d, rowHeight); g.setColor( this.fgClr ); tb.paint( g, xOffs, y0, rowHeight, startX); } else { if ( ss > startX ) { x0 = tb.getPos( startX) + xOffs; w = tb.getWidth( startX, ss); g.setColor( this.bgClr ); g.fillRect( x0, y0, w, rowHeight); g.setColor( this.fgClr ); tb.paint( g, xOffs, y0, rowHeight, startX, ss-startX); } if ( se > startX ) { x0 = tb.getPos( ss) + xOffs; w = tb.getWidth( ss, se); g.setColor( Defaults.TextAreaSelBgClr ); g.fill3DRect( x0, y0, w, rowHeight, true); g.setColor( Defaults.TextAreaSelTxtClr ); tb.paint( g, xOffs, y0, rowHeight, ss, se-ss); } x0 = tb.getPos( se) + xOffs; w = this.width - x0; g.setColor( this.bgClr ); g.fillRect( x0, y0, w, rowHeight); if ( se < tb.len ) { g.setColor( this.fgClr ); tb.paint( g, xOffs, y0, rowHeight, se); } } if ( tCursor.yindex == row ) repaintCursor( g);}void repaintLine( int row, int startX, TextBuffer tb) { repaintLine( rgr, row, startX, tb);}void repaintRow( Graphics g, int idx) { repaintLine( g, idx, 0, null);}void replaceRange( String s, int start, int end) { sSel = get2D( start); eSel = get2D( end); if ( (sSel.y > -1) && (eSel.y > -1) ) deleteSel(); else resetSel( false); insert( s, true);}void replaceSelectionWith( String s) { deleteSel(); insert( s, false);}void resetSel( boolean repaint) { boolean se = hasSel(); int y0 = Math.min( sSel.y, eSel.y); int y1 = Math.max( sSel.y, eSel.y); sSel.x = tCursor.index; sSel.y = tCursor.yindex; eSel.x = sSel.x; eSel.y = sSel.y; if ( se && repaint) repaintRows( y0, y1-y0);}int selXEnd( int row, TextBuffer tb) { Point ps = getSelStart(); Point pe = getSelEnd(); if ( row > pe.y ) return -1; if ( row < ps.y ) return -1; if ( row == pe.y ) return pe.x; return tb.len;}int selXStart( int row) { Point ps = getSelStart(); Point pe = getSelEnd(); if ( row > pe.y ) return -1; if ( row < ps.y ) return -1; if ( row == ps.y ) return ps.x; return 0;}synchronized void setContents( String s) { String[] sa = breakLines( s); rows.removeAllElements(); for ( int i=0; i<sa.length; i++) insertLine( sa[i], i); setCursorPos( 0, 0, false, true); updateScrolls(); this.repaint();}void setCursorPos( int x, int y, boolean repaint, boolean resetSel) { TextBuffer tb; int xPos; int lastX = tCursor.index; if ( resetSel ) resetSel( repaint); if ( repaint) { blankCursor(); } makeVisible( y); tb = (TextBuffer)rows.elementAt( y); tCursor.setYIndex( y, getRowYPos( y) ); if ( x > tb.len) x = tb.len; xPos = tb.getPos( x); tCursor.setIndex( x, xPos ); repaintCursor(); if ( resetSel) resetSel( false); if ( this.width > 0) { int dx = 10; if ( (x > lastX) && (xPos - xOffs > this.width -dx) ){ xOffs = this.width - xPos - dx; if (hScroll != null) { if ( updateHScroll() ) rearrange(); hScroll.setValue( -xOffs); } this.repaint(); } else if ( xPos + xOffs < xOffsInit ) { xOffs = -xPos + xOffsInit; if (hScroll != null) { if ( updateHScroll() ) rearrange(); hScroll.setValue( -xOffs); } this.repaint(); } }}public void setFont( Font f) { TextBuffer tb; int s = rows.size(); super.setFont( f); fm = this.getFontMetrics( f); if ( rgr != null ) rgr.setFont( f); tabWidth = 3*fm.charWidth( 'x'); rowHeight = fm.getHeight() + 2; for ( int i=0; i<s; i++) { tb = (TextBuffer)rows.elementAt( i); tb.setMetrics( fm, tabWidth); } tb = (TextBuffer)rows.elementAt( tCursor.yindex); tCursor.setHeight( rowHeight-1 ); tCursor.setYIndex( tCursor.yindex, getRowYPos( tCursor.yindex) ); tCursor.setIndex( tCursor.index, tb.getPos( tCursor.index) ); if ( this.isShowing() ) this.repaint();}boolean updateSel( int x, int y, boolean repaint) { int y0, y1; if ( (x == eSel.x) && (y == eSel.y) ) return false; eSel.x = x; eSel.y = y; y0 = Math.min( sSel.y, eSel.y ); y1 = Math.max( sSel.y, eSel.y ); setCursorPos( x, y, false, false); if ( repaint) { repaintRows( y0, y1 - y0 + 1); } return true;}void vPosChange( int dy) { tCursor.setYIndex( tCursor.yindex, getRowYPos( tCursor.yindex) );}}public TextArea() { this( null, 10, 20, SCROLLBARS_BOTH);}public TextArea( String text) { this( text, 10, 20, SCROLLBARS_BOTH);}public TextArea( String text, int rows, int cols) { this( text, rows, cols, SCROLLBARS_BOTH);}public TextArea( String text, int rows, int cols, int scrolls) { crows = rows; ccols = cols; setLayout( null); setFont( Defaults.TextAreaFont); if (scrolls == SCROLLBARS_VERTICAL_ONLY || scrolls == SCROLLBARS_BOTH) { tp.vScroll = new Scrollbar( Scrollbar.VERTICAL,0,0,0,0); add( tp.vScroll); } if (scrolls == SCROLLBARS_HORIZONTAL_ONLY || scrolls == SCROLLBARS_BOTH) { tp.hScroll = new Scrollbar( Scrollbar.HORIZONTAL,0,0,0,0);// tp.hScroll.setValues( 0, 5 * tabWidth, 0, 100 * tp.fm.charWidth( 'x')); tp.hScroll.setUnitIncrement( tp.fm.charWidth( 'x')); add( tp.hScroll); } buildMenu(); add( tp); tp.setListeners(); setBackground( Defaults.TextAreaBgClr); setForeground( Defaults.TextAreaTxtClr); if ( text != null ) append( text);}public TextArea( int rows, int cols) { this( null, rows, cols, SCROLLBARS_BOTH);}public void add( PopupMenu m) { tp.add( m);}public void append( String str) { tp.append( str);}/** * @deprecated as of JDK 1.1, replaced by append(String s) */public void appendText(String s) { append(s);}protected void buildMenu() { PopupMenu p = new PopupMenu(); p.add( new MenuItem("Cut")).setShortcut( new MenuShortcut( KeyEvent.VK_U, false) ); p.add( new MenuItem("Copy")).setShortcut( new MenuShortcut( KeyEvent.VK_O, false) ); p.add( new MenuItem("Paste")).setShortcut( new MenuShortcut( KeyEvent.VK_A, false) ); p.addSeparator(); p.add( new MenuItem("Select All")).setShortcut( new MenuShortcut( KeyEvent.VK_S, false) ); p.addActionListener( this); tp.add( p);}public void doLayout() { tp.innerLayout();}public Color getBackground () { // some anomaly, we forward colors to our tp, so we should return its colors // for consistencies sake return tp.getBackground();}public int getCaretPosition() { Point p = new Point( tp.tCursor.index, tp.tCursor.yindex); return tp.get1D( p);}public int getColumns() { return ccols;}public Color getForeground () { // some anomaly, we forward colors to our tp, so we should return its colors // for consistencies sake return tp.getForeground();}public Dimension getMinimumSize() { return (minimumSize());}public Dimension getMinimumSize( int rows, int cols) { return (minimumSize(rows, cols));}public Dimension getPreferredSize() { return preferredSize();}public Dimension getPreferredSize( int rows, int cols) { return preferredSize(rows, cols);}public int getRows() { return crows;}public int getScrollbarVisibility() { if ( (tp.hScroll == null) && (tp.vScroll == null) ) return SCROLLBARS_NONE; if ( tp.hScroll == null ) return SCROLLBARS_VERTICAL_ONLY; if ( tp.vScroll == null ) return SCROLLBARS_HORIZONTAL_ONLY; return SCROLLBARS_BOTH;}public String getSelectedText() { StringBuffer sb = new StringBuffer(); int y0 = Math.min( tp.sSel.y, tp.eSel.y); int yMax = Math.max( tp.sSel.y, tp.eSel.y); for ( int i=y0; i<=yMax; i++) { TextBuffer tb = (TextBuffer) tp.rows.elementAt( i); int x0 = tp.selXStart( i); int x1 = tp.selXEnd( i, tb); sb.append( tb.buf, x0, x1-x0 ); if ( i < yMax ) sb.append( " "); } return sb.toString();}public int getSelectionEnd() { int i1 = tp.get1D( tp.sSel); int i2 = tp.get1D( tp.eSel); return Math.max( i1, i2);}public int getSelectionStart() { int i1 = tp.get1D( tp.sSel); int i2 = tp.get1D( tp.eSel); return Math.min( i1, i2);}public String getText() { int i, imax = tp.rows.size()-1; StringBuffer sb = new StringBuffer( (imax+1) * 80); for ( i = 0; i <= imax; ) { TextBuffer tb = (TextBuffer) tp.rows.elementAt(i); sb.append( tb.buf, 0, tb.len); if ( i++ < imax ) sb.append( '\n'); } return sb.toString();}void hPosChange() { tp.hPosChange();}public synchronized void insert( String str, int pos) { tp.insert( str, pos);}/** * @deprecated */public Dimension minimumSize() { return (minimumSize(crows, ccols));}/** * @deprecated */public Dimension minimumSize( int rows, int cols) { return new Dimension( cols*tp.fm.charWidth( 'x'), rows*tp.fm.getHeight() );}public void paint ( Graphics g ) { // we know about our childs, we don't have to blank the background, // so let's speed up things a little g.paintChild( tp, false); if ( (tp.hScroll != null) && ((tp.hScroll.flags & IS_VISIBLE) != 0) ) g.paintChild( tp.hScroll, true); if ( (tp.vScroll != null) && ((tp.vScroll.flags & IS_VISIBLE) != 0) ) g.paintChild( tp.vScroll, true);}protected String paramString() { return super.paramString();}/** * @deprecated */public Dimension preferredSize() { return preferredSize( crows, ccols);}/** * @deprecated */public Dimension preferredSize( int rows, int cols) { return new Dimension( cols*tp.fm.charWidth( 'x'), rows*tp.fm.getHeight() );}public void repaintRow( int row) { tp.repaintLine( row, 0, null);}public synchronized void replaceRange( String str, int start, int end) { replaceText(str, start, end);}void replaceSelectionWith ( String s ) { tp.replaceSelectionWith( s);}/** * @deprecated */public synchronized void replaceText( String str, int start, int end) { tp.replaceRange( str, start, end);}public void requestFocus() { tp.requestFocus();}public void reshape ( int x, int y, int w, int h ) { super.reshape( x, y, w, h); // there is no need for validation of compound IS_NATIVE_LIKES, they are no Containers // in JDK, so we automagically have to re-layout them tp.innerLayout(); flags |= IS_VALID;}public void select( int start, int end) { Point p = tp.get2D( end); tp.sSel = tp.get2D( start); tp.updateSel( p.x, p.y, true);}public void selectAll() { TextBuffer tb = (TextBuffer) tp.rows.lastElement(); tp.sSel.x = 0; tp.sSel.y = 0; tp.eSel.x = 0; tp.eSel.y = 0; tp.updateSel( tb.len, tp.rows.size()-1, true);}public void setBackground( Color clr) { // we don't have a color of our own, forward this to tp tp.setBackground( clr);}public void setCaretPosition( int pos) { Point p = tp.get2D( pos); tp.setCursorPos( p.x, p.y, true, true);}public void setColumns( int cols) { ccols = cols;}public void setEnabled ( boolean isEnabled ) { super.setEnabled( isEnabled); tp.setEnabled( isEnabled);}public void setFont( Font f) { super.setFont( f); tp.setFont( f);}public void setForeground( Color clr) { // we don't have a color of our own, forward thisto tp tp.setForeground( clr);}public void setRows( int rows) {}public void setSelectionEnd( int end) { Point p = tp.get2D( end); tp.updateSel( p.x, p.y, true);}public void setSelectionStart( int start) { Point p = tp.get2D( start); tp.updateSel( p.x, p.y, true);}public void setText( String text) { tp.setContents( text);}public void update ( Graphics g ) { paint( g); // no background blanking required}void vPosChange( int dy) { tp.vPosChange( dy);}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -