|
Eclipse(のワークスペース)でファイルを表すリソース(IResource)がIFileインターフェース。
import org.eclipse.core.resources.IFile;
IFileの依存プラグインは「org.eclipse.core.resources」。
TextEditorから編集中のファイルを取得する方法。
import org.eclipse.core.resources.IFile; import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.editors.text.TextEditor;
TextEditor textEditor = 〜; IFileEditorInput input = (IFileEditorInput) textEditor.getEditorInput(); IFile file = input.getFile();
IFileEditorInputのパッケージは「org.eclipse.ui」だが、依存プラグインとしては「org.eclipse.ui.ide」を追加する必要がある。
(「org.eclipse.ui.ide」はマニフェストエディターでは選択肢に出てこなかったので、MANIFEST.MFに直接記述した)
IFile file = 〜; String ext = file.getFileExtension();
ファイル名が「models.
dmdl」の場合、「dmdl」(ピリオド無し)が取れる。
IFileからIPathを取得する例。
import org.eclipse.core.runtime.IPath;
IFile file = 〜; IPath path = file.getFullPath(); // IPath path = file.getProjectRelativePath(); java.io.File f = path.toFile();
IFileからIPathを取得し、さらにjava.io.Fileに変換することも出来る。
が、IFile自体はワークスペースのルートからの相対パスで表現されているので、そこから取得できるIPathやIPath.toFile()も相対パスになってしまう。
→絶対パスを取得する方法
IPath(フルパス)からIFileへ変換する例。[2013-02-17]
import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.ResourcesPlugin;
IPath path = 〜; IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); IFile file = workspaceRoot.getFile(path);
参考: stackoverflowのGet IFile from IWorkspaceRoot and location String
IFile#getLocationURI()だと、ファイルシステム上の絶対パスが返ってくる。
IFile file = 〜; URI uri = file.getLocationURI(); URL url = uri.toURL(); InputStream is = url.openStream();
また、直接InputSreamを取得することも出来る。[2013-02-19]
IFile file = 〜; InputStream is = file.getContents(true);
IFileからIDocumentを生成する方法。
import org.eclipse.jface.text.IDocument; import org.eclipse.ui.editors.text.FileDocumentProvider; import org.eclipse.ui.part.FileEditorInput;
IFile file = 〜; FileEditorInput input = new FileEditorInput(file); FileDocumentProvider provider = new FileDocumentProvider(); try { provider.connect(input); } catch (CoreException e) { throw new RuntimeException(e); } IDocument document = provider.getDocument(input);
FileBuffersを使ってIFileからIDocumentを生成する方法。[2013-06-12]
FileBuffersを使うと、IDocumentのテキストを変更することも出来る。
import org.eclipse.core.filebuffers.FileBuffers; import org.eclipse.core.filebuffers.ITextFileBuffer; import org.eclipse.core.filebuffers.ITextFileBufferManager; import org.eclipse.core.filebuffers.LocationKind;
IFile file = 〜; IPath path = file.getFullPath(); ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager(); manager.connect(path, LocationKind.IFILE, null); try { ITextFileBuffer buffer = manager.getTextFileBuffer(path, LocationKind.IFILE); IDocument document = buffer.getDocument(); 〜 //document.replace()等で変更を加える buffer.commit(null, true); } finally { manager.disconnect(path, LocationKind.IFILE, null); }
IDocumentを変更しなかった場合はbuffer.commit()を呼び出す必要は無い。
IFileに紐付いているエディター(IEditorPart)を開く例。[2013-02-17]
import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.ide.IDE;
IFile file = 〜; IWorkbench workbench = PlatformUI.getWorkbench(); IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); IWorkbenchPage page = window.getActivePage(); IEditorPart editor = IDE.openEditor(page, file);
既にエディターが開いている場合はそのエディターが取得できる。
参考: yoichiroさんのエディタを開く Part2
システムエディター(外部エディター)でファイルを開きたい場合は、システムエディターのIDを指定する。[2014-12-22]
import org.eclipse.ui.IEditorRegistry;
public static IEditorPart openSystemEditor(IFile file) { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IEditorPart editor = IDE.openEditor(page, file, IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); return editor; }
空のファイルを作成する例。[2013-03-16]
IFile file = 〜; if (!file.exists()) { createFolder(file.getProject(), file.getProjectRelativePath().removeLastSegments(1)); InputStream is = new ByteArrayInputStream(new byte[0]); file.create(is, false, null); is.close(); }
private void createFolder(IProject project, IPath path) throws CoreException { IFolder folder = project.getFolder(path); if (!folder.exists()) { createFolder(project, path.removeLastSegments(1)); folder.create(false, true, null); } }