接下来,我们开始实现 扩展点的一些 功能吧,新增选中的包和类:
为了方便起见,我将这些类的源代码贴出来
TheFirstAction .java:
package
org.vwpolo.rcp.extension.client.actions;
import org.eclipse.jface.action.Action;
/**
* <p> 。</p>
* @author 刘绕兴
* <p>2008-2-12</p>
*/
public class TheFirstAction extends Action {
/**
*
*/
public static final String ID = TheFirstAction. class .getName();
/**
* 构造函数。
*/
public TheFirstAction() {
setId(ID);
setText( " 第一个Action " );
}
public void run() {
Shell shell = Display.getCurrent().getActiveShell();
MessageDialog.openInformation(shell, " 信 息 " , " 第一个项目中的Action " );
}
}
import org.eclipse.jface.action.Action;
/**
* <p> 。</p>
* @author 刘绕兴
* <p>2008-2-12</p>
*/
public class TheFirstAction extends Action {
/**
*
*/
public static final String ID = TheFirstAction. class .getName();
/**
* 构造函数。
*/
public TheFirstAction() {
setId(ID);
setText( " 第一个Action " );
}
public void run() {
Shell shell = Display.getCurrent().getActiveShell();
MessageDialog.openInformation(shell, " 信 息 " , " 第一个项目中的Action " );
}
}
ExtensionContances .java
package
org.vwpolo.rcp.extension.client.extender;
/**
* <p> 扩展点常量。</p>
* @author 刘绕兴
* <p>2008-2-12</p>
*/
public class ExtensionContances {
/** */
public static final String EXTENSION_ID = " org.vwpolo.rcp.extender " ;
/** */
public static final String ATTR_ID = " id " ;
/** */
public static final String ATTR_NAME = " name " ;
/** */
public static final String ATTR_TYPE = " type " ;
/** */
public static final String ATTR_CLIENTID = " clientId " ;
/** */
public static final String ATTR_INDEX = " index " ;
/** */
public static final String ATTR_DESC = " desc " ;
/** */
public static final String ATTR_CLASS = " className " ;
/** */
public static final String TYPE_PERSPECTIVE = " perspective " ;
/** */
public static final String TYPE_VIEW = " view " ;
/** */
public static final String TYPE_ACTION = " action " ;
}
/**
* <p> 扩展点常量。</p>
* @author 刘绕兴
* <p>2008-2-12</p>
*/
public class ExtensionContances {
/** */
public static final String EXTENSION_ID = " org.vwpolo.rcp.extender " ;
/** */
public static final String ATTR_ID = " id " ;
/** */
public static final String ATTR_NAME = " name " ;
/** */
public static final String ATTR_TYPE = " type " ;
/** */
public static final String ATTR_CLIENTID = " clientId " ;
/** */
public static final String ATTR_INDEX = " index " ;
/** */
public static final String ATTR_DESC = " desc " ;
/** */
public static final String ATTR_CLASS = " className " ;
/** */
public static final String TYPE_PERSPECTIVE = " perspective " ;
/** */
public static final String TYPE_VIEW = " view " ;
/** */
public static final String TYPE_ACTION = " action " ;
}
ExtensionHelper .java
package
org.vwpolo.rcp.extension.client.extender;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.vwpolo.rcp.extension.client.Activator;
import org.vwpolo.rcp.extension.client.actions.TheFirstAction;
/**
* <p> 。</p>
* @author 刘绕兴
* <p>2008-2-12</p>
*/
public class ExtensionHelper {
/** */
private String ID = ExtensionContances.EXTENSION_ID;
/** */
public static ExtensionHelper instance;
/** Action Map */
private Map < String, List < ExtensionInfoBean >> actionMap = new HashMap < String, List < ExtensionInfoBean >> ();
/** 视图 Map */
private Map < String, List < ExtensionInfoBean >> viewMap = new HashMap < String, List < ExtensionInfoBean >> ();
/** 透视图 Map */
private Map < String, List < ExtensionInfoBean >> perspectiveMap = new HashMap < String, List < ExtensionInfoBean >> ();
/** */
private MenuManager extendMenu;
/**
* 构造函数。
*/
private ExtensionHelper() {
loadExtensions();
}
/**
* <p> 。</p>
* @author 刘绕兴
* @return ExtensionHelper
*/
public static ExtensionHelper getInstance() {
if (instance == null )
instance = new ExtensionHelper();
return instance;
}
/**
* <p> 读取扩展点的属性,然后生成ExtensionInfoBean 对象。</p>
* @author 刘绕兴
*/
private void loadExtensions() {
IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(Activator.EXTENSION_ID);
if (elements == null || elements.length == 0 )
return ;
for ( int i = 0 ; i < elements.length; i ++ ) {
IConfigurationElement element = elements[i];
ExtensionInfoBean bean = new ExtensionInfoBean();
bean.setId(element.getAttribute(ExtensionContances.ATTR_ID));
bean.setName(element.getAttribute(ExtensionContances.ATTR_NAME));
bean.setType(element.getAttribute(ExtensionContances.ATTR_TYPE));
bean.setClientId(element.getAttribute(ExtensionContances.ATTR_CLIENTID));
String index = element.getAttribute(ExtensionContances.ATTR_INDEX);
if (index == null || index.trim().isEmpty()) {
bean.setIndex( 100 );
} else {
bean.setIndex(Integer.parseInt(index));
}
bean.setDesc(element.getAttribute(ExtensionContances.ATTR_DESC));
if (ExtensionContances.TYPE_ACTION.equalsIgnoreCase(bean.getType())) {
try {
String classPath = element.getAttribute(ExtensionContances.ATTR_CLASS);
Object object = null ;
if ( ! classPath.isEmpty()) {
object = element.createExecutableExtension(ExtensionContances.ATTR_CLASS);
} else {
object = element.createExecutableExtension(ExtensionContances.ATTR_CLIENTID);
}
if (object instanceof IAction) {
bean.setAction((IAction) object);
}
} catch (Exception e) {
e.printStackTrace();
}
}
appendGroup(bean);
}
}
/**
* <p> 。</p>
* @author 刘绕兴
* @param bean
*/
private void appendGroup(ExtensionInfoBean bean) {
if (ExtensionContances.TYPE_PERSPECTIVE.equalsIgnoreCase(bean.getType()))
addToMap(perspectiveMap, bean);
else if (ExtensionContances.TYPE_VIEW.equalsIgnoreCase(bean.getType()))
addToMap(viewMap, bean);
else if (ExtensionContances.TYPE_ACTION.equalsIgnoreCase(bean.getType()))
addToMap(actionMap, bean);
}
/**
* <p> 。</p>
* @author 刘绕兴
* @param map
* @param bean
*/
private void addToMap(Map < String, List < ExtensionInfoBean >> map, ExtensionInfoBean bean) {
List < ExtensionInfoBean > list;
if (map.containsKey(ID)) {
list = (List < ExtensionInfoBean > ) map.get(ID);
} else {
list = new ArrayList < ExtensionInfoBean > ();
map.put(ID, list);
}
list.add(bean);
}
/**
* <p> 。</p>
* @author 刘绕兴
* @param menuManager
*/
public void fillMenuBar(IMenuManager menuManager) {
extendMenu = new MenuManager( " 扩展菜单 " );
menuManager.add(extendMenu);
fillMenuBar(perspectiveMap);
fillMenuBar(viewMap);
fillMenuBar(actionMap);
}
/**
* <p> 。</p>
* @author 刘绕兴
* @param map
*/
private void fillMenuBar(Map < String, List < ExtensionInfoBean >> map) {
List < ExtensionInfoBean > list = map.get(ID);
if (list == null || list.size() == 0 )
return ;
Iterator < ExtensionInfoBean > iter = list.iterator();
while (iter.hasNext()) {
ExtensionInfoBean bean = iter.next();
IAction action = bean.getAction();
if (action == null ) {
action = new TheFirstAction();
}
extendMenu.add(action);
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.vwpolo.rcp.extension.client.Activator;
import org.vwpolo.rcp.extension.client.actions.TheFirstAction;
/**
* <p> 。</p>
* @author 刘绕兴
* <p>2008-2-12</p>
*/
public class ExtensionHelper {
/** */
private String ID = ExtensionContances.EXTENSION_ID;
/** */
public static ExtensionHelper instance;
/** Action Map */
private Map < String, List < ExtensionInfoBean >> actionMap = new HashMap < String, List < ExtensionInfoBean >> ();
/** 视图 Map */
private Map < String, List < ExtensionInfoBean >> viewMap = new HashMap < String, List < ExtensionInfoBean >> ();
/** 透视图 Map */
private Map < String, List < ExtensionInfoBean >> perspectiveMap = new HashMap < String, List < ExtensionInfoBean >> ();
/** */
private MenuManager extendMenu;
/**
* 构造函数。
*/
private ExtensionHelper() {
loadExtensions();
}
/**
* <p> 。</p>
* @author 刘绕兴
* @return ExtensionHelper
*/
public static ExtensionHelper getInstance() {
if (instance == null )
instance = new ExtensionHelper();
return instance;
}
/**
* <p> 读取扩展点的属性,然后生成ExtensionInfoBean 对象。</p>
* @author 刘绕兴
*/
private void loadExtensions() {
IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(Activator.EXTENSION_ID);
if (elements == null || elements.length == 0 )
return ;
for ( int i = 0 ; i < elements.length; i ++ ) {
IConfigurationElement element = elements[i];
ExtensionInfoBean bean = new ExtensionInfoBean();
bean.setId(element.getAttribute(ExtensionContances.ATTR_ID));
bean.setName(element.getAttribute(ExtensionContances.ATTR_NAME));
bean.setType(element.getAttribute(ExtensionContances.ATTR_TYPE));
bean.setClientId(element.getAttribute(ExtensionContances.ATTR_CLIENTID));
String index = element.getAttribute(ExtensionContances.ATTR_INDEX);
if (index == null || index.trim().isEmpty()) {
bean.setIndex( 100 );
} else {
bean.setIndex(Integer.parseInt(index));
}
bean.setDesc(element.getAttribute(ExtensionContances.ATTR_DESC));
if (ExtensionContances.TYPE_ACTION.equalsIgnoreCase(bean.getType())) {
try {
String classPath = element.getAttribute(ExtensionContances.ATTR_CLASS);
Object object = null ;
if ( ! classPath.isEmpty()) {
object = element.createExecutableExtension(ExtensionContances.ATTR_CLASS);
} else {
object = element.createExecutableExtension(ExtensionContances.ATTR_CLIENTID);
}
if (object instanceof IAction) {
bean.setAction((IAction) object);
}
} catch (Exception e) {
e.printStackTrace();
}
}
appendGroup(bean);
}
}
/**
* <p> 。</p>
* @author 刘绕兴
* @param bean
*/
private void appendGroup(ExtensionInfoBean bean) {
if (ExtensionContances.TYPE_PERSPECTIVE.equalsIgnoreCase(bean.getType()))
addToMap(perspectiveMap, bean);
else if (ExtensionContances.TYPE_VIEW.equalsIgnoreCase(bean.getType()))
addToMap(viewMap, bean);
else if (ExtensionContances.TYPE_ACTION.equalsIgnoreCase(bean.getType()))
addToMap(actionMap, bean);
}
/**
* <p> 。</p>
* @author 刘绕兴
* @param map
* @param bean
*/
private void addToMap(Map < String, List < ExtensionInfoBean >> map, ExtensionInfoBean bean) {
List < ExtensionInfoBean > list;
if (map.containsKey(ID)) {
list = (List < ExtensionInfoBean > ) map.get(ID);
} else {
list = new ArrayList < ExtensionInfoBean > ();
map.put(ID, list);
}
list.add(bean);
}
/**
* <p> 。</p>
* @author 刘绕兴
* @param menuManager
*/
public void fillMenuBar(IMenuManager menuManager) {
extendMenu = new MenuManager( " 扩展菜单 " );
menuManager.add(extendMenu);
fillMenuBar(perspectiveMap);
fillMenuBar(viewMap);
fillMenuBar(actionMap);
}
/**
* <p> 。</p>
* @author 刘绕兴
* @param map
*/
private void fillMenuBar(Map < String, List < ExtensionInfoBean >> map) {
List < ExtensionInfoBean > list = map.get(ID);
if (list == null || list.size() == 0 )
return ;
Iterator < ExtensionInfoBean > iter = list.iterator();
while (iter.hasNext()) {
ExtensionInfoBean bean = iter.next();
IAction action = bean.getAction();
if (action == null ) {
action = new TheFirstAction();
}
extendMenu.add(action);
}
}
}
ExtensionInfoBean .java
package
org.vwpolo.rcp.extension.client.extender;
import org.eclipse.jface.action.IAction;
/**
* <p>扩展点实体 。</p>
* @author 刘绕兴
* <p>2008-2-12</p>
*/
public class ExtensionInfoBean {
/** */
private String id;
/** */
private String name;
/** */
private String type;
/** */
private String clientId;
/** */
private int index;
/** */
private String desc;
/** */
private IAction action;
/**
* @return id
*/
public String getId() {
return id;
}
/**
* <p>设置id 。</p>
* @param id
*/
public void setId(String id) {
this .id = id;
}
/**
* @return name
*/
public String getName() {
return name;
}
/**
* <p>设置name 。</p>
* @param name
*/
public void setName(String name) {
this .name = name;
}
/**
* @return type
*/
public String getType() {
return type;
}
/**
* <p>设置type 。</p>
* @param type
*/
public void setType(String type) {
this .type = type;
}
/**
* @return clientId
*/
public String getClientId() {
return clientId;
}
/**
* <p>设置clientId 。</p>
* @param clientId
*/
public void setClientId(String clientId) {
this .clientId = clientId;
}
/**
* @return index
*/
public int getIndex() {
return index;
}
/**
* <p>设置index 。</p>
* @param index
*/
public void setIndex( int index) {
this .index = index;
}
/**
* @return desc
*/
public String getDesc() {
return desc;
}
/**
* <p>设置desc 。</p>
* @param desc
*/
public void setDesc(String desc) {
this .desc = desc;
}
/**
* @return action
*/
public IAction getAction() {
return action;
}
/**
* <p>设置action 。</p>
* @param action
*/
public void setAction(IAction action) {
this .action = action;
}
}
import org.eclipse.jface.action.IAction;
/**
* <p>扩展点实体 。</p>
* @author 刘绕兴
* <p>2008-2-12</p>
*/
public class ExtensionInfoBean {
/** */
private String id;
/** */
private String name;
/** */
private String type;
/** */
private String clientId;
/** */
private int index;
/** */
private String desc;
/** */
private IAction action;
/**
* @return id
*/
public String getId() {
return id;
}
/**
* <p>设置id 。</p>
* @param id
*/
public void setId(String id) {
this .id = id;
}
/**
* @return name
*/
public String getName() {
return name;
}
/**
* <p>设置name 。</p>
* @param name
*/
public void setName(String name) {
this .name = name;
}
/**
* @return type
*/
public String getType() {
return type;
}
/**
* <p>设置type 。</p>
* @param type
*/
public void setType(String type) {
this .type = type;
}
/**
* @return clientId
*/
public String getClientId() {
return clientId;
}
/**
* <p>设置clientId 。</p>
* @param clientId
*/
public void setClientId(String clientId) {
this .clientId = clientId;
}
/**
* @return index
*/
public int getIndex() {
return index;
}
/**
* <p>设置index 。</p>
* @param index
*/
public void setIndex( int index) {
this .index = index;
}
/**
* @return desc
*/
public String getDesc() {
return desc;
}
/**
* <p>设置desc 。</p>
* @param desc
*/
public void setDesc(String desc) {
this .desc = desc;
}
/**
* @return action
*/
public IAction getAction() {
return action;
}
/**
* <p>设置action 。</p>
* @param action
*/
public void setAction(IAction action) {
this .action = action;
}
}