首先申明下,本文为笔者学习《Eclipse插件开发学习笔记》的笔记,并加入笔者自己的理解和归纳总结。
在【Extension Points】中添加一个自定义扩展点【userExtensionInfo】。
生成的扩展点一般是包名 + "Extension Poing ID"的值
自定义扩展点包括
自定义扩展点userExtensionInfo
,
创建自定义扩展点【userExtensionInfo】
创建节点【item】,自定义扩展点【userExtensionInfo】可多次添加【item】。
节点属性定义
optional
是可选,required
是必须,default
需要输入默认值。boolean
是布尔型,string
是字符型,java
是使用java
类,resource
是使用资源,identifier
是引用其他扩展点的配置信息。引用自定义扩展点
在【extensions】中添加【com.plugin.blog.demo.userExtensionInfo】
在【com.plugin.blog.demo.userExtensionInfo】添加【item】
-
-
扩展点的读取
// 扩展点注册数据
IExtensionRegistry extensionRegister = Platform.getExtensionRegistry();
// 获取扩展点信息
IExtensionPoint extensionPoint = extensionRegister.getExtensionPoint(
"com.plugin.blog.demo.userExtensionInfo");
// 获取扩展点的扩展信息
IExtension[] extensions = extensionPoint.getExtensions();
for (IExtension ext : extensions) {
// 扩展的实现信息
for (IConfigurationElement ce : ext.getConfigurationElements()) {
String id = ce.getAttribute("id");
String name = ce.getAttribute("name");
}
}
UserExtensionInfoItem
类,读取扩展点信息,包含id
和name
两个属性。
public class UserExtensionInfoItem {
private static List ExtensionInfoList;
private String mId;
private String mName;
public UserExtensionInfoItem(String id, String name) {
this.mId = id;
this.mName = name;
}
public String getId() {
return mId;
}
public String getName() {
return mName;
}
public static List getExtensionInfoList() {
if (ExtensionInfoList == null) {
ExtensionInfoList = new ArrayList<>();
IExtensionRegistry extensionRegister = Platform.getExtensionRegistry();
IExtensionPoint extensionPoint = extensionRegister.getExtensionPoint(
"com.plugin.blog.demo.userExtensionInfo");
IExtension[] extensions = extensionPoint.getExtensions();
for (IExtension ext : extensions) {
for (IConfigurationElement ce : ext.getConfigurationElements()) {
String id = ce.getAttribute("id");
String name = ce.getAttribute("name");
ExtensionInfoList.add(new UserExtensionInfoItem(id, name));
}
}
}
return ExtensionInfoList;
}
}
UserExtensionInfo
类,存储扩展数据。
public class UserExtensionInfo extends UserInfo {
private HashMap mContent = new HashMap<>();
public UserExtensionInfo() {
super("扩展信息", "显示和查看扩展信息");
}
public void setValue(String key, String value) {
String oldValue = mContent.get(key);
mContent.put(key, value);
firePropertyChange(key, oldValue, value);
}
public String getValue(String key) {
String value = mContent.get(key);
return value == null ? "" : value;
}
@Override
public String getText() {
String text = "";
for (UserExtensionInfoItem infoItem :
UserExtensionInfoItem.getExtensionInfoList()) {
text += infoItem.getName() + ":" + getValue(infoItem.getId()) + "\n";
}
return text;
}
}
在UserInfoManager
中添加UserExtensionInfo
public class UserInfoManager {
private UserExtensionInfo mExtensionInfo;
private void load() {
mBasicInfo = new UserBasicInfo();
mContractInfo = new UserContractInfo();
mExtensionInfo = new UserExtensionInfo();
try {
XMLMemento memento = XMLMemento.createReadRoot(new FileReader(getStoreFile()));
IMemento child = memento.getChild("basic_info");
mBasicInfo.setMale(child.getBoolean("male"));
mBasicInfo.setAge(child.getInteger("age"));
child = memento.getChild("contract_info");
mContractInfo.setPhone(child.getString("phone"));
mContractInfo.setAddress(child.getString("address"));
child = memento.getChild("extension_info");
if (child != null) {
for (UserExtensionInfoItem infoItem :
UserExtensionInfoItem.getExtensionInfoList()) {
String value = child.getString(infoItem.getId());
if (value != null) {
mExtensionInfo.setValue(infoItem.getId(), value);
}
}
}
} catch (Exception e) {
}
mBasicInfo.addPropertyChangeListener(this);
mUserInfoList.add(mBasicInfo);
mContractInfo.addPropertyChangeListener(this);
mUserInfoList.add(mContractInfo);
mExtensionInfo.addPropertyChangeListener(this);
mUserInfoList.add(mExtensionInfo);
}
public void doSave(IProgressMonitor monitor) {
XMLMemento memento = XMLMemento.createWriteRoot("user");
IMemento child = memento.createChild("basic_info");
child.putBoolean("male", mBasicInfo.isMale());
child.putInteger("age", mBasicInfo.getAge());
child = memento.createChild("contract_info");
child.putString("phone", mContractInfo.getPhone());
child.putString("address", mContractInfo.getAddress());
child = memento.createChild("extension_info");
for (UserExtensionInfoItem infoItem :
UserExtensionInfoItem.getExtensionInfoList()) {
String id = infoItem.getId();
child.putString(id, mExtensionInfo.getValue(id));
}
try {
memento.save(new FileWriter(getStoreFile()));
} catch (IOException e) {
e.printStackTrace();
}
mDirty = false;
}
}
UserExtensionInfoDetailsPage
类,扩展信息编辑界面,目前只支持Text
类型。
public class UserExtensionInfoDetailsPage extends AbstractDetailsPage {
private List mTextList = new ArrayList<>();
private List mInfoItemList =
UserExtensionInfoItem.getExtensionInfoList();
@Override
protected void createContentInfo(IManagedForm form, Composite parent) {
for (UserExtensionInfoItem info : mInfoItemList) {
mTextList.add(createText(form, parent, info.getName()));
}
}
@Override
protected void updateUI(UserInfo userInfo) {
super.updateUI(userInfo);
if (userInfo == null) {
for (Text text : mTextList) {
text.setText("");
}
} else {
UserExtensionInfo extensionInfo = (UserExtensionInfo) userInfo;
for (int index = 0; index < mTextList.size(); index++) {
String id = mInfoItemList.get(index).getId();
mTextList.get(index).setText(extensionInfo.getValue(id));
}
}
}
@Override
protected void updateInfo(UserInfo userInfo) {
super.updateInfo(userInfo);
UserExtensionInfo extensionInfo = (UserExtensionInfo) userInfo;
for (int index = 0; index < mTextList.size(); index++) {
String id = mInfoItemList.get(index).getId();
extensionInfo.setValue(id, mTextList.get(index).getText());
}
}
}
在UserInfoBlock
中添加UserExtensionInfoDetailsPage
public class UserInfoBlock {
private AbstractDetailsPage mExtensionInoDetailsPage;
public UserInfoBlock(FormPage formPage) {
this.mFormPage = formPage;
mBasicInfoDetailPage = new UserBasicInfoDetailsPage();
mContractInfoDetailsPage = new UserContractInfoDetailsPage();
mExtensionInoDetailsPage = new UserExtensionInfoDetailsPage();
}
@Override
protected void registerPages(DetailsPart detailsPart) {
detailsPart.registerPage(UserBasicInfo.class, mBasicInfoDetailPage);
detailsPart.registerPage(UserContractInfo.class, mContractInfoDetailsPage);
detailsPart.registerPage(UserExtensionInfo.class, mExtensionInoDetailsPage);
}
}
相关文章
Java Plug-in(一) 创建插件项目
Java Plug-in(二) 菜单
Java Plug-in(三) 视图
Java Plug-in(四) 视图菜单
Java Plug-in(五) 编辑器
Java Plug-in(六) 编辑器菜单
Java Plug-in(七) 透视图
Java Plug-in(八) 向导页
Java Plug-in(九) 首选项
Java Plug-in(十) 自定义扩展点
Java Plug-in(十一) 创建RCP项目