1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson, École Polytechnique de Montréal
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
10 * Bernd Hufmann - Added supplementary files handling (in class TmfTraceElement)
11 * Geneviève Bastien - Copied supplementary files handling from TmfTracElement
12 * Moved to this class code to copy a model element
13 * Renamed from TmfWithFolderElement to TmfCommonProjectElement
14 * Patrick Tasse - Add support for folder elements
15 *******************************************************************************/
17 package org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
;
19 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
21 import java
.io
.ByteArrayInputStream
;
22 import java
.io
.InputStream
;
23 import java
.util
.ArrayList
;
24 import java
.util
.List
;
25 import java
.util
.stream
.Collectors
;
27 import org
.eclipse
.core
.resources
.IContainer
;
28 import org
.eclipse
.core
.resources
.IFile
;
29 import org
.eclipse
.core
.resources
.IFolder
;
30 import org
.eclipse
.core
.resources
.IResource
;
31 import org
.eclipse
.core
.resources
.IWorkspaceRoot
;
32 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
33 import org
.eclipse
.core
.runtime
.CoreException
;
34 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
35 import org
.eclipse
.core
.runtime
.IPath
;
36 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
37 import org
.eclipse
.core
.runtime
.Path
;
38 import org
.eclipse
.core
.runtime
.Platform
;
39 import org
.eclipse
.jdt
.annotation
.NonNull
;
40 import org
.eclipse
.osgi
.util
.NLS
;
41 import org
.eclipse
.swt
.graphics
.Image
;
42 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
43 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.editors
.ITmfEventsEditorConstants
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
.TraceElementType
;
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
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
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
;
59 import org
.osgi
.framework
.Bundle
;
62 * Base class for tracing project elements: it implements the common behavior of
63 * all project elements: supplementary files, analysis, types, etc.
65 * @author Geneviève Bastien
67 public abstract class TmfCommonProjectElement
extends TmfProjectModelElement
{
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
73 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
75 /* Direct child elements */
76 private TmfViewsElement fViewsElement
= null;
77 private TmfOnDemandAnalysesElement fOnDemandAnalysesElement
= null;
78 private TmfReportsElement fReportsElement
= null;
80 /** This trace type ID as defined in plugin.xml */
81 private String fTraceTypeId
= null;
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
88 * Constructor. Creates model element.
91 * The name of the element
97 public TmfCommonProjectElement(String name
, IResource resource
, TmfProjectModelElement parent
) {
98 super(name
, resource
, parent
);
100 TmfSignalManager
.register(this);
103 // ------------------------------------------------------------------------
104 // TmfProjectModelElement
105 // ------------------------------------------------------------------------
111 protected void refreshChildren() {
112 /* Get the base path to put the resource to */
113 IPath tracePath
= getResource().getFullPath();
114 IWorkspaceRoot root
= ResourcesPlugin
.getWorkspace().getRoot();
116 if (fViewsElement
== null) {
117 /* Add the "Views" node */
118 IFolder viewsNodeRes
= root
.getFolder(tracePath
.append(TmfViewsElement
.PATH_ELEMENT
));
119 fViewsElement
= new TmfViewsElement(viewsNodeRes
, this);
120 addChild(fViewsElement
);
122 fViewsElement
.refreshChildren();
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
);
130 fOnDemandAnalysesElement
.refreshChildren();
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
);
138 fReportsElement
.refreshChildren();
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
);
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();
160 Bundle bundle
= Platform
.getBundle(name
);
161 if (bundle
!= null) {
162 Image image
= TmfProjectModelIcons
.loadIcon(bundle
, iconAttr
);
170 /* Let subclasses specify an icon */
174 // ------------------------------------------------------------------------
176 // ------------------------------------------------------------------------
179 * Get the child element "Views". There should always be one.
181 * @return The child element
184 protected TmfViewsElement
getChildElementViews() {
185 return fViewsElement
;
189 * Returns the trace type ID.
191 * @return trace type ID.
193 public String
getTraceType() {
198 * Refreshes the trace type field by reading the trace type persistent
199 * property of the resource.
201 public void refreshTraceType() {
203 fTraceTypeId
= TmfTraceType
.getTraceTypeId(getResource());
204 } catch (CoreException e
) {
205 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfCommonProjectElement_ErrorRefreshingProperty
, getName()), e
);
210 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
211 * the corresponding extension.
213 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
215 public abstract ITmfTrace
instantiateTrace();
218 * Return the supplementary folder path for this element. The returned path
219 * is relative to the project's supplementary folder.
221 * @return The supplementary folder path for this element
223 protected String
getSupplementaryFolderPath() {
224 return getElementPath() + getSuffix();
228 * Return the element path relative to its common element (traces folder,
229 * experiments folder or experiment element).
231 * @return The element path
233 public @NonNull String
getElementPath() {
234 ITmfProjectModelElement parent
= getParent();
235 while (!(parent
instanceof TmfTracesFolder
|| parent
instanceof TmfExperimentElement
|| parent
instanceof TmfExperimentFolder
)) {
236 parent
= parent
.getParent();
238 IPath path
= getResource().getFullPath().makeRelativeTo(parent
.getPath());
239 return checkNotNull(path
.toString());
243 * @return The suffix for the supplementary folder
245 protected String
getSuffix() {
246 return ""; //$NON-NLS-1$
250 * Returns a list of TmfTraceElements contained in project element.
252 * @return a list of TmfTraceElements, empty list if none
254 public List
<TmfTraceElement
> getTraces() {
255 return new ArrayList
<>();
259 * Get the instantiated trace associated with this element.
261 * @return The instantiated trace or null if trace is not (yet) available
263 public ITmfTrace
getTrace() {
264 for (ITmfTrace trace
: TmfTraceManager
.getInstance().getOpenedTraces()) {
265 if (trace
.getResource().equals(getResource())) {
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.
278 * @return the bookmarks file
279 * @throws CoreException
280 * if the bookmarks file cannot be created
282 public abstract IFile
createBookmarksFile() throws CoreException
;
285 * Actually returns the bookmark file or creates it in the project element's
288 * @param bookmarksFolder
289 * Folder where to put the bookmark file
290 * @param editorInputType
291 * The editor input type to set (trace or experiment)
292 * @return The bookmark file
293 * @throws CoreException
294 * if the bookmarks file cannot be created
296 protected IFile
createBookmarksFile(IFolder bookmarksFolder
, String editorInputType
) throws CoreException
{
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]);
302 bookmarksFile
.create(source
, IResource
.FORCE
| IResource
.HIDDEN
, null);
304 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
| IResource
.HIDDEN
, null);
305 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, editorInputType
);
311 * Returns the optional editor ID from the trace type extension.
313 * @return the editor ID or <code>null</code> if not defined.
315 public abstract String
getEditorId();
318 * Returns the file resource used to store bookmarks. The file may not
321 * @return the bookmarks file
323 public IFile
getBookmarksFile() {
324 final IFolder folder
= (IFolder
) getResource();
325 IFile file
= folder
.getFile(getName() + '_');
330 * Close open editors associated with this experiment.
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()) {
340 if (editorReference
.getEditorInput().equals(input
)) {
341 wbPage
.closeEditor(editorReference
.getEditor(false), false);
343 } catch (PartInitException e
) {
344 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfCommonProjectElement_ErrorClosingEditor
, getName()), e
);
352 * Get a friendly name for the type of element this common project element
353 * is, to be displayed in UI messages.
355 * @return A string for the type of project element this object is, for
356 * example "trace" or "experiment"
358 public abstract String
getTypeName();
361 * Copy this model element
364 * The name of the new element
365 * @param copySuppFiles
366 * Whether to copy supplementary files or not
367 * @return the new Resource object
369 public IResource
copy(final String newName
, final boolean copySuppFiles
) {
371 final IPath newPath
= getParent().getResource().getFullPath().addTrailingSeparator().append(newName
);
373 /* Copy supplementary files first, only if needed */
375 String newElementPath
= new Path(getElementPath()).removeLastSegments(1).append(newName
).toString();
376 copySupplementaryFolder(newElementPath
);
380 getResource().copy(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
381 IResource trace
= ((IFolder
) getParent().getResource()).findMember(newName
);
383 /* Delete any bookmarks file found in copied trace folder */
384 if (trace
instanceof IFolder
) {
385 IFolder folderTrace
= (IFolder
) trace
;
386 for (IResource member
: folderTrace
.members()) {
387 String traceTypeId
= TmfTraceType
.getTraceTypeId(member
);
388 if (ITmfEventsEditorConstants
.TRACE_INPUT_TYPE_CONSTANTS
.contains(traceTypeId
)) {
389 member
.delete(true, null);
390 } else if (ITmfEventsEditorConstants
.EXPERIMENT_INPUT_TYPE_CONSTANTS
.contains(traceTypeId
)) {
391 member
.delete(true, null);
396 } catch (CoreException e
) {
403 * Get the list of analysis elements
405 * @return Array of analysis elements
407 public List
<TmfAnalysisElement
> getAvailableAnalysis() {
408 return getChildElementViews().getChildren().stream()
409 .map(elem
-> (TmfAnalysisElement
) elem
)
410 .collect(Collectors
.toList());
413 // ------------------------------------------------------------------------
414 // Supplementary files operations
415 // ------------------------------------------------------------------------
418 * Deletes this element specific supplementary folder.
420 public void deleteSupplementaryFolder() {
421 IFolder supplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
423 deleteFolder(supplFolder
);
424 } catch (CoreException e
) {
425 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
429 private static void deleteFolder(IFolder folder
) throws CoreException
{
430 if (folder
.exists()) {
431 folder
.delete(true, new NullProgressMonitor());
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
);
441 * Renames the element specific supplementary folder according to the new
442 * element name or path.
444 * @param newElementPath
445 * The new element name or path
447 public void renameSupplementaryFolder(String newElementPath
) {
448 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
450 // Rename supplementary folder
452 if (oldSupplFolder
.exists()) {
453 IFolder newSupplFolder
= prepareTraceSupplementaryFolder(newElementPath
+ getSuffix(), false);
454 oldSupplFolder
.move(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
456 deleteFolder(oldSupplFolder
);
457 } catch (CoreException e
) {
458 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
463 * Copies the element specific supplementary folder to the new element name
466 * @param newElementPath
467 * The new element name or path
469 public void copySupplementaryFolder(String newElementPath
) {
470 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
472 // copy supplementary folder
473 if (oldSupplFolder
.exists()) {
475 IFolder newSupplFolder
= prepareTraceSupplementaryFolder(newElementPath
+ getSuffix(), false);
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$
484 * Copies the element specific supplementary folder a new folder.
487 * The destination folder to copy to.
489 public void copySupplementaryFolder(IFolder destination
) {
490 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
492 // copy supplementary folder
493 if (oldSupplFolder
.exists()) {
495 TraceUtils
.createFolder((IFolder
) destination
.getParent(), new NullProgressMonitor());
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$
504 * Refreshes the element specific supplementary folder information. It
505 * creates the folder if not exists. It sets the persistence property of the
508 public void refreshSupplementaryFolder() {
509 IFolder supplFolder
= createSupplementaryFolder();
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$
518 * Checks if supplementary resource exist or not.
520 * @return <code>true</code> if one or more files are under the element
521 * supplementary folder
523 public boolean hasSupplementaryResources() {
524 IResource
[] resources
= getSupplementaryResources();
525 return (resources
.length
> 0);
529 * Returns the supplementary resources under the trace supplementary folder.
531 * @return array of resources under the trace supplementary folder.
533 public IResource
[] getSupplementaryResources() {
534 IFolder supplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
535 if (supplFolder
.exists()) {
537 return supplFolder
.members();
538 } catch (CoreException e
) {
539 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
542 return new IResource
[0];
546 * Deletes the given resources.
549 * array of resources to delete.
551 public void deleteSupplementaryResources(IResource
[] resources
) {
553 for (int i
= 0; i
< resources
.length
; i
++) {
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$
563 * Deletes all supplementary resources in the supplementary directory
565 public void deleteSupplementaryResources() {
566 deleteSupplementaryResources(getSupplementaryResources());
569 private IFolder
createSupplementaryFolder() {
570 IFolder supplFolder
= prepareTraceSupplementaryFolder(getSupplementaryFolderPath(), true);
573 getResource().setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, supplFolder
.getLocation().toOSString());
574 } catch (CoreException e
) {
575 Activator
.getDefault().logError("Error setting persistant property " + TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, e
); //$NON-NLS-1$
580 // -------------------------------------------------------
582 // -------------------------------------------------------
585 * Handler for the Trace Opened signal
588 * The incoming signal
591 public void traceOpened(TmfTraceOpenedSignal signal
) {
592 IResource resource
= signal
.getTrace().getResource();
593 if ((resource
== null) || !resource
.equals(getResource())) {
597 getParent().refresh();