Communication between views in RCP [OSGI-EventAdmin]

Source: Internet
Author: User

I often see communication between views in the Eclipse news group. They often have to face the synchronization problem after the status changes (the common practice is to use ISelectionService ), it seems that you do not know much about the OSGI EventAdmin service. This is an event system that uses the publish/subscribe mode and can be easily applied to your RCP program.

1. Installation
Add org. eclipse. osgi. services and org. eclipse. equinox. event to the project dependencies.

2. Release events
Public class SenderView extends ViewPart {public static final String ID = "viewcommunication. views. SenderView ";
Private Button B;

Public void createPartControl (Composite parent ){
Parent. setLayout (new GridLayout ());
B = new Button (parent, SWT. PUSH );
B. setText ("Send Event ");
B. addSelectionListener (new SelectionAdapter (){
@ Override
Public void widgetSelected (SelectionEvent e ){
BundleContext ctx = FrameworkUtil. getBundle (SenderView. class). getBundleContext ();
ServiceReference <EventAdmin> ref = ctx. getServiceReference (EventAdmin. class );
EventAdmin eventAdmin = ctx. getService (ref );
Map <String, Object> properties = new HashMap <String, Object> ();
Properties. put ("DATA", new Date ());

Event event = new Event ("viewcommunication/syncEvent", properties );
EventAdmin. sendEvent (event );

Event = new Event ("viewcommunication/asyncEvent", properties );
EventAdmin. postEvent (event );
}
});
}

Public void setFocus (){
B. setFocus ();
}
}

3. subscribe to events
1 public class ReceiverView extends ViewPart {
2 private TableViewer;
3
4 @ Override
5 public void createPartControl (final Composite parent ){
6 parent. setLayout (new FillLayout ());
7 viewer = new TableViewer (parent );
8 viewer. getTable (). setHeaderVisible (true );
9 viewer. getTable (). setLinesVisible (true );
10 viewer. setLabelProvider (new ColumnLabelProvider (){
11 @ Override
12 public String getText (Object element ){
13 return DateFormat. getDateTimeInstance (). format (element );
14}
15 });
16
17 BundleContext ctx = FrameworkUtil. getBundle (ReceiverView. class). getBundleContext ();
18 EventHandler handler = new EventHandler (){
19 public void handleEvent (final Event event ){
20 if (parent. getDisplay (). getThread () = Thread. currentThread ()){
21 viewer. add (event. getProperty ("DATA "));
22} else {
23 parent. getDisplay (). syncExec (new Runnable (){
24 public void run (){
25 viewer. add (event. getProperty ("DATA "));
26}
27 });
28}
29}
30 };
31
32 Dictionary <String, String> properties = new Hashtable <String, String> ();
33 properties. put (EventConstants. EVENT_TOPIC, "viewcommunication /*");
34 ctx. registerService (EventHandler. class, handler, properties );
35}
36
37 @ Override
38 public void setFocus (){
39 viewer. getTable (). setFocus ();
40}
41}
42

It's not very complicated, right? At least it reduces coupling.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.