Eclipse/プラグイン開発のTIPS集/エディタをつくる
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。
#contents
**テキストエディタを作る。 [#r1c4624c]
本丸のエディタを作ってみました。テキストファイル内で、%...
***TextEditorクラスのサブクラスを作る [#q8b4fa87]
import org.eclipse.ui.editors.text.TextEditor;
public class ScriptEditor extends TextEditor {
private ColorManager colorManager; <-Colorクラスを管理...
public ScriptEditor() {
super();
colorManager = new ColorManager(); ↓後述
setSourceViewerConfiguration(new ScriptConfiguration...
setDocumentProvider(new ScriptDocumentProvider());
↑後述
}
public void dispose() {
colorManager.dispose();
super.dispose();
}
}
*** org.eclipse.jface.text.rules.RuleBasedPartitionScanne...
このクラスは、どのようにドキュメント(エディタが編集するテ...
package nu.mine.kino.plugin.script.editors;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.RuleBasedPartitionSc...
import org.eclipse.jface.text.rules.Token;
public class ScriptPartitionScanner extends RuleBasedPar...
public static final String SCRIPT_VAR = "__script_var";
public ScriptPartitionScanner() {
IToken var = new Token(SCRIPT_VAR);
IPredicateRule[] rules = new IPredicateRule[1];
// rules[1] = new TagRule(tag);
rules[0] = new MultiLineRule("%%", "%%", var);
setPredicateRules(rules);
}
}
これで、%% から %%で囲まれた場所は、SCRIPT_VARという...
*** org.eclipse.ui.editors.text.FileDocumentProvider のサ...
ScriptEditorの処理でsetDocumentProviderの引数に指定された...
protected IDocument createDocument(Object element) throw...
logger.debug("createDocument(Object) - start");
IDocument document = super.createDocument(element);
if (document != null) {
IDocumentPartitioner partitioner = new FastPartition...
new ScriptPartitionScanner(), <-さっき作ったヤツ
new String[] { ScriptPartitionScanner.SCRIPT_VAR...
partitioner.connect(document);
document.setDocumentPartitioner(partitioner);
}
logger.debug("createDocument(Object) - end");
return document;
}
***org.eclipse.jface.text.source.SourceViewerConfiguratio...
SourceViewerConfiguration#public IPresentationReconciler ...
public IPresentationReconciler getPresentationReconciler(
ISourceViewer sourceViewer) {
logger.debug("getPresentationReconciler(ISourceViewer)...
PresentationReconciler reconciler = new PresentationRe...
Color color = colorManager.getColor(IScriptColorConsta...
TextAttribute attribute = new TextAttribute(color);
SingleTokenScanner singleTokenScanner = new SingleToke...
attribute);
DefaultDamagerRepairer dr = new DefaultDamagerRepairer(
singleTokenScanner);
reconciler.setDamager(dr, ScriptPartitionScanner.SCRIP...
reconciler.setRepairer(dr, ScriptPartitionScanner.SCRI...
logger.debug("getPresentationReconciler(ISourceViewer)...
return reconciler;
}
ここまでで、SCRIPT_VARというパーティション(ここは%%〜%...
private class SingleTokenScanner extends BufferedRuleBas...
public SingleTokenScanner(TextAttribute attribute) {
setDefaultReturnToken(new Token(attribute));
}
}
ちなみに他のクラスはこんな感じ。
public interface IScriptColorConstants {
RGB SCRIPT_VAR = new RGB(255, 0, 0); <-赤。
}
public class ColorManager {
protected Map fColorTable = new HashMap(10);
public void dispose() {
Iterator e = fColorTable.values().iterator();
while (e.hasNext())
((Color) e.next()).dispose();
}
public Color getColor(RGB rgb) {
Color color = (Color) fColorTable.get(rgb);
if (color == null) {
color = new Color(Display.getCurrent(), rgb);
fColorTable.put(rgb, color);
}
return color;
}
}
実際にEclipseでこのプラグインを実行してテキストファイルを...
Color defaultColor = colorManager
.getColor(IScriptColorConstants.DEFAULT);
TextAttribute defaultAttr = new TextAttribute(defaultCol...
dr = new DefaultDamagerRepairer(new SingleTokenScanner(d...
reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYP...
** アウトラインを使う [#d12c3305]
[[プロパティシートを使う>Eclipse/プラグイン開発のTIPS集/...
public Object getAdapter(Class adapter) {
if (IContentOutlinePage.class.equals(adapter)) {
if (fOutlinePage == null) {
fOutlinePage = new ScriptContentOutlinePage(
getDocumentProvider(), this);
if (getEditorInput() != null) {
fOutlinePage.setInput(getEditorInput());
}
fOutlinePage.addSelectionChangedListener(this);
}
return fOutlinePage;
}
return super.getAdapter(adapter);
}
ScriptContentOutlinePageはIContentOutlinePageをimplements...
** TIPS集 [#xb55af55]
***あるテキストを選択状態にする。 [#y6efd2aa]
例えば、アウトラインでクリックした箇所にカーソルを移動し...
- 処理をTextEditorのサブクラスでやる場合
protectedな下記のメソッドが使用できます。
ISelection selection = event.getSelection();
if (!selection.isEmpty()) {
int start = 開始位置
int length = 長さ
getSourceViewer().getTextWidget().setSelectionRange(...
length);
}
他に、
getSourceViewer().getTextWidget().setSelection(start,sta...
や
ISourceViewer sourceViewer = getSourceViewer();
StyledText textWidget = getSourceViewer().getTextWidget();
try {
textWidget.setRedraw(false);
sourceViewer.revealRange(start, length);
sourceViewer.setSelectedRange(start, length);
} finally {
textWidget.setRedraw(true);
}
- 処理をTextEditorのサブクラスではないものでやる場合
publicな下記のメソッドを使用します。
ITextEditor#selectAndReveal(int start, int length);
*** アクティブなエディタへの参照の取得 [#uf7ea3d1]
// ワークベンチの取得
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = workbench.getActiveWorkbenchWi...
//アクティブなエディタの取得
IEditorPart editor = window.getActivePage().getActiveEdi...
AbstractTextEditor aEditor = (AbstractTextEditor) editor;
*** エディタ内のドキュメントを取得 [#o1b1fa35]
IDocument document =
aEditor.getDocumentProvider().getDocument(editor.getEd...
** 関連リンク [#sf5a98b6]
-[[このサイト内のサンプルソースのCVS:http://www.masatom.i...
-[[SourceViewerを使用するドキュメントの分割と構文強調表示...
-[[自作エディタの定義:http://yoichiro.cocolog-nifty.com/e...
-[[作って覚えるEclipseプラグイン(3)テキストエディタプラ...
-[[Eclipse エディタ調査報告書:http://staff.aist.go.jp/y-i...
----
この記事は
#vote(おもしろかった[20],そうでもない[0])
-JavaEditorで使用するパーティション名があるクラス: org.ec...
-何故か、他のクラスでITextEditor#setHighlightRange してた...
-つうかそもそもselectAndRevealだけでハイライトも変わるん...
-XMLBuddyをプラグインでインポートして、オラオラってソース...
-パーティション切るときにTokenに指定する引数と、スキャナ...
#topicpath
SIZE(10){現在のアクセス:&counter;}
終了行:
// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。
#contents
**テキストエディタを作る。 [#r1c4624c]
本丸のエディタを作ってみました。テキストファイル内で、%...
***TextEditorクラスのサブクラスを作る [#q8b4fa87]
import org.eclipse.ui.editors.text.TextEditor;
public class ScriptEditor extends TextEditor {
private ColorManager colorManager; <-Colorクラスを管理...
public ScriptEditor() {
super();
colorManager = new ColorManager(); ↓後述
setSourceViewerConfiguration(new ScriptConfiguration...
setDocumentProvider(new ScriptDocumentProvider());
↑後述
}
public void dispose() {
colorManager.dispose();
super.dispose();
}
}
*** org.eclipse.jface.text.rules.RuleBasedPartitionScanne...
このクラスは、どのようにドキュメント(エディタが編集するテ...
package nu.mine.kino.plugin.script.editors;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.RuleBasedPartitionSc...
import org.eclipse.jface.text.rules.Token;
public class ScriptPartitionScanner extends RuleBasedPar...
public static final String SCRIPT_VAR = "__script_var";
public ScriptPartitionScanner() {
IToken var = new Token(SCRIPT_VAR);
IPredicateRule[] rules = new IPredicateRule[1];
// rules[1] = new TagRule(tag);
rules[0] = new MultiLineRule("%%", "%%", var);
setPredicateRules(rules);
}
}
これで、%% から %%で囲まれた場所は、SCRIPT_VARという...
*** org.eclipse.ui.editors.text.FileDocumentProvider のサ...
ScriptEditorの処理でsetDocumentProviderの引数に指定された...
protected IDocument createDocument(Object element) throw...
logger.debug("createDocument(Object) - start");
IDocument document = super.createDocument(element);
if (document != null) {
IDocumentPartitioner partitioner = new FastPartition...
new ScriptPartitionScanner(), <-さっき作ったヤツ
new String[] { ScriptPartitionScanner.SCRIPT_VAR...
partitioner.connect(document);
document.setDocumentPartitioner(partitioner);
}
logger.debug("createDocument(Object) - end");
return document;
}
***org.eclipse.jface.text.source.SourceViewerConfiguratio...
SourceViewerConfiguration#public IPresentationReconciler ...
public IPresentationReconciler getPresentationReconciler(
ISourceViewer sourceViewer) {
logger.debug("getPresentationReconciler(ISourceViewer)...
PresentationReconciler reconciler = new PresentationRe...
Color color = colorManager.getColor(IScriptColorConsta...
TextAttribute attribute = new TextAttribute(color);
SingleTokenScanner singleTokenScanner = new SingleToke...
attribute);
DefaultDamagerRepairer dr = new DefaultDamagerRepairer(
singleTokenScanner);
reconciler.setDamager(dr, ScriptPartitionScanner.SCRIP...
reconciler.setRepairer(dr, ScriptPartitionScanner.SCRI...
logger.debug("getPresentationReconciler(ISourceViewer)...
return reconciler;
}
ここまでで、SCRIPT_VARというパーティション(ここは%%〜%...
private class SingleTokenScanner extends BufferedRuleBas...
public SingleTokenScanner(TextAttribute attribute) {
setDefaultReturnToken(new Token(attribute));
}
}
ちなみに他のクラスはこんな感じ。
public interface IScriptColorConstants {
RGB SCRIPT_VAR = new RGB(255, 0, 0); <-赤。
}
public class ColorManager {
protected Map fColorTable = new HashMap(10);
public void dispose() {
Iterator e = fColorTable.values().iterator();
while (e.hasNext())
((Color) e.next()).dispose();
}
public Color getColor(RGB rgb) {
Color color = (Color) fColorTable.get(rgb);
if (color == null) {
color = new Color(Display.getCurrent(), rgb);
fColorTable.put(rgb, color);
}
return color;
}
}
実際にEclipseでこのプラグインを実行してテキストファイルを...
Color defaultColor = colorManager
.getColor(IScriptColorConstants.DEFAULT);
TextAttribute defaultAttr = new TextAttribute(defaultCol...
dr = new DefaultDamagerRepairer(new SingleTokenScanner(d...
reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYP...
** アウトラインを使う [#d12c3305]
[[プロパティシートを使う>Eclipse/プラグイン開発のTIPS集/...
public Object getAdapter(Class adapter) {
if (IContentOutlinePage.class.equals(adapter)) {
if (fOutlinePage == null) {
fOutlinePage = new ScriptContentOutlinePage(
getDocumentProvider(), this);
if (getEditorInput() != null) {
fOutlinePage.setInput(getEditorInput());
}
fOutlinePage.addSelectionChangedListener(this);
}
return fOutlinePage;
}
return super.getAdapter(adapter);
}
ScriptContentOutlinePageはIContentOutlinePageをimplements...
** TIPS集 [#xb55af55]
***あるテキストを選択状態にする。 [#y6efd2aa]
例えば、アウトラインでクリックした箇所にカーソルを移動し...
- 処理をTextEditorのサブクラスでやる場合
protectedな下記のメソッドが使用できます。
ISelection selection = event.getSelection();
if (!selection.isEmpty()) {
int start = 開始位置
int length = 長さ
getSourceViewer().getTextWidget().setSelectionRange(...
length);
}
他に、
getSourceViewer().getTextWidget().setSelection(start,sta...
や
ISourceViewer sourceViewer = getSourceViewer();
StyledText textWidget = getSourceViewer().getTextWidget();
try {
textWidget.setRedraw(false);
sourceViewer.revealRange(start, length);
sourceViewer.setSelectedRange(start, length);
} finally {
textWidget.setRedraw(true);
}
- 処理をTextEditorのサブクラスではないものでやる場合
publicな下記のメソッドを使用します。
ITextEditor#selectAndReveal(int start, int length);
*** アクティブなエディタへの参照の取得 [#uf7ea3d1]
// ワークベンチの取得
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = workbench.getActiveWorkbenchWi...
//アクティブなエディタの取得
IEditorPart editor = window.getActivePage().getActiveEdi...
AbstractTextEditor aEditor = (AbstractTextEditor) editor;
*** エディタ内のドキュメントを取得 [#o1b1fa35]
IDocument document =
aEditor.getDocumentProvider().getDocument(editor.getEd...
** 関連リンク [#sf5a98b6]
-[[このサイト内のサンプルソースのCVS:http://www.masatom.i...
-[[SourceViewerを使用するドキュメントの分割と構文強調表示...
-[[自作エディタの定義:http://yoichiro.cocolog-nifty.com/e...
-[[作って覚えるEclipseプラグイン(3)テキストエディタプラ...
-[[Eclipse エディタ調査報告書:http://staff.aist.go.jp/y-i...
----
この記事は
#vote(おもしろかった[20],そうでもない[0])
-JavaEditorで使用するパーティション名があるクラス: org.ec...
-何故か、他のクラスでITextEditor#setHighlightRange してた...
-つうかそもそもselectAndRevealだけでハイライトも変わるん...
-XMLBuddyをプラグインでインポートして、オラオラってソース...
-パーティション切るときにTokenに指定する引数と、スキャナ...
#topicpath
SIZE(10){現在のアクセス:&counter;}
ページ名: