/*******************************************************************************
 * Copyright (c) 2012 - 2015 Signal Iduna Corporation and others.
 * 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:
 * Signal Iduna Corporation - initial API and implementation
 * akquinet AG
 *******************************************************************************/
package org.testeditor.ui.handlers.teamshare;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.core.services.translation.TranslationService;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.testeditor.core.exceptions.SystemException;
import org.testeditor.core.model.teststructure.TestStructure;
import org.testeditor.ui.utilities.TestEditorTranslationService;
import org.testeditor.ui.wizardpages.teamshare.TeamShareApproveWizardPage;

/**
 * executes the approveElement-event.
 * 
 */
public class ApproveElementHandler extends AbstractUpdateOrApproveHandler {

	private static final Logger LOGGER = Logger.getLogger(ApproveElementHandler.class);

	@Inject
	private TestEditorTranslationService translationService;

	@Inject
	private TranslationService translate;

	@Inject
	@Named(IServiceConstants.ACTIVE_SHELL)
	private Shell shell;

	private TeamShareApproveWizardPage approveProjectPage;

	/**
	 * executes the event for the selected-elements.
	 * 
	 * @param context
	 *            IEclipseContext
	 */
	@Execute
	public void execute(IEclipseContext context) {
		EPartService partService = context.get(EPartService.class);
		partService.saveAll(true);
		// New Wizard
		Wizard nwiz = new Wizard() {

			@Override
			public boolean performFinish() {
				return true;
			}
		};

		// Add the new-page to the wizard
		approveProjectPage = ContextInjectionFactory.make(TeamShareApproveWizardPage.class, context);

		nwiz.addPage(approveProjectPage);

		// Show the wizard...
		WizardDialog wizardDialog = new WizardDialog(shell, nwiz);

		if (wizardDialog.open() == Window.OK) {
			super.execute(context.get(IEventBroker.class));
		}
	}

	@Override
	boolean executeSpecials(TestStructure testStructure) {
		try {
			getTeamService(testStructure).approve(testStructure, translate, approveProjectPage.getSvnComment());
		} catch (final SystemException e) {
			Display.getDefault().syncExec(new Runnable() {

				@Override
				public void run() {
					Shell activeShell = Display.getDefault().getActiveShell();
					MessageDialog.openError(activeShell, translationService.translate("%error"), e.getMessage());
				}
			});
			LOGGER.error(e.getMessage(), e);
			return false;
		}
		return true;
	}

	@Override
	String getMessage() {
		return translationService.translate("%popupmenu.label.approveElement");
	}

	@Override
	void showCompletedMessage() {
		MessageDialog.openInformation(getDisplay().getActiveShell(), translationService.translate("%info"),
				translationService.translate("%approve.completed"));

	}

}

@@ -2370,16 +2370,20 @@
 ss);%0D%0A%09%09
+if (
 partServ
@@ -2399,19 +2399,22 @@
 ll(true)
-;
+) %7B
 %0D%0A
+%09
 %09%09// New
@@ -2422,16 +2422,17 @@
 Wizard%0D%0A
+%09
 %09%09Wizard
@@ -2457,16 +2457,17 @@
 () %7B%0D%0A%0D%0A
+%09
 %09%09%09@Over
@@ -2475,16 +2475,17 @@
 ide%0D%0A%09%09%09
+%09
 public b
@@ -2514,16 +2514,17 @@
  %7B%0D%0A%09%09%09%09
+%09
 return t
@@ -2529,24 +2529,25 @@
  true;%0D%0A
+%09
 %09%09%09%7D%0D%0A%09%09
 %7D;%0D%0A%0D%0A%09%09
@@ -2542,14 +2542,16 @@
 %0D%0A%09%09
+%09
 %7D;%0D%0A%0D%0A
+%09
 %09%09//
@@ -2583,16 +2583,17 @@
 wizard%0D%0A
+%09
 %09%09approv
@@ -2683,16 +2683,17 @@
 xt);%0D%0A%0D%0A
+%09
 %09%09nwiz.a
@@ -2725,16 +2725,17 @@
 );%0D%0A%0D%0A%09%09
+%09
 // Show 
@@ -2749,16 +2749,17 @@
 ard...%0D%0A
+%09
 %09%09Wizard
@@ -2814,16 +2814,17 @@
 iz);%0D%0A%0D%0A
+%09
 %09%09if (wi
@@ -2861,16 +2861,17 @@
 ) %7B%0D%0A%09%09%09
+%09
 super.ex
@@ -2911,16 +2911,22 @@
 ass));%0D%0A
+%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%09%7D%0D

Check if user cancels save operation