How do you effectively control commands, menus and handlers in a large pluggable application to get a consistent use of the commands across the complete application? This is definitely one of the areas where the Eclipse IDE is not a good sample application. Just notice how all team providers add commands with the same text and function and how multiple identical menu items some times show up...
Here the Adapter framework of Eclipse can be a big help if used properly.
While the Adapter framework arguably is one of the more difficult to understand and use properly, the is also a framework that will solve many of the problems you might face is larger non-trivial applications. Especially when used in conjunction with the menus extension point and handlers. This presentation will show how to use the adapter framework of the Eclipse framework with special emphasis on the menus and handlers.
EclipseCon '11 - Using Adapters to Handle Menus and Handlers in Large Scale Applications
1. Using Adapters to Handle Menus and Handlers in
Large Scale Applications
How do you effectively control commands, menus and handlers in a large pluggable
application to get a consistent use of the commands across the complete application?
This is definitely one of the areas where the Eclipse IDE is not a good sample application.
Just notice how all team providers add commands with the same text and function and
how multiple identical menu items some times show up...
Here the Adapter framework of Eclipse can be a big help if used properly.
While the Adapter framework arguably is one of the more difficult to understand and use
properly, the is also a framework that will solve many of the problems you might face is
larger non-trivial applications. Especially when used in conjunction with the menus
extension point and handlers. This presentation will show how to use the adapter
framework of the Eclipse framework with special emphasis on the menus and handlers.
Redistribution and other use of this material requires written permission from The RCP Company.
L0001 - 2010-11-27
2. The Agenda
The problem with commands and
handlers in large Eclipse based
applications
The Adapter framework of Eclipse
How to use the Adapter framework to
solve the problem with focus on the
actual code used
2
L0001 - 2010-11-27
3. Complexity of Handler Expressions
Expressions for handlers (activeWhen and
enabledWhen) are often difficult to get
correct
If the expression depends on internal
state a property tester is needed
The flow is more complicated
The tests get more involved
3
L0001 - 2010-11-27
4. The Problems
If we want to implement a specific
command for a large number of different
domain objects, then a large portion of
the code for the handles will be identical
E.g. handlers for “cut”, “print”, connect”,
etc
4
L0001 - 2010-11-27
5. The Problems
Multiple - basically identical - commands
In large enterprise applications, this is
almost impossible to manage
How about key bindings?
5
L0001 - 2010-11-27
6. The Idea
Given
a specific object in the domain
model (here a customer Benjamin”)
a command, we like to perform on
the object (here “Show Segment”)
...use the Adapter Manager of Eclipse to
get the type specific behavior for the
object
If no specific behavior can be found, the
command is not active
Use the same method in the generic
handler
6
L0001 - 2010-11-27
7. Why Adapters?
UI Concerns
Label Provider
Domain Model
Contact
Tree Providers
Content Providers
Segment
Customer
Domain Model
Contract Functionality
Edit
Print
Offer
Price Item
Call
Save
7
L0001 - 2010-11-27
8. Why Adapters?
UI Concerns
Label Provider
Domain Model
}
Contact
Tree Providers
Content Providers
Segment
Customer
Manager
Domain Model
Contract Functionality
Edit
Print
Offer
Price Item
Call
Save
8
L0001 - 2010-11-27
9. Adding Adapters
Implement the interface directly
Sub-class IAdaptable
Declare using org.eclipse.core.runtime.adapters
Register directly at run-time
<extension point="org.eclipse.core.runtime.adapters">
<factory
adaptableType="com.rcpcompany.ex.adapterhandlers.model.Segment"
class="com.rcpcompany.ex.adapterhandlers.adapters.SegmentAdapterFactory">
<adapter type="com.rcpcompany.ex.adapterhandlers.functionality.IDoShowSegment" />
</factory>
</extension>
9
L0001 - 2010-11-27
11. A Few Closing Remarks
The way the adapt expression works, it is best to place the the adapter in
the same plug-in as the domain model - see BugZilla #82973 for a long and
complicated description for the background
The adapt expression is going to be invoked often, so use stateless adapter
object rather than stateful adapters to the same adapter object can be re-
used
Put the interfaces for the relevant functionality - e.g. IDoShowSegment - in a
separate plug-in along with the declaration of the same commands
11
L0001 - 2010-11-27
12. Summing Up
All handlers will have almost identical
12
L0001 - 2010-11-27
13. Resources
This presentation is based on the same outline as the following blog entry
http://blog.rcp-company.com/2010/02/using-adapters-in-handlers-to-
control.html
13
L0001 - 2010-11-27
14. About Me
Tonny Madsen, Founder and Owner of The RCP Company
20 years of experience in system development in major
companies
9 years of experience with Eclipse and Eclipse RCP
Chairman of Eclipse.dk
Extern Lecturer at IT-University on Model Driven Development and
Domain Specific Languages
Regular speaker at EclipseCon, Eclipse Summit Europe, Eclipse
Summit India, etc
Reached via
tonny.madsen@rcp-company.com
Phone: +45 70 22 25 92
Twitter: tonnymadsen
Blog: http://blog.rcp-company.com/
LinkedIn: http://linkedin.com/in/tonnymadsen
14
L0001 - 2010-11-27
Notas del editor
\n
\n
\n
Before &#x201C;checkEnabled&#x201D; was implemented in Eclipse (3.3 I believe), you also had to specify a visibleWhen expression which was a or&#x2019;ed together version of the activewhen expression for all relevant handlers. A difficult position\n
This often happens because the involved teams cannot\n
\n
This can entail all sorts of problems, where the top two are:\n- conflicts between the different involved interface - what is getNsme()\n- often you don&#x2019;t want have you UI concerns and application functionality in your domain model - separation of concerns\n\n
Here we solve the problems by having a manager that can return the correct class given one of the domain objects and a wanted functionality\n\nThe participants:\n- The Adabtable Type: the types that can be adapted\n- The Adapter: the type that can be adapted to\n- Factory:entity that can adapt a specific adaptable type to a number of adapters\n- Manager: manager of factories\n\n