Thursday, March 29, 2012

Commands in Eclipse: some advanced patterns


This presentation was about the commands framework in Eclipse.  While titled as advanced, I found that most of the patterns were things that I've already came across, and solved.

Commands/Handlers:

  • Command provides enabled state, and any arbitrary state
  • Handler controls commands enabled state
    • Can be global or local (based on state)
  • Keybindings can point to different handlers base on state
  • Menus and toolbars expose commands to user visually
  • Programatically done through Eclipse services (not OSGi services)
Patterns:
  • Using a parameter:
    • Static string key/value pair
    • Used to change behaviour of handler
    • Can support other types, via parameter convertor
  • Toggle
    • Handler's duty to change command state
    • HandlerUtil.toggleCommandState()
  • Radio
    • Command needs parameter for state, managed by handler
  • Tool item drop down
    • Usually a command with parameter, filled by menu ID
  • Dynamic sections in menu
    • Command contribution items
  • Operating on selection
    • Use core expressions
    • Use core expressions with a property tester for more complicated tests
  • Control in toolbar
    • Contribution filling in a composite, has to handle orientation, not part of a part
  • Property tester
    • Used for complicated tests done in code
    • Best to try to avoid these since plug-in needs to be loaded for them to work
    • API to cause reevaluation
    • Use handler services to update application state
  • Use services to access commands and handlers from platform; don't try to access them directly.
  • Don't call execute commands/handlers directly, instead invoke them via services

Commands in E4 and 3 are very similar.

No comments:

Post a Comment