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
.ResourcesPlugin
;
32 import org
.eclipse
.core
.runtime
.CoreException
;
33 import org
.eclipse
.core
.runtime
.IConfigurationElement
;
34 import org
.eclipse
.core
.runtime
.IPath
;
35 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
36 import org
.eclipse
.core
.runtime
.Path
;
37 import org
.eclipse
.core
.runtime
.Platform
;
38 import org
.eclipse
.jdt
.annotation
.NonNull
;
39 import org
.eclipse
.osgi
.util
.NLS
;
40 import org
.eclipse
.swt
.graphics
.Image
;
41 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
42 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.editors
.ITmfEventsEditorConstants
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
.TraceElementType
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
51 import org
.eclipse
.ui
.IEditorReference
;
52 import org
.eclipse
.ui
.IWorkbench
;
53 import org
.eclipse
.ui
.IWorkbenchPage
;
54 import org
.eclipse
.ui
.IWorkbenchWindow
;
55 import org
.eclipse
.ui
.PartInitException
;
56 import org
.eclipse
.ui
.PlatformUI
;
57 import org
.eclipse
.ui
.part
.FileEditorInput
;
58 import org
.osgi
.framework
.Bundle
;
61 * Base class for tracing project elements: it implements the common behavior of
62 * all project elements: supplementary files, analysis, types, etc.
64 * @author Geneviève Bastien
66 public abstract class TmfCommonProjectElement
extends TmfProjectModelElement
{
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
72 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
74 private TmfViewsElement fViewsElement
= null;
76 /** This trace type ID as defined in plugin.xml */
77 private String fTraceTypeId
= null;
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
84 * Constructor. Creates model element.
87 * The name of the element
93 public TmfCommonProjectElement(String name
, IResource resource
, TmfProjectModelElement parent
) {
94 super(name
, resource
, parent
);
96 TmfSignalManager
.register(this);
99 // ------------------------------------------------------------------------
100 // TmfProjectModelElement
101 // ------------------------------------------------------------------------
107 protected void refreshChildren() {
108 /* Get the base path to put the resource to */
109 IPath tracePath
= getResource().getFullPath();
111 if (fViewsElement
== null) {
112 /* Add the "Views" node */
113 IFolder viewsNodeRes
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(tracePath
.append(TmfViewsElement
.PATH_ELEMENT
));
114 fViewsElement
= new TmfViewsElement(viewsNodeRes
, this);
115 addChild(fViewsElement
);
117 fViewsElement
.refreshChildren();
124 public Image
getIcon() {
125 String traceType
= getTraceType();
126 if (traceType
== null || TmfTraceType
.getTraceType(traceType
) == null) {
127 // request the label to the Eclipse platform
128 Image icon
= TmfProjectModelIcons
.WORKSPACE_LABEL_PROVIDER
.getImage(getResource());
129 return (icon
== null ? TmfProjectModelIcons
.DEFAULT_TRACE_ICON
: icon
);
132 IConfigurationElement traceUIAttributes
= TmfTraceTypeUIUtils
.getTraceUIAttributes(traceType
,
133 (this instanceof TmfTraceElement
) ? TraceElementType
.TRACE
: TraceElementType
.EXPERIMENT
);
134 if (traceUIAttributes
!= null) {
135 String iconAttr
= traceUIAttributes
.getAttribute(TmfTraceTypeUIUtils
.ICON_ATTR
);
136 if (iconAttr
!= null) {
137 String name
= traceUIAttributes
.getContributor().getName();
139 Bundle bundle
= Platform
.getBundle(name
);
140 if (bundle
!= null) {
141 Image image
= TmfProjectModelIcons
.loadIcon(bundle
, iconAttr
);
149 /* Let subclasses specify an icon */
153 // ------------------------------------------------------------------------
155 // ------------------------------------------------------------------------
158 * Get the child element "Views". There should always be one.
160 * @return The child element
163 protected TmfViewsElement
getChildElementViews() {
164 return fViewsElement
;
168 * Returns the trace type ID.
170 * @return trace type ID.
172 public String
getTraceType() {
177 * Refreshes the trace type field by reading the trace type persistent
178 * property of the resource.
180 public void refreshTraceType() {
182 fTraceTypeId
= TmfTraceType
.getTraceTypeId(getResource());
183 } catch (CoreException e
) {
184 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfCommonProjectElement_ErrorRefreshingProperty
, getName()), e
);
189 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
190 * the corresponding extension.
192 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
194 public abstract ITmfTrace
instantiateTrace();
197 * Return the supplementary folder path for this element. The returned path
198 * is relative to the project's supplementary folder.
200 * @return The supplementary folder path for this element
202 protected String
getSupplementaryFolderPath() {
203 return getElementPath() + getSuffix();
207 * Return the element path relative to its common element (traces folder,
208 * experiments folder or experiment element).
210 * @return The element path
212 public @NonNull String
getElementPath() {
213 ITmfProjectModelElement parent
= getParent();
214 while (!(parent
instanceof TmfTracesFolder
|| parent
instanceof TmfExperimentElement
|| parent
instanceof TmfExperimentFolder
)) {
215 parent
= parent
.getParent();
217 IPath path
= getResource().getFullPath().makeRelativeTo(parent
.getPath());
218 return checkNotNull(path
.toString());
222 * @return The suffix for the supplementary folder
224 protected String
getSuffix() {
225 return ""; //$NON-NLS-1$
229 * Returns a list of TmfTraceElements contained in project element.
231 * @return a list of TmfTraceElements, empty list if none
233 public List
<TmfTraceElement
> getTraces() {
234 return new ArrayList
<>();
238 * Get the instantiated trace associated with this element.
240 * @return The instantiated trace or null if trace is not (yet) available
242 public ITmfTrace
getTrace() {
243 for (ITmfTrace trace
: TmfTraceManager
.getInstance().getOpenedTraces()) {
244 if (trace
.getResource().equals(getResource())) {
252 * Returns the file resource used to store bookmarks after creating it if
253 * necessary. If the trace resource is a file, it is returned directly. If
254 * the trace resource is a folder, a linked file is returned. The file will
255 * be created if it does not exist.
257 * @return the bookmarks file
258 * @throws CoreException
259 * if the bookmarks file cannot be created
261 public abstract IFile
createBookmarksFile() throws CoreException
;
264 * Actually returns the bookmark file or creates it in the project element's
267 * @param bookmarksFolder
268 * Folder where to put the bookmark file
269 * @param editorInputType
270 * The editor input type to set (trace or experiment)
271 * @return The bookmark file
272 * @throws CoreException
273 * if the bookmarks file cannot be created
275 protected IFile
createBookmarksFile(IFolder bookmarksFolder
, String editorInputType
) throws CoreException
{
276 IFile file
= getBookmarksFile();
277 if (!file
.exists()) {
278 final IFile bookmarksFile
= bookmarksFolder
.getFile(BOOKMARKS_HIDDEN_FILE
);
279 if (!bookmarksFile
.exists()) {
280 final InputStream source
= new ByteArrayInputStream(new byte[0]);
281 bookmarksFile
.create(source
, IResource
.FORCE
| IResource
.HIDDEN
, null);
283 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
| IResource
.HIDDEN
, null);
284 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, editorInputType
);
290 * Returns the optional editor ID from the trace type extension.
292 * @return the editor ID or <code>null</code> if not defined.
294 public abstract String
getEditorId();
297 * Returns the file resource used to store bookmarks. The file may not
300 * @return the bookmarks file
302 public IFile
getBookmarksFile() {
303 final IFolder folder
= (IFolder
) getResource();
304 IFile file
= folder
.getFile(getName() + '_');
309 * Close open editors associated with this experiment.
311 public void closeEditors() {
312 IFile file
= getBookmarksFile();
313 FileEditorInput input
= new FileEditorInput(file
);
314 IWorkbench wb
= PlatformUI
.getWorkbench();
315 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
316 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
317 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
319 if (editorReference
.getEditorInput().equals(input
)) {
320 wbPage
.closeEditor(editorReference
.getEditor(false), false);
322 } catch (PartInitException e
) {
323 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfCommonProjectElement_ErrorClosingEditor
, getName()), e
);
331 * Get a friendly name for the type of element this common project element
332 * is, to be displayed in UI messages.
334 * @return A string for the type of project element this object is, for
335 * example "trace" or "experiment"
337 public abstract String
getTypeName();
340 * Copy this model element
343 * The name of the new element
344 * @param copySuppFiles
345 * Whether to copy supplementary files or not
346 * @return the new Resource object
348 public IResource
copy(final String newName
, final boolean copySuppFiles
) {
350 final IPath newPath
= getParent().getResource().getFullPath().addTrailingSeparator().append(newName
);
352 /* Copy supplementary files first, only if needed */
354 String newElementPath
= new Path(getElementPath()).removeLastSegments(1).append(newName
).toString();
355 copySupplementaryFolder(newElementPath
);
359 getResource().copy(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
360 IResource trace
= ((IFolder
) getParent().getResource()).findMember(newName
);
362 /* Delete any bookmarks file found in copied trace folder */
363 if (trace
instanceof IFolder
) {
364 IFolder folderTrace
= (IFolder
) trace
;
365 for (IResource member
: folderTrace
.members()) {
366 String traceTypeId
= TmfTraceType
.getTraceTypeId(member
);
367 if (ITmfEventsEditorConstants
.TRACE_INPUT_TYPE_CONSTANTS
.contains(traceTypeId
)) {
368 member
.delete(true, null);
369 } else if (ITmfEventsEditorConstants
.EXPERIMENT_INPUT_TYPE_CONSTANTS
.contains(traceTypeId
)) {
370 member
.delete(true, null);
375 } catch (CoreException e
) {
382 * Get the list of analysis elements
384 * @return Array of analysis elements
386 public List
<TmfAnalysisElement
> getAvailableAnalysis() {
387 return getChildElementViews().getChildren().stream()
388 .map(elem
-> (TmfAnalysisElement
) elem
)
389 .collect(Collectors
.toList());
392 // ------------------------------------------------------------------------
393 // Supplementary files operations
394 // ------------------------------------------------------------------------
397 * Deletes this element specific supplementary folder.
399 public void deleteSupplementaryFolder() {
400 IFolder supplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
402 deleteFolder(supplFolder
);
403 } catch (CoreException e
) {
404 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
408 private static void deleteFolder(IFolder folder
) throws CoreException
{
409 if (folder
.exists()) {
410 folder
.delete(true, new NullProgressMonitor());
412 IContainer parent
= folder
.getParent();
413 // delete empty folders up to the parent project
414 if (parent
instanceof IFolder
&& (!parent
.exists() || parent
.members().length
== 0)) {
415 deleteFolder((IFolder
) parent
);
420 * Renames the element specific supplementary folder according to the new
421 * element name or path.
423 * @param newElementPath
424 * The new element name or path
426 public void renameSupplementaryFolder(String newElementPath
) {
427 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
429 // Rename supplementary folder
431 if (oldSupplFolder
.exists()) {
432 IFolder newSupplFolder
= prepareTraceSupplementaryFolder(newElementPath
+ getSuffix(), false);
433 oldSupplFolder
.move(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
435 deleteFolder(oldSupplFolder
);
436 } catch (CoreException e
) {
437 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
442 * Copies the element specific supplementary folder to the new element name
445 * @param newElementPath
446 * The new element name or path
448 public void copySupplementaryFolder(String newElementPath
) {
449 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
451 // copy supplementary folder
452 if (oldSupplFolder
.exists()) {
454 IFolder newSupplFolder
= prepareTraceSupplementaryFolder(newElementPath
+ getSuffix(), false);
455 oldSupplFolder
.copy(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
456 } catch (CoreException e
) {
457 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
463 * Copies the element specific supplementary folder a new folder.
466 * The destination folder to copy to.
468 public void copySupplementaryFolder(IFolder destination
) {
469 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
471 // copy supplementary folder
472 if (oldSupplFolder
.exists()) {
474 TraceUtils
.createFolder((IFolder
) destination
.getParent(), new NullProgressMonitor());
475 oldSupplFolder
.copy(destination
.getFullPath(), true, new NullProgressMonitor());
476 } catch (CoreException e
) {
477 Activator
.getDefault().logError("Error copying supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
483 * Refreshes the element specific supplementary folder information. It
484 * creates the folder if not exists. It sets the persistence property of the
487 public void refreshSupplementaryFolder() {
488 IFolder supplFolder
= createSupplementaryFolder();
490 supplFolder
.refreshLocal(IResource
.DEPTH_INFINITE
, new NullProgressMonitor());
491 } catch (CoreException e
) {
492 Activator
.getDefault().logError("Error refreshing supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
497 * Checks if supplementary resource exist or not.
499 * @return <code>true</code> if one or more files are under the element
500 * supplementary folder
502 public boolean hasSupplementaryResources() {
503 IResource
[] resources
= getSupplementaryResources();
504 return (resources
.length
> 0);
508 * Returns the supplementary resources under the trace supplementary folder.
510 * @return array of resources under the trace supplementary folder.
512 public IResource
[] getSupplementaryResources() {
513 IFolder supplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
514 if (supplFolder
.exists()) {
516 return supplFolder
.members();
517 } catch (CoreException e
) {
518 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
521 return new IResource
[0];
525 * Deletes the given resources.
528 * array of resources to delete.
530 public void deleteSupplementaryResources(IResource
[] resources
) {
532 for (int i
= 0; i
< resources
.length
; i
++) {
534 resources
[i
].delete(true, new NullProgressMonitor());
535 } catch (CoreException e
) {
536 Activator
.getDefault().logError("Error deleting supplementary resource " + resources
[i
], e
); //$NON-NLS-1$
542 * Deletes all supplementary resources in the supplementary directory
544 public void deleteSupplementaryResources() {
545 deleteSupplementaryResources(getSupplementaryResources());
548 private IFolder
createSupplementaryFolder() {
549 IFolder supplFolder
= prepareTraceSupplementaryFolder(getSupplementaryFolderPath(), true);
552 getResource().setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, supplFolder
.getLocation().toOSString());
553 } catch (CoreException e
) {
554 Activator
.getDefault().logError("Error setting persistant property " + TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, e
); //$NON-NLS-1$
559 // -------------------------------------------------------
561 // -------------------------------------------------------
564 * Handler for the Trace Opened signal
567 * The incoming signal
570 public void traceOpened(TmfTraceOpenedSignal signal
) {
571 IResource resource
= signal
.getTrace().getResource();
572 if ((resource
== null) || !resource
.equals(getResource())) {
576 getParent().refresh();