首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

Eclipse的Command施用教程(二)

2012-10-29 
Eclipse的Command应用教程(二)1.?Eclipse CommandsFor an introduction into Eclipse Commands please see

Eclipse的Command应用教程(二)
1.?Eclipse Commands

For an introduction into Eclipse Commands please see Eclipse Commands Tutorial .

2.? Enabled when (visible when)

The command framework allows to restrict the availability and visibility of commands, handlers and ui contributions via the core expressions .

In this example we want the command only be enabled if one item from a list of items is selected.

Create a new project "de.vogella.rcp.commands.enable" based on the "RCP application with a view" example.

Add a command with the ID "de.vogella.rcp.commands.enable.command". Add this command to the menu and the toolbar and create the following default handler.

?

@Override"Info", "Info for you");

Using right mouse click add a definition "oneElementSelected". Add a "with" variable "selection".

?

?

On the "activeMenuSelection" right mouse click and add a count with the value "1". The result should look like

?

?

On your handler, right mouse click and select enabledWhen. Right mouse click and add a "reference" to it with the value "oneElementSelected". The result should look like.

?

?

The expression were are using is based on "selection". Therefore the list must register itself as selection provider to inform the workbench in case something is selected. Therefore add "getSite().setSelectionProvider(viewer);" in createPartControl of View.java.

?

// Makes the selection available to the workbenchgetSite().setSelectionProvider(viewer);}

?

If you now run the application your command should only be enabled if one element in the list is selected.

?

?

?

<script type="text/javascript"></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

3.?Eclipse standard commands

The advantage of using standard commands is that you get the keybinding, icons, etc for free.

?

?

?

// Method belongs to class ApplicationActionBarAdvisor@Override

?

?

Create a new RCP application "de.vogella.rcp.commands.standardcommands" with the template "Hello RCP".

Create a handler for the command "org.eclipse.ui.edit.delete" which display a message box. Add the command "org.eclipse.ui.edit.delete" to the menu.

?

?

?

?

The result should look like the following.

?

?

4.?Calling commands directly via code

You can call a command directly. For example if you have a command "add.command" defined you can call it via the following coding.

?

// From a view you get the site which allow to get the serviceIHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService."add.command", null);} "add.command not found");// Give message}}

?

This coding above is called within a view which provides access to the site and to services. You can also get the service via the WorkbenchWindow.

5.?Providing your own expression

You can write your own Java class which can be used define a variable which can then be used to define if a certain UI element shall be active or not. For example you can read the authorization of the user from your own class and then return the value which is assign to the user. This return value can then be used in a visible / enabled statement. This is similar to the usage of the core expressions we have seen earlier.

Create a RCP application "de.vogella.rcp.commands.sourceprovider" with the template "Hello RCP".

We will create a source provider. This source provider can provide variables which can be used in defining the visibility of commands. Add the extension point "org.eclipse.ui.services" to your plugin and create a new service provider.

?

?

?

?

This defines a service which provides the variable "de.vogella.rcp.commands.sourceprovider.active" to the workbench. This class must implement ISourceProvider. This value can then be used similar to a core expression. Maintain the following code.

?

"de.vogella.rcp.commands.sourceprovider.active";"ENABLED";"DISENABLED";@Override// We could return several values but for this example one value is sufficient@Override// You cannot return NULL@SuppressWarnings("unchecked")@Override// This method can be used from other commands to change the state// Most likely you would use a setter to define directly the state and not use this toogle method// But hey, this works well for my example

?

@Override// Get the source provider serviceISourceProviderService sourceProviderService = (ISourceProviderService) HandlerUtil.getActiveWorkbenchWindow(event).getService(ISourceProviderService.// Now get my serviceCommandState commandStateService = (CommandState) sourceProviderService.getSourceProvider(CommandState.MY_STATE);commandStateService.toogleEnabled();

?

This looks in plugin.xml like the following.

?

"org.eclipse.ui.menus">      <menuContribution            locationURI="menu:org.eclipse.ui.main.menu">         <command               commandId="de.vogella.rcp.commands.sourceprovider.command1"               label="Command1"               style="push">            <visibleWhen                  checkEnabled="false">               <with                     variable="de.vogella.rcp.commands.sourceprovider.active">                  <equals                        value="ENABLED">                  </equals>               </with>            </visibleWhen>         </command>         <command               commandId="de.vogella.rcp.commands.sourceprovider.command2"               label="Command2"               style="push">         </command>      </menuContribution></extension>

?

If you now start your application can select the second command the first command will not be available anymore. If you press it again the first command will be displayed again.

6.?Using parameters in commands

You can also define parameters in commands. Create project "de.vogella.rcp.commands.parameterfirst" using the "Hello RCP" template.

Create a command with the id "de.vogella.rcp.commands.parameterfirst.helloName" and a default handler "de.vogella.rcp.commands.parameterfirst.handler.HelloName".

Right click on your command, select New -> commandParameter

?

?

Use the following id for the parameter "de.vogella.rcp.commands.parameterfirst.commandParameter1"

?

?

In the handler you have to evaluate the parameter.

?

@Override"de.vogella.rcp.commands.parameterfirst.commandParameter1");MessageDialog.openInformation(HandlerUtil.getActiveShell(event),"Hello", "Hello " + name);

?

As name maintain the name of the parameter, pass as value the value you want to use in your handler.

?

?

Add the same command again to the menu and pass another parameter.

If you run your application and select the menu entry the value should be used in the message box.

7.?Defining commands at runtime

You can create commands at runtime. Create project "de.vogella.rcp.commands.runtimecommands" using the "Hello RCP" template.

Define a menu contribution. Maintain the class "de.vogella.rcp.commands.runtimecommands.DefineCommands" in this menu contribution.

?

?

Create the following class.

?

@Override"","org.eclipse.ui.file.exit",SWT.PUSH);p.label = "Exit the application";p.icon = Activator.getImageDescriptor("icons/alt_window_16.gif");CommandContributionItem item = new CommandContributionItem(p);item.setVisible(true);additions.addContributionItem(item, null);}}

?

Run the example, your application should have the Exit command in the menu.

热点排行