Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Update ui for OSGi-EventAdmin-Event(Receiving an Event from EventAdmin want to update the RAP UI)
Update ui for OSGi-EventAdmin-Event [message #652279] Thu, 03 February 2011 07:55 Go to next message
Stefan Klaus is currently offline Stefan Klaus
Messages: 20
Registered: February 2011
Junior Member
Hello!

I'm trying to show an Event received via OSGi-EventAdmin. Within my RCP-App everything works fine. Thanks to Tom Schindl and http://tomsondev.bestsolution.at/2011/01/03/enhanced-rcp-how -views-can-communicate/ .

Now in RAP I tried first following:

class ... {
...
Composite parent;
TableViewer tableViewer;
	
	@Override
	protected Composite createEventCompositeInternal(final Composite parent) {
		this.parent = parent;
                this.tableViewer = new TableViewer(composite);

                BundleContext ctx = FrameworkUtil.getBundle(Client.class)
				.getBundleContext();
		EventHandler handler = new EventHandler() {
			public void handleEvent(final Event event) {
				UICallBack.activate("alarm");
				Thread bgThread = new Thread(createRunnable(event));
				bgThread.setDaemon(true);
				bgThread.start();

			}
		};

		Dictionary<String, String> properties = new Hashtable<String, String>();
		properties.put(EventConstants.EVENT_TOPIC, "events");
		ctx.registerService(EventHandler.class.getName(), handler, properties);

		return composite;
	}

	private Runnable createRunnable(final Event event) {
		return new Runnable() {
			public void run() {
				// do some work...
				// schedule the UI update
				parent.getDisplay().asyncExec(new Runnable() {
					public void run() {
						// update the UI
						tableViewer.add(event.toString());
					}
				});
				// Deactivate the UI call-back
				parent.getDisplay().asyncExec(new Runnable() {
					public void run() {
						UICallBack.deactivate("alarm");
					}
				});
			}
		};
	}


But this throws the following exception:
Log ErrorException while dispatching event org.osgi.service.event.Event [topic=events] to handler client.EventCompositeFacadeImpl$2@5ecfe500
java.lang.IllegalStateException: No context available outside of the request service lifecycle.


Looking in the RAP-FAQ for the IllegalstateException then tried UICallBack#runNonUIThreadWithFakeContext:
class...{
        Composite parent;
	TableViewer tableViewer;
	
	@Override
	protected Composite createEventCompositeInternal(final Composite parent) {
		this.parent = parent;
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new FillLayout());

		this.tableViewer = new TableViewer(composite);
		this.tableViewer.getTable().setHeaderVisible(true);
		this.tableViewer.getTable().setLinesVisible(true);
		this.tableViewer.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				// return DateFormat.getDateTimeInstance().format(element);
				return element.toString();
			}
		});

		BundleContext ctx = FrameworkUtil.getBundle(Client.class)
				.getBundleContext();
		EventHandler handler = new EventHandler() {
			public void handleEvent(final Event event) {
		UICallBack.runNonUIThreadWithFakeContext(parent.getDisplay(),
						new Runnable() {
							public void run() {
								// do some work...
								// schedule the UI update
								parent.getDisplay().asyncExec(new Runnable() {
									public void run() {
										// update the UI
										tableViewer.add(event.toString());
									}
								});
							}
						});

			}
		};

		Dictionary<String, String> properties = new Hashtable<String, String>();
		properties.put(EventConstants.EVENT_TOPIC, "events");
		ctx.registerService(EventHandler.class.getName(), handler, properties);


No Exception is thrown, but the UI does not automatically refresh. I have to first resize the browser window or the click in the tableviewer to see the new added events to the table.

Any suggestions?

Thanks for your help.
Stefan
Re: Update ui for OSGi-EventAdmin-Event [message #652288 is a reply to message #652279] Thu, 03 February 2011 08:36 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan Furnadjiev
Messages: 1783
Registered: July 2009
Senior Member
Hi Stefan,
in your second example the UICallBack is not activated at all, thus UI
does not automatically refresh.
HTH,
Ivan

On 2/3/2011 2:55 PM, Stefan wrote:
> Hello!
>
> I'm trying to show an Event received via OSGi-EventAdmin. Within my
> RCP-App everything works fine. Thanks to Tom Schindl and
> http://tomsondev.bestsolution.at/2011/01/03/enhanced-rcp-how -views-can-communicate/
> .
>
> Now in RAP I tried first following:
>
> class ... {
> ..
> Composite parent;
> TableViewer tableViewer;
>
> @Override
> protected Composite createEventCompositeInternal(final Composite
> parent) {
> this.parent = parent;
> this.tableViewer = new TableViewer(composite);
>
> BundleContext ctx = FrameworkUtil.getBundle(Client.class)
> .getBundleContext();
> EventHandler handler = new EventHandler() {
> public void handleEvent(final Event event) {
> UICallBack.activate("alarm");
> Thread bgThread = new Thread(createRunnable(event));
> bgThread.setDaemon(true);
> bgThread.start();
>
> }
> };
>
> Dictionary<String, String> properties = new Hashtable<String,
> String>();
> properties.put(EventConstants.EVENT_TOPIC, "events");
> ctx.registerService(EventHandler.class.getName(), handler,
> properties);
>
> return composite;
> }
>
> private Runnable createRunnable(final Event event) {
> return new Runnable() {
> public void run() {
> // do some work...
> // schedule the UI update
> parent.getDisplay().asyncExec(new Runnable() {
> public void run() {
> // update the UI
> tableViewer.add(event.toString());
> }
> });
> // Deactivate the UI call-back
> parent.getDisplay().asyncExec(new Runnable() {
> public void run() {
> UICallBack.deactivate("alarm");
> }
> });
> }
> };
> }
>
>
> But this throws the following exception:
> Log ErrorException while dispatching event
> org.osgi.service.event.Event [topic=events] to handler
> client.EventCompositeFacadeImpl$2@5ecfe500
> java.lang.IllegalStateException: No context available outside of the
> request service lifecycle.
>
> Looking in the RAP-FAQ for the IllegalstateException then tried
> UICallBack#runNonUIThreadWithFakeContext:
>
> class...{
> Composite parent;
> TableViewer tableViewer;
>
> @Override
> protected Composite createEventCompositeInternal(final Composite
> parent) {
> this.parent = parent;
> Composite composite = new Composite(parent, SWT.NONE);
> composite.setLayout(new FillLayout());
>
> this.tableViewer = new TableViewer(composite);
> this.tableViewer.getTable().setHeaderVisible(true);
> this.tableViewer.getTable().setLinesVisible(true);
> this.tableViewer.setLabelProvider(new ColumnLabelProvider() {
> @Override
> public String getText(Object element) {
> // return
> DateFormat.getDateTimeInstance().format(element);
> return element.toString();
> }
> });
>
> BundleContext ctx = FrameworkUtil.getBundle(Client.class)
> .getBundleContext();
> EventHandler handler = new EventHandler() {
> public void handleEvent(final Event event) {
> UICallBack.runNonUIThreadWithFakeContext(parent.getDisplay() ,
> new Runnable() {
> public void run() {
> // do some work...
> // schedule the UI update
> parent.getDisplay().asyncExec(new
> Runnable() {
> public void run() {
> // update the UI
>
> tableViewer.add(event.toString());
> }
> });
> }
> });
>
> }
> };
>
> Dictionary<String, String> properties = new Hashtable<String,
> String>();
> properties.put(EventConstants.EVENT_TOPIC, "events");
> ctx.registerService(EventHandler.class.getName(), handler,
> properties);
>
>
> No Exception is thrown, but the UI does not automatically refresh. I
> have to first resize the browser window or the click in the
> tableviewer to see the new added events to the table.
>
> Any suggestions?
> Thanks for your help.
> Stefan
Re: Update ui for OSGi-EventAdmin-Event [message #652324 is a reply to message #652279] Thu, 03 February 2011 10:19 Go to previous messageGo to next message
Stefan Klaus is currently offline Stefan Klaus
Messages: 20
Registered: February 2011
Junior Member
Short answer but very helpful! Very Happy

For others searching the same question here the running code:
...
		UICallBack.activate("alarm");
		
		BundleContext ctx = FrameworkUtil.getBundle(Client.class)
				.getBundleContext();
		EventHandler handler = new EventHandler() {
			public void handleEvent(final Event event) {

				
				Thread bgThread = new Thread(createRunnable(event));
				bgThread.setDaemon(true);
				bgThread.start();

			}
		};

               Dictionary<String, String> properties = new Hashtable<String, String>();
		properties.put(EventConstants.EVENT_TOPIC, "Events");
		ctx.registerService(EventHandler.class.getName(), handler, properties);

...
private Runnable createRunnable(final Event event) {
		return new Runnable() {
			public void run() {
				UICallBack.runNonUIThreadWithFakeContext(parent.getDisplay(),
						new Runnable() {
							public void run() {
								// do some work...
								// schedule the UI update
								parent.getDisplay().asyncExec(new Runnable() {
									public void run() {
										// update the UI
										tableViewer.add(event.toString());
									}
								});
							}
						});
			}
		};
	}

		


Re: Update ui for OSGi-EventAdmin-Event [message #652335 is a reply to message #652324] Thu, 03 February 2011 10:36 Go to previous message
Ivan Furnadjiev is currently offline Ivan Furnadjiev
Messages: 1783
Registered: July 2009
Senior Member
Hi Stefan,
it's great that your code is running fine now. I will suggest to
deactivate the UICallBack when it is not needed anymore. In other words
- activate before you start the background thread, deactivate it when
the job is done.
Best,
Ivan

On 2/3/2011 5:19 PM, Stefan wrote:
> Short answer but very helpful! :d
> For others searching the same question here the running code:
>
> ..
> UICallBack.activate("alarm");
>
> BundleContext ctx = FrameworkUtil.getBundle(Client.class)
> .getBundleContext();
> EventHandler handler = new EventHandler() {
> public void handleEvent(final Event event) {
>
>
> Thread bgThread = new Thread(createRunnable(event));
> bgThread.setDaemon(true);
> bgThread.start();
>
> }
> };
>
> Dictionary<String, String> properties = new
> Hashtable<String, String>();
> properties.put(EventConstants.EVENT_TOPIC, "Events");
> ctx.registerService(EventHandler.class.getName(), handler,
> properties);
>
> ..
> private Runnable createRunnable(final Event event) {
> return new Runnable() {
> public void run() {
>
> UICallBack.runNonUIThreadWithFakeContext(parent.getDisplay() ,
> new Runnable() {
> public void run() {
> // do some work...
> // schedule the UI update
> parent.getDisplay().asyncExec(new
> Runnable() {
> public void run() {
> // update the UI
>
> tableViewer.add(event.toString());
> }
> });
> }
> });
> }
> };
> }
>
>
>
>
>
Previous Topic:Canvas drawing performance optimization
Next Topic:TableColumn moveable
Goto Forum:
  


Current Time: Thu Jul 31 20:03:42 EDT 2014

Powered by FUDForum. Page generated in 0.01529 seconds