本文介绍如何自定义一个Eclipse中的工程属性页面。
- 扩展点 org.eclipse.ui.propertyPages
[codesyntax lang="xml"]
<extension point="org.eclipse.ui.propertyPages">
<page objectClass="org.eclipse.core.resources.IResource"
adaptable="true" name="%osgi.imported_packages"
class="org.suren.osgi.ui.properties.ImportPackagePage"
id="org.suren.osgi.ui.properties.ImportPackagePage">
</page>
</extension>
[/codesyntax] 实现类很简单,就是使用SWT创建一个页面就好。重点是要获取当前选择的工程,然后显示该工程的信息。下面是例子: [codesyntax lang="java"]
/**
* Copyright 1998-2015, http://surenpi.com. All Rights Reserved.
*/
package org.suren.osgi.ui.properties;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;
import org.suren.osgi.util.JavaProjectUtil;
/**
* @author suren
* @since jdk1.6
* 2015年12月8日
*/
public class ImportPackagePage extends PropertyPage {
@Override
protected Control createContents(Composite parent) {
IAdaptable ele = getElement();
IJavaProject javaProject = JavaProjectUtil.getJavaProject(ele);
List<String> importPackages = new ArrayList<String>();
JavaProjectUtil.findImportPackage(javaProject, importPackages);
Composite contents = new Composite(parent, SWT.NULL);
contents.setLayout(new FillLayout());
Set<String> packagesSet = new TreeSet<String>();
for(String pkg : importPackages)
{
pkg = pkg.substring(0, pkg.lastIndexOf("."));
packagesSet.add(pkg);
}
StringBuffer buf = new StringBuffer();
Iterator<String> it = packagesSet.iterator();
while(it.hasNext())
{
String pkg = it.next();
buf.append("\n").append(pkg);
}
if(buf.length() > 0)
{
buf.delete(0, 2);
}
new Text(contents, SWT.MULTI).setText(buf.toString());;
contents.layout(true, true);
return contents;
}
}
[/codesyntax]
Eclipse插件扩展点大全(扩展点清单)