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 java
.io
.ByteArrayInputStream
;
20 import java
.io
.InputStream
;
21 import java
.util
.ArrayList
;
22 import java
.util
.HashMap
;
23 import java
.util
.List
;
26 import org
.eclipse
.core
.resources
.IContainer
;
27 import org
.eclipse
.core
.resources
.IFile
;
28 import org
.eclipse
.core
.resources
.IFolder
;
29 import org
.eclipse
.core
.resources
.IResource
;
30 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
31 import org
.eclipse
.core
.runtime
.CoreException
;
32 import org
.eclipse
.core
.runtime
.IPath
;
33 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
34 import org
.eclipse
.core
.runtime
.Path
;
35 import org
.eclipse
.jdt
.annotation
.NonNull
;
36 import org
.eclipse
.osgi
.util
.NLS
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.editors
.ITmfEventsEditorConstants
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAnalysisManager
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TraceTypeHelper
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
49 import org
.eclipse
.ui
.IEditorReference
;
50 import org
.eclipse
.ui
.IWorkbench
;
51 import org
.eclipse
.ui
.IWorkbenchPage
;
52 import org
.eclipse
.ui
.IWorkbenchWindow
;
53 import org
.eclipse
.ui
.PartInitException
;
54 import org
.eclipse
.ui
.PlatformUI
;
55 import org
.eclipse
.ui
.part
.FileEditorInput
;
58 * Base class for tracing project elements: it implements the common behavior of
59 * all project elements: supplementary files, analysis, types, etc.
61 * @author Geneviève Bastien
63 public abstract class TmfCommonProjectElement
extends TmfProjectModelElement
{
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
69 // This trace type ID as defined in plugin.xml
70 private String fTraceTypeId
= null;
72 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
79 * Constructor. Creates model element.
82 * The name of the element
88 public TmfCommonProjectElement(String name
, IResource resource
, TmfProjectModelElement parent
) {
89 super(name
, resource
, parent
);
91 TmfSignalManager
.register(this);
94 // ------------------------------------------------------------------------
95 // TmfProjectModelElement
96 // ------------------------------------------------------------------------
102 protected void refreshChildren() {
104 /* Refreshes the analysis under this trace */
105 Map
<String
, TmfAnalysisElement
> childrenMap
= new HashMap
<>();
106 for (TmfAnalysisElement analysis
: getAvailableAnalysis()) {
107 childrenMap
.put(analysis
.getAnalysisId(), analysis
);
110 TraceTypeHelper helper
= TmfTraceType
.getTraceType(getTraceType());
112 Class
<@NonNull ?
extends ITmfTrace
> traceClass
= null;
114 if (helper
!= null) {
115 traceClass
= helper
.getTraceClass();
118 /* Remove all analysis and return */
119 if (traceClass
== null) {
120 for (TmfAnalysisElement analysis
: childrenMap
.values()) {
121 removeChild(analysis
);
126 /** Get the base path to put the resource to */
127 IPath path
= getResource().getFullPath();
129 /* Add all new analysis modules or refresh outputs of existing ones */
130 for (IAnalysisModuleHelper module
: TmfAnalysisManager
.getAnalysisModules(traceClass
).values()) {
132 /* If the analysis is not a child of the trace, create it */
133 TmfAnalysisElement analysis
= childrenMap
.remove(module
.getId());
134 if (analysis
== null) {
136 * No need for the resource to exist, nothing will be done with
139 IFolder newresource
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(path
.append(module
.getId()));
140 analysis
= new TmfAnalysisElement(module
.getName(), newresource
, this, module
);
143 analysis
.refreshChildren();
146 /* Remove analysis that are not children of this trace anymore */
147 for (TmfAnalysisElement analysis
: childrenMap
.values()) {
148 removeChild(analysis
);
152 // ------------------------------------------------------------------------
154 // ------------------------------------------------------------------------
157 * Returns the trace type ID.
159 * @return trace type ID.
161 public String
getTraceType() {
166 * Refreshes the trace type field by reading the trace type persistent
167 * property of the resource.
169 public void refreshTraceType() {
171 fTraceTypeId
= TmfTraceType
.getTraceTypeId(getResource());
172 } catch (CoreException e
) {
173 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfCommonProjectElement_ErrorRefreshingProperty
, getName()), e
);
178 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
179 * the corresponding extension.
181 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
183 public abstract ITmfTrace
instantiateTrace();
186 * Return the supplementary folder path for this element. The returned path
187 * is relative to the project's supplementary folder.
189 * @return The supplementary folder path for this element
191 protected String
getSupplementaryFolderPath() {
192 return getElementPath() + getSuffix();
196 * Return the element path relative to its common element (traces folder,
197 * experiments folder or experiment element).
199 * @return The element path
201 public String
getElementPath() {
202 ITmfProjectModelElement parent
= getParent();
203 while (!(parent
instanceof TmfTracesFolder
|| parent
instanceof TmfExperimentElement
|| parent
instanceof TmfExperimentFolder
)) {
204 parent
= parent
.getParent();
206 IPath path
= getResource().getFullPath().makeRelativeTo(parent
.getPath());
207 return path
.toString();
211 * @return The suffix for the supplementary folder
213 protected String
getSuffix() {
214 return ""; //$NON-NLS-1$
218 * Returns a list of TmfTraceElements contained in project element.
220 * @return a list of TmfTraceElements, empty list if none
222 public List
<TmfTraceElement
> getTraces() {
223 return new ArrayList
<>();
227 * Get the instantiated trace associated with this element.
229 * @return The instantiated trace or null if trace is not (yet) available
231 public ITmfTrace
getTrace() {
232 for (ITmfTrace trace
: TmfTraceManager
.getInstance().getOpenedTraces()) {
233 if (trace
.getResource().equals(getResource())) {
241 * Returns the file resource used to store bookmarks after creating it if
242 * necessary. If the trace resource is a file, it is returned directly. If
243 * the trace resource is a folder, a linked file is returned. The file will
244 * be created if it does not exist.
246 * @return the bookmarks file
247 * @throws CoreException
248 * if the bookmarks file cannot be created
250 public abstract IFile
createBookmarksFile() throws CoreException
;
253 * Actually returns the bookmark file or creates it in the project element's
256 * @param bookmarksFolder
257 * Folder where to put the bookmark file
258 * @param editorInputType
259 * The editor input type to set (trace or experiment)
260 * @return The bookmark file
261 * @throws CoreException
262 * if the bookmarks file cannot be created
264 protected IFile
createBookmarksFile(IFolder bookmarksFolder
, String editorInputType
) throws CoreException
{
265 IFile file
= getBookmarksFile();
266 if (!file
.exists()) {
267 final IFile bookmarksFile
= bookmarksFolder
.getFile(BOOKMARKS_HIDDEN_FILE
);
268 if (!bookmarksFile
.exists()) {
269 final InputStream source
= new ByteArrayInputStream(new byte[0]);
270 bookmarksFile
.create(source
, IResource
.FORCE
| IResource
.HIDDEN
, null);
272 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
| IResource
.HIDDEN
, null);
273 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, editorInputType
);
279 * Returns the optional editor ID from the trace type extension.
281 * @return the editor ID or <code>null</code> if not defined.
283 public abstract String
getEditorId();
286 * Returns the file resource used to store bookmarks. The file may not
289 * @return the bookmarks file
291 public IFile
getBookmarksFile() {
292 final IFolder folder
= (IFolder
) getResource();
293 IFile file
= folder
.getFile(getName() + '_');
298 * Close open editors associated with this experiment.
300 public void closeEditors() {
301 IFile file
= getBookmarksFile();
302 FileEditorInput input
= new FileEditorInput(file
);
303 IWorkbench wb
= PlatformUI
.getWorkbench();
304 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
305 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
306 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
308 if (editorReference
.getEditorInput().equals(input
)) {
309 wbPage
.closeEditor(editorReference
.getEditor(false), false);
311 } catch (PartInitException e
) {
312 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfCommonProjectElement_ErrorClosingEditor
, getName()), e
);
320 * Get a friendly name for the type of element this common project element
321 * is, to be displayed in UI messages.
323 * @return A string for the type of project element this object is, for
324 * example "trace" or "experiment"
326 public abstract String
getTypeName();
329 * Copy this model element
332 * The name of the new element
333 * @param copySuppFiles
334 * Whether to copy supplementary files or not
335 * @return the new Resource object
337 public IResource
copy(final String newName
, final boolean copySuppFiles
) {
339 final IPath newPath
= getParent().getResource().getFullPath().addTrailingSeparator().append(newName
);
341 /* Copy supplementary files first, only if needed */
343 String newElementPath
= new Path(getElementPath()).removeLastSegments(1).append(newName
).toString();
344 copySupplementaryFolder(newElementPath
);
348 getResource().copy(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
349 IResource trace
= ((IFolder
) getParent().getResource()).findMember(newName
);
351 /* Delete any bookmarks file found in copied trace folder */
352 if (trace
instanceof IFolder
) {
353 IFolder folderTrace
= (IFolder
) trace
;
354 for (IResource member
: folderTrace
.members()) {
355 String traceTypeId
= TmfTraceType
.getTraceTypeId(member
);
356 if (ITmfEventsEditorConstants
.TRACE_INPUT_TYPE_CONSTANTS
.contains(traceTypeId
)) {
357 member
.delete(true, null);
358 } else if (ITmfEventsEditorConstants
.EXPERIMENT_INPUT_TYPE_CONSTANTS
.contains(traceTypeId
)) {
359 member
.delete(true, null);
364 } catch (CoreException e
) {
371 * Get the list of analysis elements
373 * @return Array of analysis elements
375 public List
<TmfAnalysisElement
> getAvailableAnalysis() {
376 List
<ITmfProjectModelElement
> children
= getChildren();
377 List
<TmfAnalysisElement
> analysis
= new ArrayList
<>();
378 for (ITmfProjectModelElement child
: children
) {
379 if (child
instanceof TmfAnalysisElement
) {
380 analysis
.add((TmfAnalysisElement
) child
);
386 // ------------------------------------------------------------------------
387 // Supplementary files operations
388 // ------------------------------------------------------------------------
391 * Deletes this element specific supplementary folder.
393 public void deleteSupplementaryFolder() {
394 IFolder supplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
396 deleteFolder(supplFolder
);
397 } catch (CoreException e
) {
398 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
402 private static void deleteFolder(IFolder folder
) throws CoreException
{
403 if (folder
.exists()) {
404 folder
.delete(true, new NullProgressMonitor());
406 IContainer parent
= folder
.getParent();
407 // delete empty folders up to the parent project
408 if (parent
instanceof IFolder
&& (!parent
.exists() || parent
.members().length
== 0)) {
409 deleteFolder((IFolder
) parent
);
414 * Renames the element specific supplementary folder according to the new
415 * element name or path.
417 * @param newElementPath
418 * The new element name or path
420 public void renameSupplementaryFolder(String newElementPath
) {
421 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
423 // Rename supplementary folder
425 if (oldSupplFolder
.exists()) {
426 IFolder newSupplFolder
= prepareTraceSupplementaryFolder(newElementPath
+ getSuffix(), false);
427 oldSupplFolder
.move(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
429 deleteFolder(oldSupplFolder
);
430 } catch (CoreException e
) {
431 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
436 * Copies the element specific supplementary folder to the new element name
439 * @param newElementPath
440 * The new element name or path
442 public void copySupplementaryFolder(String newElementPath
) {
443 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
445 // copy supplementary folder
446 if (oldSupplFolder
.exists()) {
448 IFolder newSupplFolder
= prepareTraceSupplementaryFolder(newElementPath
+ getSuffix(), false);
449 oldSupplFolder
.copy(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
450 } catch (CoreException e
) {
451 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
457 * Copies the element specific supplementary folder a new folder.
460 * The destination folder to copy to.
462 public void copySupplementaryFolder(IFolder destination
) {
463 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
465 // copy supplementary folder
466 if (oldSupplFolder
.exists()) {
468 TraceUtils
.createFolder((IFolder
) destination
.getParent(), new NullProgressMonitor());
469 oldSupplFolder
.copy(destination
.getFullPath(), true, new NullProgressMonitor());
470 } catch (CoreException e
) {
471 Activator
.getDefault().logError("Error copying supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
477 * Refreshes the element specific supplementary folder information. It
478 * creates the folder if not exists. It sets the persistence property of the
481 public void refreshSupplementaryFolder() {
482 IFolder supplFolder
= createSupplementaryFolder();
484 supplFolder
.refreshLocal(IResource
.DEPTH_INFINITE
, new NullProgressMonitor());
485 } catch (CoreException e
) {
486 Activator
.getDefault().logError("Error refreshing supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
491 * Checks if supplementary resource exist or not.
493 * @return <code>true</code> if one or more files are under the element
494 * supplementary folder
496 public boolean hasSupplementaryResources() {
497 IResource
[] resources
= getSupplementaryResources();
498 return (resources
.length
> 0);
502 * Returns the supplementary resources under the trace supplementary folder.
504 * @return array of resources under the trace supplementary folder.
506 public IResource
[] getSupplementaryResources() {
507 IFolder supplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
508 if (supplFolder
.exists()) {
510 return supplFolder
.members();
511 } catch (CoreException e
) {
512 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
515 return new IResource
[0];
519 * Deletes the given resources.
522 * array of resources to delete.
524 public void deleteSupplementaryResources(IResource
[] resources
) {
526 for (int i
= 0; i
< resources
.length
; i
++) {
528 resources
[i
].delete(true, new NullProgressMonitor());
529 } catch (CoreException e
) {
530 Activator
.getDefault().logError("Error deleting supplementary resource " + resources
[i
], e
); //$NON-NLS-1$
536 * Deletes all supplementary resources in the supplementary directory
538 public void deleteSupplementaryResources() {
539 deleteSupplementaryResources(getSupplementaryResources());
542 private IFolder
createSupplementaryFolder() {
543 IFolder supplFolder
= prepareTraceSupplementaryFolder(getSupplementaryFolderPath(), true);
546 getResource().setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, supplFolder
.getLocation().toOSString());
547 } catch (CoreException e
) {
548 Activator
.getDefault().logError("Error setting persistant property " + TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, e
); //$NON-NLS-1$
553 // -------------------------------------------------------
555 // -------------------------------------------------------
558 * Handler for the Trace Opened signal
561 * The incoming signal
564 public void traceOpened(TmfTraceOpenedSignal signal
) {
565 IResource resource
= signal
.getTrace().getResource();
566 if ((resource
== null) || !resource
.equals(getResource())) {
570 getParent().refresh();