package de.ugoe.cs.swe.memos.gui.menu;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.ui.PlatformUI;

import de.ugoe.cs.swe.memos.ImportManager;
import de.ugoe.cs.swe.memos.Utils;
import de.ugoe.cs.swe.memos.gui.dialogs.ImportDialog;

public class importHandler implements IHandler {
	private static boolean enabled = true;

	public static void setEnabled(boolean state) {
		enabled = state;
	}

	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		boolean flag = true;
		while (flag) {

			ImportDialog importdialog = new ImportDialog(PlatformUI
					.getWorkbench().getActiveWorkbenchWindow().getShell());
			ImportManager importmanager = ImportManager.getInstance();

			if (importdialog.open() == IDialogConstants.OK_ID) {
				
				if (!importdialog.getImportPath().toLowerCase()
						.endsWith(".xml")) {
					Utils.showError("Sie müssen eine XML Datei für den Import ausgewählt haben!");
				}else
					flag = false;

				if (!flag) {
					importmanager.setImportPath(importdialog.getImportFile());
					importmanager.loadMemosFromXML();
				}
			}else
				importdialog.close();
		}

		return null;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return enabled;
	}

	@Override
	public boolean isHandled() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {
		// TODO Auto-generated method stub

	}

}
