在Part4中我们为节点添加了右击菜单,但是使用这种方式会给所有节点都添加菜单。如果我们只想给某种类型的结点添加菜单,就要使用扩展点方式了。
1、首先新建一个Action:在工程org.eclipse.myTest.shapes.diagram中的Actions包中新建一个类ChangeImageAction,并使它继承AbstractActionDelegate类并实现接口IObjectActionDelegate,在这个Action中首先选择图片,然后再给指定结点改变图片,其完整代码如下:
package org.eclipse.myTest.shapes.diagram.edit.actions;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.ui.action.AbstractActionDelegate;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.myTest.shapes.ReserveShape1;
import org.eclipse.myTest.shapes.diagram.edit.parts.ReserveShape1EditPart;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.PlatformUI;
public class ChangeImageAction extends AbstractActionDelegate implements
IObjectActionDelegate {
@Override
protected void doRun(IProgressMonitor progressMonitor) {
// 打开文件对话框,找到要使用的图片
FileDialog fileDlg = new FileDialog(PlatformUI.getWorkbench()
.getDisplay().getActiveShell());
String imagePath = fileDlg.open();
if(imagePath==null)return ;
// 获得所选择的IStructuredSelection
IStructuredSelection selection = getStructuredSelection();
Object selEditPart = selection.getFirstElement();
// 如果选中的是ReserveShape1EditPart类型的EditPart
if (selEditPart instanceof ReserveShape1EditPart) {
ReserveShape1EditPart editPart = (ReserveShape1EditPart) selEditPart;
// 获得模型(此处未用到)
ReserveShape1 model = (ReserveShape1) editPart
.resolveSemanticElement();
// 获得领域模型
TransactionalEditingDomain domain = editPart.getEditingDomain();
// 创建命令
ChangeImageCommand command = new ChangeImageCommand(domain,
editPart, imagePath);
// 执行命令
domain.getCommandStack().execute(command);
}
}
// 改变图片到指定结点的命令
public class ChangeImageCommand extends RecordingCommand {
private ReserveShape1EditPart editPart = null;
private String imagePath = "";
public ChangeImageCommand(
TransactionalEditingDomain transactionalEditingDomain,
ReserveShape1EditPart editPart, String imagePath) {
super(transactionalEditingDomain);
this.editPart = editPart;
this.imagePath = imagePath;
}
@Override
protected void doExecute() {
editPart.changeImageData(imagePath);
editPart.getPrimaryShape().repaint();
}
}
}
2、打开工程org.eclipse.myTest.shapes.diagram下的MANIFEST.MF文件,选择Extentions,在All Extentions中添加扩展点:
在上图点击Add,找到org.eclipse.ui.popupMenus并添加。
3、右击org.eclipse.ui.popupMenus添加objectContribution,并在右边设置其ID为org.eclipse.myTest.shapes.diagram.ReserveShape1EditPart,objectClass选择我们想要操作的EditPart,这里是org.eclipse.myTest.shapes.diagram.edit.parts.ReserveShape1EditPart。
4、右击上边添加的objectContribution,添加action,在右边设置其ID与class都是org.eclipse.myTest.shapes.diagram.edit.actions.ChangeImageAction,其中class就是选择执行这个菜单项时的Action。
label设置为Change Image,并设置这个菜单的小图标(事先已经在工程org.eclipse.myTest.shapes.diagram下的icons\custom目录下添加了小图标文件ChangeImageAction.png)。完成后如下:
5、在ReserveShape1EditPart中添加方法:
/**
* 修改OrgImageData
*/
public void changeImageData(String imagePath) {
OrgImageData = new ImageData(imagePath);
}
6、运行截图:
代码下载:http://www.oschina.net/code/snippet_164134_6488
来源:oschina
链接:https://my.oschina.net/u/164134/blog/32958