如何将整个工具栏项传递给键绑定?

时间:2017-01-31 10:56:31

标签: eclipse-rcp toolbar

我有一个在Windows平台上运行的RCP应用程序。在这个应用程序中有很多命令在工具栏中,所以我想分配一个快捷键 一个一个地打开工具栏怎么做?

我使用了键盘命名概念,但工具栏项目=快捷键发生了,所以我只想要一个键然后如何做到Image

2 个答案:

答案 0 :(得分:0)

你不能这样做。键绑定始终绑定到单个命令。

如果你想按顺序做事,可以使用带有多个页面的向导或类似的东西。

使用cheat sheet引导用户完成操作可能是另一种方式。

答案 1 :(得分:0)

我这样做但方式不同。整个工具栏项通过处理程序传递。我可以分享你的代码:

正向编辑代码:

package handler;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
import rcp_demo.Editor.EmployeeEditor;
import rcp_demo.Editor.EmployeeEditorInput;
import rcp_demo.Editor.EnquiryEditor;
import rcp_demo.Editor.EnquiryEditorInput;
import rcp_demo.Editor.FoodDetailsEditor;
import rcp_demo.Editor.FoodDetailsEditorInput;
import rcp_demo.Editor.StaffdetailsEditor;
import rcp_demo.Editor.StaffdetailsEditorInput;
//Defauklt Editor ID
import rcp_demo.Editor.UserEditor;
import rcp_demo.Editor.UserEditorInput;

/**
 * @author summet
 * 
 */
public class SwitchingHandler extends AbstractHandler {

/*
 * (non-Javadoc)
 * 
 * @see
 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
 * ExecutionEvent)
 */

String tempEditorID = null;

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    System.out.println("SwitchingHandler command call");
    // TODO Auto-generated method stub
    // HandlerUtil.getActiveWorkbenchWindow
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
    IWorkbenchPage page = window.getActivePage();
    IEditorReference[] editorRefs = page.getEditorReferences();
    // get the editor instance by given id (pEditorId)

    if (page.getActiveEditor() == null || page.getActiveEditor().equals("")) {
        // Default Editor Open via command
        UserEditorInput input = new UserEditorInput();
        try {
            page.openEditor(input, UserEditor.ID);
        } catch (PartInitException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    } else {

        // First check Temp ID;
        String tempID = checkActiveEditorID(page);
        if (tempID.equals(UserEditor.BID)) {
            System.out.println("You are in UserEditor.ID");
            page.closeAllEditors(true);

            EmployeeEditorInput einput = new EmployeeEditorInput();
            try {
                page.closeAllEditors(true);
                page.openEditor(einput, EmployeeEditor.ID);
                tempID = null;
            } catch (PartInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } else if (tempID.equals(EmployeeEditor.BID)) {
            System.out.println("You are in EmployeeEditor.ID");

            StaffdetailsEditorInput sinput = new StaffdetailsEditorInput();
            try {
                page.closeAllEditors(true);
                page.openEditor(sinput, StaffdetailsEditor.ID);
                tempID = null;
            } catch (PartInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if (tempID.equals(StaffdetailsEditor.BID)) {
            System.out.println("You are in StaffdetailsEditor.ID");
            FoodDetailsEditorInput finput = new FoodDetailsEditorInput();
            try {
                page.closeAllEditors(true);
                page.openEditor(finput, FoodDetailsEditor.ID);
                tempID = null;
            } catch (PartInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if (tempID.equals(FoodDetailsEditor.BID)) {
            System.out.println("You are in FoodDetailsEditor.ID");
            EnquiryEditorInput eeinput = new EnquiryEditorInput();
            try {
                page.closeAllEditors(true);
                page.openEditor(eeinput, EnquiryEditor.ID);
                tempID = null;
            } catch (PartInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if (tempID.equals(EnquiryEditor.BID)) {
            System.out.println("You are in EnquiryEditor.ID");

            UserEditorInput uinput = new UserEditorInput();
            try {
                page.closeAllEditors(true);
                page.openEditor(uinput, UserEditor.ID);
                tempID = null;
            } catch (PartInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


    }

    return null;
}

private String checkActiveEditorID(IWorkbenchPage page) {

    tempEditorID = page.getActiveEditor().getTitle();
    return tempEditorID;
    // TODO Auto-generated method stub

}
}

后退编辑代码:

 package handler;
 import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
import rcp_demo.Editor.EmployeeEditor;
import rcp_demo.Editor.EmployeeEditorInput;
import rcp_demo.Editor.EnquiryEditor;
import rcp_demo.Editor.EnquiryEditorInput;
import rcp_demo.Editor.FoodDetailsEditor;
import rcp_demo.Editor.FoodDetailsEditorInput;
import rcp_demo.Editor.StaffdetailsEditor;
import rcp_demo.Editor.StaffdetailsEditorInput;
import rcp_demo.Editor.UserEditor;
import rcp_demo.Editor.UserEditorInput;
public class BackEditorHandler extends AbstractHandler {

/*
 * (non-Javadoc)
 * 
 * @see
 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
 * ExecutionEvent)
 */

String tempBackEditorID = null;

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    System.out.println("BackEditorHandler  call");
    // TODO Auto-generated method stub

    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
    IWorkbenchPage page = window.getActivePage();
    IEditorReference[] editorRefs = page.getEditorReferences();
    // get the editor instance by given id (pEditorId)

    if (page.getActiveEditor() == null || page.getActiveEditor().equals("")) {
        // Default Editor Open via command
        UserEditorInput input = new UserEditorInput();
        try {
            page.openEditor(input, UserEditor.ID);
        } catch (PartInitException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    } else {
        // First check Temp ID;
        String tempBackID = checkActiveEditorID(page);
        System.out.println("tempID:--" + tempBackID);

        if (tempBackID.equals(UserEditor.BID)) {
            System.out.println("You are in UserEditor.ID");
            page.closeAllEditors(true);
            EnquiryEditorInput eeinput = new EnquiryEditorInput();
            try {
                page.closeAllEditors(true);
                page.openEditor(eeinput, EnquiryEditor.ID);
                tempBackID = null;
            } catch (PartInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } else if (tempBackID.equals(EmployeeEditor.BID)) {
            System.out.println("You are in EmployeeEditor.ID");
            UserEditorInput uinput = new UserEditorInput();
            try {
                page.closeAllEditors(true);
                page.openEditor(uinput, UserEditor.ID);
                tempBackID = null;
            } catch (PartInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if (tempBackID.equals(StaffdetailsEditor.BID)) {
            System.out.println("You are in StaffdetailsEditor.ID");
            EmployeeEditorInput einput = new EmployeeEditorInput();
            try {
                page.closeAllEditors(true);
                page.openEditor(einput, EmployeeEditor.ID);
                tempBackID = null;
            } catch (PartInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if (tempBackID.equals(FoodDetailsEditor.BID)) {
            System.out.println("You are in FoodDetailsEditor.ID");
            StaffdetailsEditorInput sinput = new StaffdetailsEditorInput();
            try {
                page.closeAllEditors(true);
                page.openEditor(sinput, StaffdetailsEditor.ID);
                tempBackID = null;
            } catch (PartInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if (tempBackID.equals(EnquiryEditor.BID)) {
            System.out.println("You are in EnquiryEditor.ID");
            FoodDetailsEditorInput finput = new FoodDetailsEditorInput();
            try {
                page.closeAllEditors(true);
                page.openEditor(finput, FoodDetailsEditor.ID);
                tempBackID = null;
            } catch (PartInitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    return null;
}

private String checkActiveEditorID(IWorkbenchPage page) {

    tempBackEditorID = page.getActiveEditor().getTitle();
    return tempBackEditorID;
    // TODO Auto-generated method stub

  }
  }