/*******************************************************************************
 * Copyright (c) 2007, 2009 David Green 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:
 *     David Green - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylyn.wikitext.ui.commands;

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.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.internal.wikitext.ui.editor.MarkupEditor;
import org.eclipse.mylyn.wikitext.core.WikiText;
import org.eclipse.mylyn.wikitext.core.parser.markup.MarkupLanguage;
import org.eclipse.ui.PlatformUI;

import com.ibm.icu.text.MessageFormat;

/**
 * An abstract base class for handlers that use the workbench selection to operate on resources
 * 
 * @author David Green
 * 
 */
public abstract class AbstractMarkupResourceHandler extends AbstractHandler {

	protected MarkupLanguage markupLanguage;

	@SuppressWarnings("unchecked")
	public Object execute(ExecutionEvent event) throws ExecutionException {

		ISelection currentSelection = null;

		try {
			currentSelection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
		} catch (Exception e) {
			// ignore
		}

		if (currentSelection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
			Iterator<Object> it = structuredSelection.iterator();
			while (it.hasNext()) {
				Object o = it.next();
				IFile file = null;
				if (o instanceof IAdaptable) {
					file = (IFile) ((IAdaptable) o).getAdapter(IFile.class);
				}
				if (file != null) {
					String name = file.getName();
					int idxOfDot = name.lastIndexOf('.');
					if (idxOfDot != -1) {
						name = name.substring(0, idxOfDot);
					}
					// use a temporary so that the setting does not stick even if the handler is reused.
					MarkupLanguage prev = markupLanguage;
					try {
						if (markupLanguage == null) {
							markupLanguage = MarkupEditor.loadMarkupLanguagePreference(file);
							if (markupLanguage == null) {
								markupLanguage = WikiText.getMarkupLanguageForFilename(file.getName());
							}
							if (markupLanguage == null) {
								MessageDialog.openError(
										PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
										Messages.AbstractMarkupResourceHandler_unexpectedError, MessageFormat.format(Messages.AbstractMarkupResourceHandler_markupLanguageMappingFailed,  
												new Object[] { file.getName() }));
								return null;
							}
						}

						handleFile(file, name);
					} finally {
						markupLanguage = prev;
					}
				}
			}
		}

		return null;
	}

	protected abstract void handleFile(IFile file, String name);

	public MarkupLanguage getMarkupLanguage() {
		return markupLanguage;
	}

	public void setMarkupLanguage(MarkupLanguage markupLanguage) {
		this.markupLanguage = markupLanguage;
	}

}

@@ -1505,16 +1505,58 @@
 guage;%0A%0A
+%09private IStructuredSelection selection;%0A%0A
 %09@Suppre
@@ -1579,16 +1579,16 @@
 ecked%22)%0A
-
 %09public 
@@ -1689,117 +1689,92 @@
 n = 
-null;%0A%0A%09%09try %7B%0A%09%09%09currentSelection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().get
+selection;%0A%09%09if (currentSelection == null) %7B%0A%09%09%09try %7B%0A%09%09%09%09currentSelection = compute
 Sele
@@ -1782,16 +1782,17 @@
 tion();%0A
+%09
 %09%09%7D catc
@@ -1812,16 +1812,17 @@
 e) %7B%0A%09%09%09
+%09
 // ignor
@@ -1823,16 +1823,21 @@
  ignore%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09i
@@ -3002,16 +3002,29 @@
 .format(
+%0A%09%09%09%09%09%09%09%09%09%09%09%09
 Messages
@@ -3082,18 +3082,16 @@
 gFailed,
-  
 %0A%09%09%09%09%09%09%09
@@ -3287,16 +3287,182 @@
 ll;%0A%09%7D%0A%0A
+%09/**%0A%09 * @since 1.1%0A%09 */%0A%09protected ISelection computeSelection() %7B%0A%09%09return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();%0A%09%7D%0A%0A
 %09protect
@@ -3654,16 +3654,16 @@
 uage) %7B%0A
-
 %09%09this.m
@@ -3698,10 +3698,224 @@
 ge;%0A%09%7D%0A%0A
+%09/**%0A%09 * @since 1.1%0A%09 */%0A%09public IStructuredSelection getSelection() %7B%0A%09%09return selection;%0A%09%7D%0A%0A%09/**%0A%09 * @since 1.1%0A%09 */%0A%09public void setSelection(IStructuredSelection selection) %7B%0A%09%09this.selection = selection;%0A%09%7D%0A%0A
 %7D%0A

Allow clients to set the selection