DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Commands Part 1: Actions Vs Commands

Commands Part 1: Actions Vs Commands

Prakash  user avatar by
Prakash
·
Jan. 05, 09 · Java Zone · Interview
Like (0)
Save
Tweet
8.62K Views

Join the DZone community and get the full member experience.

Join For Free

As you would have seen, there are two different ways to contributing to the Workbench: Actions and Commands. Although Commands are newer and advanced, I've always preferred using Actions, simply because of my comfort level in using them. Now that I've started fixing some bugs in the Command framework, I'm forced to look into the details. The more deeper I look into the Commands, the more I'm loving it. So I decided to write a series of on the Commands and this is the first one in the series. Many of the information presented here is obtained by looking some old bugs, wiki and digging into CVS history. If I'm missing anything or wrong about something let me know.

Lets start with Actions. We are able to contribute to the menus, toolbars, pull down menu, etc. We are able to specify the UI details like label or tooltip in the plugin.xml itself, which helps in lazy loading. So whats wrong with them?

  • The UI and handling are always tied. There is no way you can separate each other
  • While Actions can be contributed to different parts of the workbench (popup menu/tool bar), all of them were different extension points and so you end up duplicating the XML in multiple places. The worst of it is that not all the extension points expect the same configuration.
  • Specifying Actions in multiple places is a maintenance nightmare. If you have to change the icon of an action, you need to change in all the places.
  • Another issue with duplicating Actions in plugin.xml is that multiple instance of the same Actions will be created in the memory

Lets see how the Commands Framework eliminates all this. Commands are defined by the org.eclipse.ui.commands extension point. A typical command would look like:

<command 
         id="com.eclipse-tips.commands.someCommand" 
         name="Some Command"> 
</command>

 

Yeah thats it. That defines a Command! If you want to place this in a tool bar, you have to use the extension point org.eclipse.ui.menus:

<menuContribution 
      locationURI="toolbar:org.eclipse.ui.main.toolbar"> 
   <toolbar 
         id="com.eclipse-tips.commands.toolbar1"> 
      <command 
            commandId="com.eclipse-tips.commands.someCommand" 
            id="com.eclipse-tips.commands.someCommandInToolBar"> 
      </command> 
   </toolbar> 
</menuContribution> 

 

Now that takes of the placement in the UI. But what about the code that gets executed? That goes into a another extension point, org.eclipse.ui.handlers:

<handler 
         class="com.eclipse_tips.commads.SomeCommandHandler" 
         commandId="com.eclipse-tips.commands.someCommand"> 
</handler> 


One last piece is to add an icon to the Command
No price for guessing that you need to use another extension point, org.eclipse.ui.commandImages:

<image 
         commandId="com.eclipse-tips.commands.someCommand" 
         icon="icons/sample.gif"> 
</image> 



All set. Lets see the Command in action:
image
As you see, in the actions we would just use only one extension point, instead of the four which we have used now. Does it sounds like a round about way or doing things? Probably once we get to know about multiple handlers for a Command and context based association & enablement of the handlers etc, we will see the beauty of the framework. That would be the next in this series.

From http://blog.eclipse-tips.com

Command (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Major PostgreSQL Features You Should Know About
  • Java’s Encapsulation - When the Getter and Setter Became Your Enemy
  • The Power of Enum: Make Your Code More Readable and Efficient [Video]
  • 8 Must-Have Project Reports You Can Use Today

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo