tmf: Introduce "External Analyses" and "Reports" project elements
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / project / model / TmfCommonProjectElement.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson, École Polytechnique de Montréal
3 *
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
8 *
9 * Contributors:
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 *******************************************************************************/
16
17 package org.eclipse.tracecompass.tmf.ui.project.model;
18
19 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
20
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;
26
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;
60
61 /**
62 * Base class for tracing project elements: it implements the common behavior of
63 * all project elements: supplementary files, analysis, types, etc.
64 *
65 * @author Geneviève Bastien
66 */
67 public abstract class TmfCommonProjectElement extends TmfProjectModelElement {
68
69 // ------------------------------------------------------------------------
70 // Attributes
71 // ------------------------------------------------------------------------
72
73 private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
74
75 /* Direct child elements */
76 private TmfViewsElement fViewsElement = null;
77 private TmfOnDemandAnalysesElement fOnDemandAnalysesElement = null;
78 private TmfReportsElement fReportsElement = null;
79
80 /** This trace type ID as defined in plugin.xml */
81 private String fTraceTypeId = null;
82
83 // ------------------------------------------------------------------------
84 // Constructors
85 // ------------------------------------------------------------------------
86
87 /**
88 * Constructor. Creates model element.
89 *
90 * @param name
91 * The name of the element
92 * @param resource
93 * The resource.
94 * @param parent
95 * The parent element
96 */
97 public TmfCommonProjectElement(String name, IResource resource, TmfProjectModelElement parent) {
98 super(name, resource, parent);
99 refreshTraceType();
100 TmfSignalManager.register(this);
101 }
102
103 // ------------------------------------------------------------------------
104 // TmfProjectModelElement
105 // ------------------------------------------------------------------------
106
107 /**
108 * @since 2.0
109 */
110 @Override
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();
115
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);
121 }
122 fViewsElement.refreshChildren();
123
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);
129 }
130 fOnDemandAnalysesElement.refreshChildren();
131
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);
137 }
138 fReportsElement.refreshChildren();
139 }
140
141 /**
142 * @since 2.0
143 */
144 @Override
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);
151 }
152
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();
159 if (name != null) {
160 Bundle bundle = Platform.getBundle(name);
161 if (bundle != null) {
162 Image image = TmfProjectModelIcons.loadIcon(bundle, iconAttr);
163 if (image != null) {
164 return image;
165 }
166 }
167 }
168 }
169 }
170 /* Let subclasses specify an icon */
171 return null;
172 }
173
174 // ------------------------------------------------------------------------
175 // Operations
176 // ------------------------------------------------------------------------
177
178 /**
179 * Get the child element "Views". There should always be one.
180 *
181 * @return The child element
182 * @since 2.0
183 */
184 protected TmfViewsElement getChildElementViews() {
185 return fViewsElement;
186 }
187
188 /**
189 * Returns the trace type ID.
190 *
191 * @return trace type ID.
192 */
193 public String getTraceType() {
194 return fTraceTypeId;
195 }
196
197 /**
198 * Refreshes the trace type field by reading the trace type persistent
199 * property of the resource.
200 */
201 public void refreshTraceType() {
202 try {
203 fTraceTypeId = TmfTraceType.getTraceTypeId(getResource());
204 } catch (CoreException e) {
205 Activator.getDefault().logError(NLS.bind(Messages.TmfCommonProjectElement_ErrorRefreshingProperty, getName()), e);
206 }
207 }
208
209 /**
210 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
211 * the corresponding extension.
212 *
213 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
214 */
215 public abstract ITmfTrace instantiateTrace();
216
217 /**
218 * Return the supplementary folder path for this element. The returned path
219 * is relative to the project's supplementary folder.
220 *
221 * @return The supplementary folder path for this element
222 */
223 protected String getSupplementaryFolderPath() {
224 return getElementPath() + getSuffix();
225 }
226
227 /**
228 * Return the element path relative to its common element (traces folder,
229 * experiments folder or experiment element).
230 *
231 * @return The element path
232 */
233 public @NonNull String getElementPath() {
234 ITmfProjectModelElement parent = getParent();
235 while (!(parent instanceof TmfTracesFolder || parent instanceof TmfExperimentElement || parent instanceof TmfExperimentFolder)) {
236 parent = parent.getParent();
237 }
238 IPath path = getResource().getFullPath().makeRelativeTo(parent.getPath());
239 return checkNotNull(path.toString());
240 }
241
242 /**
243 * @return The suffix for the supplementary folder
244 */
245 protected String getSuffix() {
246 return ""; //$NON-NLS-1$
247 }
248
249 /**
250 * Returns a list of TmfTraceElements contained in project element.
251 *
252 * @return a list of TmfTraceElements, empty list if none
253 */
254 public List<TmfTraceElement> getTraces() {
255 return new ArrayList<>();
256 }
257
258 /**
259 * Get the instantiated trace associated with this element.
260 *
261 * @return The instantiated trace or null if trace is not (yet) available
262 */
263 public ITmfTrace getTrace() {
264 for (ITmfTrace trace : TmfTraceManager.getInstance().getOpenedTraces()) {
265 if (trace.getResource().equals(getResource())) {
266 return trace;
267 }
268 }
269 return null;
270 }
271
272 /**
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.
277 *
278 * @return the bookmarks file
279 * @throws CoreException
280 * if the bookmarks file cannot be created
281 */
282 public abstract IFile createBookmarksFile() throws CoreException;
283
284 /**
285 * Actually returns the bookmark file or creates it in the project element's
286 * folder
287 *
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
295 */
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);
303 }
304 file.createLink(bookmarksFile.getLocation(), IResource.REPLACE | IResource.HIDDEN, null);
305 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, editorInputType);
306 }
307 return file;
308 }
309
310 /**
311 * Returns the optional editor ID from the trace type extension.
312 *
313 * @return the editor ID or <code>null</code> if not defined.
314 */
315 public abstract String getEditorId();
316
317 /**
318 * Returns the file resource used to store bookmarks. The file may not
319 * exist.
320 *
321 * @return the bookmarks file
322 */
323 public IFile getBookmarksFile() {
324 final IFolder folder = (IFolder) getResource();
325 IFile file = folder.getFile(getName() + '_');
326 return file;
327 }
328
329 /**
330 * Close open editors associated with this experiment.
331 */
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()) {
339 try {
340 if (editorReference.getEditorInput().equals(input)) {
341 wbPage.closeEditor(editorReference.getEditor(false), false);
342 }
343 } catch (PartInitException e) {
344 Activator.getDefault().logError(NLS.bind(Messages.TmfCommonProjectElement_ErrorClosingEditor, getName()), e);
345 }
346 }
347 }
348 }
349 }
350
351 /**
352 * Get a friendly name for the type of element this common project element
353 * is, to be displayed in UI messages.
354 *
355 * @return A string for the type of project element this object is, for
356 * example "trace" or "experiment"
357 */
358 public abstract String getTypeName();
359
360 /**
361 * Copy this model element
362 *
363 * @param newName
364 * The name of the new element
365 * @param copySuppFiles
366 * Whether to copy supplementary files or not
367 * @return the new Resource object
368 */
369 public IResource copy(final String newName, final boolean copySuppFiles) {
370
371 final IPath newPath = getParent().getResource().getFullPath().addTrailingSeparator().append(newName);
372
373 /* Copy supplementary files first, only if needed */
374 if (copySuppFiles) {
375 String newElementPath = new Path(getElementPath()).removeLastSegments(1).append(newName).toString();
376 copySupplementaryFolder(newElementPath);
377 }
378 /* Copy the trace */
379 try {
380 getResource().copy(newPath, IResource.FORCE | IResource.SHALLOW, null);
381 IResource trace = ((IFolder) getParent().getResource()).findMember(newName);
382
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);
392 }
393 }
394 }
395 return trace;
396 } catch (CoreException e) {
397
398 }
399 return null;
400 }
401
402 /**
403 * Get the list of analysis elements
404 *
405 * @return Array of analysis elements
406 */
407 public List<TmfAnalysisElement> getAvailableAnalysis() {
408 return getChildElementViews().getChildren().stream()
409 .map(elem -> (TmfAnalysisElement) elem)
410 .collect(Collectors.toList());
411 }
412
413 // ------------------------------------------------------------------------
414 // Supplementary files operations
415 // ------------------------------------------------------------------------
416
417 /**
418 * Deletes this element specific supplementary folder.
419 */
420 public void deleteSupplementaryFolder() {
421 IFolder supplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath());
422 try {
423 deleteFolder(supplFolder);
424 } catch (CoreException e) {
425 Activator.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$
426 }
427 }
428
429 private static void deleteFolder(IFolder folder) throws CoreException {
430 if (folder.exists()) {
431 folder.delete(true, new NullProgressMonitor());
432 }
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);
437 }
438 }
439
440 /**
441 * Renames the element specific supplementary folder according to the new
442 * element name or path.
443 *
444 * @param newElementPath
445 * The new element name or path
446 */
447 public void renameSupplementaryFolder(String newElementPath) {
448 IFolder oldSupplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath());
449
450 // Rename supplementary folder
451 try {
452 if (oldSupplFolder.exists()) {
453 IFolder newSupplFolder = prepareTraceSupplementaryFolder(newElementPath + getSuffix(), false);
454 oldSupplFolder.move(newSupplFolder.getFullPath(), true, new NullProgressMonitor());
455 }
456 deleteFolder(oldSupplFolder);
457 } catch (CoreException e) {
458 Activator.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$
459 }
460 }
461
462 /**
463 * Copies the element specific supplementary folder to the new element name
464 * or path.
465 *
466 * @param newElementPath
467 * The new element name or path
468 */
469 public void copySupplementaryFolder(String newElementPath) {
470 IFolder oldSupplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath());
471
472 // copy supplementary folder
473 if (oldSupplFolder.exists()) {
474 try {
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$
479 }
480 }
481 }
482
483 /**
484 * Copies the element specific supplementary folder a new folder.
485 *
486 * @param destination
487 * The destination folder to copy to.
488 */
489 public void copySupplementaryFolder(IFolder destination) {
490 IFolder oldSupplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath());
491
492 // copy supplementary folder
493 if (oldSupplFolder.exists()) {
494 try {
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$
499 }
500 }
501 }
502
503 /**
504 * Refreshes the element specific supplementary folder information. It
505 * creates the folder if not exists. It sets the persistence property of the
506 * trace resource
507 */
508 public void refreshSupplementaryFolder() {
509 IFolder supplFolder = createSupplementaryFolder();
510 try {
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$
514 }
515 }
516
517 /**
518 * Checks if supplementary resource exist or not.
519 *
520 * @return <code>true</code> if one or more files are under the element
521 * supplementary folder
522 */
523 public boolean hasSupplementaryResources() {
524 IResource[] resources = getSupplementaryResources();
525 return (resources.length > 0);
526 }
527
528 /**
529 * Returns the supplementary resources under the trace supplementary folder.
530 *
531 * @return array of resources under the trace supplementary folder.
532 */
533 public IResource[] getSupplementaryResources() {
534 IFolder supplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath());
535 if (supplFolder.exists()) {
536 try {
537 return supplFolder.members();
538 } catch (CoreException e) {
539 Activator.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$
540 }
541 }
542 return new IResource[0];
543 }
544
545 /**
546 * Deletes the given resources.
547 *
548 * @param resources
549 * array of resources to delete.
550 */
551 public void deleteSupplementaryResources(IResource[] resources) {
552
553 for (int i = 0; i < resources.length; i++) {
554 try {
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$
558 }
559 }
560 }
561
562 /**
563 * Deletes all supplementary resources in the supplementary directory
564 */
565 public void deleteSupplementaryResources() {
566 deleteSupplementaryResources(getSupplementaryResources());
567 }
568
569 private IFolder createSupplementaryFolder() {
570 IFolder supplFolder = prepareTraceSupplementaryFolder(getSupplementaryFolderPath(), true);
571
572 try {
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$
576 }
577 return supplFolder;
578 }
579
580 // -------------------------------------------------------
581 // Signal handlers
582 // -------------------------------------------------------
583
584 /**
585 * Handler for the Trace Opened signal
586 *
587 * @param signal
588 * The incoming signal
589 */
590 @TmfSignalHandler
591 public void traceOpened(TmfTraceOpenedSignal signal) {
592 IResource resource = signal.getTrace().getResource();
593 if ((resource == null) || !resource.equals(getResource())) {
594 return;
595 }
596
597 getParent().refresh();
598 }
599
600 }
This page took 0.045105 seconds and 5 git commands to generate.