Bug 572946 - [e4][Tooling] support new model-fragment header in the
wizard

Change-Id: I9f511568c1cf964036b16523fcd2db65e71151d4
Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.ui.tools/+/179492
Tested-by: Platform Bot <platform-bot@eclipse.org>
diff --git a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/Messages.java b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/Messages.java
index bc1b40b..d33ff60 100644
--- a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/Messages.java
+++ b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/Messages.java
@@ -22,6 +22,11 @@
  */
 public class Messages extends NLS {
 	private static final String BUNDLE_NAME = "org.eclipse.e4.internal.tools.messages"; //$NON-NLS-1$
+	public static String ContributionMode;
+	public static String ContributionMode_Static;
+	public static String ContributionMode_Dynamic;
+	public static String ContributionMode_Static_Info;
+	public static String ContributionMode_Dynamic_Info;
 	public static String AbstractNewClassPage_Browse;
 	public static String AbstractNewClassPage_ChooseAPackage;
 	public static String AbstractNewClassPage_ChoosePackage;
diff --git a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/messages.properties b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/messages.properties
index 33c49ca..f39fdcb 100644
--- a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/messages.properties
+++ b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/messages.properties
@@ -78,3 +78,10 @@
 NewToolControlClassWizard_CreateGUIMethod=Create GUI Method
 NewToolControlClassWizard_CreateNewToolControl=Create a new tool control class
 NewToolControlClassWizard_NewToolControl=New Tool Control
+ContributionMode=Contribution Mode
+ContributionMode_Static=Static
+ContributionMode_Static_Info=The model fragment will be registered in plugin.xml.\n\
+It will be applied only once in the life-time of the application
+ContributionMode_Dynamic=Dynamic (Eclipse 4.20 or later)
+ContributionMode_Dynamic_Info=The model fragment will be registered via a MANIFEST.MF header.\n\
+it will be refreshed whenever your bundle is updated.
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 70bc88b..6b96776 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
@@ -320,7 +320,7 @@
 	 * Register the fragment.e4xmi with the org.eclipse.e4.workbench.model
 	 * extension point, if there is not already a fragment registered.
 	 */
-	private void registerWithExtensionPointIfRequired(IProject project, WorkspaceBundlePluginModel fModel, IFile file)
+	protected void registerWithExtensionPointIfRequired(IProject project, WorkspaceBundlePluginModel fModel, IFile file)
 			throws CoreException {
 
 		final String WORKBENCH_MODEL_EP_ID = "org.eclipse.e4.workbench.model"; //$NON-NLS-1$
diff --git a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/NewContributionModelWizard.java b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/NewContributionModelWizard.java
index b995d2d..ec13d87 100644
--- a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/NewContributionModelWizard.java
+++ b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/NewContributionModelWizard.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010-2014 BestSolution.at and others.
+ * Copyright (c) 2010-2021 BestSolution.at and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -11,16 +11,58 @@
  * Contributors:
  * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
  * Marco Descher <marco@descher.at> - Bug 392907, Bug 434371
+ * Christoph Läubrich - Bug 572946 - [e4][Tooling] support new model-fragment header in the wizard
  ******************************************************************************/
 package org.eclipse.e4.internal.tools.wizards.model;
 
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.e4.internal.tools.Messages;
 import org.eclipse.e4.ui.model.fragment.MFragmentFactory;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.pde.internal.core.bundle.WorkspaceBundlePluginModel;
+import org.eclipse.pde.internal.core.ibundle.IBundle;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Label;
 
+@SuppressWarnings("restriction")
 public class NewContributionModelWizard extends BaseApplicationModelWizard {
 
+	private static final String MODEL_FRAGMENT_HEADER = "Model-Fragment"; //$NON-NLS-1$
+
+	private enum ContributionMode {
+		STATIC(Messages.ContributionMode_Static, Messages.ContributionMode_Static_Info), DYNAMIC(
+				Messages.ContributionMode_Dynamic, Messages.ContributionMode_Dynamic_Info);
+
+		private String label;
+		private String description;
+
+		ContributionMode(String label, String description) {
+			this.label = label;
+			this.description = description;
+		}
+
+		@Override
+		public String toString() {
+			return label;
+		}
+
+		/**
+		 * @return the description
+		 */
+		String getDescription() {
+			return description;
+		}
+	}
+
 	@Override
 	public String getDefaultFileName() {
 		return "fragment.e4xmi"; //$NON-NLS-1$
@@ -33,11 +75,77 @@
 
 	@Override
 	protected NewModelFilePage createWizardPage(ISelection selection) {
-		return new NewModelFilePage(selection, getDefaultFileName());
+		return new NewContributionModelFilePage(selection, getDefaultFileName());
 	}
 
 	@Override
 	protected void adjustDependencies(IFile file) {
 		super.adjustFragmentDependencies(file);
 	}
+
+	@Override
+	protected void registerWithExtensionPointIfRequired(IProject project, WorkspaceBundlePluginModel fModel, IFile file)
+			throws CoreException {
+		NewContributionModelFilePage page = (NewContributionModelFilePage) getPage(
+				NewContributionModelFilePage.PAGE_NAME);
+		if (page.mode == ContributionMode.DYNAMIC) {
+			IBundle bundle = fModel.getBundleModel().getBundle();
+			bundle.setHeader(MODEL_FRAGMENT_HEADER, file.getName());
+			fModel.save();
+		} else {
+			super.registerWithExtensionPointIfRequired(project, fModel, file);
+		}
+	}
+
+	private static final class NewContributionModelFilePage extends NewModelFilePage {
+
+		ContributionMode mode = ContributionMode.STATIC;
+
+		public NewContributionModelFilePage(ISelection selection, String defaultFilename) {
+			super(selection, defaultFilename);
+		}
+
+		@Override
+		protected void createAdditionalControls(Composite parent) {
+			Label label = new Label(parent, SWT.NULL);
+			label.setText(Messages.ContributionMode);
+			ContributionMode[] values = ContributionMode.values();
+			Button[] buttons = new Button[values.length];
+			Composite buttonParent = new Composite(parent, SWT.NULL);
+			buttonParent.setLayout(new GridLayout(values.length, false));
+			for (int i = 0; i < values.length; i++) {
+				ContributionMode mode = values[i];
+				Button button = buttons[i] = new Button(buttonParent, SWT.RADIO);
+				button.setSelection(this.mode == mode);
+				button.setData(mode);
+				button.setText(mode.toString());
+			}
+			new Label(parent, SWT.NULL);
+			new Label(parent, SWT.NULL);
+			Label modeDescriptionLabel = new Label(parent, SWT.NONE);
+			modeDescriptionLabel.setText(mode.getDescription());
+			GridData modeDescriptionLabelLayoutData = new GridData(GridData.FILL_BOTH);
+			modeDescriptionLabelLayoutData.horizontalSpan = 2;
+			modeDescriptionLabel.setLayoutData(modeDescriptionLabelLayoutData);
+			for (Button button : buttons) {
+				button.addSelectionListener(new SelectionListener() {
+
+					@Override
+					public void widgetSelected(SelectionEvent e) {
+						if (button.getSelection()) {
+							ContributionMode mode = (ContributionMode) button.getData();
+							NewContributionModelFilePage.this.mode = mode;
+							modeDescriptionLabel.setText(mode.getDescription());
+						}
+					}
+
+					@Override
+					public void widgetDefaultSelected(SelectionEvent e) {
+
+					}
+				});
+			}
+		}
+	}
+
 }
\ No newline at end of file
diff --git a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/NewModelFilePage.java b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/NewModelFilePage.java
index 0b15133..8255196 100644
--- a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/NewModelFilePage.java
+++ b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/model/NewModelFilePage.java
@@ -43,6 +43,8 @@
  */
 
 public class NewModelFilePage extends WizardPage {
+	public static final String PAGE_NAME = "wizardPage"; //$NON-NLS-1$
+
 	private Text containerText;
 
 	private Text fileText;
@@ -56,7 +58,7 @@
 	 *
 	 */
 	public NewModelFilePage(ISelection selection, String defaultFilename) {
-		super("wizardPage"); //$NON-NLS-1$
+		super(PAGE_NAME);
 		setTitle(Messages.NewModelFilePage_NewApplicationModel);
 		setDescription(Messages.NewModelFilePage_TheWizardCreates);
 		this.selection = selection;