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
.osgi
.util
.NLS
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.editors
.ITmfEventsEditorConstants
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAnalysisManager
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TraceTypeHelper
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
48 import org
.eclipse
.ui
.IEditorReference
;
49 import org
.eclipse
.ui
.IWorkbench
;
50 import org
.eclipse
.ui
.IWorkbenchPage
;
51 import org
.eclipse
.ui
.IWorkbenchWindow
;
52 import org
.eclipse
.ui
.PartInitException
;
53 import org
.eclipse
.ui
.PlatformUI
;
54 import org
.eclipse
.ui
.part
.FileEditorInput
;
57 * Base class for tracing project elements: it implements the common behavior of
58 * all project elements: supplementary files, analysis, types, etc.
60 * @author Geneviève Bastien
62 public abstract class TmfCommonProjectElement
extends TmfProjectModelElement
{
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
68 // This trace type ID as defined in plugin.xml
69 private String fTraceTypeId
= null;
71 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
78 * Constructor. Creates model element.
81 * The name of the element
87 public TmfCommonProjectElement(String name
, IResource resource
, TmfProjectModelElement parent
) {
88 super(name
, resource
, parent
);
89 parent
.addChild(this);
91 TmfSignalManager
.register(this);
94 // ------------------------------------------------------------------------
95 // TmfProjectModelElement
96 // ------------------------------------------------------------------------
99 void refreshChildren() {
101 /* Refreshes the analysis under this trace */
102 Map
<String
, TmfAnalysisElement
> childrenMap
= new HashMap
<>();
103 for (TmfAnalysisElement analysis
: getAvailableAnalysis()) {
104 childrenMap
.put(analysis
.getAnalysisId(), analysis
);
107 TraceTypeHelper helper
= TmfTraceType
.getTraceType(getTraceType());
109 Class
<?
extends ITmfTrace
> traceClass
= null;
111 if (helper
!= null) {
112 traceClass
= helper
.getTraceClass();
115 /* Remove all analysis and return */
116 if (traceClass
== null) {
117 for (TmfAnalysisElement analysis
: childrenMap
.values()) {
118 removeChild(analysis
);
123 /** Get the base path to put the resource to */
124 IPath path
= fResource
.getFullPath();
126 /* Add all new analysis modules or refresh outputs of existing ones */
127 for (IAnalysisModuleHelper module
: TmfAnalysisManager
.getAnalysisModules(traceClass
).values()) {
129 /* If the analysis is not a child of the trace, create it */
130 TmfAnalysisElement analysis
= childrenMap
.remove(module
.getId());
131 if (analysis
== null) {
133 * No need for the resource to exist, nothing will be done with
136 IFolder newresource
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(path
.append(module
.getId()));
137 analysis
= new TmfAnalysisElement(module
.getName(), newresource
, this, module
);
139 analysis
.refreshChildren();
142 /* Remove analysis that are not children of this trace anymore */
143 for (TmfAnalysisElement analysis
: childrenMap
.values()) {
144 removeChild(analysis
);
148 // ------------------------------------------------------------------------
150 // ------------------------------------------------------------------------
153 * Returns the trace type ID.
155 * @return trace type ID.
157 public String
getTraceType() {
162 * Refreshes the trace type field by reading the trace type persistent
163 * property of the resource.
165 public void refreshTraceType() {
167 fTraceTypeId
= TmfTraceType
.getTraceTypeId(getResource());
168 } catch (CoreException e
) {
169 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfCommonProjectElement_ErrorRefreshingProperty
, getName()), e
);
174 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
175 * the corresponding extension.
177 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
179 public abstract ITmfTrace
instantiateTrace();
182 * Return the supplementary folder path for this element. The returned path
183 * is relative to the project's supplementary folder.
185 * @return The supplementary folder path for this element
187 protected String
getSupplementaryFolderPath() {
188 return getElementPath() + getSuffix();
192 * Return the element path relative to its common element (traces folder,
193 * experiments folder or experiment element).
195 * @return The element path
197 public String
getElementPath() {
198 ITmfProjectModelElement parent
= getParent();
199 while (!(parent
instanceof TmfTracesFolder
|| parent
instanceof TmfExperimentElement
|| parent
instanceof TmfExperimentFolder
)) {
200 parent
= parent
.getParent();
202 IPath path
= fResource
.getFullPath().makeRelativeTo(parent
.getPath());
203 return path
.toString();
207 * @return The suffix for the supplementary folder
209 protected String
getSuffix() {
210 return ""; //$NON-NLS-1$
214 * Returns a list of TmfTraceElements contained in project element.
216 * @return a list of TmfTraceElements, empty list if none
218 public List
<TmfTraceElement
> getTraces() {
219 return new ArrayList
<>();
223 * Get the instantiated trace associated with this element.
225 * @return The instantiated trace or null if trace is not (yet) available
227 public ITmfTrace
getTrace() {
228 for (ITmfTrace trace
: TmfTraceManager
.getInstance().getOpenedTraces()) {
229 if (trace
.getResource().equals(getResource())) {
237 * Returns the file resource used to store bookmarks after creating it if
238 * necessary. If the trace resource is a file, it is returned directly. If
239 * the trace resource is a folder, a linked file is returned. The file will
240 * be created if it does not exist.
242 * @return the bookmarks file
243 * @throws CoreException
244 * if the bookmarks file cannot be created
246 public abstract IFile
createBookmarksFile() throws CoreException
;
249 * Actually returns the bookmark file or creates it in the project element's
252 * @param bookmarksFolder
253 * Folder where to put the bookmark file
254 * @param editorInputType
255 * The editor input type to set (trace or experiment)
256 * @return The bookmark file
257 * @throws CoreException
258 * if the bookmarks file cannot be created
260 protected IFile
createBookmarksFile(IFolder bookmarksFolder
, String editorInputType
) throws CoreException
{
261 IFile file
= getBookmarksFile();
262 if (!file
.exists()) {
263 final IFile bookmarksFile
= bookmarksFolder
.getFile(BOOKMARKS_HIDDEN_FILE
);
264 if (!bookmarksFile
.exists()) {
265 final InputStream source
= new ByteArrayInputStream(new byte[0]);
266 bookmarksFile
.create(source
, IResource
.FORCE
| IResource
.HIDDEN
, null);
268 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
| IResource
.HIDDEN
, null);
269 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, editorInputType
);
275 * Returns the optional editor ID from the trace type extension.
277 * @return the editor ID or <code>null</code> if not defined.
279 public abstract String
getEditorId();
282 * Returns the file resource used to store bookmarks. The file may not
285 * @return the bookmarks file
287 public IFile
getBookmarksFile() {
288 final IFolder folder
= (IFolder
) fResource
;
289 IFile file
= folder
.getFile(getName() + '_');
294 * Close open editors associated with this experiment.
296 public void closeEditors() {
297 IFile file
= getBookmarksFile();
298 FileEditorInput input
= new FileEditorInput(file
);
299 IWorkbench wb
= PlatformUI
.getWorkbench();
300 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
301 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
302 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
304 if (editorReference
.getEditorInput().equals(input
)) {
305 wbPage
.closeEditor(editorReference
.getEditor(false), false);
307 } catch (PartInitException e
) {
308 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfCommonProjectElement_ErrorClosingEditor
, getName()), e
);
316 * Get a friendly name for the type of element this common project element
317 * is, to be displayed in UI messages.
319 * @return A string for the type of project element this object is, for
320 * example "trace" or "experiment"
322 public abstract String
getTypeName();
325 * Copy this model element
328 * The name of the new element
329 * @param copySuppFiles
330 * Whether to copy supplementary files or not
331 * @return the new Resource object
333 public IResource
copy(final String newName
, final boolean copySuppFiles
) {
335 final IPath newPath
= getParent().getResource().getFullPath().addTrailingSeparator().append(newName
);
337 /* Copy supplementary files first, only if needed */
339 String newElementPath
= new Path(getElementPath()).removeLastSegments(1).append(newName
).toString();
340 copySupplementaryFolder(newElementPath
);
344 getResource().copy(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
345 IResource trace
= ((IFolder
) getParent().getResource()).findMember(newName
);
347 /* Delete any bookmarks file found in copied trace folder */
348 if (trace
instanceof IFolder
) {
349 IFolder folderTrace
= (IFolder
) trace
;
350 for (IResource member
: folderTrace
.members()) {
351 String traceTypeId
= TmfTraceType
.getTraceTypeId(member
);
352 if (ITmfEventsEditorConstants
.TRACE_INPUT_TYPE_CONSTANTS
.contains(traceTypeId
)) {
353 member
.delete(true, null);
354 } else if (ITmfEventsEditorConstants
.EXPERIMENT_INPUT_TYPE_CONSTANTS
.contains(traceTypeId
)) {
355 member
.delete(true, null);
360 } catch (CoreException e
) {
367 * Get the list of analysis elements
369 * @return Array of analysis elements
371 public List
<TmfAnalysisElement
> getAvailableAnalysis() {
372 List
<ITmfProjectModelElement
> children
= getChildren();
373 List
<TmfAnalysisElement
> analysis
= new ArrayList
<>();
374 for (ITmfProjectModelElement child
: children
) {
375 if (child
instanceof TmfAnalysisElement
) {
376 analysis
.add((TmfAnalysisElement
) child
);
382 // ------------------------------------------------------------------------
383 // Supplementary files operations
384 // ------------------------------------------------------------------------
387 * Deletes this element specific supplementary folder.
389 public void deleteSupplementaryFolder() {
390 IFolder supplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
392 deleteFolder(supplFolder
);
393 } catch (CoreException e
) {
394 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
398 private static void deleteFolder(IFolder folder
) throws CoreException
{
399 if (folder
.exists()) {
400 folder
.delete(true, new NullProgressMonitor());
402 IContainer parent
= folder
.getParent();
403 // delete empty folders up to the parent project
404 if (parent
instanceof IFolder
&& (!parent
.exists() || parent
.members().length
== 0)) {
405 deleteFolder((IFolder
) parent
);
410 * Renames the element specific supplementary folder according to the new
411 * element name or path.
413 * @param newElementPath
414 * The new element name or path
416 public void renameSupplementaryFolder(String newElementPath
) {
417 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
419 // Rename supplementary folder
421 if (oldSupplFolder
.exists()) {
422 IFolder newSupplFolder
= prepareTraceSupplementaryFolder(newElementPath
+ getSuffix(), false);
423 oldSupplFolder
.move(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
425 deleteFolder(oldSupplFolder
);
426 } catch (CoreException e
) {
427 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
432 * Copies the element specific supplementary folder to the new element name
435 * @param newElementPath
436 * The new element name or path
438 public void copySupplementaryFolder(String newElementPath
) {
439 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
441 // copy supplementary folder
442 if (oldSupplFolder
.exists()) {
444 IFolder newSupplFolder
= prepareTraceSupplementaryFolder(newElementPath
+ getSuffix(), false);
445 oldSupplFolder
.copy(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
446 } catch (CoreException e
) {
447 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
453 * Copies the element specific supplementary folder a new folder.
456 * The destination folder to copy to.
458 public void copySupplementaryFolder(IFolder destination
) {
459 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
461 // copy supplementary folder
462 if (oldSupplFolder
.exists()) {
464 TraceUtils
.createFolder((IFolder
) destination
.getParent(), new NullProgressMonitor());
465 oldSupplFolder
.copy(destination
.getFullPath(), true, new NullProgressMonitor());
466 } catch (CoreException e
) {
467 Activator
.getDefault().logError("Error copying supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
473 * Refreshes the element specific supplementary folder information. It
474 * creates the folder if not exists. It sets the persistence property of the
477 public void refreshSupplementaryFolder() {
478 IFolder supplFolder
= createSupplementaryFolder();
480 supplFolder
.refreshLocal(IResource
.DEPTH_INFINITE
, new NullProgressMonitor());
481 } catch (CoreException e
) {
482 Activator
.getDefault().logError("Error refreshing supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
487 * Checks if supplementary resource exist or not.
489 * @return <code>true</code> if one or more files are under the element
490 * supplementary folder
492 public boolean hasSupplementaryResources() {
493 IResource
[] resources
= getSupplementaryResources();
494 return (resources
.length
> 0);
498 * Returns the supplementary resources under the trace supplementary folder.
500 * @return array of resources under the trace supplementary folder.
502 public IResource
[] getSupplementaryResources() {
503 IFolder supplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
504 if (supplFolder
.exists()) {
506 return supplFolder
.members();
507 } catch (CoreException e
) {
508 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
511 return new IResource
[0];
515 * Deletes the given resources.
518 * array of resources to delete.
520 public void deleteSupplementaryResources(IResource
[] resources
) {
522 for (int i
= 0; i
< resources
.length
; i
++) {
524 resources
[i
].delete(true, new NullProgressMonitor());
525 } catch (CoreException e
) {
526 Activator
.getDefault().logError("Error deleting supplementary resource " + resources
[i
], e
); //$NON-NLS-1$
532 * Deletes all supplementary resources in the supplementary directory
534 public void deleteSupplementaryResources() {
535 deleteSupplementaryResources(getSupplementaryResources());
538 private IFolder
createSupplementaryFolder() {
539 IFolder supplFolder
= prepareTraceSupplementaryFolder(getSupplementaryFolderPath(), true);
542 fResource
.setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, supplFolder
.getLocationURI().getPath());
543 } catch (CoreException e
) {
544 Activator
.getDefault().logError("Error setting persistant property " + TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, e
); //$NON-NLS-1$
549 // -------------------------------------------------------
551 // -------------------------------------------------------
554 * Handler for the Trace Opened signal
557 * The incoming signal
560 public void traceOpened(TmfTraceOpenedSignal signal
) {
561 IResource resource
= signal
.getTrace().getResource();
562 if ((resource
== null) || !resource
.equals(getResource())) {
566 getParent().refresh();