Search code examples
javaeventseclipse-rcpjfaceeclipse-rap

Untyped events were supported in RAP?


I'm developing an application based on RCP. Recently, I have moved it to RAP. Before, I used some untyped events in RCP to pass some necessary data for different purposes. For example, I have a button called "Get Data" to get data from server. I have some tableviewer to hold data that were received from server.When user click this button, I want to raise an event, and in each table will listen that event. I did as following:

//Part 1

Button btgetData=new Button(compositeLabels,SWT.PUSH);
        btgetData.setText("Get Data");
        btgetData.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                //raise event here
                Event event =new Event();
                event.detail=12345;
            getRoot().getShell().notifyListeners(MYEVENTCONST, event);
            }           
        });

//Part 2: 
//In each composite that hold TableViewer , listen event

getShell().addListener(MYEVENTCONST, new Listener() 
            public void handleEvent(Event event) {
      if(event.detail==12345)
              doProcessing();               
        });

In RCP, each TableViewer listen normally that event each time user click button "Get Data". But it didn't work on RAP, I don't know if RAP already supported this kind of untyped events. The function doProcessing() is never called :(.

I don't want to change the way when passing data. Please let me know if I could keep going in the old way. I greatly appreciate for any comment.


Solution

  • Custom event types are currently not supported in RAP (mostly because of bug 334028).

    However, although you can do that in SWT, I would consider this a misuse of SWT Events. It might be acceptable for a custom widget, but these methods are reserved for events on the widget level while you use them on application level. Also, the JavaDoc of Widget#notifyListeners() and Widget#addListener() clearly states:

    The event type is one of the event constants defined in class SWT.

    Instead of using a custom event, you can let your composites implement a custom interface:

    public interface ProcessingControl {
    
      void doProcessing();
    
    }
    

    and define an update method that recursively traverses the children of a composite:

    static void updateChildren( Composite parent ) {
      for( Control child : parent.getChildren() ) {
        if( child instanceof ProcessingControl ) {
          ( ( ProcessingControl )child ).doProcessing();
        }
        if( child instanceof Composite ) {
          updateChildren( ( Composite )child );
        }
      }
    }
    

    Then you can call updateChildren( shell );.

    If you prefer to use events and listeners, you should use java.util.EventObject and EventListener for your purpose.