/
LemmaUiUtils.xtend
281 lines (247 loc) · 10 KB
/
LemmaUiUtils.xtend
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package de.fhdo.lemma.eclipse.ui.utils
import org.eclipse.ui.PlatformUI
import org.eclipse.core.resources.ResourcesPlugin
import org.eclipse.core.resources.IResource
import org.eclipse.core.resources.IFolder
import java.util.ArrayDeque
import org.eclipse.core.resources.IFile
import java.util.List
import org.eclipse.core.resources.IProject
import org.osgi.framework.FrameworkUtil
import org.eclipse.core.runtime.FileLocator
import org.eclipse.jface.resource.ImageDescriptor
import org.eclipse.core.runtime.Path
import org.eclipse.jface.resource.ResourceManager
import org.eclipse.emf.common.util.URI
import org.eclipse.xtext.resource.XtextResource
import org.eclipse.xtext.validation.CheckMode
import org.eclipse.xtext.util.CancelIndicator
import org.eclipse.xtext.EcoreUtil2
import org.eclipse.xtext.resource.IResourceServiceProvider
import org.eclipse.emf.ecore.resource.ResourceSet
import com.google.common.base.Function
import org.eclipse.emf.ecore.xmi.XMIResource
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.swt.widgets.Shell
import org.eclipse.ui.IEditorPart
import java.io.File
import org.eclipse.ui.handlers.HandlerUtil
import org.eclipse.core.commands.ExecutionEvent
import org.eclipse.jface.viewers.IStructuredSelection
import org.eclipse.core.runtime.IAdaptable
/**
* Utility class for the LEMMA UI plugin.
*
* @author <a href="mailto:florian.rademacher@fh-dortmund.de">Florian Rademacher</a>
*/
final class LemmaUiUtils {
public static val DATA_DSL_EDITOR_ID = "de.fhdo.lemma.data.DataDsl"
public static val MAPPING_DSL_EDITOR_ID = "de.fhdo.lemma.technology.mappingdsl.MappingDsl"
public static val OPERATION_DSL_EDITOR_ID = "de.fhdo.lemma.operationdsl.OperationDsl"
public static val SERVICE_DSL_EDITOR_ID = "de.fhdo.lemma.ServiceDsl"
public static val TECHNOLOGY_DSL_EDITOR_ID = "de.fhdo.lemma.technology.technologydsl"
/**
* Helper to get file extensions for registered DSL editors
*/
static def getFileExtensions(String... forEditorIds) {
val fileEditorMappings = PlatformUI.workbench.editorRegistry.fileEditorMappings
val editorDescriptions = fileEditorMappings.filter[
editors.map[id].exists[forEditorIds.contains(it)]
]
return editorDescriptions.map[extension].toList
}
/**
* Walk through workspace projects and find files with given extensions. If no extensions are
* specified, all files of all projects will be retrieved.
*/
static def findFilesInWorkspaceProjects(String... extensions) {
val resultFiles = <IProject, List<IFile>> newHashMap
val projects = ResourcesPlugin.workspace.root.projects
projects.forEach[
val resultFilesForProject = findFilesInProject(extensions)
if (!resultFilesForProject.empty)
resultFiles.put(it, resultFilesForProject)
]
return resultFiles
}
/**
* Find files in project with the given extensions. If no extensions are specified, all files
* will be retrieved.
*/
static def findFilesInProject(IProject project, String... extensions) {
if (project === null)
return null
if (!project.open)
return emptyList
val foldersAndFiles = project.members.filter[it instanceof IFolder || it instanceof IFile]
val resourcesTodo = new ArrayDeque<IResource>(foldersAndFiles.toList)
val resultFiles = <IFile> newArrayList
while (!resourcesTodo.empty) {
val resource = resourcesTodo.pop
if (resource instanceof IFile) {
if (extensions.empty)
resultFiles.add(resource)
else if (hasExtension(resource, extensions))
resultFiles.add(resource)
} else if (resource instanceof IFolder)
resource.members.filter[it instanceof IFolder || it instanceof IFile].forEach[
resourcesTodo.push(it)
]
}
return resultFiles
}
/**
* Check if a file has one of the given extensions
*/
static def hasExtension(IFile file, String... fileExtensions) {
if (file.fileExtension === null || file.fileExtension.empty)
return false
val extensionLowerCase = file.fileExtension.toLowerCase
return fileExtensions.map[toLowerCase].contains(extensionLowerCase)
}
/**
* Create an Image for the given filename with the given resource manager
*/
static def createImage(ResourceManager resourceManager, Class<?> clazz, String filename) {
return resourceManager.createImage(createImageDescriptor(clazz, filename))
}
/**
* Create an ImageDescriptor for the given filename and the bundle of this class
*/
static def createImageDescriptor(Class<?> clazz, String filename) {
val bundle = FrameworkUtil.getBundle(clazz)
val url = FileLocator.find(bundle, new Path("icons/" + filename), null)
return ImageDescriptor.createFromURL(url)
}
/**
* Load an Xtext file as XtextResource
*/
static def loadXtextResource(IFile file) {
val resourceSet = createResourceSetFor(file)
if (resourceSet === null)
return null
val fileUri = URI.createPlatformResourceURI(file.fullPath.toString, true)
val fileResource = resourceSet.getResource(fileUri, true)
fileResource.load(null)
return fileResource as XtextResource
}
/**
* Load an XMI file
*/
static def loadXmiResource(IFile file) {
val resourceSet = new ResourceSetImpl()
val fullPathUri = URI.createPlatformResourceURI(file.fullPath.toString, true)
val resource = resourceSet.getResource(fullPathUri, true)
resource.load(null)
return resource as XMIResource
}
/**
* Store file in XMI interchange format
*/
static def storeAsXmi(IFile file, String targetFilePath) {
val xtextResource = loadXtextResource(file)
EcoreUtil2.resolveAll(xtextResource)
val resourceSet = createResourceSetFor(file)
val xmiResource = resourceSet.createResource(URI.createURI(targetFilePath))
xmiResource.contents.add(xtextResource.contents.get(0))
xmiResource.save(null)
}
/**
* Validate an Xtext resource
*/
static def validateXtextResource(XtextResource resource) {
val validator = resource.resourceServiceProvider.resourceValidator
val issues = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl)
return issues
}
/**
* Remove extension from filename and return filename without extension
*/
static def removeExtension(IFile file, Function<IFile, String> getBasePath) {
if (file === null || getBasePath === null)
return null
if (file.name === null || file.name.empty)
return ""
if (file.fileExtension === null || file.fileExtension.empty)
return file.name
val basePath = getBasePath.apply(file)
return removeExtension(basePath)
}
/**
* Remove extension from filename and return filename without extension. The extension starts at
* the last occurrence of a dot (".") in the filename.
*/
static def removeExtension(String filename) {
if (filename === null)
return null
return filename.substring(0, filename.lastIndexOf("."))
}
/**
* Get extension of filename. The extension starts at the last occurrence of a dot (".") in the
* filename.
*/
static def getExtension(String filename) {
if (filename === null)
return null
val filenameWithoutPath = new File(filename).name
val lastIndexOfDot = filenameWithoutPath.lastIndexOf(".")
return if (lastIndexOfDot > -1 && filenameWithoutPath.length > 1)
filenameWithoutPath.substring(lastIndexOfDot + 1)
else
""
}
/**
* Helper to create ResourceSet from file
*/
static def createResourceSetFor(IFile file) {
val resourceSetProviderRegistry = IResourceServiceProvider.Registry.INSTANCE
val fileUri = URI.createURI(file.fullPath.toString)
val resourceSetProvider = resourceSetProviderRegistry.getResourceServiceProvider(fileUri)
return resourceSetProvider.get(ResourceSet)
}
/**
* Run UI event loop
*/
static def runEventLoop(Shell shell) {
/* Code taken from Window.open() */
val display = shell.display
while (shell !== null && !shell.disposed)
if (!display.readAndDispatch())
display.sleep()
if (!display.disposed)
display.update()
}
/**
* Get all open editor instances of the current Eclipse workbench
*/
static def <T extends IEditorPart> getOpenEditorsOfType(Class<T> editorType) {
val allOpenEditorReferences = PlatformUI.workbench
.workbenchWindows
.map[pages.toList].flatten
.map[editorReferences.toList].flatten
return allOpenEditorReferences.filter[
val editor = it.getEditor(false)
editor !== null && editor.getAdapter(editorType) !== null
].map[it.getEditor(false).getAdapter(editorType) as T]
}
/**
* Get selected IResources from ExecutionEvent
*/
static def getSelectedResources(ExecutionEvent event) {
val window = HandlerUtil.getActiveWorkbenchWindowChecked(event)
val selection = window.selectionService.selection
if (!(selection instanceof IStructuredSelection))
return emptyList
val selectedResources = <IResource>newArrayList
for (element : (selection as IStructuredSelection).toList) {
if (element instanceof IResource)
selectedResources.add(element)
else if (element instanceof IAdaptable) {
val resource = element.getAdapter(IResource)
if (resource !== null)
selectedResources.add(resource)
}
}
return selectedResources
}
}