Monday, June 25, 2007

IAction and Runnable

Does anybody know why org.eclipse.jface.IAction does not explicitly implement java.lang.Runnable interface?
I have always interpreted the IAction interface as "decorated Runnable" and I think there may be certain use cases where you might want your actions implement java.lang.Runnable.
Why is it like that? It seems that I miss some point here...

7 comments:

Michael said...

What prevents you from implementing java.lang.Runnable in your actions?

Ilya Shinkarenko said...

Well, nothing, of course.
Interesting is what was the idea behind not declaring Runnable explicitly in IAction.

Antoine said...

I think Actions were around before Runnable classes. The Runnable class is supposed to be executed into an environment, most of the time a ProgressMonitorDialog with the IRunnableWithProgress, or directly, when calling the UI thread to run something:

PlatformUI.getWorkbench().getDisplay().syncExec(runnable);

An action is not handled in the same way as most of the time, it is declared through extension points or added to action bars.

In the rare cases where the two worlds intersect, you can follow Michael's advice.

Ilya Shinkarenko said...

antoine,

I don't speak about IRunnableWithProgress and action delegates.

I just try to understand why Eclipse designers have not declared the fundamental java.lang.Runnable while declaring the IAction interface. Both have exactly the same key method: void run().

Was it just hate to Sun ;-) or was there some reasonable design idea behind?

Robert Konigsberg said...

Probably because it really isn't.

Runnable is a class designed to run for an object's lifetime, while IAction is designed to execute behavior associated with a UI command.

If IAction is a Runnable, then all IActions need to reasonably be thrown into Threads, and there's probably no reason to put that kind of constraint on IAction.

That's my guess.

Boris Bokowski said...

You are right, the method signature for run() is exactly the same in Runnable and IAction. However, if you look at the Javadoc, you will see that IAction and Runnable serve different purposes, and an IAction cannot be treated as (just) a Runnable - for example, actions usually don't run in a separate thread. Because API is made up of public methods/interfaces/classes and their Javadoc specification, making IAction extend Runnable would be a source of confusion.

I also don't see how declaring IAction as a subtype of Runnable would help clients. Actions are meant to be executed by the framework in response to UI events such as selecting a menu item or pressing a button. Clients usually subclass Action (which implements IAction) but don't manage action objects themselves or call their run methods.

Anonymous said...

Good post and this post helped me alot in my college assignement. Gratefulness you on your information.