1 /*******************************************************************************
2  * Copyright (c) 2013 Remain BV, Industrial-TSI BV and others.
3  *
4  *
5  * This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License 2.0
7  * which accompanies this distribution, and is available at
8  * https://www.eclipse.org/legal/epl-2.0/
9  *
10  * SPDX-License-Identifier: EPL-2.0
11  *
12  * Contributors:
13  * Wim Jongmam <wim.jongman@remainsoftware.com> - initial API and implementation
14  * Steven Spungin <steven@spungin.tv> - Ongoing Maintenance
15  ******************************************************************************/
16 package org.eclipse.e4.tools.emf.ui.internal.imp;
17 
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.e4.tools.emf.ui.common.component.AbstractComponentEditor;
21 import org.eclipse.e4.tools.emf.ui.internal.ResourceProvider;
22 import org.eclipse.e4.tools.services.IResourcePool;
23 import org.eclipse.e4.ui.model.application.MApplication;
24 import org.eclipse.e4.ui.model.application.MApplicationElement;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.wizard.Wizard;
27 
28 public class ModelImportWizard extends Wizard {
29 
30 	private final Class<? extends MApplicationElement> applicationElement;
31 
32 	private ModelImportPage1 page1;
33 
34 	private final MApplication application;
35 
36 	private final AbstractComponentEditor<?> editor;
37 
38 	private final String hint;
39 
ModelImportWizard(Class<? extends MApplicationElement> applicationElement, AbstractComponentEditor<?> editor, IResourcePool resourcePool)40 	public ModelImportWizard(Class<? extends MApplicationElement> applicationElement, AbstractComponentEditor<?> editor,
41 			IResourcePool resourcePool) {
42 		this(applicationElement, editor, "", resourcePool); //$NON-NLS-1$
43 	}
44 
ModelImportWizard(Class<? extends MApplicationElement> applicationElement, AbstractComponentEditor<?> editor, String hint, IResourcePool resourcePool)45 	public ModelImportWizard(Class<? extends MApplicationElement> applicationElement, AbstractComponentEditor<?> editor,
46 			String hint, IResourcePool resourcePool) {
47 		this.applicationElement = applicationElement;
48 		this.editor = editor;
49 		this.hint = hint;
50 		Object modelSelection = editor.getEditor().getModelProvider().getRoot().get(0);
51 		if (modelSelection instanceof MApplication) {
52 			application = (MApplication) modelSelection;
53 		} else {
54 			application = null;
55 		}
56 		setWindowTitle(Messages.ModelImportWizard_Model
57 			+ " " + applicationElement.getSimpleName() + " " + Messages.ModelImportWizard_ImportWizard); //$NON-NLS-1$ //$NON-NLS-2$
58 		setDefaultPageImageDescriptor(ImageDescriptor.createFromImage(resourcePool
59 			.getImageUnchecked(ResourceProvider.IMG_Wizban16_imp3x_wiz)));
60 		Assert.isNotNull(RegistryUtil.getStruct(applicationElement, getHint()),
61 			Messages.ModelImportWizard_UnknownElement + ": " + applicationElement.getClass().getName()); //$NON-NLS-1$
62 	}
63 
64 	@Override
addPages()65 	public void addPages() {
66 		page1 = new ModelImportPage1();
67 		page1.setWizard(this);
68 		addPage(page1);
69 	}
70 
71 	@Override
performFinish()72 	public boolean performFinish() {
73 
74 		return true;
75 	}
76 
77 	/**
78 	 * @return the {@link MApplicationElement} passed in the constructor.
79 	 */
getApplicationElement()80 	public Class<? extends MApplicationElement> getApplicationElement() {
81 		return applicationElement;
82 	}
83 
84 	/**
85 	 * @return the extension point name associated with the {@link MApplicationElement} that is passed in the
86 	 *         constructor of
87 	 *         this wizard.
88 	 * @see #MAPPING_EXTENSION
89 	 * @see #getApplicationElement()
90 	 */
getExtensionPointName()91 	protected String getExtensionPointName() {
92 		return RegistryUtil.getStruct(applicationElement, getHint()).getExtensionPointName();
93 	}
94 
95 	/**
96 	 * @return the extension point id associated with the {@link MApplicationElement} that is passed in the constructor
97 	 *         of
98 	 *         this wizard.
99 	 * @see #MAPPING_EXTENSION
100 	 * @see #getApplicationElement()
101 	 */
getExtensionPoint()102 	protected String getExtensionPoint() {
103 		return RegistryUtil.getStruct(applicationElement, getHint()).getExtensionPoint();
104 	}
105 
106 	/**
107 	 * @return the attribute name of the {@link IConfigurationElement} that
108 	 *         contains the description that you want to see in the wizard page.
109 	 * @see #MAPPING_NAME
110 	 */
getMappingName()111 	protected String getMappingName() {
112 		return RegistryUtil.getStruct(applicationElement, getHint()).getMappingName();
113 	}
114 
115 	/**
116 	 * Returns the list of {@link MApplicationElement}s of the type passed in
117 	 * the constructor of the wizard.
118 	 *
119 	 *
120 	 *
121 	 * @return an array of {@link MApplicationElement}
122 	 */
getElements(Class<? extends MApplicationElement> type)123 	public MApplicationElement[] getElements(Class<? extends MApplicationElement> type) {
124 		return RegistryUtil.getModelElements(type, getHint(), application, page1.getConfigurationElements());
125 	}
126 
getEditor()127 	public AbstractComponentEditor<?> getEditor() {
128 		return editor;
129 	}
130 
131 	/**
132 	 * Returns if this is a live model.
133 	 *
134 	 * @return true or false
135 	 */
isLiveModel()136 	public boolean isLiveModel() {
137 		return editor.getEditor().isLiveModel();
138 	}
139 
140 	/**
141 	 * Returns the hint that explains the meaning of the caller.
142 	 *
143 	 * @return the hint as a String
144 	 */
getHint()145 	public String getHint() {
146 		return hint;
147 	}
148 }
149