GMF之Shapes實例Part5:使用擴展點爲指定類型結點添加右擊菜單並改變圖片

在Part4中咱們爲節點添加了右擊菜單,可是使用這種方式會給全部節點都添加菜單。若是咱們只想給某種類型的結點添加菜單,就要使用擴展點方式了。dom

一、首先新建一個Action:在工程org.eclipse.myTest.shapes.diagram中的Actions包中新建一個類ChangeImageAction,並使它繼承AbstractActionDelegate類並實現接口IObjectActionDelegate,在這個Action中首先選擇圖片,而後再給指定結點改變圖片,其完整代碼以下:eclipse

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();
        }
    }
}

 二、打開工程org.eclipse.myTest.shapes.diagram下的MANIFEST.MF文件,選擇Extentions,在All Extentions中添加擴展點:ide

在上圖點擊Add,找到org.eclipse.ui.popupMenus並添加。ui

三、右擊org.eclipse.ui.popupMenus添加objectContribution,並在右邊設置其ID爲org.eclipse.myTest.shapes.diagram.ReserveShape1EditPart,objectClass選擇咱們想要操做的EditPart,這裏是org.eclipse.myTest.shapes.diagram.edit.parts.ReserveShape1EditPart。this

四、右擊上邊添加的objectContribution,添加action,在右邊設置其ID與class都是org.eclipse.myTest.shapes.diagram.edit.actions.ChangeImageAction,其中class就是選擇執行這個菜單項時的Action。.net

label設置爲Change Image,並設置這個菜單的小圖標(事先已經在工程org.eclipse.myTest.shapes.diagram下的icons\custom目錄下添加了小圖標文件ChangeImageAction.png)。完成後以下:code

五、在ReserveShape1EditPart中添加方法:orm

/**
 * 修改OrgImageData
 */
public void changeImageData(String imagePath) {
    OrgImageData = new ImageData(imagePath);
}

六、運行截圖:繼承

 代碼下載:http://www.oschina.net/code/snippet_164134_6488接口

相關文章
相關標籤/搜索