Java エディターのプレビューでコードを操作

JDT コア・プラグインは、Java エレメントのプログラマチックな作成、削除、 および変更を行うためのいくつかのメカニズムを提供します。 JDT コアが提供する API の概要については、Java コードの操作を参照してください。

UI では、変更を保管する前に、ユーザーにプレビューを確認させることが役に立つ場合がよくあります。 これは、変更するコンパイル単位を Java エディターで開くことで行うことができます。 ユーザーはこのとき、Java エディターの内容をディスクに保管したり、元の内容に戻すことができます。 以下は、コンパイル単位をエディターで開いて、エディターによって作成された作業用コピーを使用して変更を行うコードの断片です。

void modifyCompilationUnit(ICompilationUnit cunit) throws PartInitException, CoreException {
IEditorPart editor= JavaUI.openInEditor(cunit);
IEditorInput input= editor.getEditorInput();
IWorkingCopyManager manager= JavaUI.getWorkingCopyManager();
manager.connect(input);
try {
ICompilationUnit workingCopy= manager.getWorkingCopy(input);
// do the modifications on workingCopy using the normal JDT Core API. } finally {
manager.disconnect(input);
}
// either keep the editor dirty or use editor.doSave(IProgressMonitor monitor) // to save the changes programmatically.
}

Copyright IBM Corporation and others 2000, 2002. All Rights Reserved.