package de.afbb.bibo.ui.handler;

import java.util.Iterator;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import de.afbb.bibo.share.impl.NavigationTreeViewNode;
import de.afbb.bibo.share.model.Borrower;
import de.afbb.bibo.ui.view.BorrowerView;

/**
 * handler that manages a {@link Borrower}
 *
 * @author David Becker
 */
public class ManageBorrowerHandler extends AbstractHandler {

	@Override
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		final ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
				.getSelection();
		openForSelection((IStructuredSelection) selection);
		return null;
	}

	private static void openForSelection(final IStructuredSelection selection) {
		final Iterator<?> iterator = selection.iterator();
		while (iterator.hasNext()) {
			final Object next = iterator.next();
			if (next instanceof NavigationTreeViewNode) {
				final Object input = ((NavigationTreeViewNode) next).getValue();
				if (input instanceof Borrower) {
					try {
						final IViewPart showView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
								.showView(BorrowerView.ID);
						if (showView instanceof BorrowerView) {
							((BorrowerView) showView).setInput((Borrower) input);
						}
					} catch (final PartInitException e) {
						e.printStackTrace();
					}
				}
			}
		}

	}
}

@@ -206,32 +206,81 @@
 tionException;%0D%0A
+import org.eclipse.jface.dialogs.MessageDialog;%0D%0A
 import org.eclip
@@ -395,24 +395,65 @@
 IViewPart;%0D%0A
+import org.eclipse.ui.IWorkbenchWindow;%0D%0A
 import org.e
@@ -1501,24 +1501,44 @@
 al I
-ViewPart showVie
+WorkbenchWindow activeWorkbenchWindo
 w = 
@@ -1554,32 +1554,42 @@
 I.getWorkbench()
+%0D%0A%09%09%09%09%09%09%09%09
 .getActiveWorkbe
@@ -1595,24 +1595,81 @@
 enchWindow()
+;%0D%0A%09%09%09%09%09%09final IViewPart showView = activeWorkbenchWindow
 .getActivePa
@@ -1672,26 +1672,16 @@
 vePage()
-%0D%0A%09%09%09%09%09%09%09%09
 .showVie
@@ -1759,33 +1759,326 @@
 %09%09%09%09
-((BorrowerView) showView)
+final BorrowerView view = (BorrowerView) showView;%0D%0A%09%09%09%09%09%09%09final boolean dirty = view.isDirty();%0D%0A%09%09%09%09%09%09%09if (dirty && MessageDialog.openQuestion(activeWorkbenchWindow.getShell(),%0D%0A%09%09%09%09%09%09%09%09%09%22Offene %C3%84nderungen verwerfen?%22,%0D%0A%09%09%09%09%09%09%09%09%09%22Es bestehen noch offen %C3%84nderungen.%5Cn%C3%84nderungen verwerfen?%22) %7C%7C !dirty) %7B%0D%0A%09%09%09%09%09%09%09%09view
 .set
@@ -2099,24 +2099,34 @@
 r) input);%0D%0A
+%09%09%09%09%09%09%09%7D%0D%0A
 %09%09%09%09%09%09%7D%0D%0A%09%09%09

implement question dialog for manage borrowers