package ch.elexis.core.ui.commands;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;

import ch.elexis.core.ui.util.viewers.CommonViewer;
import ch.elexis.core.ui.views.rechnung.KonsZumVerrechnenView;
import ch.elexis.data.Patient;
import ch.rgw.tools.Tree;

public class KonsZumVerrechnenLinkCommand extends AbstractHandler {
	public static final String CMD_ID = "ch.elexis.core.command.linkViews";
	private TreeSelectionChangedListener leftSideSelChangeListener, rightSideSelChangeListener;
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException{
		boolean alreadyToggled = HandlerUtil.toggleCommandState(event.getCommand());
		
		IWorkbenchPage activePage =
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		KonsZumVerrechnenView kzvView =
			(KonsZumVerrechnenView) activePage.findView(KonsZumVerrechnenView.ID);
		
		final CommonViewer cv = kzvView.getLeftSide();
		final TreeViewer tvSel = kzvView.getRightSide();
		
		// goes from toggled to not toggled
		if (alreadyToggled) {
			cv.getViewerWidget().removeSelectionChangedListener(leftSideSelChangeListener);
			tvSel.removeSelectionChangedListener(rightSideSelChangeListener);
		} else {
			// toggled
			leftSideSelChangeListener = new TreeSelectionChangedListener(tvSel);
			rightSideSelChangeListener =
				new TreeSelectionChangedListener((TreeViewer) cv.getViewerWidget());
			
			cv.getViewerWidget().addSelectionChangedListener(leftSideSelChangeListener);
			tvSel.addSelectionChangedListener(rightSideSelChangeListener);
		}
		
		return null;
	}
	
	class TreeSelectionChangedListener implements ISelectionChangedListener {
		private TreeViewer treeViewer;
		
		public TreeSelectionChangedListener(TreeViewer treeViewer){
			this.treeViewer = treeViewer;
		}
		
		@Override
		public void selectionChanged(SelectionChangedEvent event){
			IStructuredSelection selection = (IStructuredSelection) event.getSelection();
			Patient selPatient = (Patient) ((Tree) selection.getFirstElement()).contents;
			
			for (TreeItem i : treeViewer.getTree().getItems()) {
				Patient p = (Patient) ((Tree) i.getData()).contents;
				if (p.getId().equals(selPatient.getId())) {
					treeViewer.getTree().setSelection(i);
				}
			}
			treeViewer.refresh();
		}
	}
	
}

@@ -671,16 +671,80 @@
 enView;%0A
+import ch.elexis.data.Fall;%0Aimport ch.elexis.data.Konsultation;%0A
 import c
@@ -2478,90 +2478,561 @@
 %0A%09%09%09
-Patient selPatient = (Patient) ((Tree) selection.getFirstElement()).contents
+Object selObj = ((Tree) selection.getFirstElement()).contents;%0A%09%09%09Patient selPatient = null;%0A%09%09%09%0A%09%09%09// get belonging patient%0A%09%09%09if (selObj instanceof Patient) %7B%0A%09%09%09%09selPatient = (Patient) selObj;%0A%09%09%09%7D else if (selObj instanceof Fall) %7B%0A%09%09%09%09Fall fall = (Fall) selObj;%0A%09%09%09%09selPatient = fall.getPatient();%0A%09%09%09%7D else if (selObj instanceof Konsultation) %7B%0A%09%09%09%09Konsultation kons = (Konsultation) selObj;%0A%09%09%09%09Fall fall = kons.getFall();%0A%09%09%09%09if (fall != null && fall.exists()) %7B%0A%09%09%09%09%09selPatient = fall.getPatient()
 ;%0A%09%09%09
+%09%7D
 %0A
+%09%09%09%7D%0A%09%09%09%0A%09%09%09if (selPatient != null) %7B%0A%09
 %09%09%09f
@@ -3087,16 +3087,17 @@
 ) %7B%0A%09%09%09%09
+%09
 Patient 
@@ -3141,16 +3141,17 @@
 ntents;%0A
+%09
 %09%09%09%09if (
@@ -3195,16 +3195,17 @@
  %7B%0A%09%09%09%09%09
+%09
 treeView
@@ -3234,16 +3234,17 @@
 ion(i);%0A
+%09
 %09%09%09%09%7D%0A%09%09
@@ -3244,18 +3244,20 @@
 %09%09%09%7D%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09treeV
@@ -3273,16 +3273,21 @@
 resh();%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%09

Check datatype when syncing RG-Vorschlag and PatientList