Swing/SimpleSyntaxHighlight のバックアップの現在との差分(No.11)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/SimpleSyntaxHighlight へ行く。
- 1 (2009-11-03 (火) 04:07:29)
- 2 (2011-07-07 (木) 21:05:25)
- 3 (2011-07-14 (木) 16:02:03)
- 4 (2012-01-01 (日) 14:50:12)
- 5 (2013-01-05 (土) 19:41:48)
- 6 (2013-08-17 (土) 01:15:35)
- 7 (2013-11-05 (火) 19:23:14)
- 8 (2014-03-18 (火) 18:56:39)
- 9 (2014-10-13 (月) 10:43:20)
- 10 (2014-12-22 (月) 15:02:09)
- 11 (2015-01-14 (水) 15:01:22)
- 12 (2015-07-25 (土) 22:54:25)
- 13 (2017-03-28 (火) 14:55:02)
- 14 (2017-03-30 (木) 14:19:58)
- 15 (2018-03-07 (水) 19:10:06)
- 16 (2020-03-17 (火) 18:29:39)
- 17 (2021-09-19 (日) 10:52:20)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- 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を行います。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTTAw3RBeI/AAAAAAAAAjs/LtUs6l9UpZo/s800/SimpleSyntaxHighlight.png --- * 概要 [#ta3c8a38] `JTextPane`でキーワードの`Syntax Highlight`を行います。このサンプルは、[http://www.discoverteenergy.com/Files/SyntaxDocument.java SyntaxDocument.java]からキーワードのハイライト部分を抜き出して作成しています。 * 概要 [#summary] `JTextPane`でキーワードの`Syntax Highlight`を行います。このサンプルは、[https://community.oracle.com/thread/2105230 Fast styled JTextPane editor | Oracle Community]などのサンプルコードからキーワードのハイライト部分を参考にして作成しています。 #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTTAw3RBeI/AAAAAAAAAjs/LtUs6l9UpZo/s800/SimpleSyntaxHighlight.png) * サンプルコード [#p84c741c] * サンプルコード [#sourcecode] #code(link){{ //This code is taken from: //http://www.discoverteenergy.com/Files/SyntaxDocument.java SyntaxDocument.java // This code is taken from: SyntaxDocument.java, MultiSyntaxDocument.java // Fast styled JTextPane editor | Oracle Community // @author camickr // @author David Underhill // https://community.oracle.com/thread/2105230 // modified by aterai aterai@outlook.com class SimpleSyntaxDocument extends DefaultStyledDocument { private final Style normal; //MutableAttributeSet normal = new SimpleAttributeSet(); private final Style normal; private static final String OPERANDS = ".,"; public SimpleSyntaxDocument() { super(); Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE); normal = addStyle("normal", def); StyleConstants.setForeground(normal, Color.BLACK); StyleConstants.setForeground(addStyle("red", normal), Color.RED); StyleConstants.setForeground(addStyle("red", normal), Color.RED); StyleConstants.setForeground(addStyle("green", normal), Color.GREEN); StyleConstants.setForeground(addStyle("blue", normal), Color.BLUE); StyleConstants.setForeground(addStyle("blue", normal), Color.BLUE); } @Override public void insertString(int offset, String str, AttributeSet a) throws BadLocationException { super.insertString(offset, str, a); processChangedLines(offset, str.length()); } @Override public void remove(int offset, int length) throws BadLocationException { super.remove(offset, length); processChangedLines(offset, 0); } 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); for (int line = startLine; line <= endLine; line++) { applyHighlighting(line); } } private void applyHighlighting(String content, int line) throws BadLocationException { 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(); 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) { private void checkForTokens(int startOffset, int endOffset) throws BadLocationException { int index = startOffset; while (index <= endOffset) { while (isDelimiter(content.substring(index, index + 1))) { while (isDelimiter(getText(index, 1))) { if (index < endOffset) { index++; } else { return; } } index = getOtherToken(content, index, endOffset); index = getOtherToken(index, endOffset); } } private int getOtherToken(String content, int startOffset, int endOffset) { private int getOtherToken(int startOffset, int endOffset) throws BadLocationException { int endOfToken = startOffset + 1; while (endOfToken <= endOffset) { if (isDelimiter(content.substring(endOfToken, endOfToken + 1))) { if (isDelimiter(getText(endOfToken, 1))) { break; } endOfToken++; } String token = content.substring(startOffset, endOfToken); String token = getText(startOffset, endOfToken - startOffset); Style s = getStyle(token); if (s != null) { setCharacterAttributes(startOffset, endOfToken - startOffset, s, false); } return endOfToken + 1; } 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; } } }} * 解説 [#t2ef148a] 上記のサンプルでは、`red`, `green`, `blue`といったキーワードを入力すると、その色で文字列をハイライトするようになっています。区切り文字は、空白、`.`(ドット)、`,`(カンマ)の`3`つです。 * 解説 [#explanation] 上記のサンプルでは、`red`、`green`、`blue`といったキーワードを入力するとその色で文字列をハイライトするよう設定しています。 `Java`のシンタックスハイライトを行うサンプルコード([http://www.discoverteenergy.com/Files/SyntaxDocument.java SyntaxDocument.java])からキーワードのハイライト部分を抜き出して作成しています。 - 区切り文字は、空白、`.`(ドット)、`,`(カンマ)の`3`つのみ - `Java`のシンタックスハイライトを行うサンプルコード(参考リンクの`SyntaxDocument.java`など)からキーワードのハイライト部分を抜き出して作成 * 参考リンク [#q149fcae] - [http://www.discoverteenergy.com/Files/SyntaxDocument.java SyntaxDocument.java] -- 作者、ライセンス、参照元のページが何処だったかなどが不明…。 * 参考リンク [#reference] - [https://community.oracle.com/thread/2105230 Fast styled JTextPane editor | Oracle Community] -- %%オリジナルの作者、ライセンス、参照元のページが何処かなどが不明…%% -- [http://www.programcreek.com/java-api-examples/index.php?example_code_path=weka-weka.gui.scripting-SyntaxDocument.java Java Code Example for]によると、元は[https://community.oracle.com/thread/2105230 Fast styled JTextPane editor | Oracle Community]の投稿で、作者は camickr さん、David Underhill さん。そう言われるとなんとなくそんな記憶があるような…、あのフォーラムはもういろいろ変更されすぎていてどうにも… - [http://web.archive.org/web/20120802021725/http://java.sun.com/products/jfc/tsc/articles/text/editor_kit/index.html Customizing a Text Editor] - [http://ostermiller.org/syntax/editor.html Text Editor Tutorial] * コメント [#x982351a] * コメント [#comment] #comment #comment