/*******************************************************************************
 * Copyright (c) 2013 MEDEVIT <office@medevit.at>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     MEDEVIT <office@medevit.at> - initial API and implementation
 ******************************************************************************/
package ch.elexis.core.application.listeners;

import org.eclipse.jface.dialogs.MessageDialog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.data.events.ElexisEvent;
import ch.elexis.core.data.events.ElexisEventListenerImpl;
import ch.elexis.core.data.interfaces.events.MessageEvent;
import ch.elexis.core.ui.UiDesk;

/**
 * Listens to generic MessageEvents thrown by the core that should be presented to the user.
 */
public class MessageEventListener extends ElexisEventListenerImpl {
	
	private Logger log = LoggerFactory.getLogger(MessageEventListener.class.getName());
	
	public MessageEventListener(){
		super(null, MessageEvent.class, ElexisEvent.EVENT_NOTIFICATION, ElexisEvent.PRIORITY_SYNC);
	}
	
	@Override
	public void run(ElexisEvent ev){
		MessageEvent me = (MessageEvent) ev.getGenericObject();
		log.debug("MessageEvent [TITLE] " + me.title);
		switch (me.mt) {
		case ERROR:
			MessageDialog.openError(UiDesk.getTopShell(), me.title, me.message);
			break;
		case WARN:
			MessageDialog.openWarning(UiDesk.getTopShell(), me.title, me.message);
			break;
		default:
			MessageDialog.openInformation(UiDesk.getTopShell(), me.title, me.message);
			break;
		}
	}
}

@@ -636,16 +636,56 @@
 Dialog;%0A
+import org.eclipse.swt.widgets.Display;%0A
 import o
@@ -1350,16 +1350,22 @@
 oid run(
+final 
 ElexisEv
@@ -1373,16 +1373,102 @@
 nt ev)%7B%0A
+%09%09Display.getDefault().syncExec(new Runnable() %7B%0A%09%09%09@Override%0A%09%09%09public void run()%7B%0A%09%09
 %09%09Messag
@@ -1517,16 +1517,18 @@
 ject();%0A
+%09%09
 %09%09log.de
@@ -1568,16 +1568,18 @@
 title);%0A
+%09%09
 %09%09switch
@@ -1589,16 +1589,18 @@
 e.mt) %7B%0A
+%09%09
 %09%09case E
@@ -1605,16 +1605,18 @@
  ERROR:%0A
+%09%09
 %09%09%09Messa
@@ -1682,23 +1682,27 @@
 ge);%0A%09%09%09
+%09%09
 break;%0A
+%09%09
 %09%09case W
@@ -1705,24 +1705,26 @@
 se WARN:%0A%09%09%09
+%09%09
 MessageDialo
@@ -1777,32 +1777,34 @@
 me.message);%0A%09%09%09
+%09%09
 break;%0A%09%09default
@@ -1796,16 +1796,18 @@
 reak;%0A%09%09
+%09
+%09
 default:
@@ -1807,16 +1807,18 @@
 efault:%0A
+%09%09
 %09%09%09Messa
@@ -1890,16 +1890,18 @@
 ge);%0A%09%09%09
+%09%09
 break;%0A%09
@@ -1901,15 +1901,28 @@
 reak;%0A%09%09
-%7D
+%09%09%7D%0A%09%09%09%7D%0A%09%09%7D);
 %0A%09%7D%0A%7D%0A

make sure message events are displayed in display thread