Time for action: passing command parameters

Displaying a message to System.out shows that the command works, but what if the command needed to pick up local state? Fortunately, the @Named and @Inject annotations allow objects to be injected into the method when it is called.

  1. Modify the hello method so that instead of printing a message to System.out, it opens a dialog window, using the active shell:
    public void hello(@Named(IServiceConstants.ACTIVE_SHELL) Shell s){
      MessageDialog.openInformation(s, "Hello World",
        "Welcome to Eclipse 4 technology");
    }
  2. Other arguments can be passed in from the context, managed by the IEclipseContext interface. For example, using the math.random function from earlier, a value could be injected into the handler:
    public void hello(@Named(IServiceConstants.ACTIVE_SHELL) Shell s,
      @Named("math.random") double value) {
  3. If the same handler is being used for different functions (for example, Paste and Paste Special), they can be disambiguated by passing in a hard-coded value in the command. Modify the helloCommand to add a parameter, by opening the Application.e4xmi and navigating to the Application | Commands | helloCommand; right-click and go to Add child | Command Parameter to add a command parameter:
    1. ID: com.packtpub.e4.application.commandparameter.hello.value
    2. Name: hello.value
    3. Ensure that the Optional checkbox is selected:
    Time for action: passing command parameters
  4. To pass a value into the command, open the Application.e4xmi and navigate to the Application | Windows and Dialogs | Main Menu | Menu (File) | Handled Menu Item (Hello) | Parameters node. Right-click on Parameters and choose Add child | Parameter to create a parameter:
    1. ID: com.packtpub.e4.application.parameter.hello.value
    2. Name: com.packtpub.e4.application.commandparameter.hello.value
    3. Value: Hello World Parameter
    Time for action: passing command parameters
  5. Finally, modify the command handler so that it receives the value encoded with the handler:
    public void hello(@Named(IServiceConstants.ACTIVE_SHELL) Shell s,
      @Optional
      @Named("com.packtpub.e4.application.commandparameter.hello.value")
       String hello,
      @Named("math.random") double value) {
        MessageDialog.openInformation(s, "Hello World", hello + value);
    }
  6. Run the application, and go to the File | Hello menu. The parameter will be passed in to the handler.

What just happened?

Any values can be injected into the method when it is invoked, provided that they are available in the context when the handler is called. These can be taken from standard constants (such as those in IServiceConstants) or be custom values injected at runtime. Other values include:

  • ACTIVE_WINDOW: The currently displayed window
  • ACTIVE_PART: The currently selected part
  • ACTIVE_SELECTION: The current selection

If the values are one of a set of values, they can be encoded in the menu or other command invocation. They can also be set via code which calls IEclipseContext.set() with an appropriate value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.242.165