Search code examples
eclipseeventsrcp

Decoding sub-events from Eclipse Event Service


An Eclipse e4 RCP application has two Parts, one is an editor-like part inside a PartStack to be able to open multiple parts at the same time.

The other parts only shows the file names of the editor-like parts currently open. Every time an editor-part is created (or closed), the IEventBroker send and event to be decoded by the other part (second-part).

To achieve the above, I have created an interface with the event topics and the methods to send/receive the events in the respective classes.

The only way I have found so far to decode which event has been sent from editor-part to the second-part is by means of the following code.

Is there any simpler/efficient way to receive/decode the events? I don not like that in this line:

broker.post(FileEvents.FILE_NEW, pair);

it is sent duplicated the string "FileEvents.FILE_NEW".

This is the inteface with the event constants:

public interface FileEvents {
    String FILE_ALL_EVENTS = "FILE/*";
    String FILE_NEW = "FILE/NEW";
    String FILE_CLOSE = "FILE/CLOSE";
}

This is the editor-like part to send the events:

public class EditorPart {
@Inject
IEventBroker broker;

SimpleEntry<String, File> pair;
File file;

//...

@PostConstruct
public void createComposite(){
    file = new File("myFilePath");
    pair = new SimpleEntry(FileEvents.FILE_NEW, file);
    broker.post(FileEvents.FILE_NEW, pair);
}

@PreDestroy
public void dispose(){
    pair = new SimpleEntry(FileEvents.FILE_CLOSE, file);
    broker.post(FileEvents.FILE_CLOSE, pair);
}
}

The part to receive the events

public class EventDecoderPart {

File receivedFile = new File("");
//....

@Inject
@Optional
public void updateListOfFiles(@UIEventTopic(FileEvents.FILE_ALL_EVENTS) SimpleEntry<String, File> receivedFileEntry) {

    switch(receivedFileEntry.getKey)
        case FileEvents.FILE_NEW:
            receivedFile = receivedFileEntry.getValue();
            //Process receivedFile...
            break;
        case FileEvents.FILE_CLOSE:
            receivedFile = receivedFileEntry.getValue();
            //Process receivedFile...
            break;
}
}

Thanks a lot


Solution

  • You can use two methods to receive the two events separately:

    @Inject
    @Optional
    public void newFile(@UIEventTopic(FileEvents.FILE_NEW) SimpleEntry<String, File> receivedFileEntry) {
       // TODO new file event
    }
    
    @Inject
    @Optional
    public void closeFile(@UIEventTopic(FileEvents.FILE_CLOSE) SimpleEntry<String, File> receivedFileEntry) {
       // TODO close file event
    }
    

    There is then no need to save the event type in the SimpleEntry.

    Alternatively you can use a single method but with the full org.osgi.service.event.Event event data which contains the topic and your data:

    @Inject
    @Optional
    public void updateListOfFiles(@UIEventTopic(FileEvents.FILE_ALL_EVENTS) Event event) {
    
      String topic = event.getTopic();
    
      Object data = event.getProperty(IEventBroker.DATA);
    
      SimpleEntry<String, File> receivedFileEntry = (SimpleEntry<String, File>)data;
    }