Bug 462610 - ApplicationPartServiceImpl#findPart violates contract by throwing IllegalStateException

Move EPartService#createPart(MPartDescriptor) to EModelService to allow
creating an MPart from an MPartDescriptor.

Change-Id: I3b66f5b9d0e3996ff308dedf8cdfbe8816c70b88
diff --git a/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/ModelServiceImpl.java b/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/ModelServiceImpl.java
index 168ed0b..a97372c 100644
--- a/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/ModelServiceImpl.java
+++ b/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/ModelServiceImpl.java
@@ -1046,6 +1046,32 @@
 	}
 
 	@Override
+	public MPart createPart(MPartDescriptor descriptor) {
+		if (descriptor == null) {
+			return null;
+		}
+		MPart part = createModelElement(MPart.class);
+		part.setElementId(descriptor.getElementId());
+		part.getMenus().addAll(EcoreUtil.copyAll(descriptor.getMenus()));
+		if (descriptor.getToolbar() != null) {
+			part.setToolbar((MToolBar) EcoreUtil.copy((EObject) descriptor.getToolbar()));
+		}
+		part.setContributorURI(descriptor.getContributorURI());
+		part.setCloseable(descriptor.isCloseable());
+		part.setContributionURI(descriptor.getContributionURI());
+		part.setLabel(descriptor.getLabel());
+		part.setIconURI(descriptor.getIconURI());
+		part.setTooltip(descriptor.getTooltip());
+		part.getHandlers().addAll(EcoreUtil.copyAll(descriptor.getHandlers()));
+		part.getTags().addAll(descriptor.getTags());
+		part.getVariables().addAll(descriptor.getVariables());
+		part.getProperties().putAll(descriptor.getProperties());
+		part.getPersistedState().putAll(descriptor.getPersistedState());
+		part.getBindingContexts().addAll(descriptor.getBindingContexts());
+		return part;
+	}
+
+	@Override
 	public void hideLocalPlaceholders(MWindow window, MPerspective perspective) {
 		List<MPlaceholder> globals = findElements(window, null, MPlaceholder.class, null,
 				OUTSIDE_PERSPECTIVE | IN_SHARED_AREA);
diff --git a/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/PartServiceImpl.java b/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/PartServiceImpl.java
index 69d6544..7ecab32 100644
--- a/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/PartServiceImpl.java
+++ b/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/PartServiceImpl.java
@@ -50,7 +50,6 @@
 import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
 import org.eclipse.e4.ui.model.application.ui.basic.MStackElement;
 import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
-import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
 import org.eclipse.e4.ui.services.EContextService;
 import org.eclipse.e4.ui.services.IServiceConstants;
 import org.eclipse.e4.ui.workbench.IPresentationEngine;
@@ -59,12 +58,13 @@
 import org.eclipse.e4.ui.workbench.modeling.EPartService;
 import org.eclipse.e4.ui.workbench.modeling.IPartListener;
 import org.eclipse.e4.ui.workbench.modeling.ISaveHandler;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.osgi.util.NLS;
 import org.osgi.service.event.Event;
 import org.osgi.service.event.EventHandler;
 
+/**
+ * A window-based {@link EPartService}.
+ */
 public class PartServiceImpl implements EPartService {
 
 	/**
@@ -818,35 +818,10 @@
 		return activePart;
 	}
 
-	private MPart createPart(MPartDescriptor descriptor) {
-		if (descriptor == null) {
-			return null;
-		}
-		MPart part = modelService.createModelElement(MPart.class);
-		part.setElementId(descriptor.getElementId());
-		part.getMenus().addAll(EcoreUtil.copyAll(descriptor.getMenus()));
-		if (descriptor.getToolbar() != null) {
-			part.setToolbar((MToolBar) EcoreUtil.copy((EObject) descriptor.getToolbar()));
-		}
-		part.setContributorURI(descriptor.getContributorURI());
-		part.setCloseable(descriptor.isCloseable());
-		part.setContributionURI(descriptor.getContributionURI());
-		part.setLabel(descriptor.getLabel());
-		part.setIconURI(descriptor.getIconURI());
-		part.setTooltip(descriptor.getTooltip());
-		part.getHandlers().addAll(EcoreUtil.copyAll(descriptor.getHandlers()));
-		part.getTags().addAll(descriptor.getTags());
-		part.getVariables().addAll(descriptor.getVariables());
-		part.getProperties().putAll(descriptor.getProperties());
-		part.getPersistedState().putAll(descriptor.getPersistedState());
-		part.getBindingContexts().addAll(descriptor.getBindingContexts());
-		return part;
-	}
-
 	@Override
 	public MPart createPart(String id) {
 		MPartDescriptor descriptor = modelService.getPartDescriptor(id);
-		return createPart(descriptor);
+		return modelService.createPart(descriptor);
 	}
 
 	@Override
@@ -872,7 +847,7 @@
 
 		if (sharedPart == null) {
 			MPartDescriptor descriptor = modelService.getPartDescriptor(id);
-			sharedPart = createPart(descriptor);
+			sharedPart = modelService.createPart(descriptor);
 			if (sharedPart == null) {
 				return null;
 			}
@@ -1180,7 +1155,7 @@
 		MPart part = findPart(id);
 		if (part == null) {
 			MPartDescriptor descriptor = modelService.getPartDescriptor(id);
-			part = createPart(descriptor);
+			part = modelService.createPart(descriptor);
 			if (part == null) {
 				return null;
 			}
diff --git a/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/modeling/EModelService.java b/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/modeling/EModelService.java
index 9f693f0..31843e2 100644
--- a/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/modeling/EModelService.java
+++ b/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/modeling/EModelService.java
@@ -22,6 +22,7 @@
 import org.eclipse.e4.ui.model.application.ui.SideValue;
 import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
 import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainerElement;
 import org.eclipse.e4.ui.model.application.ui.basic.MTrimBar;
 import org.eclipse.e4.ui.model.application.ui.basic.MTrimmedWindow;
@@ -508,6 +509,17 @@
 	public MPartDescriptor getPartDescriptor(String id);
 
 	/**
+	 * Creates a new part from the given descriptor.
+	 *
+	 * @param descriptor
+	 *            a part descriptor, must not be <code>null</code>
+	 * @return a new part
+	 * @see EPartService#createPart(String)
+	 * @since 1.5
+	 */
+	public MPart createPart(MPartDescriptor descriptor);
+
+	/**
 	 * This method ensures that there will never be two placeholders for the same referenced element
 	 * visible in the presentation at the same time. It does this by hiding placeholders which are
 	 * contained in any MPerspective if there is a placeholder for the element in any 'shared' area