+// $Id$
+/**
+* Copyright (C) 2007 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+
+package eu.etaxonomy.taxeditor.bulkeditor.derivedunit.handler;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.commands.common.NotDefinedException;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
+import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
+import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil;
+import eu.etaxonomy.taxeditor.bulkeditor.derivedunit.operation.AddDerivedUnitFacadeMediaOperation;
+import eu.etaxonomy.taxeditor.bulkeditor.input.OccurrenceEditorInput;
+import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
+import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
+
+/**
+ * @author n.hoffmann
+ * @created Feb 11, 2011
+ * @version 1.0
+ */
+public abstract class AbstractAddDerivedUnitFacadeMediaHandler extends AbstractHandler {
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
+ */
+ @Override
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ IWorkbenchPart part = HandlerUtil.getActivePart(event);
+ IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
+
+
+ IEditorPart editor = HandlerUtil.getActiveEditor(event);
+ IEditorInput input = editor.getEditorInput();
+
+
+ if(input instanceof OccurrenceEditorInput){
+ BulkEditor bulkEditor = (BulkEditor) editor;
+ ISelection selection = bulkEditor.getSelectionProvider().getSelection();
+
+ if(selection instanceof IStructuredSelection){
+ Object element = ((IStructuredSelection) selection).getFirstElement();
+ if(element instanceof DerivedUnitBase){
+
+ try{
+ AbstractPostOperation operation = new AddDerivedUnitFacadeMediaOperation(event.getCommand().getName(), BulkEditorUtil.getUndoContext(), (DerivedUnitBase) element, getMode(), postOperationEnabled);
+ BulkEditorUtil.executeOperation(operation);
+ } catch (NotDefinedException e) {
+ BulkEditorUtil.warn(getClass(), "Command name not set.");
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @return
+ */
+ protected abstract int getMode();
+
+}