Commit | Line | Data |
---|---|---|
99504bb8 | 1 | /******************************************************************************* |
58ffe079 | 2 | * Copyright (c) 2010, 2015 Ericsson, École Polytechnique de Montréal |
99504bb8 GB |
3 | * |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Bernd Hufmann - Added supplementary files handling (in class TmfTraceElement) | |
11 | * Geneviève Bastien - Copied supplementary files handling from TmfTracElement | |
16036bc2 GB |
12 | * Moved to this class code to copy a model element |
13 | * Renamed from TmfWithFolderElement to TmfCommonProjectElement | |
339d539c | 14 | * Patrick Tasse - Add support for folder elements |
99504bb8 GB |
15 | *******************************************************************************/ |
16 | ||
2bdf0193 | 17 | package org.eclipse.tracecompass.tmf.ui.project.model; |
99504bb8 | 18 | |
dff70ccd AM |
19 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
20 | ||
8f5221c2 GB |
21 | import java.io.ByteArrayInputStream; |
22 | import java.io.InputStream; | |
23 | import java.util.ArrayList; | |
8f5221c2 | 24 | import java.util.List; |
5c727157 | 25 | import java.util.stream.Collectors; |
8f5221c2 | 26 | |
339d539c | 27 | import org.eclipse.core.resources.IContainer; |
8f5221c2 | 28 | import org.eclipse.core.resources.IFile; |
99504bb8 GB |
29 | import org.eclipse.core.resources.IFolder; |
30 | import org.eclipse.core.resources.IResource; | |
ddfa2689 | 31 | import org.eclipse.core.resources.IWorkspaceRoot; |
8f5221c2 | 32 | import org.eclipse.core.resources.ResourcesPlugin; |
99504bb8 | 33 | import org.eclipse.core.runtime.CoreException; |
dff70ccd | 34 | import org.eclipse.core.runtime.IConfigurationElement; |
beb19106 | 35 | import org.eclipse.core.runtime.IPath; |
99504bb8 | 36 | import org.eclipse.core.runtime.NullProgressMonitor; |
339d539c | 37 | import org.eclipse.core.runtime.Path; |
dff70ccd | 38 | import org.eclipse.core.runtime.Platform; |
367e2932 | 39 | import org.eclipse.jdt.annotation.NonNull; |
8f5221c2 | 40 | import org.eclipse.osgi.util.NLS; |
dff70ccd | 41 | import org.eclipse.swt.graphics.Image; |
2bdf0193 | 42 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
3d04a314 | 43 | import org.eclipse.tracecompass.internal.tmf.ui.editors.ITmfEventsEditorConstants; |
2bdf0193 | 44 | import org.eclipse.tracecompass.tmf.core.TmfCommonConstants; |
2bdf0193 | 45 | import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType; |
dff70ccd | 46 | import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType.TraceElementType; |
2bdf0193 AM |
47 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler; |
48 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; | |
49 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; | |
50 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
2bdf0193 | 51 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
8f5221c2 GB |
52 | import org.eclipse.ui.IEditorReference; |
53 | import org.eclipse.ui.IWorkbench; | |
54 | import org.eclipse.ui.IWorkbenchPage; | |
55 | import org.eclipse.ui.IWorkbenchWindow; | |
56 | import org.eclipse.ui.PartInitException; | |
57 | import org.eclipse.ui.PlatformUI; | |
58 | import org.eclipse.ui.part.FileEditorInput; | |
dff70ccd | 59 | import org.osgi.framework.Bundle; |
99504bb8 | 60 | |
99504bb8 | 61 | /** |
16036bc2 GB |
62 | * Base class for tracing project elements: it implements the common behavior of |
63 | * all project elements: supplementary files, analysis, types, etc. | |
99504bb8 | 64 | * |
e73a4ba5 | 65 | * @author Geneviève Bastien |
99504bb8 | 66 | */ |
16036bc2 | 67 | public abstract class TmfCommonProjectElement extends TmfProjectModelElement { |
99504bb8 | 68 | |
8f5221c2 GB |
69 | // ------------------------------------------------------------------------ |
70 | // Attributes | |
71 | // ------------------------------------------------------------------------ | |
72 | ||
8f5221c2 GB |
73 | private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$ |
74 | ||
ddfa2689 | 75 | /* Direct child elements */ |
5c727157 | 76 | private TmfViewsElement fViewsElement = null; |
ddfa2689 AM |
77 | private TmfOnDemandAnalysesElement fOnDemandAnalysesElement = null; |
78 | private TmfReportsElement fReportsElement = null; | |
5c727157 AM |
79 | |
80 | /** This trace type ID as defined in plugin.xml */ | |
81 | private String fTraceTypeId = null; | |
82 | ||
99504bb8 GB |
83 | // ------------------------------------------------------------------------ |
84 | // Constructors | |
85 | // ------------------------------------------------------------------------ | |
86 | ||
99504bb8 | 87 | /** |
16036bc2 GB |
88 | * Constructor. Creates model element. |
89 | * | |
90 | * @param name | |
91 | * The name of the element | |
92 | * @param resource | |
93 | * The resource. | |
94 | * @param parent | |
95 | * The parent element | |
99504bb8 | 96 | */ |
16036bc2 | 97 | public TmfCommonProjectElement(String name, IResource resource, TmfProjectModelElement parent) { |
99504bb8 | 98 | super(name, resource, parent); |
8f5221c2 GB |
99 | refreshTraceType(); |
100 | TmfSignalManager.register(this); | |
101 | } | |
102 | ||
103 | // ------------------------------------------------------------------------ | |
104 | // TmfProjectModelElement | |
105 | // ------------------------------------------------------------------------ | |
106 | ||
b3e4798c AM |
107 | /** |
108 | * @since 2.0 | |
109 | */ | |
8f5221c2 | 110 | @Override |
b3e4798c | 111 | protected void refreshChildren() { |
5c727157 AM |
112 | /* Get the base path to put the resource to */ |
113 | IPath tracePath = getResource().getFullPath(); | |
ddfa2689 | 114 | IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
5c727157 AM |
115 | |
116 | if (fViewsElement == null) { | |
117 | /* Add the "Views" node */ | |
ddfa2689 | 118 | IFolder viewsNodeRes = root.getFolder(tracePath.append(TmfViewsElement.PATH_ELEMENT)); |
5c727157 AM |
119 | fViewsElement = new TmfViewsElement(viewsNodeRes, this); |
120 | addChild(fViewsElement); | |
8f5221c2 | 121 | } |
5c727157 | 122 | fViewsElement.refreshChildren(); |
ddfa2689 AM |
123 | |
124 | if (fOnDemandAnalysesElement == null) { | |
125 | /* Add the "On-demand Analyses" node */ | |
126 | IFolder analysesNodeRes = root.getFolder(tracePath.append(TmfOnDemandAnalysesElement.PATH_ELEMENT)); | |
127 | fOnDemandAnalysesElement = new TmfOnDemandAnalysesElement(analysesNodeRes, this); | |
128 | addChild(fOnDemandAnalysesElement); | |
129 | } | |
130 | fOnDemandAnalysesElement.refreshChildren(); | |
131 | ||
132 | if (fReportsElement == null) { | |
133 | /* Add the "Reports" node */ | |
134 | IFolder reportsNodeRes = root.getFolder(tracePath.append(TmfReportsElement.PATH_ELEMENT)); | |
135 | fReportsElement = new TmfReportsElement(reportsNodeRes, this); | |
136 | addChild(fReportsElement); | |
137 | } | |
138 | fReportsElement.refreshChildren(); | |
8f5221c2 GB |
139 | } |
140 | ||
dff70ccd AM |
141 | /** |
142 | * @since 2.0 | |
143 | */ | |
144 | @Override | |
145 | public Image getIcon() { | |
146 | String traceType = getTraceType(); | |
147 | if (traceType == null || TmfTraceType.getTraceType(traceType) == null) { | |
148 | // request the label to the Eclipse platform | |
149 | Image icon = TmfProjectModelIcons.WORKSPACE_LABEL_PROVIDER.getImage(getResource()); | |
150 | return (icon == null ? TmfProjectModelIcons.DEFAULT_TRACE_ICON : icon); | |
151 | } | |
152 | ||
153 | IConfigurationElement traceUIAttributes = TmfTraceTypeUIUtils.getTraceUIAttributes(traceType, | |
154 | (this instanceof TmfTraceElement) ? TraceElementType.TRACE : TraceElementType.EXPERIMENT); | |
155 | if (traceUIAttributes != null) { | |
156 | String iconAttr = traceUIAttributes.getAttribute(TmfTraceTypeUIUtils.ICON_ATTR); | |
157 | if (iconAttr != null) { | |
158 | String name = traceUIAttributes.getContributor().getName(); | |
159 | if (name != null) { | |
160 | Bundle bundle = Platform.getBundle(name); | |
161 | if (bundle != null) { | |
162 | Image image = TmfProjectModelIcons.loadIcon(bundle, iconAttr); | |
163 | if (image != null) { | |
164 | return image; | |
165 | } | |
166 | } | |
167 | } | |
168 | } | |
169 | } | |
170 | /* Let subclasses specify an icon */ | |
171 | return null; | |
172 | } | |
173 | ||
8f5221c2 GB |
174 | // ------------------------------------------------------------------------ |
175 | // Operations | |
176 | // ------------------------------------------------------------------------ | |
177 | ||
5c727157 AM |
178 | /** |
179 | * Get the child element "Views". There should always be one. | |
180 | * | |
181 | * @return The child element | |
182 | * @since 2.0 | |
183 | */ | |
184 | protected TmfViewsElement getChildElementViews() { | |
185 | return fViewsElement; | |
186 | } | |
187 | ||
8f5221c2 GB |
188 | /** |
189 | * Returns the trace type ID. | |
190 | * | |
191 | * @return trace type ID. | |
192 | */ | |
193 | public String getTraceType() { | |
194 | return fTraceTypeId; | |
195 | } | |
196 | ||
197 | /** | |
4b3b667b PT |
198 | * Refreshes the trace type field by reading the trace type persistent |
199 | * property of the resource. | |
8f5221c2 GB |
200 | */ |
201 | public void refreshTraceType() { | |
202 | try { | |
4b3b667b | 203 | fTraceTypeId = TmfTraceType.getTraceTypeId(getResource()); |
8f5221c2 GB |
204 | } catch (CoreException e) { |
205 | Activator.getDefault().logError(NLS.bind(Messages.TmfCommonProjectElement_ErrorRefreshingProperty, getName()), e); | |
206 | } | |
99504bb8 GB |
207 | } |
208 | ||
8f5221c2 GB |
209 | /** |
210 | * Instantiate a <code>ITmfTrace</code> object based on the trace type and | |
211 | * the corresponding extension. | |
212 | * | |
213 | * @return the <code>ITmfTrace</code> or <code>null</code> for an error | |
214 | */ | |
215 | public abstract ITmfTrace instantiateTrace(); | |
216 | ||
99504bb8 | 217 | /** |
339d539c PT |
218 | * Return the supplementary folder path for this element. The returned path |
219 | * is relative to the project's supplementary folder. | |
99504bb8 | 220 | * |
339d539c | 221 | * @return The supplementary folder path for this element |
99504bb8 | 222 | */ |
339d539c PT |
223 | protected String getSupplementaryFolderPath() { |
224 | return getElementPath() + getSuffix(); | |
99504bb8 GB |
225 | } |
226 | ||
227 | /** | |
339d539c PT |
228 | * Return the element path relative to its common element (traces folder, |
229 | * experiments folder or experiment element). | |
e9e2c5ee | 230 | * |
339d539c PT |
231 | * @return The element path |
232 | */ | |
dff70ccd | 233 | public @NonNull String getElementPath() { |
339d539c PT |
234 | ITmfProjectModelElement parent = getParent(); |
235 | while (!(parent instanceof TmfTracesFolder || parent instanceof TmfExperimentElement || parent instanceof TmfExperimentFolder)) { | |
236 | parent = parent.getParent(); | |
237 | } | |
b3e4798c | 238 | IPath path = getResource().getFullPath().makeRelativeTo(parent.getPath()); |
dff70ccd | 239 | return checkNotNull(path.toString()); |
339d539c PT |
240 | } |
241 | ||
242 | /** | |
243 | * @return The suffix for the supplementary folder | |
99504bb8 GB |
244 | */ |
245 | protected String getSuffix() { | |
246 | return ""; //$NON-NLS-1$ | |
247 | } | |
248 | ||
8f5221c2 GB |
249 | /** |
250 | * Returns a list of TmfTraceElements contained in project element. | |
251 | * | |
252 | * @return a list of TmfTraceElements, empty list if none | |
253 | */ | |
254 | public List<TmfTraceElement> getTraces() { | |
255 | return new ArrayList<>(); | |
256 | } | |
257 | ||
258 | /** | |
259 | * Get the instantiated trace associated with this element. | |
260 | * | |
261 | * @return The instantiated trace or null if trace is not (yet) available | |
262 | */ | |
263 | public ITmfTrace getTrace() { | |
264 | for (ITmfTrace trace : TmfTraceManager.getInstance().getOpenedTraces()) { | |
265 | if (trace.getResource().equals(getResource())) { | |
266 | return trace; | |
267 | } | |
268 | } | |
269 | return null; | |
270 | } | |
271 | ||
272 | /** | |
273 | * Returns the file resource used to store bookmarks after creating it if | |
274 | * necessary. If the trace resource is a file, it is returned directly. If | |
275 | * the trace resource is a folder, a linked file is returned. The file will | |
276 | * be created if it does not exist. | |
277 | * | |
278 | * @return the bookmarks file | |
279 | * @throws CoreException | |
280 | * if the bookmarks file cannot be created | |
281 | */ | |
282 | public abstract IFile createBookmarksFile() throws CoreException; | |
283 | ||
284 | /** | |
285 | * Actually returns the bookmark file or creates it in the project element's | |
286 | * folder | |
287 | * | |
288 | * @param bookmarksFolder | |
289 | * Folder where to put the bookmark file | |
58ffe079 MAL |
290 | * @param editorInputType |
291 | * The editor input type to set (trace or experiment) | |
8f5221c2 GB |
292 | * @return The bookmark file |
293 | * @throws CoreException | |
294 | * if the bookmarks file cannot be created | |
295 | */ | |
58ffe079 | 296 | protected IFile createBookmarksFile(IFolder bookmarksFolder, String editorInputType) throws CoreException { |
8f5221c2 GB |
297 | IFile file = getBookmarksFile(); |
298 | if (!file.exists()) { | |
299 | final IFile bookmarksFile = bookmarksFolder.getFile(BOOKMARKS_HIDDEN_FILE); | |
300 | if (!bookmarksFile.exists()) { | |
301 | final InputStream source = new ByteArrayInputStream(new byte[0]); | |
858a35cf | 302 | bookmarksFile.create(source, IResource.FORCE | IResource.HIDDEN, null); |
8f5221c2 | 303 | } |
858a35cf | 304 | file.createLink(bookmarksFile.getLocation(), IResource.REPLACE | IResource.HIDDEN, null); |
58ffe079 | 305 | file.setPersistentProperty(TmfCommonConstants.TRACETYPE, editorInputType); |
8f5221c2 GB |
306 | } |
307 | return file; | |
308 | } | |
309 | ||
310 | /** | |
311 | * Returns the optional editor ID from the trace type extension. | |
312 | * | |
313 | * @return the editor ID or <code>null</code> if not defined. | |
314 | */ | |
315 | public abstract String getEditorId(); | |
316 | ||
317 | /** | |
318 | * Returns the file resource used to store bookmarks. The file may not | |
319 | * exist. | |
320 | * | |
321 | * @return the bookmarks file | |
322 | */ | |
323 | public IFile getBookmarksFile() { | |
b3e4798c | 324 | final IFolder folder = (IFolder) getResource(); |
8f5221c2 GB |
325 | IFile file = folder.getFile(getName() + '_'); |
326 | return file; | |
327 | } | |
328 | ||
329 | /** | |
330 | * Close open editors associated with this experiment. | |
331 | */ | |
332 | public void closeEditors() { | |
333 | IFile file = getBookmarksFile(); | |
334 | FileEditorInput input = new FileEditorInput(file); | |
335 | IWorkbench wb = PlatformUI.getWorkbench(); | |
336 | for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) { | |
337 | for (IWorkbenchPage wbPage : wbWindow.getPages()) { | |
338 | for (IEditorReference editorReference : wbPage.getEditorReferences()) { | |
339 | try { | |
340 | if (editorReference.getEditorInput().equals(input)) { | |
341 | wbPage.closeEditor(editorReference.getEditor(false), false); | |
342 | } | |
343 | } catch (PartInitException e) { | |
344 | Activator.getDefault().logError(NLS.bind(Messages.TmfCommonProjectElement_ErrorClosingEditor, getName()), e); | |
345 | } | |
346 | } | |
347 | } | |
348 | } | |
349 | } | |
350 | ||
351 | /** | |
352 | * Get a friendly name for the type of element this common project element | |
353 | * is, to be displayed in UI messages. | |
354 | * | |
355 | * @return A string for the type of project element this object is, for | |
356 | * example "trace" or "experiment" | |
357 | */ | |
358 | public abstract String getTypeName(); | |
359 | ||
360 | /** | |
361 | * Copy this model element | |
362 | * | |
363 | * @param newName | |
364 | * The name of the new element | |
365 | * @param copySuppFiles | |
366 | * Whether to copy supplementary files or not | |
367 | * @return the new Resource object | |
368 | */ | |
369 | public IResource copy(final String newName, final boolean copySuppFiles) { | |
370 | ||
371 | final IPath newPath = getParent().getResource().getFullPath().addTrailingSeparator().append(newName); | |
372 | ||
373 | /* Copy supplementary files first, only if needed */ | |
374 | if (copySuppFiles) { | |
339d539c PT |
375 | String newElementPath = new Path(getElementPath()).removeLastSegments(1).append(newName).toString(); |
376 | copySupplementaryFolder(newElementPath); | |
8f5221c2 GB |
377 | } |
378 | /* Copy the trace */ | |
379 | try { | |
380 | getResource().copy(newPath, IResource.FORCE | IResource.SHALLOW, null); | |
e9e2c5ee | 381 | IResource trace = ((IFolder) getParent().getResource()).findMember(newName); |
8f5221c2 GB |
382 | |
383 | /* Delete any bookmarks file found in copied trace folder */ | |
e9e2c5ee MK |
384 | if (trace instanceof IFolder) { |
385 | IFolder folderTrace = (IFolder) trace; | |
386 | for (IResource member : folderTrace.members()) { | |
4b3b667b | 387 | String traceTypeId = TmfTraceType.getTraceTypeId(member); |
3d04a314 | 388 | if (ITmfEventsEditorConstants.TRACE_INPUT_TYPE_CONSTANTS.contains(traceTypeId)) { |
8f5221c2 | 389 | member.delete(true, null); |
3d04a314 | 390 | } else if (ITmfEventsEditorConstants.EXPERIMENT_INPUT_TYPE_CONSTANTS.contains(traceTypeId)) { |
8f5221c2 GB |
391 | member.delete(true, null); |
392 | } | |
393 | } | |
394 | } | |
e9e2c5ee | 395 | return trace; |
8f5221c2 GB |
396 | } catch (CoreException e) { |
397 | ||
398 | } | |
399 | return null; | |
400 | } | |
401 | ||
402 | /** | |
403 | * Get the list of analysis elements | |
404 | * | |
405 | * @return Array of analysis elements | |
406 | */ | |
407 | public List<TmfAnalysisElement> getAvailableAnalysis() { | |
5c727157 AM |
408 | return getChildElementViews().getChildren().stream() |
409 | .map(elem -> (TmfAnalysisElement) elem) | |
410 | .collect(Collectors.toList()); | |
8f5221c2 GB |
411 | } |
412 | ||
16036bc2 GB |
413 | // ------------------------------------------------------------------------ |
414 | // Supplementary files operations | |
415 | // ------------------------------------------------------------------------ | |
416 | ||
99504bb8 GB |
417 | /** |
418 | * Deletes this element specific supplementary folder. | |
419 | */ | |
420 | public void deleteSupplementaryFolder() { | |
339d539c PT |
421 | IFolder supplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath()); |
422 | try { | |
423 | deleteFolder(supplFolder); | |
424 | } catch (CoreException e) { | |
425 | Activator.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$ | |
426 | } | |
427 | } | |
428 | ||
429 | private static void deleteFolder(IFolder folder) throws CoreException { | |
430 | if (folder.exists()) { | |
431 | folder.delete(true, new NullProgressMonitor()); | |
432 | } | |
433 | IContainer parent = folder.getParent(); | |
434 | // delete empty folders up to the parent project | |
435 | if (parent instanceof IFolder && (!parent.exists() || parent.members().length == 0)) { | |
436 | deleteFolder((IFolder) parent); | |
99504bb8 GB |
437 | } |
438 | } | |
439 | ||
440 | /** | |
16036bc2 | 441 | * Renames the element specific supplementary folder according to the new |
339d539c | 442 | * element name or path. |
99504bb8 | 443 | * |
339d539c PT |
444 | * @param newElementPath |
445 | * The new element name or path | |
99504bb8 | 446 | */ |
339d539c PT |
447 | public void renameSupplementaryFolder(String newElementPath) { |
448 | IFolder oldSupplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath()); | |
99504bb8 GB |
449 | |
450 | // Rename supplementary folder | |
339d539c PT |
451 | try { |
452 | if (oldSupplFolder.exists()) { | |
453 | IFolder newSupplFolder = prepareTraceSupplementaryFolder(newElementPath + getSuffix(), false); | |
99504bb8 | 454 | oldSupplFolder.move(newSupplFolder.getFullPath(), true, new NullProgressMonitor()); |
99504bb8 | 455 | } |
339d539c PT |
456 | deleteFolder(oldSupplFolder); |
457 | } catch (CoreException e) { | |
458 | Activator.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$ | |
99504bb8 GB |
459 | } |
460 | } | |
461 | ||
462 | /** | |
339d539c PT |
463 | * Copies the element specific supplementary folder to the new element name |
464 | * or path. | |
99504bb8 | 465 | * |
339d539c PT |
466 | * @param newElementPath |
467 | * The new element name or path | |
99504bb8 | 468 | */ |
339d539c PT |
469 | public void copySupplementaryFolder(String newElementPath) { |
470 | IFolder oldSupplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath()); | |
99504bb8 GB |
471 | |
472 | // copy supplementary folder | |
473 | if (oldSupplFolder.exists()) { | |
474 | try { | |
339d539c | 475 | IFolder newSupplFolder = prepareTraceSupplementaryFolder(newElementPath + getSuffix(), false); |
99504bb8 GB |
476 | oldSupplFolder.copy(newSupplFolder.getFullPath(), true, new NullProgressMonitor()); |
477 | } catch (CoreException e) { | |
478 | Activator.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$ | |
479 | } | |
480 | } | |
481 | } | |
482 | ||
483 | /** | |
484 | * Copies the element specific supplementary folder a new folder. | |
485 | * | |
16036bc2 GB |
486 | * @param destination |
487 | * The destination folder to copy to. | |
99504bb8 GB |
488 | */ |
489 | public void copySupplementaryFolder(IFolder destination) { | |
339d539c | 490 | IFolder oldSupplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath()); |
99504bb8 GB |
491 | |
492 | // copy supplementary folder | |
493 | if (oldSupplFolder.exists()) { | |
494 | try { | |
c2f1c5b6 | 495 | TraceUtils.createFolder((IFolder) destination.getParent(), new NullProgressMonitor()); |
99504bb8 GB |
496 | oldSupplFolder.copy(destination.getFullPath(), true, new NullProgressMonitor()); |
497 | } catch (CoreException e) { | |
498 | Activator.getDefault().logError("Error copying supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$ | |
499 | } | |
500 | } | |
501 | } | |
502 | ||
99504bb8 | 503 | /** |
16036bc2 GB |
504 | * Refreshes the element specific supplementary folder information. It |
505 | * creates the folder if not exists. It sets the persistence property of the | |
506 | * trace resource | |
99504bb8 GB |
507 | */ |
508 | public void refreshSupplementaryFolder() { | |
6b44794a MK |
509 | IFolder supplFolder = createSupplementaryFolder(); |
510 | try { | |
511 | supplFolder.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); | |
512 | } catch (CoreException e) { | |
513 | Activator.getDefault().logError("Error refreshing supplementary folder " + supplFolder, e); //$NON-NLS-1$ | |
514 | } | |
99504bb8 GB |
515 | } |
516 | ||
517 | /** | |
518 | * Checks if supplementary resource exist or not. | |
519 | * | |
16036bc2 GB |
520 | * @return <code>true</code> if one or more files are under the element |
521 | * supplementary folder | |
99504bb8 GB |
522 | */ |
523 | public boolean hasSupplementaryResources() { | |
524 | IResource[] resources = getSupplementaryResources(); | |
525 | return (resources.length > 0); | |
526 | } | |
527 | ||
528 | /** | |
529 | * Returns the supplementary resources under the trace supplementary folder. | |
530 | * | |
531 | * @return array of resources under the trace supplementary folder. | |
532 | */ | |
533 | public IResource[] getSupplementaryResources() { | |
339d539c | 534 | IFolder supplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath()); |
99504bb8 GB |
535 | if (supplFolder.exists()) { |
536 | try { | |
537 | return supplFolder.members(); | |
538 | } catch (CoreException e) { | |
539 | Activator.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$ | |
540 | } | |
541 | } | |
542 | return new IResource[0]; | |
543 | } | |
544 | ||
545 | /** | |
546 | * Deletes the given resources. | |
547 | * | |
16036bc2 GB |
548 | * @param resources |
549 | * array of resources to delete. | |
99504bb8 GB |
550 | */ |
551 | public void deleteSupplementaryResources(IResource[] resources) { | |
552 | ||
553 | for (int i = 0; i < resources.length; i++) { | |
554 | try { | |
555 | resources[i].delete(true, new NullProgressMonitor()); | |
556 | } catch (CoreException e) { | |
557 | Activator.getDefault().logError("Error deleting supplementary resource " + resources[i], e); //$NON-NLS-1$ | |
558 | } | |
559 | } | |
560 | } | |
561 | ||
4726e963 BH |
562 | /** |
563 | * Deletes all supplementary resources in the supplementary directory | |
564 | */ | |
565 | public void deleteSupplementaryResources() { | |
566 | deleteSupplementaryResources(getSupplementaryResources()); | |
567 | } | |
568 | ||
6b44794a | 569 | private IFolder createSupplementaryFolder() { |
339d539c | 570 | IFolder supplFolder = prepareTraceSupplementaryFolder(getSupplementaryFolderPath(), true); |
99504bb8 GB |
571 | |
572 | try { | |
b3e4798c | 573 | getResource().setPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, supplFolder.getLocation().toOSString()); |
99504bb8 GB |
574 | } catch (CoreException e) { |
575 | Activator.getDefault().logError("Error setting persistant property " + TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, e); //$NON-NLS-1$ | |
576 | } | |
6b44794a | 577 | return supplFolder; |
99504bb8 GB |
578 | } |
579 | ||
8f5221c2 GB |
580 | // ------------------------------------------------------- |
581 | // Signal handlers | |
582 | // ------------------------------------------------------- | |
16036bc2 | 583 | |
beb19106 | 584 | /** |
8f5221c2 | 585 | * Handler for the Trace Opened signal |
beb19106 | 586 | * |
8f5221c2 GB |
587 | * @param signal |
588 | * The incoming signal | |
beb19106 | 589 | */ |
8f5221c2 GB |
590 | @TmfSignalHandler |
591 | public void traceOpened(TmfTraceOpenedSignal signal) { | |
592 | IResource resource = signal.getTrace().getResource(); | |
593 | if ((resource == null) || !resource.equals(getResource())) { | |
594 | return; | |
beb19106 | 595 | } |
beb19106 | 596 | |
8f5221c2 | 597 | getParent().refresh(); |
beb19106 GB |
598 | } |
599 | ||
99504bb8 | 600 | } |