/*******************************************************************************
 * Copyright (c) 2012 Torkild U. Resheim.
 * 
 * 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: 
 *     Torkild U. Resheim - initial API and implementation
 *******************************************************************************/
package no.resheim.eclipse.utils.launcher.macosx.handlers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.SWT;
import org.eclipse.ui.internal.ide.ChooseWorkspaceData;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;
import org.eclipse.ui.menus.ExtensionContributionFactory;
import org.eclipse.ui.menus.IContributionRoot;
import org.eclipse.ui.services.IServiceLocator;

@SuppressWarnings("restriction")
public class OpenWorkspaceMenu extends ExtensionContributionFactory {

	private static final String COMMAND_ID = "no.resheim.eclipse.launcherutil.commands.newInstance"; //$NON-NLS-1$

	public OpenWorkspaceMenu() {
	}

	private IContributionItem[] getContributionItems(IServiceLocator serviceLocator) {
		ArrayList<IContributionItem> list = new ArrayList<IContributionItem>();
		final ChooseWorkspaceData data = new ChooseWorkspaceData(Platform.getInstanceLocation().getURL());
		data.readPersistedData();
		String current = data.getInitialDefault();
		String[] workspaces = data.getRecentWorkspaces();
		for (int i = 0; i < workspaces.length; i++) {
			if (workspaces[i] != null && !workspaces[i].equals(current)) {
				list.add(createOpenCommand(serviceLocator, workspaces[i], workspaces[i]));
			}
		}
		if (list.size() > 0) {
			list.add(new Separator());
		}
		list.add(createOpenCommand(serviceLocator, Messages.OpenWorkspaceMenu_Other, null));
		return list.toArray(new IContributionItem[list.size()]);
	}

	@Override
	public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
		MenuManager submenu = new MenuManager(Messages.OpenWorkspaceMenu_Open);
		IContributionItem[] items = getContributionItems(serviceLocator);
		for (IContributionItem iContributionItem : items) {
			submenu.add(iContributionItem);
		}
		additions.addContributionItem(submenu, null);
	}

	public CommandContributionItem createOpenCommand(IServiceLocator serviceLocator, String label, String workspace) {
		CommandContributionItemParameter p = new CommandContributionItemParameter(serviceLocator, "", //$NON-NLS-1$
				COMMAND_ID, SWT.PUSH);
		if (workspace != null) {
			Map<Object, Object> parameters = new HashMap<Object, Object>();
			parameters.put(OpenWorkspaceHandler.PARAMETER_ID, workspace);
			p.parameters = parameters;
		}
		p.label = label;
		CommandContributionItem item = new CommandContributionItem(p);
		item.setVisible(true);
		return item;
	}

}

@@ -677,41 +677,185 @@
 ort 
-org.eclipse.core.runtime.Platform
+no.resheim.eclipse.utils.launcher.core.LauncherPlugin;%0A%0Aimport org.eclipse.core.runtime.IStatus;%0Aimport org.eclipse.core.runtime.Platform;%0Aimport org.eclipse.core.runtime.Status
 ;%0Aim
@@ -1342,16 +1342,68 @@
 Locator;
+%0Aimport org.eclipse.ui.statushandlers.StatusManager;
 %0A%0A@Suppr
@@ -1805,16 +1805,25 @@
 tem%3E();%0A
+%09%09try %7B%0A%09
 %09%09final 
@@ -1917,16 +1917,17 @@
 L());%0A%09%09
+%09
 data.rea
@@ -1940,24 +1940,25 @@
 stedData();%0A
+%09
 %09%09String cur
@@ -1990,16 +1990,17 @@
 ault();%0A
+%09
 %09%09String
@@ -2041,24 +2041,25 @@
 spaces();%0A%09%09
+%09
 for (int i =
@@ -2092,16 +2092,17 @@
  i++) %7B%0A
+%09
 %09%09%09if (w
@@ -2163,16 +2163,17 @@
 ) %7B%0A%09%09%09%09
+%09
 list.add
@@ -2243,17 +2243,20 @@
 ));%0A
+%09
 %09%09%09%7D%0A%09%09
+%09
 %7D%0A
+%09
 %09%09if
@@ -2276,16 +2276,17 @@
  %3E 0) %7B%0A
+%09
 %09%09%09list.
@@ -2309,18 +2309,20 @@
 r());%0A%09%09
+%09
 %7D%0A
+%09
 %09%09list.a
@@ -2400,16 +2400,242 @@
 null));%0A
+%09%09%7D catch (Exception e) %7B%0A%09%09%09IStatus newStatus = new Status(IStatus.ERROR, LauncherPlugin.PLUGIN_ID,%0A%09%09%09%09%09%22Could not create workspace chooser menu item.%22, e); //$NON-NLS-1$%0A%09%09%09StatusManager.getManager().handle(newStatus);%0A%09%09%7D%0A
 %09%09return

Handle exception when creating contribution item.