Chinaunix首页 | 论坛 | 博客
  • 博客访问: 516107
  • 博文数量: 95
  • 博客积分: 5168
  • 博客等级: 大校
  • 技术积分: 1271
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-28 23:31
文章分类

全部博文(95)

文章存档

2013年(2)

2012年(3)

2011年(1)

2010年(8)

2009年(81)

分类:

2009-08-04 23:31:32

 

-----------------------------------------------------------
本文系本站原创,欢迎转载!

转载请注明出处:http://sjj0412.cublog.cn/

-----------------------------------------------------------

下面来谈下各种action

所有的action实现于Iaction接口。

Iworkbenchaction多一个dispose接口。

Actionfatory createaction实现WorkbenchCommandAction比如exit等接口。

一般通过:

WorkbenchCommandAction action =new WorkbenchCommandAction("org.eclipse.ui.file.exit", window); // org.eclipse.ui.file.exit是命令

private static class WorkbenchCommandAction extends CommandAction implements IWorkbenchAction

CommandAction extends Action

多了一个initrunwithevent函数。

protected void init(IServiceLocator serviceLocator, String commandIdIn,

           Map parameterMap) {

       if (handlerService != null) {

           // already initialized

           return;

       }

       handlerService = (IHandlerService) serviceLocator

              .getService(IHandlerService.class);

       ICommandService commandService = (ICommandService) serviceLocator

              .getService(ICommandService.class);

       ICommandImageService commandImageService = (ICommandImageService) serviceLocator

              .getService(ICommandImageService.class);

 

       createCommand(commandService, commandIdIn, parameterMap);

       if (parameterizedCommand != null) {

           setId(parameterizedCommand.getId());

           setActionDefinitionId(parameterizedCommand.getId());

           try {

              setText(parameterizedCommand.getName());

           } catch (NotDefinedException e) {

              // if we get this far it shouldn't be a problem

           }

           parameterizedCommand.getCommand().addCommandListener(

                  getCommandListener());

           parameterizedCommand.getCommand().setEnabled(

                  handlerService.getCurrentState());

           setEnabled(parameterizedCommand.getCommand().isEnabled());

           setImageDescriptor(commandImageService.getImageDescriptor(

                  commandIdIn, ICommandImageService.TYPE_DEFAULT));

           setDisabledImageDescriptor(commandImageService.getImageDescriptor(

                  commandIdIn, ICommandImageService.TYPE_DISABLED));

           setHoverImageDescriptor(commandImageService.getImageDescriptor(

                  commandIdIn, ICommandImageService.TYPE_HOVER));

       }

    }

 

Run会执行runWithEvent

    public void runWithEvent(Event event) {

       if (handlerService == null) {

           String commandId = (parameterizedCommand == null ? "unknownCommand" //$NON-NLS-1$

                  : parameterizedCommand.getId());//命令

           WorkbenchPlugin.log("Cannot run " + commandId //$NON-NLS-1$

                  + " before command action has been initialized"); //$NON-NLS-1$

           return;

       }

       try {

           if (parameterizedCommand != null) {

              handlerService.executeCommand(parameterizedCommand, event);

           }

       } catch (Exception e) {

           WorkbenchPlugin.log(e);

       }

    }

 

 

ImportResourcesAction,ExportResourcesActionresource相关的action都实现

BaseSelectionListenerAction

 

public class ExportResourcesAction extends BaseSelectionListenerAction

        implements ActionFactory.IWorkbenchAction {

即多BaseSelectionListenerActionz这个类就多了selectionChanged函数。

 

public void runWithEvent(Event event) {

        // Set the running flag during the run so that selection changes are deferred.

        // See selectionChanged(IStructuredSelection) for more details.

        running = true;

        try {

            run();

        } finally {

            running = false;

            IStructuredSelection s = deferredSelection;

            deferredSelection = null;

            if (s != null) {

                selectionChanged(s);

            }

        }

}

 

iActionaddPropertyChangeListener

AbstractAction extends EventManager implements IAction

Action继承abstractaction

Iworkbenchaction多了一个dispose函数。

actionContext保存了selection的内容。

OpenActionGroupcustomactiongroup一样都是继承ResourceNavigatorActionGroup,他是抽象的,必须继承

actionGroup就是用来创建很多action的接口(通过iActionBar参数,这个可以获得menumanger,ToolBarManger)。

这个,这个用来调用fillmenuccontext

ResourceNavigatorActionGroup多了一个makeActions(),同时多了与resouce有关的函数getImageDescriptorNavigator

Makeaction用来创建一些action,然后fill的时候使用,故makeaction通常在构造函数里调用。

比如OpenActionGroup(这个是navigator里的open菜单,它根据选择的文件然后现出各种编辑器选项供打开选择):

public void fillContextMenu(IMenuManager menu) {

        IStructuredSelection selection = (IStructuredSelection) getContext()

                .getSelection();

 

        boolean anyResourceSelected = !selection.isEmpty()

                && ResourceSelectionUtil.allResourcesAreOfType(selection,

                        IResource.PROJECT | IResource.FOLDER | IResource.FILE);

        boolean onlyFilesSelected = !selection.isEmpty()

                && ResourceSelectionUtil.allResourcesAreOfType(selection,

                        IResource.FILE);

 

        if (onlyFilesSelected) {

//选择文件时

            openFileAction.selectionChanged(selection);

            menu.add(openFileAction);

            fillOpenWithMenu(menu, selection);

        }

 

        if (anyResourceSelected) {

            addNewWindowAction(menu, selection);

        }

}

   private void fillOpenWithMenu(IMenuManager menu,

            IStructuredSelection selection) {

 

        // Only supported if exactly one file is selected.

        if (selection.size() != 1) {

           return;

       }

        Object element = selection.getFirstElement();

        if (!(element instanceof IFile)) {

           return;

       }

 

        MenuManager submenu = new MenuManager(ResourceNavigatorMessages.ResourceNavigator_openWith, OPEN_WITH_ID);

        submenu.add(new OpenWithMenu(navigator.getSite().getPage(),

                (IFile) element));

        menu.add(submenu);

    }

fillOpenWithMenu显示各种可供打开的编辑器。

这里的openFileActionOpenWithMenu就是上面提到都和

ActionContext可以获得iselection

 

public class ActionContext {

 

    /**

     * The selection.

     */

    private ISelection selection;

 

    /**

     * The input element.

     */

    private Object input;

 

    /**

     * Creates a new action context with the given selection.

     */

    public ActionContext(ISelection selection) {

        setSelection(selection);

    }

 

    /**

     * Returns the selection.

     */

    public ISelection getSelection() {

        return selection;

    }

 

    /**

     * Sets the selection.

     */

    public void setSelection(ISelection selection) {

        this.selection = selection;

    }

 

    /**

     * Returns the input element.

     */

    public Object getInput() {

        return input;

    }

 

    /**

     * Sets the input element.

     *

     * @param input

     */

    public void setInput(Object input) {

        this.input = input;

    }

}

UndoAction:

然后是刚才前面有的UndoAction

这个undoaction要有editpart参数,因为它执行返回操作肯定是要执行具体编辑器的返回操作。

public class UndoAction

    extends StackAction

{

 

public void run() {

    getCommandStack().undo();

}

protected CommandStack getCommandStack() {

    return (CommandStack)getWorkbenchPart().getAdapter(CommandStack.class);

}

可见他执行的其实是editorpartCommandStack.undo.

 

阅读(1297) | 评论(0) | 转发(1) |
0

上一篇:Eclipse菜单探密(1)

下一篇:gef图形显示机制

给主人留下些什么吧!~~