📄 xeditor.java
字号:
package util2;import java.awt.*;import javax.swing.*;import javax.swing.text.*;import javax.swing.border.*;import javax.swing.undo.*;import java.awt.event.*;import java.io.*;import java.util.*;import javax.swing.event.*;import javax.swing.event.CaretListener;import javax.swing.event.CaretEvent;import util3.*;import util4.*;import util5.*;public class Xeditor extends JEditorPane implements KeyListener,CaretListener, UndoControlListener,MouseListener{ UtilUndoer undo=new UtilUndoer(); UndoHandler undoHandler=new UndoHandler(); //UndoAction undoAction=new UndoAction(); //RedoAction redoAction=new RedoAction(); //static JButton undoBtn=new JButton("undo"),redoBtn=new JButton("redo"); Count cou; boolean ketyEnable=true; CountListener listener; boolean Modified=false; ERCListener ercl; public Xeditor(){ ActionMap m=this.getActionMap(); getDocument().addUndoableEditListener(this.undoHandler); //undoBtn.addActionListener(this.undoAction); //redoBtn.addActionListener(this.redoAction); addKeyListener(this); addCaretListener(this); cou=new Count(); undo.addUndoControlListener(this); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } public void addERCListener(ERCListener ercl){ this.ercl=ercl; } public UtilUndoer getUndoManager(){return this.undo;} public void paste(){ //System.out.print("paste(); "); this.cou.typed(); this.ketyEnable=false; super.paste(); this.cou.countIt(); }//when use read(InputStream , Object) to load document read(..) will create another//Document then you should readd undoable edit listener public void setDocument(Document doc){ super.setDocument(doc); doc.addUndoableEditListener(this.undoHandler); } public void cut(){//System.out.print("cut() "); this.cou.typed(); super.cut(); this.ketyEnable=false; this.cou.countIt(); } public void copy(){//System.out.print("copy() "); super.copy(); this.ketyEnable=false; } public void insert(String str, int pos) { //System.out.print("insert "); this.cou.typed(); Document doc = getDocument(); if (doc != null) { try { doc.insertString(pos, str, null); } catch (BadLocationException e) { //throw new IllegalArgumentException(e.getMessage()); } } this.cou.countIt(); } public int getLineOfOffset(int offset) throws BadLocationException { Document doc = getDocument(); if (offset < 0) { throw new BadLocationException("Can't translate offset to line", -1); } else if (offset > doc.getLength()) { throw new BadLocationException("Can't translate offset to line", doc.getLength()+1); } else { Element map = getDocument().getDefaultRootElement(); return map.getElementIndex(offset); } } public int getLineStartOffset(int line) throws BadLocationException { Element map = getDocument().getDefaultRootElement(); if (line < 0) { throw new BadLocationException("Negative line", -1); } else if (line >= map.getElementCount()) { throw new BadLocationException("No such line", getDocument().getLength()+1); } else { Element lineElem = map.getElement(line); return lineElem.getStartOffset(); } } public void setCaretDot(int dot){ this.setCaretPosition(dot); } public int getCaretDot(){return this.cou.curren;} public void editorChanged(){this.setModified(true);} public int getTabSize() { int size = 8; Document doc = getDocument(); if (doc != null) { Integer i = (Integer) doc.getProperty(PlainDocument.tabSizeAttribute); if (i != null) { size = i.intValue(); } } return size; } public void setTabSize(int size) { Document doc = getDocument(); if (doc != null) { int old = getTabSize(); doc.putProperty(PlainDocument.tabSizeAttribute, new Integer(size)); firePropertyChange("tabSize", old, size); } } public boolean getScrollableTracksViewportWidth(){ boolean b=super.getScrollableTracksViewportWidth(); if(b){//System.out.println("pr"); JViewport vp=((JViewport)(this.getParent())); vp.setViewSize(new Dimension(vp.getWidth()+1500,vp.getHeight())); } return b; } public void keyTyped(KeyEvent e){//System.out.print("keyTyped "); if(this.ketyEnable) this.cou.typed(); } public void keyPressed(KeyEvent e){//System.out.print("keyPressed "); //System.out.print(e.VK_ENTER+" "+e.VK_BACK_SPACE+" "+e.VK_DELETE+" "+e.VK_TAB); this.ketyEnable=true; switch(e.getKeyCode()) { case 10 : {//e.VK_ENTER this.cou.typed(); this.ketyEnable=false; break; } case 9 : {//e.VK_TAB this.cou.typed(); this.ketyEnable=false; break; } case 8 : {//e.VK_BACK_SPACE cou.typed(); this.ketyEnable=false; break; } case 127 : {//e.VK_DELETE this.cou.typed(); this.cou.countIt(); break; } default : return; } } public void keyReleased(KeyEvent e){} public void caretUpdate(CaretEvent e){//System.out.print("Updated "); int dot=e.getDot(); /*boolean sh=false; try{ while(true){ if(this.getText(--dot,1).charAt(0)=='\r') {sh=true; continue;} else {dot++; break;} } } catch(Exception ex){dot++;} if(sh) this.getCaret().setDot(dot);*/ this.cou.countIt(dot); //this.cou.countIt(e.getDot()); } //listener MouseListener public void mouseClicked(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){ if(e.isPopupTrigger()){ this.ercl.dispPopMen(e); //ERCListener } } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void eraseUR(){ undo.discardAllEdits(); //this time should examine the discardAllEdits(); } public void setModified(boolean m){ this.Modified=m; } public boolean getModified(){return this.Modified;} class Count { int startPoint=0,nextTo=0,currenPoint,curren,beforType,endPoint=0; boolean type; void countIt(){this.countIt(curren);} void countIt(int dot){ this.curren=dot; Xeditor edi=Xeditor.this; String s=""; try{int line=edi.getLineOfOffset(dot); s=" "+(line+1)+":"+ (dot-edi.getLineStartOffset(line)+1);} catch(BadLocationException e){e.printStackTrace();} edi.listener.setCountText(s); if(this.type){ Xeditor.this.setModified(true); this.currenPoint=curren; if(nextTo!=this.currenPoint){ //System.out.println("setIt!!!"); Xeditor.this.undo.setSignificant(beforType); this.startPoint=this.beforType; } this.endPoint=this.currenPoint; nextTo=currenPoint+currenPoint-beforType; type=false; } } void typed(){ this.type=true; this.beforType=this.curren; } } class UndoHandler implements UndoableEditListener { public void undoableEditHappened(UndoableEditEvent e){ //System.out.print("undoable "); try{undo.addEdit(e.getEdit());}catch(Exception xe){} //maybe I can remove this try{}catch later. //undoAction.update(); //redoAction.update(); } } public void addCountListener(CountListener listener){ this.listener=listener; } public EditorKit createDefaultEditorKit() { return new PlainEditorKit(); } /*class UndoAction extends AbstractAction { public UndoAction() { super("Undo"); setEnabled(false); } public void actionPerformed(ActionEvent e) { try { undo.undo(); } catch (CannotUndoException ex) { System.out.println("Unable to undo: " + ex); ex.printStackTrace(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -