Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9af74aba617baacc0dbe95b72e91032ffbab1bfb (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*****************************************************************************
 * Copyright (c) 2011, 2016 LIFL, CEA LIST, Christian W. Damus, 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:
 *  LIFL - Initial API and implementation
 *  Christian W. Damus - bug 434983
 *  Christian W. Damus - bug 469188
 *  Christian W. Damus - bug 485220
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.core.resource.sasheditor;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.infra.core.architecture.ArchitectureDescriptionPreferences;
import org.eclipse.papyrus.infra.core.architecture.ArchitectureFactory;
import org.eclipse.papyrus.infra.core.architecture.ArchitecturePackage;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ModelUtils;
import org.eclipse.papyrus.infra.core.sashwindows.di.DiPackage;
import org.eclipse.papyrus.infra.core.sashwindows.di.SashWindowsMngr;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;

/**
 * Set of utility methods linked to Trace for ControlMode
 *
 * @author cedric dumoulin
 *
 */
public class SashModelUtils {

	/**
	 * Gets the SashModel for the currently selected editor. <br>
	 * Warning: This method is designed to be call from ui.handlers. It is not
	 * designed to be call from Editors. This method can return null if called
	 * during the MultiEditor initialization.
	 *
	 * @see ServiceUtilsForActionHandlers.getInstance().getModelSet()
	 *
	 * @return The {@link SashModel} of the current editor, or null if not
	 *         found.
	 * @deprecated Use {@link #getSashModel(ModelSet)} or {@link #getSashModel(ServicesRegistry)} instead
	 */
	@Deprecated
	public static SashModel getSashModel() {

		try {
			return (SashModel) ServiceUtils.getInstance().getModelSet(null).getModel(SashModel.MODEL_ID);
		} catch (ServiceException e) {
			return null;
		}
	}

	/**
	 * Gets the SashModel for the currently selected editor. <br>
	 * Warning: This method is designed to be call from ui.handlers. It is not
	 * designed to be call from Editors. This method can return null if called
	 * during the MultiEditor initialization.
	 *
	 * @see ServiceUtilsForActionHandlers.getInstance().getModelSet()
	 *
	 * @return The {@link SashModel} of the current editor, or null if not
	 *         found.
	 * @throws ServiceException
	 *             If an error occurs while getting or starting the service.
	 * @deprecated Use {@link #getSashModelChecked(ServicesRegistry)} instead
	 */
	@Deprecated
	public static SashModel getSashModelChecked() throws ServiceException {

		return (SashModel) ServiceUtils.getInstance().getModelSet(null).getModel(SashModel.MODEL_ID);
	}

	/**
	 * Gets the SashModel from the {@link ModelSet}. <br>
	 *
	 * @param modelsManager
	 *            The modelManager containing the requested model.
	 *
	 * @return The {@link SashModel} registered in modelManager, or null if not
	 *         found.
	 */
	public static SashModel getSashModel(ModelSet modelsManager) {

		return (SashModel) modelsManager.getModel(SashModel.MODEL_ID);
	}

	/**
	 * Gets the SashModel from the {@link ModelSet}. <br>
	 *
	 * @param ServicesRegistry
	 *            The servie registry under which the ModelSet is registered.
	 *
	 * @return The {@link SashModel} registered in modelManager, or null if not
	 *         found.
	 */
	public static SashModel getSashModel(ServicesRegistry servicesRegistry) {

		try {
			return (SashModel) ModelUtils.getModelSetChecked(servicesRegistry).getModel(SashModel.MODEL_ID);
		} catch (ServiceException e) {
			return null;
		}
	}

	/**
	 * Gets the SashModel from the {@link ModelSet}. <br>
	 *
	 * @param ServicesRegistry
	 *            The servie registry under which the ModelSet is registered.
	 *
	 * @return The {@link SashModel} registered in modelManager, or null if not
	 *         found.
	 * @throws ServiceException
	 *             If the service can't be returned.
	 */
	public static SashModel getSashModelChecked(ServicesRegistry servicesRegistry) throws ServiceException {

		return (SashModel) ModelUtils.getModelSetChecked(servicesRegistry).getModel(SashModel.MODEL_ID);
	}

	/**
	 * Retrieve the uri of the initial di opened
	 *
	 * @param modelSet
	 * @return
	 */
	public static URI getInitialURI(ModelSet modelSet) {
		return SashModelUtils.getSashModel(modelSet).getResourceURI();
	}

	/**
	 * Returns the Sash Resource (.di or *.sash) associated to the model set. May be null.
	 *
	 * @param modelSet
	 * @return
	 */
	public static Resource getSashResource(ModelSet modelSet) {
		SashModel model = getSashModel(modelSet);
		if (model != null) {
			return model.getResource();
		}
		return null;
	}

	/**
	 * @since 2.0
	 */
	public static SashWindowsMngr getSashWindowsMngr(ModelSet modelSet) {
		SashWindowsMngr result = null;

		Resource resource = getSashResource(modelSet);
		if (resource != null) {
			result = (SashWindowsMngr) EcoreUtil.getObjectByType(resource.getContents(), DiPackage.Literals.SASH_WINDOWS_MNGR);
		}

		return result;
	}

	/**
	 * Gets an architecture description preferences element if available in the given model set
	 * 
	 * @param modelSet
	 *            the given model set
	 * @return an architecture description preferences (can be null)
	 * @since 3.0
	 */
	public static ArchitectureDescriptionPreferences getArchitectureDescriptionPreferences(ModelSet modelSet) {
		Resource resource = getSashResource(modelSet);
		if (resource != null) {
			return (ArchitectureDescriptionPreferences) EcoreUtil.getObjectByType(
					resource.getContents(), ArchitecturePackage.Literals.ARCHITECTURE_DESCRIPTION_PREFERENCES);
		}
		return null;
	}

	/**
	 * Gets an architecture description preferences element if available in the given model set
	 * 
	 * @param modelSet
	 *            the given model set
	 * @return an architecture description preferences (can be null)
	 * @since 3.0
	 */
	public static ArchitectureDescriptionPreferences getOrAddArchitectureDescriptionPreferences(ModelSet modelSet) {
		Resource resource = getSashResource(modelSet);
		if (resource != null) {
			ArchitectureDescriptionPreferences preferences = (ArchitectureDescriptionPreferences) EcoreUtil.getObjectByType(resource.getContents(),
					ArchitecturePackage.Literals.ARCHITECTURE_DESCRIPTION_PREFERENCES);
			if (preferences == null) {
				preferences = ArchitectureFactory.eINSTANCE.createArchitectureDescriptionPreferences();
				resource.getContents().add(preferences);
			}
			return preferences;
		}
		return null;
	}

}

Back to the top