Merge branch 'fragmentextractor'
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF
index 7874eea..2f80d06 100644
--- a/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.e4.tools.emf.ui/META-INF/MANIFEST.MF
@@ -30,7 +30,8 @@
  org.eclipse.e4.core.di.extensions;bundle-version="0.9.0",
  org.eclipse.jface.text;bundle-version="3.7.0",
  org.eclipse.e4.ui.widgets;bundle-version="0.11.0",
- org.eclipse.equinox.preferences;bundle-version="3.4.0"
+ org.eclipse.equinox.preferences;bundle-version="3.4.0",
+ org.eclipse.e4.tools;bundle-version="0.12.0"
 Bundle-ActivationPolicy: lazy
 Import-Package: javax.annotation;version="1.0.0",
  javax.inject;version="1.0.0",
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.java
index 206ca12..8419240 100644
--- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.java
+++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.java
@@ -620,4 +620,6 @@
 	public String Special_UnknownElement;
 	public String Special_UnknownElement_Detail;
 	public String VWindowSharedElementsEditor_Area;
+	public String ModelEditor_ExtractFragment;
+	public String ModelEditor_ExtractFragment_NoParentId;
 }
\ No newline at end of file
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.properties b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.properties
index 9bb590a..9fb51c7 100644
--- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.properties
+++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/Messages.properties
@@ -620,4 +620,6 @@
 CommandWizard_ShellTitle=Command/Handler wizard
 Special_UnknownElement=(unknown model element)
 Special_UnknownElement_Detail= Use an extension point!
-VWindowSharedElementsEditor_Area=Area
\ No newline at end of file
+VWindowSharedElementsEditor_Area=Area
+ModelEditor_ExtractFragment=Extract into a fragment
+ModelEditor_ExtractFragment_NoParentId=Can not extract because parent container has no id. \nAdd an id and try again.
\ No newline at end of file
diff --git a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ModelEditor.java b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ModelEditor.java
index 0c222e9..be8be02 100644
--- a/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ModelEditor.java
+++ b/bundles/org.eclipse.e4.tools.emf.ui/src/org/eclipse/e4/tools/emf/ui/internal/common/ModelEditor.java
@@ -12,6 +12,7 @@
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import javax.annotation.PostConstruct;
@@ -41,6 +42,7 @@
 import org.eclipse.e4.core.di.annotations.Optional;
 import org.eclipse.e4.core.di.extensions.Preference;
 import org.eclipse.e4.core.services.contributions.IContributionFactory;
+import org.eclipse.e4.internal.tools.wizards.model.ExtractContributionModelWizard;
 import org.eclipse.e4.tools.emf.ui.common.EStackLayout;
 import org.eclipse.e4.tools.emf.ui.common.IContributionClassCreator;
 import org.eclipse.e4.tools.emf.ui.common.IEditorDescriptor;
@@ -133,6 +135,7 @@
 import org.eclipse.e4.tools.services.Translation;
 import org.eclipse.e4.ui.di.Focus;
 import org.eclipse.e4.ui.di.Persist;
+import org.eclipse.e4.ui.model.application.MApplication;
 import org.eclipse.e4.ui.model.application.MApplicationElement;
 import org.eclipse.e4.ui.model.application.commands.impl.CommandsPackageImpl;
 import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl;
@@ -170,6 +173,7 @@
 import org.eclipse.jface.action.Separator;
 import org.eclipse.jface.databinding.viewers.ObservableListTreeContentProvider;
 import org.eclipse.jface.databinding.viewers.TreeStructureAdvisor;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.resource.JFaceResources;
 import org.eclipse.jface.resource.StringConverter;
@@ -191,6 +195,7 @@
 import org.eclipse.jface.viewers.TreeViewer;
 import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.jface.viewers.ViewerDropAdapter;
+import org.eclipse.jface.wizard.WizardDialog;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.CTabFolder;
 import org.eclipse.swt.custom.CTabItem;
@@ -596,7 +601,6 @@
 					List<Action> actions;
 					if (s.getFirstElement() instanceof VirtualEntry<?>) {
 						actions = virtualEditors.get(((VirtualEntry<?>) s.getFirstElement()).getId()).getActions(s.getFirstElement());
-
 						if (actions.size() > 0) {
 							MenuManager addMenu = new MenuManager(messages.ModelEditor_AddChild);
 							for (Action a : actions) {
@@ -605,6 +609,38 @@
 							}
 							manager.add(addMenu);
 						}
+
+						// build the extract action
+						if ((!((VirtualEntry<?>) s.getFirstElement()).getList().isEmpty()) && (!isModelFragment()))
+							manager.add(new Action(messages.ModelEditor_ExtractFragment, ImageDescriptor.createFromImage(resourcePool.getImageUnchecked(ResourceProvider.IMG_ModelFragments))) {
+								public void run() {
+									VirtualEntry<?> ve = (VirtualEntry<?>) s.getFirstElement();
+									EObject container = (EObject) ve.getOriginalParent();
+									String containerId = ((MApplicationElement) container).getElementId();
+									if (containerId == null || containerId.isEmpty()) {
+										MessageDialog.openError(viewer.getControl().getShell(), null, messages.ModelEditor_ExtractFragment_NoParentId);
+										return;
+									}
+									ArrayList<MApplicationElement> maes = new ArrayList<MApplicationElement>();
+									IObservableList list = ve.getList();
+									Iterator iterator = list.iterator();
+									while (iterator.hasNext()) {
+										maes.add((MApplicationElement) iterator.next());
+									}
+									ExtractContributionModelWizard extractContributionModelWizard = new ExtractContributionModelWizard(maes);
+									extractContributionModelWizard.setup(project);
+									WizardDialog wizardDialog = new WizardDialog(viewer.getControl().getShell(), extractContributionModelWizard);
+									if (wizardDialog.open() == WizardDialog.OK) {
+										for (MApplicationElement mae : maes) {
+											Command cmd = DeleteCommand.create(ModelEditor.this.modelProvider.getEditingDomain(), mae);
+											if (cmd.canExecute()) {
+												ModelEditor.this.modelProvider.getEditingDomain().getCommandStack().execute(cmd);
+											}
+										}
+									}
+								};
+							});
+
 					} else {
 						final EObject o = (EObject) s.getFirstElement();
 						AbstractComponentEditor editor = getEditor(o.eClass());
@@ -709,6 +745,30 @@
 					}
 
 				}
+				if ((s.getFirstElement() instanceof MApplicationElement) && (!isModelFragment()) && (!(s.getFirstElement() instanceof MApplication)))
+					manager.add(new Action(messages.ModelEditor_ExtractFragment, ImageDescriptor.createFromImage(resourcePool.getImageUnchecked(ResourceProvider.IMG_ModelFragments))) {
+						public void run() {
+							MApplicationElement oe = (MApplicationElement) s.getFirstElement();
+							EObject container = ((EObject) oe).eContainer();
+							String containerId = ((MApplicationElement) container).getElementId();
+							if (containerId == null || containerId.isEmpty()) {
+								MessageDialog.openError(viewer.getControl().getShell(), null, messages.ModelEditor_ExtractFragment_NoParentId);
+								return;
+							}
+							ExtractContributionModelWizard extractContributionModelWizard = new ExtractContributionModelWizard(oe);
+							extractContributionModelWizard.setup(project);
+							WizardDialog wizardDialog = new WizardDialog(viewer.getControl().getShell(), extractContributionModelWizard);
+							if (wizardDialog.open() == WizardDialog.OK) {
+								Command cmd = DeleteCommand.create(ModelEditor.this.modelProvider.getEditingDomain(), oe);
+								if (cmd.canExecute()) {
+									ModelEditor.this.modelProvider.getEditingDomain().getCommandStack().execute(cmd);
+								}
+							}
+
+						};
+
+					});
+
 			}
 		});
 		viewer.getControl().setMenu(mgr.createContextMenu(viewer.getControl()));
diff --git a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/BaseApplicationModelWizard.java b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/BaseApplicationModelWizard.java
index 72f8463..da32d9f 100644
--- a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/BaseApplicationModelWizard.java
+++ b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/BaseApplicationModelWizard.java
@@ -157,6 +157,7 @@
 			return true;
 		}
 		catch (Exception exception) {
+			exception.printStackTrace();
 			MessageDialog.openError(getShell(), "Error", exception.getMessage());
 			return false;
 		}
diff --git a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/ExtractContributionModelWizard.java b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/ExtractContributionModelWizard.java
new file mode 100644
index 0000000..c5f3d52
--- /dev/null
+++ b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/ExtractContributionModelWizard.java
@@ -0,0 +1,75 @@
+/*******************************************************************************
+ * Copyright (c) 2010 BestSolution.at 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:
+ *     Sopot Cela <sopotcela@gmail.com> - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.e4.internal.tools.wizards.model;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.e4.ui.model.application.MApplicationElement;
+import org.eclipse.e4.ui.model.fragment.MFragmentFactory;
+import org.eclipse.e4.ui.model.fragment.MModelFragments;
+import org.eclipse.e4.ui.model.fragment.MStringModelFragment;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.PlatformUI;
+
+
+public class ExtractContributionModelWizard extends BaseApplicationModelWizard {
+
+	private List<MApplicationElement> oes= new ArrayList<MApplicationElement>();
+
+	public ExtractContributionModelWizard(MApplicationElement oe){
+		super();
+		oes.add(oe);
+	}
+	
+	public ExtractContributionModelWizard(List<MApplicationElement> oes){
+		this.oes.addAll(oes);
+	}
+	
+	public ExtractContributionModelWizard(){
+		super();
+	}
+	
+	@Override
+	public String getDefaultFileName() {
+		return "fragment.e4xmi";
+	}
+	
+	protected EObject createInitialModel() {
+		MModelFragments createModelFragments = MFragmentFactory.INSTANCE.createModelFragments();
+		MStringModelFragment createStringModelFragment = MFragmentFactory.INSTANCE.createStringModelFragment();
+		for (MApplicationElement moe : oes) {
+			
+		MApplicationElement e = (MApplicationElement) EcoreUtil.copy((EObject) moe);
+		String featurename = ((EObject) moe).eContainmentFeature().getName();
+		createStringModelFragment.setParentElementId(((MApplicationElement) ((EObject) moe).eContainer()).getElementId());
+		createStringModelFragment.getElements().add(e);
+		createStringModelFragment.setFeaturename(featurename);
+		}
+		createModelFragments.getFragments().add(createStringModelFragment);
+		return (EObject)createModelFragments;
+	}
+	
+	public void setup(IProject project){
+		this.init(PlatformUI.getWorkbench(),new StructuredSelection(project));
+	}
+	@Override
+	protected NewModelFilePage createWizardPage(ISelection selection) {
+		return new NewModelFilePage(selection,getDefaultFileName());
+	}
+	
+}
\ No newline at end of file