做eclipse插件开发,菜单项的配置是在plugin.xml中是写死的,现在想做成动态菜单的形式。中间写一个类,这个类可以读取一个xml配置文件,然后可以有一些方法把提取过的数据和plugin.xml联系起来,通过这个类来动态的配置eclipse的菜单。我的想法是把读到的xml文件的内容封装到java的对象中,返回值是一个集合类型,然后我就不知道怎么做了。
图片是展示效果:
下面是我们找的一些相关资源:
[](http://dom4j.sourceforge.net/dom4j-1.6.1/guide.html "")
这是我自己写的demo:
package testforxybc;
public class UserInfo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String password;
}
package testforxybc;
import java.beans.XMLDecoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class UserInfoController {
public static void main(String args[])
{
List<UserInfo> objList = null;
try {
objList = UserInfoController.objectXMLDecoder("outxml.xml");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (UserInfo userInfo : objList) {
System.out.println(userInfo.getName());
System.out.println(userInfo.getPassword());
}
}
public static List<UserInfo> objectXMLDecoder(String objSource)
throws FileNotFoundException,IOException,Exception
{
List<UserInfo> objList = new ArrayList<UserInfo>();
File fin = new File(objSource);
FileInputStream fis = new FileInputStream(fin);
XMLDecoder decoder = new XMLDecoder(fis);
Object obj = null;
try
{
while( (obj = decoder.readObject()) != null)
{
objList.add((UserInfo) obj);
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
}
fis.close();
decoder.close();
return objList;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.ui.commands">
<category
name="Inoherb Category"
id="com.uds.inoherb.commands.category">
</category>
<command
name="法规标准库"
categoryId="com.uds.inoherb.commands.category"
id="com.uds.inoherb.commands.regulatoryStandards">
</command>
<command
name="产品标准"
categoryId="com.uds.inoherb.commands.category"
id="com.uds.inoherb.commands.productStandard">
</command>
<command
name="禁限用清单"
categoryId="com.uds.inoherb.commands.category"
id="com.uds.inoherb.commands.prohibitedList">
</command>
<command
name="全成分"
categoryId="com.uds.inoherb.commands.category"
id="com.uds.inoherb.commands.fullComposition">
</command>
<command
name="原料库"
categoryId="com.uds.inoherb.commands.category"
id="com.uds.inoherb.commands.rawMaterial">
</command>
<command
name="油脂类"
categoryId="com.uds.inoherb.commands.category"
id="com.uds.inoherb.commands.fadAndOil">
</command>
<command
name="乳化剂类"
categoryId="com.uds.inoherb.commands.category"
id="com.uds.inoherb.commands.emulsifier">
</command>
<command
name="香精类"
categoryId="com.uds.inoherb.commands.category"
id="com.uds.inoherb.commands.parfum">
</command>
<command
name="活性物类"
categoryId="com.uds.inoherb.commands.category"
id="com.uds.inoherb.commands.actives">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
commandId="com.uds.inoherb.commands.regulatoryStandards"
class="com.uds.inoherb.handlers.SampleHandler">
</handler>
<handler
commandId="com.uds.inoherb.commands.rawMaterial"
class="com.uds.inoherb.handlers.SampleHandler">
</handler>
<handler
commandId="com.uds.inoherb.commands.productStandard"
class="com.uds.inoherb.handlers.SampleHandler">
</handler>
<handler
commandId="com.uds.inoherb.commands.fadAndOil"
class="com.uds.inoherb.handlers.SampleHandler">
</handler>
</extension>
<extension
point="org.eclipse.ui.bindings">
<key
commandId="com.uds.inoherb.commands.sampleCommand"
contextId="org.eclipse.ui.contexts.window"
sequence="M1+6"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
</key>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu?after=additions">
<menu
label="企业电子数据库"
id="com.uds.inoherb.menus.sampleMenu">
<menu
label="法规标准库"
id="com.uds.inoherb.menus.sampleMenu2">
<command
commandId="com.uds.inoherb.commands.productStandard"
id="com.uds.inoherb.menus.productStandard">
</command>
<command
commandId="com.uds.inoherb.commands.prohibitedList"
id="com.uds.inoherb.menus.prohibitedList">
</command>
<command
commandId="com.uds.inoherb.commands.fullComposition"
id="com.uds.inoherb.menus.fullComposition">
</command>
</menu>
<menu
label="原料库"
id="com.uds.inoherb.menus.sampleMenu3">
<command
commandId="com.uds.inoherb.commands.fadAndOil"
id="com.uds.inoherb.menus.fadAndOil">
</command>
<command
commandId="com.uds.inoherb.commands.emulsifier"
id="com.uds.inoherb.menus.emulsifier">
</command>
<command
commandId="com.uds.inoherb.commands.parfum"
id="com.uds.inoherb.menus.parfum">
</command>
<command
commandId="com.uds.inoherb.commands.actives"
id="com.uds.inoherb.menus.actives">
</command>
</menu>
</menu>
</menuContribution>
</extension>
</plugin>