📄 programmereditordemo.java
字号:
style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, Color.red); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); styles.put("error", style); style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Monospaced"); StyleConstants.setFontSize(style, 12); StyleConstants.setBackground(style, Color.white); StyleConstants.setForeground(style, Color.orange); StyleConstants.setBold(style, false); StyleConstants.setItalic(style, false); styles.put("unknown", style); } /** * Just like a DefaultStyledDocument but intercepts inserts and * removes to color them. */ private class HighLightedDocument extends DefaultStyledDocument { public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { synchronized (doclock){ super.insertString(offs, str, a); color(offs, str.length()); documentReader.update(offs, str.length()); } } public void remove(int offs, int len) throws BadLocationException { synchronized (doclock){ super.remove(offs, len); color(offs, -len); documentReader.update(offs, -len); } } }}/** * A wrapper for a position in a document appropriate for storing * in a collection. */class DocPosition { /** * The actual position */ private int position; /** * Get the position represented by this DocPosition * * @return the position */ int getPosition(){ return position; } /** * Construct a DocPosition from the given offset into the document. * * @param position The position this DocObject will represent */ public DocPosition(int position){ this.position = position; } /** * Adjust this position. * This is useful in cases that an amount of text is inserted * or removed before this position. * * @param adjustment amount (either positive or negative) to adjust this position. * @return the DocPosition, adjusted properly. */ public DocPosition adjustPosition(int adjustment){ position += adjustment; return this; } /** * Two DocPositions are equal iff they have the same internal position. * * @return if this DocPosition represents the same position as another. */ public boolean equals(Object obj){ if (obj instanceof DocPosition){ DocPosition d = (DocPosition)(obj); if (this.position == d.position){ return true; } else { return false; } } else { return false; } } /** * A string representation useful for debugging. * * @return A string representing the position. */ public String toString(){ return "" + position; }}/** * A comparator appropriate for use with Collections of * DocPositions. */class DocPositionComparator implements Comparator{ /** * Does this Comparator equal another? * Since all DocPositionComparators are the same, they * are all equal. * * @return true for DocPositionComparators, false otherwise. */ public boolean equals(Object obj){ if (obj instanceof DocPositionComparator){ return true; } else { return false; } } /** * Compare two DocPositions * * @param o1 first DocPosition * @param o2 second DocPosition * @return negative if first < second, 0 if equal, positive if first > second */ public int compare(Object o1, Object o2){ if (o1 instanceof DocPosition && o2 instanceof DocPosition){ DocPosition d1 = (DocPosition)(o1); DocPosition d2 = (DocPosition)(o2); return (d1.getPosition() - d2.getPosition()); } else if (o1 instanceof DocPosition){ return -1; } else if (o2 instanceof DocPosition){ return 1; } else if (o1.hashCode() < o2.hashCode()){ return -1; } else if (o2.hashCode() > o1.hashCode()){ return 1; } else { return 0; } }}/** * A reader interface for an abstract document. Since * the syntax highlighting packages only accept Stings and * Readers, this must be used. * Since the close() method does nothing and a seek() method * has been added, this allows us to get some performance * improvements through reuse. It can be used even after the * lexer explicitly closes it by seeking to the place that * we want to read next, and reseting the lexer. */class DocumentReader extends Reader { /** * Modifying the document while the reader is working is like * pulling the rug out from under the reader. Alerting the * reader with this method (in a nice thread safe way, this * should not be called at the same time as a read) allows * the reader to compensate. */ public void update(int position, int adjustment){ if (position < this.position){ if (this.position < position - adjustment){ this.position = position; } else { this.position += adjustment; } } } /** * Current position in the document. Incremented * whenever a character is read. */ private long position = 0; /** * Saved position used in the mark and reset methods. */ private long mark = -1; /** * The document that we are working with. */ private AbstractDocument document; /** * Construct a reader on the given document. * * @param document the document to be read. */ public DocumentReader(AbstractDocument document){ this.document = document; } /** * Has no effect. This reader can be used even after * it has been closed. */ public void close() { } /** * Save a position for reset. * * @param readAheadLimit ignored. */ public void mark(int readAheadLimit){ mark = position; } /** * This reader support mark and reset. * * @return true */ public boolean markSupported(){ return true; } /** * Read a single character. * * @return the character or -1 if the end of the document has been reached. */ public int read(){ if (position < document.getLength()){ try { char c = document.getText((int)position, 1).charAt(0); position++; return c; } catch (BadLocationException x){ return -1; } } else { return -1; } } /** * Read and fill the buffer. * This method will always fill the buffer unless the end of the document is reached. * * @param cbuf the buffer to fill. * @return the number of characters read or -1 if no more characters are available in the document. */ public int read(char[] cbuf){ return read(cbuf, 0, cbuf.length); } /** * Read and fill the buffer. * This method will always fill the buffer unless the end of the document is reached. * * @param cbuf the buffer to fill. * @param off offset into the buffer to begin the fill. * @param len maximum number of characters to put in the buffer. * @return the number of characters read or -1 if no more characters are available in the document. */ public int read(char[] cbuf, int off, int len){ if (position < document.getLength()){ int length = len; if (position + length >= document.getLength()){ length = document.getLength() - (int)position; } if (off + length >= cbuf.length){ length = cbuf.length - off; } try { String s = document.getText((int)position, length); position += length; for (int i=0; i<length; i++){ cbuf[off+i] = s.charAt(i); } return length; } catch (BadLocationException x){ return -1; } } else { return -1; } } /** * @return true */ public boolean ready() { return true; } /** * Reset this reader to the last mark, or the beginning of the document if a mark has not been set. */ public void reset(){ if (mark == -1){ position = 0; } else { position = mark; } mark = -1; } /** * Skip characters of input. * This method will always skip the maximum number of characters unless * the end of the file is reached. * * @param n number of characters to skip. * @return the actual number of characters skipped. */ public long skip(long n){ if (position + n <= document.getLength()){ position += n; return n; } else { long oldPos = position; position = document.getLength(); return (document.getLength() - oldPos); } } /** * Seek to the given position in the document. * * @param n the offset to which to seek. */ public void seek(long n){ if (n <= document.getLength()){ position = n; } else { position = document.getLength(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -