Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b38905bf7014eb0244e40a36d7781083af01bfb (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
/*****************************************************************************
 * Copyright (c) 2012-2017 CEA LIST, 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:
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.core.resource.sasheditor;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.infra.core.architecture.ArchitectureDescription;
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.IModel;
import org.eclipse.papyrus.infra.core.resource.ModelSet;

/**
 * Set of utility methods linked to Di resource.
 *
 * @author cedric dumoulin
 *
 */
public class DiModelUtils {

	/**
	 * Returns the related di file. Warning : this method is here for historical
	 * reasons. It should be removed asap.
	 *
	 * @param file
	 *            A file (di, model or notation).
	 * @return The associated DI file.
	 * @deprecated No replacement.
	 */
	@Deprecated
	public static IFile getRelatedDiFile(IFile file) {
		if (file == null) {
			return null;
		}
		IFile diFile;
		if (DiModel.MODEL_FILE_EXTENSION.equalsIgnoreCase(file.getFileExtension())) {
			diFile = file;
		} else {
			// Find the correct file
			IPath diPath = file.getFullPath().removeFileExtension().addFileExtension(DiModel.MODEL_FILE_EXTENSION);
			diFile = file.getParent().getFile(diPath.makeRelativeTo(file.getParent().getFullPath()));
		}
		return diFile;
	}

	/**
	 * Returns the DI Resource (.di) associated to the model set. May be null.
	 *
	 * @param modelSet
	 * @return
	 */
	public static Resource getDiResource(ModelSet modelSet) {
		IModel diModel = modelSet.getModel(DiModel.DI_MODEL_ID);
		if (diModel instanceof DiModel) {
			return ((DiModel) diModel).getResource();
		}
		return null;
	}

	/**
	 * Gets an architecture description element if available in the given model set
	 * 
	 * @param modelSet the given model set
	 * @return an architecture description (can be null)
	 * @since 2.3
	 */
	public static ArchitectureDescription getArchitectureDescription(ModelSet modelSet) {
		Resource resource = getDiResource(modelSet);
		if (resource != null) {
			return (ArchitectureDescription) EcoreUtil.getObjectByType(
					resource.getContents(), ArchitecturePackage.Literals.ARCHITECTURE_DESCRIPTION);
		}
		return null;
	}

	/**
	 * Gets an architecture description element if available in the given model set or adds one if not available
	 * 
	 * @param modelSet the given model set
	 * @return an architecture description
	 * @since 2.3
	 */
	public static ArchitectureDescription getOrAddArchitectureDescription(ModelSet modelSet) {
		Resource resource = getDiResource(modelSet);
		if (resource != null) {
			ArchitectureDescription description = (ArchitectureDescription) 
					EcoreUtil.getObjectByType(resource.getContents(), 
							ArchitecturePackage.Literals.ARCHITECTURE_DESCRIPTION);
			if (description == null) {
				description = ArchitectureFactory.eINSTANCE.createArchitectureDescription();
				resource.getContents().add(description);
			}
			return description;
		}
		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 2.3
	 */
	public static ArchitectureDescriptionPreferences getArchitectureDescriptionPreferences(ModelSet modelSet) {
		Resource resource = getDiResource(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 2.3
	 */
	public static ArchitectureDescriptionPreferences getOrAddArchitectureDescriptionPreferences(ModelSet modelSet) {
		Resource resource = getDiResource(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