1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 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
.linuxtools
.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
.linuxtools
.internal
.tmf
.ui
.Activator
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.TmfAnalysisManager
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTxtTrace
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTxtTraceDefinition
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomXmlTrace
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TmfTraceType
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TraceTypeHelper
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
47 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
48 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
49 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
50 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
51 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
52 import org
.eclipse
.osgi
.util
.NLS
;
53 import org
.eclipse
.ui
.IEditorReference
;
54 import org
.eclipse
.ui
.IWorkbench
;
55 import org
.eclipse
.ui
.IWorkbenchPage
;
56 import org
.eclipse
.ui
.IWorkbenchWindow
;
57 import org
.eclipse
.ui
.PartInitException
;
58 import org
.eclipse
.ui
.PlatformUI
;
59 import org
.eclipse
.ui
.part
.FileEditorInput
;
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
68 public abstract class TmfCommonProjectElement
extends TmfProjectModelElement
{
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
74 // This trace type ID as defined in plugin.xml
75 private String fTraceTypeId
= null;
77 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
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
);
95 parent
.addChild(this);
97 TmfSignalManager
.register(this);
100 // ------------------------------------------------------------------------
101 // TmfProjectModelElement
102 // ------------------------------------------------------------------------
105 void refreshChildren() {
107 /* Refreshes the analysis under this trace */
108 Map
<String
, TmfAnalysisElement
> childrenMap
= new HashMap
<>();
109 for (TmfAnalysisElement analysis
: getAvailableAnalysis()) {
110 childrenMap
.put(analysis
.getAnalysisId(), analysis
);
113 TraceTypeHelper helper
= TmfTraceType
.getTraceType(getTraceType());
115 Class
<?
extends ITmfTrace
> traceClass
= null;
117 if (helper
== null && getTraceType() != null) {
118 if (fTraceTypeId
.startsWith(CustomTxtTrace
.class.getCanonicalName())) {
119 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
120 if (fTraceTypeId
.equals(CustomTxtTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
121 traceClass
= CustomTxtTrace
.class;
125 if (fTraceTypeId
.startsWith(CustomXmlTrace
.class.getCanonicalName())) {
126 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
127 if (fTraceTypeId
.equals(CustomXmlTrace
.class.getCanonicalName() + ":" + def
.definitionName
)) { //$NON-NLS-1$
128 traceClass
= CustomTxtTrace
.class;
132 } else if (helper
!= null) {
133 traceClass
= helper
.getTraceClass();
136 /* Remove all analysis and return */
137 if (traceClass
== null) {
138 for (TmfAnalysisElement analysis
: childrenMap
.values()) {
139 removeChild(analysis
);
144 /** Get the base path to put the resource to */
145 IPath path
= fResource
.getFullPath();
147 /* Add all new analysis modules or refresh outputs of existing ones */
148 for (IAnalysisModuleHelper module
: TmfAnalysisManager
.getAnalysisModules(traceClass
).values()) {
150 /* If the analysis is not a child of the trace, create it */
151 TmfAnalysisElement analysis
= childrenMap
.remove(module
.getId());
152 if (analysis
== null) {
154 * No need for the resource to exist, nothing will be done with
157 IFolder newresource
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(path
.append(module
.getId()));
158 analysis
= new TmfAnalysisElement(module
.getName(), newresource
, this, module
.getId());
160 analysis
.refreshChildren();
163 /* Remove analysis that are not children of this trace anymore */
164 for (TmfAnalysisElement analysis
: childrenMap
.values()) {
165 removeChild(analysis
);
169 // ------------------------------------------------------------------------
171 // ------------------------------------------------------------------------
174 * Returns the trace type ID.
176 * @return trace type ID.
178 public String
getTraceType() {
183 * Refreshes the trace type filed by reading the trace type persistent
184 * property of the resource referenece.
186 public void refreshTraceType() {
188 fTraceTypeId
= getResource().getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
189 } catch (CoreException e
) {
190 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfCommonProjectElement_ErrorRefreshingProperty
, getName()), e
);
195 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
196 * the corresponding extension.
198 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
200 public abstract ITmfTrace
instantiateTrace();
203 * Return the supplementary folder path for this element. The returned path
204 * is relative to the project's supplementary folder.
206 * @return The supplementary folder path for this element
208 protected String
getSupplementaryFolderPath() {
209 return getElementPath() + getSuffix();
213 * Return the element path relative to its common element (traces folder,
214 * experiments folder or experiment element).
215 * @return The element path
217 public String
getElementPath() {
218 ITmfProjectModelElement parent
= getParent();
219 while (!(parent
instanceof TmfTracesFolder
|| parent
instanceof TmfExperimentElement
|| parent
instanceof TmfExperimentFolder
)) {
220 parent
= parent
.getParent();
222 IPath path
= fResource
.getFullPath().makeRelativeTo(parent
.getPath());
223 return path
.toString();
227 * @return The suffix for the supplementary folder
229 protected String
getSuffix() {
230 return ""; //$NON-NLS-1$
234 * Returns a list of TmfTraceElements contained in project element.
236 * @return a list of TmfTraceElements, empty list if none
238 public List
<TmfTraceElement
> getTraces() {
239 return new ArrayList
<>();
243 * Get the instantiated trace associated with this element.
245 * @return The instantiated trace or null if trace is not (yet) available
247 public ITmfTrace
getTrace() {
248 for (ITmfTrace trace
: TmfTraceManager
.getInstance().getOpenedTraces()) {
249 if (trace
.getResource().equals(getResource())) {
257 * Returns the file resource used to store bookmarks after creating it if
258 * necessary. If the trace resource is a file, it is returned directly. If
259 * the trace resource is a folder, a linked file is returned. The file will
260 * be created if it does not exist.
262 * @return the bookmarks file
263 * @throws CoreException
264 * if the bookmarks file cannot be created
266 public abstract IFile
createBookmarksFile() throws CoreException
;
269 * Actually returns the bookmark file or creates it in the project element's
272 * @param bookmarksFolder
273 * Folder where to put the bookmark file
275 * The canonical name to set as tracetype
276 * @return The bookmark file
277 * @throws CoreException
278 * if the bookmarks file cannot be created
280 protected IFile
createBookmarksFile(IFolder bookmarksFolder
, String traceType
) throws CoreException
{
281 IFile file
= getBookmarksFile();
282 if (!file
.exists()) {
283 final IFile bookmarksFile
= bookmarksFolder
.getFile(BOOKMARKS_HIDDEN_FILE
);
284 if (!bookmarksFile
.exists()) {
285 final InputStream source
= new ByteArrayInputStream(new byte[0]);
286 bookmarksFile
.create(source
, true, null);
288 bookmarksFile
.setHidden(true);
289 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
, null);
290 file
.setHidden(true);
291 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceType
);
297 * Returns the optional editor ID from the trace type extension.
299 * @return the editor ID or <code>null</code> if not defined.
301 public abstract String
getEditorId();
304 * Returns the file resource used to store bookmarks. The file may not
307 * @return the bookmarks file
309 public IFile
getBookmarksFile() {
310 final IFolder folder
= (IFolder
) fResource
;
311 IFile file
= folder
.getFile(getName() + '_');
316 * Close open editors associated with this experiment.
318 public void closeEditors() {
319 IFile file
= getBookmarksFile();
320 FileEditorInput input
= new FileEditorInput(file
);
321 IWorkbench wb
= PlatformUI
.getWorkbench();
322 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
323 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
324 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
326 if (editorReference
.getEditorInput().equals(input
)) {
327 wbPage
.closeEditor(editorReference
.getEditor(false), false);
329 } catch (PartInitException e
) {
330 Activator
.getDefault().logError(NLS
.bind(Messages
.TmfCommonProjectElement_ErrorClosingEditor
, getName()), e
);
338 * Get a friendly name for the type of element this common project element
339 * is, to be displayed in UI messages.
341 * @return A string for the type of project element this object is, for
342 * example "trace" or "experiment"
344 public abstract String
getTypeName();
347 * Copy this model element
350 * The name of the new element
351 * @param copySuppFiles
352 * Whether to copy supplementary files or not
353 * @return the new Resource object
355 public IResource
copy(final String newName
, final boolean copySuppFiles
) {
357 final IPath newPath
= getParent().getResource().getFullPath().addTrailingSeparator().append(newName
);
359 /* Copy supplementary files first, only if needed */
361 String newElementPath
= new Path(getElementPath()).removeLastSegments(1).append(newName
).toString();
362 copySupplementaryFolder(newElementPath
);
366 getResource().copy(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
368 /* Delete any bookmarks file found in copied trace folder */
369 IFolder folder
= ((IFolder
) getParent().getResource()).getFolder(newName
);
370 if (folder
.exists()) {
371 for (IResource member
: folder
.members()) {
372 if (TmfTrace
.class.getCanonicalName().equals(member
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
))) {
373 member
.delete(true, null);
374 } else if (TmfExperiment
.class.getCanonicalName().equals(member
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
))) {
375 member
.delete(true, null);
380 } catch (CoreException e
) {
387 * Get the list of analysis elements
389 * @return Array of analysis elements
391 public List
<TmfAnalysisElement
> getAvailableAnalysis() {
392 List
<ITmfProjectModelElement
> children
= getChildren();
393 List
<TmfAnalysisElement
> analysis
= new ArrayList
<>();
394 for (ITmfProjectModelElement child
: children
) {
395 if (child
instanceof TmfAnalysisElement
) {
396 analysis
.add((TmfAnalysisElement
) child
);
402 // ------------------------------------------------------------------------
403 // Supplementary files operations
404 // ------------------------------------------------------------------------
407 * Deletes this element specific supplementary folder.
409 public void deleteSupplementaryFolder() {
410 IFolder supplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
412 deleteFolder(supplFolder
);
413 } catch (CoreException e
) {
414 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
418 private static void deleteFolder(IFolder folder
) throws CoreException
{
419 if (folder
.exists()) {
420 folder
.delete(true, new NullProgressMonitor());
422 IContainer parent
= folder
.getParent();
423 // delete empty folders up to the parent project
424 if (parent
instanceof IFolder
&& (!parent
.exists() || parent
.members().length
== 0)) {
425 deleteFolder((IFolder
) parent
);
430 * Renames the element specific supplementary folder according to the new
431 * element name or path.
433 * @param newElementPath
434 * The new element name or path
436 public void renameSupplementaryFolder(String newElementPath
) {
437 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
439 // Rename supplementary folder
441 if (oldSupplFolder
.exists()) {
442 IFolder newSupplFolder
= prepareTraceSupplementaryFolder(newElementPath
+ getSuffix(), false);
443 oldSupplFolder
.move(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
445 deleteFolder(oldSupplFolder
);
446 } catch (CoreException e
) {
447 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
452 * Copies the element specific supplementary folder to the new element name
455 * @param newElementPath
456 * The new element name or path
458 public void copySupplementaryFolder(String newElementPath
) {
459 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
461 // copy supplementary folder
462 if (oldSupplFolder
.exists()) {
464 IFolder newSupplFolder
= prepareTraceSupplementaryFolder(newElementPath
+ getSuffix(), false);
465 oldSupplFolder
.copy(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
466 } catch (CoreException e
) {
467 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
473 * Copies the element specific supplementary folder a new folder.
476 * The destination folder to copy to.
478 public void copySupplementaryFolder(IFolder destination
) {
479 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
481 // copy supplementary folder
482 if (oldSupplFolder
.exists()) {
484 TraceUtils
.createFolder((IFolder
) destination
.getParent(), new NullProgressMonitor());
485 oldSupplFolder
.copy(destination
.getFullPath(), true, new NullProgressMonitor());
486 } catch (CoreException e
) {
487 Activator
.getDefault().logError("Error copying supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
493 * Refreshes the element specific supplementary folder information. It
494 * creates the folder if not exists. It sets the persistence property of the
497 public void refreshSupplementaryFolder() {
498 createSupplementaryFolder();
502 * Checks if supplementary resource exist or not.
504 * @return <code>true</code> if one or more files are under the element
505 * supplementary folder
507 public boolean hasSupplementaryResources() {
508 IResource
[] resources
= getSupplementaryResources();
509 return (resources
.length
> 0);
513 * Returns the supplementary resources under the trace supplementary folder.
515 * @return array of resources under the trace supplementary folder.
517 public IResource
[] getSupplementaryResources() {
518 IFolder supplFolder
= getTraceSupplementaryFolder(getSupplementaryFolderPath());
519 if (supplFolder
.exists()) {
521 return supplFolder
.members();
522 } catch (CoreException e
) {
523 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
526 return new IResource
[0];
530 * Deletes the given resources.
533 * array of resources to delete.
535 public void deleteSupplementaryResources(IResource
[] resources
) {
537 for (int i
= 0; i
< resources
.length
; i
++) {
539 resources
[i
].delete(true, new NullProgressMonitor());
540 } catch (CoreException e
) {
541 Activator
.getDefault().logError("Error deleting supplementary resource " + resources
[i
], e
); //$NON-NLS-1$
547 * Deletes all supplementary resources in the supplementary directory
549 public void deleteSupplementaryResources() {
550 deleteSupplementaryResources(getSupplementaryResources());
553 private void createSupplementaryFolder() {
554 IFolder supplFolder
= prepareTraceSupplementaryFolder(getSupplementaryFolderPath(), true);
557 fResource
.setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, supplFolder
.getLocationURI().getPath());
558 } catch (CoreException e
) {
559 Activator
.getDefault().logError("Error setting persistant property " + TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, e
); //$NON-NLS-1$
564 // -------------------------------------------------------
566 // -------------------------------------------------------
569 * Handler for the Trace Opened signal
572 * The incoming signal
575 public void traceOpened(TmfTraceOpenedSignal signal
) {
576 IResource resource
= signal
.getTrace().getResource();
577 if ((resource
== null) || !resource
.equals(getResource())) {
581 getParent().refresh();