您的位置:首页 > 编程语言 > Java开发

Eclipse 插件开发心得小结

2011-02-17 15:17 309 查看
1、Eclipse 中插件开发多语言的实现

为了使用 .properties 文件,需要在 META-INF/MANIFEST.MF 文件中定义:

Bundle-Localization: plugin

这样就会自动加载 plugin.properties 文件(中文找 plugin_zh_CN.properties)

然后在 plugin.xml 文件中,将字符串替换为 %key 就可以了

建议先使用 Eclipse 的外部化字符串目录:

Bundle-Localization: OSGI-INF/l10n/plugin


2、Eclipse 插件开发初始化隐藏某工具栏按钮

在网上找了好久都找不到解决办法,最后搜索 eclipse 安装目录,从它自己的插件里面找到样例了。样例来自 org.eclipse.jdt.ui/plugin.xml

<extension point="org.eclipse.ui.perspectiveExtensions">
<perspectiveExtension targetID="*">
<!-- 注意这里的 id 是 action 或 command 的 id -->
<hiddenToolBarItem id="org.eclipse.jdt.ui.actions.OpenProjectWizard" />
</perspectiveExtension>
</extension>


3、插件中获取 Eclipse 版本号

String sEclipseVersion = System.getProperty("osgi.framework.version");


4、插件中获取路径

// 得到插件所在的路径
Platform.asLocalURL(Platform.getBundle("your plugin ID").getEntry("")).getFile();

// 得到当前工作空间的路径
Platform.getInstanceLocation().getURL().getFile();

// 得到当前工作空间下的所有工程
ResourcesPlugin.getWorkspace().getRoot().getProjects();

// 得到某 PLUGIN 的路径:
Platform.getBundle("mypluginid").getLocation().
// eclipse采用osgi后好像还可以:Activator.getDefault().getBundle().getLocation();  //前提是这个插件有Activator这个类.这个类继承了ECLIPSE的Plugin类
// eclipse采用osgi前好像好像是:MyPlugin.getDefault().getBundle().getLocation();  //前提是这个插件有MyPlugin这个类.这个类继承了ECLIPSE的Plugin类

// 得到工作区路径: Platform.getlocation();
// 或 ResourcesPlugin.getWorkspace(); 好像 Platform.getInstanceLocation() 也可行

// 得到ECLIPSE安装路径
Platform.getInstallLocation();

// 从插件中获得绝对路径:
AaaaPlugin.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath();

// 通过文件得到 Project:
IProject project = ((IFile)o).getProject();

// 通过文件得到全路径:
String path = ((IFile)o).getLocation().makeAbsolute().toFile().getAbsolutePath();

// 得到整个Workspace的根:
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

// 从根来查找资源:
IResource resource = root.findMember(new Path(containerName));

// 从Bundle来查找资源:
Bundle bundle = Platform.getBundle(pluginId);URL fullPathString = BundleUtility.find(bundle, filePath);

// 得到 Appliaction workspace:
Platform.asLocalURL(PRODUCT_BUNDLE.getEntry("")).getPath()).getAbsolutePath();

// 得到 runtimeworkspace:
Platform.getInstanceLocation().getURL().getPath();

// 从编辑器来获得编辑文件
IEditorPart editor = ((DefaultEditDomain)(parent.getViewer().getEditDomain())).getEditorPart();
IEditorInput input = editor.getEditorInput();
if (input instanceof IFileEditorInput)
{
IFile file = ((IFileEditorInput)input).getFile();
}

// 获取插件的绝对路径:
FileLocator.resolve(BuildUIPlugin.getDefault().getBundle().getEntry("/")).getFile();


5、Preference Page 和 PropertyPage

// 打开 PreferencePageDialog:
PreferenceManager manager = window.getWorkbench().getPreferenceManager();
PreferenceDialog dialog = new PreferenceDialog(window.getShell(), manager);
dialog.open();

// 打开 PropertyPageDialog:
PropertyDialog dialog = PropertyDialog.createDialogOn(shell, null, element);
dialog.open();

// 打开只有选定首选项页的 Dialog:
WorkbenchPreferenceDialog dialog = WorkbenchPreferenceDialog.createDialogOn(parent.getShell(), id);
dialog.showOnly(new String[] { id });
dialog.open();


7、使用 Eclipse 中对 Web Browser 的支持

try
{
IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
IWebBrowser browser = support.createBrowser("Some_ID");
browser.openURL(new URL(("http://www.eclipse.org")));
}
catch (PartInitException e)
{
e.printStackTrace();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}


8、OSGi 包 header 说明

Bundle-Activator 指定用于启动和停止包的类的名称。
Bundle-Classpath 指定包含类和资源的 JAR 文件或目录。句号(‘.’)、默认值指定了包的 JAR 的根目录。
Bundle-ContactAddress 包含供应商的联系地址。
Bundle-Copyright 包含此包的版权说明。
Bundle-DocURL 指定了一个指向有关此包的文档的 URL。
Bundle-Localization 指定了包的本地化文件的位置,其默认值为 OSGI-INF/l10n/bundle。
Bundle-ManifestVersion 指定该包遵从 OSGi 规范 V3 或者 OSGi 规范 V4 中的规则。
Bundle-Name 指定该包的可读名称(无空格)。
Bundle-SymbolicName 一个强制的 header,用于为此包指定一个惟一的名称。
Bundle-Vendor 包含一个包供应商的可读名称。
Bundle-Version 指定包的版本,默认为 0.0.0。
Export-Package 指定此包的导出包(exported package)。
Fragment-Host 定义此片段的本地包(host bundle)。
Import-Package 声明此包的导入包(imported package)。
Require-Bundle 指定从其他包所需的导出。
Import-Service 不建议使用
Export-Service 不建议使用


注:参考页面:

http://www.diybl.com/course/3_program/java/javajs/200865/122139.html

http://allenyoung.iteye.com/blog/59312
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: