Swing/SimpleSyntaxHighlight のバックアップの現在との差分(No.9)
- category: swing folder: SimpleSyntaxHighlight title: JTextPaneでキーワードのSyntaxHighlight tags: [JTextPane, StyledDocument, Element, Highlight] author: aterai pubdate: 2009-09-21T02:07:00+09:00 description: JTextPaneでSyntax Highlightを行います。 description: JTextPaneでキーワードのSyntax Highlightを行います。 image:
概要
JTextPane
でSyntax Highlight
を行います。このサンプルは、SyntaxDocument.javaからキーワードのハイライト部分を抜き出して作成しています。
概要
JTextPane
でキーワードのSyntax Highlight
を行います。このサンプルは、Fast styled JTextPane editor | Oracle Communityなどのサンプルコードからキーワードのハイライト部分を参考にして作成しています。
Screenshot
Advertisement
サンプルコード
サンプルコード
#spandel
//This code is taken from:
#spanend
#spandel
//http://www.discoverteenergy.com/Files/SyntaxDocument.java SyntaxDocument.java
#spanend
#spanadd
// This code is taken from: SyntaxDocument.java, MultiSyntaxDocument.java
#spanend
#spanadd
// Fast styled JTextPane editor | Oracle Community
#spanend
#spanadd
// @author camickr
#spanend
#spanadd
// @author David Underhill
#spanend
#spanadd
// https://community.oracle.com/thread/2105230
#spanend
#spanadd
// modified by aterai aterai@outlook.com
#spanend
class SimpleSyntaxDocument extends DefaultStyledDocument {
HashMap<String,AttributeSet> keywords = new HashMap<>();
MutableAttributeSet normal = new SimpleAttributeSet();
private final Style normal;
private static final String OPERANDS = ".,";
public SimpleSyntaxDocument() {
super();
Style def = StyleContext.getDefaultStyleContext().getStyle(
StyleContext.DEFAULT_STYLE);
normal = addStyle("normal", def);
StyleConstants.setForeground(normal, Color.BLACK);
MutableAttributeSet color;
StyleConstants.setForeground(color = new SimpleAttributeSet(), Color.RED);
keywords.put("red", color);
StyleConstants.setForeground(color = new SimpleAttributeSet(), Color.GREEN);
keywords.put("green", color);
StyleConstants.setForeground(color = new SimpleAttributeSet(), Color.BLUE);
keywords.put("blue", color);
StyleConstants.setForeground(addStyle("red", normal), Color.RED);
StyleConstants.setForeground(addStyle("green", normal), Color.GREEN);
StyleConstants.setForeground(addStyle("blue", normal), Color.BLUE);
}
#spanadd
#spanend
@Override public void insertString(int offset, String str, AttributeSet a)
throws BadLocationException {
throws BadLocationException {
super.insertString(offset, str, a);
processChangedLines(offset, str.length());
}
#spanadd
#spanend
@Override public void remove(int offset, int length)
throws BadLocationException {
throws BadLocationException {
super.remove(offset, length);
processChangedLines(offset, 0);
}
#spanadd
#spanend
private void processChangedLines(int offset, int length)
throws BadLocationException {
Element root = getDefaultRootElement();
String content = getText(0, getLength());
int startLine = root.getElementIndex( offset );
int endLine = root.getElementIndex( offset + length );
for (int i = startLine; i <= endLine; i++) {
applyHighlighting(content, i);
throws BadLocationException {
Element root = getDefaultRootElement();
int startLine = root.getElementIndex(offset);
int endLine = root.getElementIndex(offset + length);
for (int line = startLine; line <= endLine; line++) {
applyHighlighting(line);
}
}
private void applyHighlighting(String content, int line)
throws BadLocationException {
#spanadd
#spanend
private void applyHighlighting(int line) throws BadLocationException {
Element root = getDefaultRootElement();
int startOffset = root.getElement( line ).getStartOffset();
int endOffset = root.getElement( line ).getEndOffset() - 1;
int lineLength = endOffset - startOffset;
int contentLength = content.length();
if (endOffset >= contentLength) endOffset = contentLength - 1;
int startOffset = root.getElement(line).getStartOffset();
int endOffset = root.getElement(line).getEndOffset() - 1;
int lineLength = endOffset - startOffset;
int contentLength = getLength();
if (endOffset >= contentLength) {
endOffset = contentLength - 1;
}
setCharacterAttributes(startOffset, lineLength, normal, true);
checkForTokens(content, startOffset, endOffset);
checkForTokens(startOffset, endOffset);
}
private void checkForTokens(String content, int startOffset, int endOffset) {
while (startOffset <= endOffset) {
while (isDelimiter(content.substring(startOffset, startOffset+1))) {
if (startOffset < endOffset) {
startOffset++;
#spanadd
#spanend
private void checkForTokens(int startOffset, int endOffset)
throws BadLocationException {
int index = startOffset;
while (index <= endOffset) {
while (isDelimiter(getText(index, 1))) {
if (index < endOffset) {
index++;
} else {
return;
}
}
startOffset = getOtherToken(content, startOffset, endOffset);
index = getOtherToken(index, endOffset);
}
}
private int getOtherToken(String content, int startOffset, int endOffset) {
#spanadd
#spanend
private int getOtherToken(int startOffset, int endOffset)
throws BadLocationException {
int endOfToken = startOffset + 1;
while ( endOfToken <= endOffset ) {
if ( isDelimiter( content.substring(endOfToken, endOfToken + 1) ) ) {
while (endOfToken <= endOffset) {
if (isDelimiter(getText(endOfToken, 1))) {
break;
}
endOfToken++;
}
String token = content.substring(startOffset, endOfToken);
if ( keywords.containsKey( token ) ) {
setCharacterAttributes(
startOffset, endOfToken - startOffset, keywords.get(token), false);
String token = getText(startOffset, endOfToken - startOffset);
Style s = getStyle(token);
if (s != null) {
setCharacterAttributes(startOffset, endOfToken - startOffset, s, false);
}
return endOfToken + 1;
}
String operands = ".,";
#spanadd
#spanend
protected boolean isDelimiter(String character) {
return Character.isWhitespace(character.charAt(0)) ||
operands.indexOf(character)!=-1;
return Character.isWhitespace(
character.charAt(0)) || OPERANDS.indexOf(character) != -1;
}
}
View in GitHub: Java, Kotlin解説
上記のサンプルでは、red
, green
, blue
といったキーワードを入力すると、その色で文字列をハイライトするようになっています。区切り文字は、空白、.
(ドット)、,
(カンマ)の3
つです。
解説
上記のサンプルでは、red
、green
、blue
といったキーワードを入力するとその色で文字列をハイライトするよう設定しています。
Java
のシンタックスハイライトを行うサンプルコード(SyntaxDocument.java)からキーワードのハイライト部分を抜き出して作成しています。
- 区切り文字は、空白、
.
(ドット)、,
(カンマ)の3
つのみ -
Java
のシンタックスハイライトを行うサンプルコード(参考リンクのSyntaxDocument.java
など)からキーワードのハイライト部分を抜き出して作成
参考リンク
- SyntaxDocument.java
- 作者、ライセンス、参照元のページが何処だったかなどが不明…。
参考リンク
- Fast styled JTextPane editor | Oracle Community
-
オリジナルの作者、ライセンス、参照元のページが何処かなどが不明… - Java Code Example forによると、元はFast styled JTextPane editor | Oracle Communityの投稿で、作者は camickr さん、David Underhill さん。そう言われるとなんとなくそんな記憶があるような…、あのフォーラムはもういろいろ変更されすぎていてどうにも…
-
- Customizing a Text Editor
- Text Editor Tutorial