リソースの取得方法

Eclipseの実行ファイル eclipse.exe の場所
java.io.File f = new File("eclipse.exe");
String path = f.getAbsolutePath()
ワークスペースの場所
ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()
プラグイン内のリソース
try {
 Bundle bundle = Platform.getBundle("com.xxx");//プラグインID
 URL bundleUrl = bundle.getResource("icons");//プラグインからのリソースパス
 URL fileUrl = org.eclipse.core.runtime.FileLocator.toFileURL(bundleUrl);//変換
 bundlePath = fileUrl.toString();

 //getPath()でも可
 pluginEntryPath = fileUrl.getPath();
 // なぜか先頭は'/'となる
 // path = /C:/・・・eclipse/workspace/com.xxx/
 if (pluginEntryPath.charAt(0) == '/') {
   pluginEntryPath = pluginEntryPath.substring(1);
 }

} catch (IOException e) {
}

プロジェクト一覧
ResourcesPlugin.getWorkspace().getRoot().getProjects()


リソース変換
IFile to File
IFile f = ...;
IPath location = f.getLocation();
if (location != null)
   java.io.File file = location.toFile()

org.eclipse.core.runtime.IPath → org.eclipse.emf.common.util.URI
IPath path = delta.getMovedFromPath();
URI uri = URI.createPlatformResourceURI(path.toString(), true);

org.eclipse.core.resources.IFile → org.eclipse.emf.common.util.URI
IFile file = ...;
URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);

org.eclipse.core.resources.IFile → org.eclipse.emf.ecore.resource.Resource
String path = ((IFile)parentElement).getFullPath().toString();
URI uri = URI.createPlatformResourceURI(path, true);
TransactionalEditingDomain editingDomain = EditingDomainManager.getEditingDomain(file);
ResourceSet resourceSet = editingDomain.getResourceSet();
Resource resource = resourceSet.getResource(uri, true);

org.eclipse.emf.ecore.resource.Resource → org.eclipse.core.resources.IFile
org.eclipse.emf.ecore.resource.Resource r = ...;
IFile file = org.eclipse.emf.workspace.util.WorkspaceSynchronizer.getFile(r)

EObject eobj = (EObject) selected;
Resource r = eobj.eResource();
String strFile = r.getURI().toPlatformString(false);
IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new  Path(strFile));


//プラグイン内のプロパティファイルの取得方法
Bundle bundle = Platform.getBundle("xxx.yyy.zzz.plugin");
URL propURL = FileLocator.toFileURL(bundle.getEntry("/resource/message.properties"));
File file = new File(propURL.getFile());
Properties properties = new Properties();
properties.load(new FileInputStream(file));
最終更新:2011年01月05日 14:23
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。