Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a94a678548849250cbd4752e6a92911dc2332967 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST.
 *
 *
 * 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:
 *  Patrick Tessier (CEA LIST) patrick.tessier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.req.reqif.integration.assistant;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.papyrus.editor.PapyrusMultiDiagramEditor;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Element;
/**
 * Abstract wizard that contains method to get the selection, the model Set...
 *
 */
public abstract class AbstractWizardForPapyrus extends Wizard {

	protected TransactionalEditingDomain transactionalEditingDomain = null;
	protected PapyrusMultiDiagramEditor papyrusEditor;
	protected ModelSet modelSet;
	protected ResourceSet resourceSet;

	/**
	 * 
	 * Constructor.
	 *
	 */
	public AbstractWizardForPapyrus() {
		super();
	}

	/**
	 * this method is used in order to initialize  service registry domain and model set: indispensable element in order to execute command
	 * @param workbench the eclipse workbench
	 * @param selection the current Selection
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {

		//get the service registry of papyrus from the selection
		ServicesRegistry registry=null;
		try {
			registry = ServiceUtilsForSelection.getInstance().getServiceRegistry(selection);
		} catch (ServiceException e1) {
			e1.printStackTrace();
		}
		try {
			modelSet = registry.getService(ModelSet.class);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @return the service registry of Papyrus, maybe null 
	 * 
	 */
	public ServicesRegistry getServiceRegistry() {

		IEditorPart editor;
		try {
			editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
			ServicesRegistry serviceRegistry = (ServicesRegistry)editor.getAdapter(ServicesRegistry.class);
			if(serviceRegistry != null) {
				return serviceRegistry;
			}
		} catch (NullPointerException e) {
			// Can't get the active editor
			System.err.println("Can't get the current Eclipse Active Editor. No ServiceRegistry found.");
		}

		// Not found
		System.err.println("Can't get the ServiceRegistry from current Eclipse Active Editor");
		return null;
	}

	/**
	 * 
	 * @return the current modelSet of papyrus
	 * @throws ServiceException
	 */
	protected ModelSet getModelSet() throws ServiceException {
		return getServiceRegistry().getService(ModelSet.class);
	}

	/**
	 * getSelected element in the diagram or in the model explorer
	 * @return Element or null
	 */
	protected ArrayList<Element> getSelectionSet() {
		ArrayList<Element> selectedSet =new ArrayList<Element>();
		ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
		ISelection selection = selectionService.getSelection();


		if(selection instanceof IStructuredSelection) {
			@SuppressWarnings("rawtypes")
			Iterator selectedobjectIteractor = ((IStructuredSelection)selection).iterator();
			while (selectedobjectIteractor.hasNext()) {
				Object currentSelection = selectedobjectIteractor.next();

				EObject selectedEObject = EMFHelper.getEObject(currentSelection);
				if (selectedEObject instanceof org.eclipse.uml2.uml.Package){
					selectedSet.add((Element)selectedEObject);
				}
			}
		}

		return selectedSet;
	}
}

Back to the top