<%@ jet package="org.eclipse.emf.codegen.ecore.templates.editor" imports="java.util.List java.util.Iterator org.eclipse.emf.codegen.ecore.genmodel.*" class="Advisor" version="$Id: Advisor.javajet,v 1.3 2004/06/15 20:12:14 marcelop Exp $" %>
<%
/**
 * <copyright>
 *
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 *
 * </copyright>
 */
%>
<%GenModel genModel = (GenModel)argument;%>
<%@ include file="../Header.jetinc"%>
package <%=genModel.getEditorPluginPackageName()%>;


import java.io.File;
import java.util.Iterator;

import org.eclipse.core.runtime.IPlatformRunnable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ContributionItemFactory;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IFolderLayout;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import org.eclipse.emf.common.ui.action.WorkbenchWindowActionDelegate;
import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.edit.ui.action.LoadResourceAction;

import <%=genModel.getQualifiedEditorPluginClassName()%>;
<%genModel.markImportLocation(stringBuffer);%>


/**
 * Customized {@link WorkbenchAdvisor} for the RCP application.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public final class <%=genModel.getEditorAdvisorClassName()%> extends WorkbenchAdvisor
{
<%if (genModel.getCopyrightText() != null) {%>
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public static final String copyright = "<%=genModel.getCopyrightText()%>";<%=genModel.getNonNLS()%>

<%}%>
	/**
	 * This looks up a string in the plugin's plugin.properties file.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	private static String getString(String key)
	{
		return <%=genModel.getEditorPluginClassName()%>.INSTANCE.getString(key);
	}

	/**
	 * This looks up a string in plugin.properties, making a substitution.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	private static String getString(String key, Object s1)
	{
		return <%=genModel.getQualifiedEditorPluginClassName()%>.INSTANCE.getString(key, new Object [] { s1 });
	}

	/**
	 * RCP's application
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public static class Application implements IPlatformRunnable
	{
		/**
		 * @see org.eclipse.core.runtime.IPlatformRunnable#run(java.lang.Object)
		 * <!-- begin-user-doc -->
		 * <!-- end-user-doc -->
		 * @generated
		 */
		public Object run(Object args)
		{
			WorkbenchAdvisor workbenchAdvisor = new <%=genModel.getEditorAdvisorClassName()%>();
			Display display = PlatformUI.createDisplay();
			try
			{
				int returnCode = PlatformUI.createAndRunWorkbench(display, workbenchAdvisor);
				if (returnCode == PlatformUI.RETURN_RESTART)
				{
					return IPlatformRunnable.EXIT_RESTART;
				}
				else
				{
					return IPlatformRunnable.EXIT_OK;
				}
			}
			finally
			{
				display.dispose();
			}
		}
	}

	/**
	 * RCP's perspective
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public static class Perspective implements IPerspectiveFactory
	{
		/**
		 * Perspective ID
		 * <!-- begin-user-doc -->
		 * <!-- end-user-doc -->
		 * @generated
		 */
		public static final String ID_PERSPECTIVE = "<%=genModel.getQualifiedEditorAdvisorClassName()%>Perspective";<%=genModel.getNonNLS()%>

		/**
		 * @see org.eclipse.ui.IPerspectiveFactory#createInitialLayout(org.eclipse.ui.IPageLayout)
		 * <!-- begin-user-doc -->
		 * <!-- end-user-doc -->
		 * @generated
		 */
		public void createInitialLayout(IPageLayout layout)
		{
			layout.setEditorAreaVisible(true);
			layout.addPerspectiveShortcut(ID_PERSPECTIVE);

			IFolderLayout right = layout.createFolder("right", IPageLayout.RIGHT, (float)0.66, layout.getEditorArea());<%=genModel.getNonNLS()%>
			right.addView(IPageLayout.ID_OUTLINE);

			IFolderLayout bottonRight = layout.createFolder("bottonRight", IPageLayout.BOTTOM, (float)0.60, "right");<%=genModel.getNonNLS()%><%=genModel.getNonNLS(2)%>
			bottonRight.addView(IPageLayout.ID_PROP_SHEET);
		}
	}

	/**
	 * About action for the RCP application.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public static class AboutAction extends WorkbenchWindowActionDelegate
	{
		/**
		 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
		 * <!-- begin-user-doc -->
		 * <!-- end-user-doc -->
		 * @generated
		 */
		public void run(IAction action)
		{
			MessageDialog.openInformation(getWindow().getShell(), getString("_UI_About_title"),<%=genModel.getNonNLS()%>
			getString("_UI_About_text"));<%=genModel.getNonNLS()%>
		}
	}

	/**
	 * Open action for the objects from the <%=genModel.getModelName()%> model.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public static class OpenAction extends WorkbenchWindowActionDelegate
	{
		/**
		 * Opens the editors for the files selected using the file dialog.
		 * <!-- begin-user-doc -->
		 * <!-- end-user-doc -->
		 * @generated
		 */
		public void run(IAction action)
		{
			String filePath = openFilePathDialog(getWindow().getShell(), null, SWT.OPEN);
			if (filePath != null)
			{
				openEditor(getWindow().getWorkbench(), URI.createFileURI(filePath));
			}
		}
	}

	/**
	 * Open URI action for the objects from the <%=genModel.getModelName()%> model.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public static class OpenURIAction extends WorkbenchWindowActionDelegate
	{
		/**
		 * Opens the editors for the files selected using the LoadResourceDialog.
		 * <!-- begin-user-doc -->
		 * <!-- end-user-doc -->
		 * @generated
		 */
		public void run(IAction action)
		{
			LoadResourceAction.LoadResourceDialog loadResourceDialog = new LoadResourceAction.LoadResourceDialog(getWindow().getShell());
			if (Dialog.OK == loadResourceDialog.open())
			{
				for (Iterator i = loadResourceDialog.getURIs().iterator(); i.hasNext();)
				{
					openEditor(getWindow().getWorkbench(), (URI)i.next());
				}
			}
		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public static String openFilePathDialog(Shell shell, String fileExtensionFilter, int style)
	{
		FileDialog fileDialog = new FileDialog(shell, style);
<% List genPackages = genModel.getAllGenPackagesWithClassifiers(); if (!genPackages.isEmpty()) {%>
		if (fileExtensionFilter == null)
		{
			fileExtensionFilter =
  <%for (Iterator i = genPackages.iterator(); i.hasNext(); ) { GenPackage genPackage = (GenPackage)i.next(); %>
  				"*." + getString("_UI_<%=genPackage.getEditorClassName()%>FilenameExtension")<%if (i.hasNext()) {%> + ";" +<%=genModel.getNonNLS()%><%=genModel.getNonNLS(2)%><%=genModel.getNonNLS(3)%><%} else {%>;<%=genModel.getNonNLS()%><%=genModel.getNonNLS(2)%><%}%>
  <%}%>
		}
		fileDialog.setFilterExtensions(new String[]{fileExtensionFilter});
<%}%>

		fileDialog.open();
		if (fileDialog.getFileName() != null && fileDialog.getFileName().length() > 0)
		{
			return fileDialog.getFilterPath() + File.separator + fileDialog.getFileName();
		}
		else
		{
			return null;
		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public static boolean openEditor(IWorkbench workbench, URI fileURI)
	{
		IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
		IWorkbenchPage page = workbenchWindow.getActivePage();

		IEditorDescriptor editorDescriptor = workbench.getEditorRegistry().getDefaultEditor(fileURI.toFileString());
		if (editorDescriptor == null)
		{
			MessageDialog.openError(
			workbenchWindow.getShell(),
			getString("_UI_Error_title"), <%=genModel.getNonNLS()%>
			getString("_WARN_No_Editor", fileURI.toFileString())); <%=genModel.getNonNLS()%>
			return false;
		}
		else
		{
			try
			{
				page.openEditor(new URIEditorInput(fileURI), editorDescriptor.getId());
			}
			catch (PartInitException exception)
			{
				MessageDialog.openError(
				workbenchWindow.getShell(),
				getString("_UI_OpenEditorError_label"), <%=genModel.getNonNLS()%>
				exception.getMessage());
				return false;
			}
		}

		return true;
	}

	/**
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#getInitialWindowPerspectiveId()
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getInitialWindowPerspectiveId()
	{
		return Perspective.ID_PERSPECTIVE;
	}

	/**
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#initialize(org.eclipse.ui.application.IWorkbenchConfigurer)
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void initialize(IWorkbenchConfigurer configurer)
	{
		super.initialize(configurer);
		configurer.setSaveAndRestore(true);
	}

	/**
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#preWindowOpen(org.eclipse.ui.application.IWorkbenchWindowConfigurer)
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void preWindowOpen(IWorkbenchWindowConfigurer configurer)
	{
		super.preWindowOpen(configurer);
		configurer.setInitialSize(new Point(600, 450));
		configurer.setShowCoolBar(false);
		configurer.setShowStatusLine(true);
		configurer.setTitle(getString("_UI_Application_title"));<%=genModel.getNonNLS()%>
	}

	/**
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#fillActionBars(org.eclipse.ui.IWorkbenchWindow, org.eclipse.ui.application.IActionBarConfigurer, int)
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void fillActionBars(IWorkbenchWindow window, IActionBarConfigurer configurer, int flags)
	{
		super.fillActionBars(window, configurer, flags);
		if ((flags & FILL_MENU_BAR) != 0)
		{
			fillMenuBar(window, configurer);
		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected void fillMenuBar(IWorkbenchWindow window, IActionBarConfigurer configurer)
	{
		IMenuManager menuBar = configurer.getMenuManager();
		menuBar.add(createFileMenu(window));
		menuBar.add(createEditMenu(window));
		menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		menuBar.add(createWindowMenu(window));
		menuBar.add(createHelpMenu(window));
	}

	/**
	 * Creates the 'File' menu.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected IMenuManager createFileMenu(IWorkbenchWindow window)
	{
		IMenuManager menu = new MenuManager(getString("_UI_Menu_File_label"),<%=genModel.getNonNLS()%>
		IWorkbenchActionConstants.M_FILE);    
		menu.add(new GroupMarker(IWorkbenchActionConstants.FILE_START));

		IMenuManager newMenu = new MenuManager(getString("_UI_Menu_New_label"), "new");<%=genModel.getNonNLS()%> <%=genModel.getNonNLS(2)%>
		newMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));

		menu.add(newMenu);
		menu.add(new Separator());
		menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		menu.add(new Separator());
		menu.add(ActionFactory.CLOSE.create(window));
		menu.add(ActionFactory.CLOSE_ALL.create(window));
		menu.add(new Separator());
		menu.add(ActionFactory.SAVE.create(window));
		menu.add(ActionFactory.SAVE_AS.create(window));
		menu.add(ActionFactory.SAVE_ALL.create(window));
		menu.add(new Separator());
		menu.add(ActionFactory.QUIT.create(window));
		menu.add(new GroupMarker(IWorkbenchActionConstants.FILE_END));
		return menu;
	}

	/**
	 * Creates the 'Edit' menu.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected IMenuManager createEditMenu(IWorkbenchWindow window)
	{
		IMenuManager menu = new MenuManager(getString("_UI_Menu_Edit_label"),<%=genModel.getNonNLS()%>
		IWorkbenchActionConstants.M_EDIT);
		menu.add(new GroupMarker(IWorkbenchActionConstants.EDIT_START));

		menu.add(ActionFactory.UNDO.create(window));
		menu.add(ActionFactory.REDO.create(window));
		menu.add(new GroupMarker(IWorkbenchActionConstants.UNDO_EXT));
		menu.add(new Separator());

		menu.add(ActionFactory.CUT.create(window));
		menu.add(ActionFactory.COPY.create(window));
		menu.add(ActionFactory.PASTE.create(window));
		menu.add(new GroupMarker(IWorkbenchActionConstants.CUT_EXT));
		menu.add(new Separator());

		menu.add(ActionFactory.DELETE.create(window));
		menu.add(ActionFactory.SELECT_ALL.create(window));
		menu.add(new Separator());

		menu.add(new GroupMarker(IWorkbenchActionConstants.ADD_EXT));

		menu.add(new GroupMarker(IWorkbenchActionConstants.EDIT_END));
		menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		return menu;
	}

	/**
	 * Creates the 'Window' menu.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected IMenuManager createWindowMenu(IWorkbenchWindow window)
	{
		IMenuManager menu = new MenuManager(getString("_UI_Menu_Window_label"),<%=genModel.getNonNLS()%>
		IWorkbenchActionConstants.M_WINDOW);

		menu.add(ActionFactory.OPEN_NEW_WINDOW.create(window));
		menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		menu.add(ContributionItemFactory.OPEN_WINDOWS.create(window));

		return menu;
	}

	/**
	 * Creates the 'Help' menu.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected IMenuManager createHelpMenu(IWorkbenchWindow window)
	{
		IMenuManager menu = new MenuManager(getString("_UI_Menu_Help_label"), IWorkbenchActionConstants.M_HELP);<%=genModel.getNonNLS()%>
		// Welcome or intro page would go here
		// Help contents would go here
		// Tips and tricks page would go here
		menu.add(new GroupMarker(IWorkbenchActionConstants.HELP_START));
		menu.add(new GroupMarker(IWorkbenchActionConstants.HELP_END));
		menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		return menu;
	}
}
<%genModel.emitSortedImports();%>
