Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
beb19106 | 2 | * Copyright (c) 2010, 2013 Ericsson, École Polytechnique de Montréal |
c4c81d91 | 3 | * |
12c155f5 FC |
4 | * All rights reserved. This program and the accompanying materials are |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
c4c81d91 | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
beb19106 | 11 | * Geneviève Bastien - Copied code to add/remove traces in this class |
a72a6830 | 12 | * Patrick Tasse - Close editors to release resources |
12c155f5 FC |
13 | *******************************************************************************/ |
14 | ||
15 | package org.eclipse.linuxtools.tmf.ui.project.model; | |
16 | ||
c4c81d91 PT |
17 | import java.io.ByteArrayInputStream; |
18 | import java.io.InputStream; | |
12c155f5 | 19 | import java.util.ArrayList; |
5a5c2fc7 | 20 | import java.util.Arrays; |
f537c959 | 21 | import java.util.HashMap; |
12c155f5 | 22 | import java.util.List; |
beb19106 | 23 | import java.util.Map; |
12c155f5 | 24 | |
c4c81d91 | 25 | import org.eclipse.core.resources.IFile; |
12c155f5 | 26 | import org.eclipse.core.resources.IFolder; |
c4c81d91 | 27 | import org.eclipse.core.resources.IResource; |
beb19106 GB |
28 | import org.eclipse.core.resources.IWorkspace; |
29 | import org.eclipse.core.resources.ResourcesPlugin; | |
c4c81d91 | 30 | import org.eclipse.core.runtime.CoreException; |
beb19106 GB |
31 | import org.eclipse.core.runtime.IPath; |
32 | import org.eclipse.core.runtime.QualifiedName; | |
33 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
c4c81d91 PT |
34 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
35 | import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment; | |
080600d9 | 36 | import org.eclipse.linuxtools.tmf.ui.properties.ReadOnlyTextPropertyDescriptor; |
beb19106 GB |
37 | import org.eclipse.ui.IEditorReference; |
38 | import org.eclipse.ui.IWorkbench; | |
39 | import org.eclipse.ui.IWorkbenchPage; | |
40 | import org.eclipse.ui.IWorkbenchWindow; | |
a72a6830 | 41 | import org.eclipse.ui.PartInitException; |
beb19106 GB |
42 | import org.eclipse.ui.PlatformUI; |
43 | import org.eclipse.ui.part.FileEditorInput; | |
12c155f5 FC |
44 | import org.eclipse.ui.views.properties.IPropertyDescriptor; |
45 | import org.eclipse.ui.views.properties.IPropertySource2; | |
12c155f5 FC |
46 | |
47 | /** | |
b544077e | 48 | * Implementation of TMF Experiment Model Element. |
12c155f5 | 49 | * <p> |
b544077e BH |
50 | * @version 1.0 |
51 | * @author Francois Chouinard | |
c4c81d91 | 52 | * |
12c155f5 | 53 | */ |
99504bb8 | 54 | public class TmfExperimentElement extends TmfWithFolderElement implements IPropertySource2 { |
12c155f5 FC |
55 | |
56 | // ------------------------------------------------------------------------ | |
57 | // Constants | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
60 | // Property View stuff | |
61 | private static final String sfInfoCategory = "Info"; //$NON-NLS-1$ | |
62 | private static final String sfName = "name"; //$NON-NLS-1$ | |
63 | private static final String sfPath = "path"; //$NON-NLS-1$ | |
64 | private static final String sfLocation = "location"; //$NON-NLS-1$ | |
99504bb8 | 65 | private static final String sfFolderSuffix = "_exp"; //$NON-NLS-1$ |
12c155f5 | 66 | |
253d5be1 BH |
67 | private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName); |
68 | private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath); | |
69 | private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfLocation, | |
12c155f5 FC |
70 | sfLocation); |
71 | ||
72 | private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, | |
73 | sfLocationDescriptor }; | |
74 | ||
75 | static { | |
76 | sfNameDescriptor.setCategory(sfInfoCategory); | |
77 | sfPathDescriptor.setCategory(sfInfoCategory); | |
78 | sfLocationDescriptor.setCategory(sfInfoCategory); | |
79 | } | |
80 | ||
c4c81d91 PT |
81 | private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$ |
82 | ||
12c155f5 FC |
83 | // ------------------------------------------------------------------------ |
84 | // Constructors | |
85 | // ------------------------------------------------------------------------ | |
b544077e | 86 | /** |
c4c81d91 | 87 | * Constructor |
b544077e BH |
88 | * @param name The name of the experiment |
89 | * @param folder The folder reference | |
90 | * @param parent The experiment folder reference. | |
91 | */ | |
12c155f5 FC |
92 | public TmfExperimentElement(String name, IFolder folder, TmfExperimentFolder parent) { |
93 | super(name, folder, parent); | |
94 | parent.addChild(this); | |
95 | } | |
96 | ||
97 | // ------------------------------------------------------------------------ | |
98 | // TmfProjectModelElement | |
99 | // ------------------------------------------------------------------------ | |
100 | ||
101 | @Override | |
102 | public IFolder getResource() { | |
103 | return (IFolder) fResource; | |
104 | } | |
105 | ||
106 | @Override | |
107 | public TmfProjectElement getProject() { | |
108 | return (TmfProjectElement) getParent().getParent(); | |
109 | } | |
110 | ||
f537c959 PT |
111 | @Override |
112 | void refreshChildren() { | |
113 | IFolder folder = getResource(); | |
114 | ||
115 | // Get the children from the model | |
116 | Map<String, ITmfProjectModelElement> childrenMap = new HashMap<>(); | |
117 | for (ITmfProjectModelElement element : getChildren()) { | |
118 | childrenMap.put(element.getResource().getName(), element); | |
119 | } | |
120 | ||
94227c30 GB |
121 | /* |
122 | * TODO: add the experiment analysis when experiment types are available | |
123 | * and we can have analysis for experiments | |
124 | */ | |
f537c959 PT |
125 | try { |
126 | IResource[] members = folder.members(); | |
127 | for (IResource resource : members) { | |
128 | String name = resource.getName(); | |
129 | ITmfProjectModelElement element = childrenMap.get(name); | |
130 | if (element instanceof TmfTraceElement) { | |
131 | childrenMap.remove(name); | |
132 | } else if (!resource.isHidden()) { | |
133 | // exclude hidden resources (e.g. bookmarks file) | |
134 | element = new TmfTraceElement(name, resource, this); | |
135 | } | |
136 | } | |
137 | } catch (CoreException e) { | |
138 | } | |
139 | ||
140 | // Cleanup dangling children from the model | |
141 | for (ITmfProjectModelElement danglingChild : childrenMap.values()) { | |
142 | removeChild(danglingChild); | |
143 | } | |
144 | } | |
145 | ||
12c155f5 FC |
146 | // ------------------------------------------------------------------------ |
147 | // Operations | |
148 | // ------------------------------------------------------------------------ | |
11252342 | 149 | |
b544077e BH |
150 | /** |
151 | * Returns a list of TmfTraceElements contained in this experiment. | |
152 | * @return a list of TmfTraceElements | |
153 | */ | |
12c155f5 FC |
154 | public List<TmfTraceElement> getTraces() { |
155 | List<ITmfProjectModelElement> children = getChildren(); | |
507b1336 | 156 | List<TmfTraceElement> traces = new ArrayList<>(); |
12c155f5 FC |
157 | for (ITmfProjectModelElement child : children) { |
158 | if (child instanceof TmfTraceElement) { | |
159 | traces.add((TmfTraceElement) child); | |
160 | } | |
161 | } | |
162 | return traces; | |
163 | } | |
164 | ||
beb19106 GB |
165 | |
166 | /** | |
167 | * Adds a trace to the experiment | |
168 | * | |
169 | * @param trace The trace element to add | |
170 | * @since 2.0 | |
171 | */ | |
172 | public void addTrace(TmfTraceElement trace) { | |
173 | /** | |
174 | * Create a link to the actual trace and set the trace type | |
175 | */ | |
176 | IFolder experiment = getResource(); | |
177 | IResource resource = trace.getResource(); | |
178 | IPath location = resource.getLocation(); | |
179 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); | |
180 | try { | |
181 | Map<QualifiedName, String> properties = trace.getResource().getPersistentProperties(); | |
beb19106 | 182 | String traceType = properties.get(TmfCommonConstants.TRACETYPE); |
beb19106 GB |
183 | |
184 | if (resource instanceof IFolder) { | |
185 | IFolder folder = experiment.getFolder(trace.getName()); | |
186 | if (workspace.validateLinkLocation(folder, location).isOK()) { | |
187 | folder.createLink(location, IResource.REPLACE, null); | |
2d64a788 | 188 | setProperties(folder, traceType); |
beb19106 GB |
189 | |
190 | } else { | |
191 | Activator.getDefault().logError("Error creating link. Invalid trace location " + location); //$NON-NLS-1$ | |
192 | } | |
193 | } else { | |
194 | IFile file = experiment.getFile(trace.getName()); | |
195 | if (workspace.validateLinkLocation(file, location).isOK()) { | |
196 | file.createLink(location, IResource.REPLACE, null); | |
2d64a788 | 197 | setProperties(file, traceType); |
beb19106 GB |
198 | } else { |
199 | Activator.getDefault().logError("Error creating link. Invalid trace location " + location); //$NON-NLS-1$ | |
200 | } | |
201 | } | |
202 | } catch (CoreException e) { | |
203 | Activator.getDefault().logError("Error creating link to location " + location, e); //$NON-NLS-1$ | |
204 | } | |
205 | ||
206 | } | |
207 | ||
208 | /** | |
209 | * Removes a trace from an experiment | |
210 | * | |
211 | * @param trace The trace to remove | |
212 | * @throws CoreException exception | |
213 | * @since 2.0 | |
214 | */ | |
215 | public void removeTrace(TmfTraceElement trace) throws CoreException { | |
216 | ||
217 | // Close the experiment if open | |
a72a6830 | 218 | closeEditors(); |
beb19106 GB |
219 | |
220 | /* Finally, remove the trace from experiment*/ | |
221 | removeChild(trace); | |
222 | trace.getResource().delete(true, null); | |
d9030b38 | 223 | deleteSupplementaryResources(); |
beb19106 GB |
224 | } |
225 | ||
2d64a788 | 226 | private static void setProperties(IResource resource, String traceType) throws CoreException { |
beb19106 | 227 | resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType); |
beb19106 GB |
228 | } |
229 | ||
c4c81d91 | 230 | /** |
81fe3479 PT |
231 | * Returns the file resource used to store bookmarks after creating it if necessary. |
232 | * The file will be created if it does not exist. | |
c4c81d91 PT |
233 | * @return the bookmarks file |
234 | * @throws CoreException if the bookmarks file cannot be created | |
235 | * @since 2.0 | |
236 | */ | |
81fe3479 PT |
237 | public IFile createBookmarksFile() throws CoreException { |
238 | IFile file = getBookmarksFile(); | |
c4c81d91 | 239 | if (!file.exists()) { |
81fe3479 PT |
240 | final IFile bookmarksFile = getProject().getExperimentsFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE); |
241 | if (!bookmarksFile.exists()) { | |
242 | final InputStream source = new ByteArrayInputStream(new byte[0]); | |
243 | bookmarksFile.create(source, true, null); | |
244 | } | |
245 | bookmarksFile.setHidden(true); | |
c4c81d91 | 246 | file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null); |
81fe3479 PT |
247 | file.setHidden(true); |
248 | file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfExperiment.class.getCanonicalName()); | |
c4c81d91 | 249 | } |
81fe3479 PT |
250 | return file; |
251 | } | |
252 | ||
253 | /** | |
254 | * Returns the file resource used to store bookmarks. | |
255 | * The file may not exist. | |
256 | * @return the bookmarks file | |
257 | * @since 2.0 | |
258 | */ | |
259 | public IFile getBookmarksFile() { | |
260 | final IFolder folder = (IFolder) fResource; | |
261 | IFile file = folder.getFile(getName() + '_'); | |
c4c81d91 PT |
262 | return file; |
263 | } | |
264 | ||
12c155f5 FC |
265 | // ------------------------------------------------------------------------ |
266 | // IPropertySource2 | |
267 | // ------------------------------------------------------------------------ | |
268 | ||
269 | @Override | |
270 | public Object getEditableValue() { | |
271 | return null; | |
272 | } | |
273 | ||
274 | @Override | |
275 | public IPropertyDescriptor[] getPropertyDescriptors() { | |
77fdc5df | 276 | return Arrays.copyOf(sfDescriptors, sfDescriptors.length); |
12c155f5 FC |
277 | } |
278 | ||
279 | @Override | |
280 | public Object getPropertyValue(Object id) { | |
281 | ||
c4c81d91 | 282 | if (sfName.equals(id)) { |
12c155f5 | 283 | return getName(); |
c4c81d91 | 284 | } |
12c155f5 | 285 | |
c4c81d91 | 286 | if (sfPath.equals(id)) { |
12c155f5 | 287 | return getPath().toString(); |
c4c81d91 | 288 | } |
12c155f5 | 289 | |
c4c81d91 | 290 | if (sfLocation.equals(id)) { |
12c155f5 | 291 | return getLocation().toString(); |
c4c81d91 | 292 | } |
12c155f5 FC |
293 | |
294 | return null; | |
295 | } | |
c4c81d91 | 296 | |
12c155f5 FC |
297 | @Override |
298 | public void resetPropertyValue(Object id) { | |
299 | } | |
300 | ||
301 | @Override | |
302 | public void setPropertyValue(Object id, Object value) { | |
303 | } | |
304 | ||
305 | @Override | |
306 | public boolean isPropertyResettable(Object id) { | |
307 | return false; | |
308 | } | |
309 | ||
310 | @Override | |
311 | public boolean isPropertySet(Object id) { | |
312 | return false; | |
313 | } | |
314 | ||
99504bb8 GB |
315 | /** |
316 | * Return the suffix for resource names | |
317 | * @return The folder suffix | |
318 | */ | |
319 | @Override | |
320 | public String getSuffix() { | |
321 | return sfFolderSuffix; | |
322 | } | |
323 | ||
a72a6830 PT |
324 | /** |
325 | * Close open editors associated with this experiment. | |
326 | * @since 2.0 | |
327 | */ | |
328 | public void closeEditors() { | |
329 | IFile file = getBookmarksFile(); | |
330 | FileEditorInput input = new FileEditorInput(file); | |
331 | IWorkbench wb = PlatformUI.getWorkbench(); | |
332 | for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) { | |
333 | for (IWorkbenchPage wbPage : wbWindow.getPages()) { | |
334 | for (IEditorReference editorReference : wbPage.getEditorReferences()) { | |
335 | try { | |
336 | if (editorReference.getEditorInput().equals(input)) { | |
337 | wbPage.closeEditor(editorReference.getEditor(false), false); | |
338 | } | |
339 | } catch (PartInitException e) { | |
340 | Activator.getDefault().logError("Error closing editor for experiment " + getName(), e); //$NON-NLS-1$ | |
341 | } | |
342 | } | |
343 | } | |
344 | } | |
345 | } | |
c068a752 GB |
346 | |
347 | /** | |
94227c30 | 348 | * Get the list of analysis model elements under this experiment |
c068a752 GB |
349 | * |
350 | * @return Array of analysis elements | |
351 | * @since 3.0 | |
352 | */ | |
353 | public List<TmfAnalysisElement> getAvailableAnalysis() { | |
507b1336 | 354 | List<TmfAnalysisElement> list = new ArrayList<>(); |
c068a752 GB |
355 | |
356 | /** | |
357 | * TODO : implement this cleanly and test it when experiment types are | |
358 | * available | |
359 | */ | |
360 | ||
361 | return list; | |
362 | } | |
12c155f5 | 363 | } |