tmf: use TmfWorkspaceModifyOperation in delete suppl. files handler
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfCommonProjectElement.java
CommitLineData
99504bb8 1/*******************************************************************************
16036bc2 2 * Copyright (c) 2010, 2014 Ericsson, École Polytechnique de Montréal
99504bb8
GB
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
16036bc2
GB
12 * Moved to this class code to copy a model element
13 * Renamed from TmfWithFolderElement to TmfCommonProjectElement
339d539c 14 * Patrick Tasse - Add support for folder elements
99504bb8
GB
15 *******************************************************************************/
16
17package org.eclipse.linuxtools.tmf.ui.project.model;
18
8f5221c2
GB
19import java.io.ByteArrayInputStream;
20import java.io.InputStream;
21import java.util.ArrayList;
22import java.util.HashMap;
23import java.util.List;
24import java.util.Map;
25
339d539c 26import org.eclipse.core.resources.IContainer;
8f5221c2 27import org.eclipse.core.resources.IFile;
99504bb8
GB
28import org.eclipse.core.resources.IFolder;
29import org.eclipse.core.resources.IResource;
8f5221c2 30import org.eclipse.core.resources.ResourcesPlugin;
99504bb8 31import org.eclipse.core.runtime.CoreException;
beb19106 32import org.eclipse.core.runtime.IPath;
99504bb8 33import org.eclipse.core.runtime.NullProgressMonitor;
339d539c 34import org.eclipse.core.runtime.Path;
99504bb8
GB
35import org.eclipse.linuxtools.internal.tmf.ui.Activator;
36import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
8f5221c2
GB
37import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper;
38import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager;
39import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTrace;
40import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTraceDefinition;
41import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTrace;
42import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTraceDefinition;
43import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
44import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper;
45import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
46import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
47import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
48import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
beb19106
GB
49import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
50import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
8f5221c2
GB
51import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
52import org.eclipse.osgi.util.NLS;
53import org.eclipse.ui.IEditorReference;
54import org.eclipse.ui.IWorkbench;
55import org.eclipse.ui.IWorkbenchPage;
56import org.eclipse.ui.IWorkbenchWindow;
57import org.eclipse.ui.PartInitException;
58import org.eclipse.ui.PlatformUI;
59import org.eclipse.ui.part.FileEditorInput;
99504bb8 60
99504bb8 61/**
16036bc2
GB
62 * Base class for tracing project elements: it implements the common behavior of
63 * all project elements: supplementary files, analysis, types, etc.
99504bb8 64 *
e73a4ba5 65 * @author Geneviève Bastien
16036bc2 66 * @since 3.0
99504bb8 67 */
16036bc2 68public abstract class TmfCommonProjectElement extends TmfProjectModelElement {
99504bb8 69
8f5221c2
GB
70 // ------------------------------------------------------------------------
71 // Attributes
72 // ------------------------------------------------------------------------
73
74 // This trace type ID as defined in plugin.xml
75 private String fTraceTypeId = null;
76
77 private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
78
99504bb8
GB
79 // ------------------------------------------------------------------------
80 // Constructors
81 // ------------------------------------------------------------------------
82
99504bb8 83 /**
16036bc2
GB
84 * Constructor. Creates model element.
85 *
86 * @param name
87 * The name of the element
88 * @param resource
89 * The resource.
90 * @param parent
91 * The parent element
99504bb8 92 */
16036bc2 93 public TmfCommonProjectElement(String name, IResource resource, TmfProjectModelElement parent) {
99504bb8 94 super(name, resource, parent);
8f5221c2
GB
95 parent.addChild(this);
96 refreshTraceType();
97 TmfSignalManager.register(this);
98 }
99
100 // ------------------------------------------------------------------------
101 // TmfProjectModelElement
102 // ------------------------------------------------------------------------
103
104 @Override
105 void refreshChildren() {
106
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);
111 }
112
a4a116c3 113 TraceTypeHelper helper = TmfTraceType.getTraceType(getTraceType());
8f5221c2
GB
114
115 Class<? extends ITmfTrace> traceClass = null;
116
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;
122 }
123 }
124 }
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;
129 }
130 }
131 }
132 } else if (helper != null) {
133 traceClass = helper.getTraceClass();
134 }
135
136 /* Remove all analysis and return */
137 if (traceClass == null) {
138 for (TmfAnalysisElement analysis : childrenMap.values()) {
139 removeChild(analysis);
140 }
141 return;
142 }
143
144 /** Get the base path to put the resource to */
145 IPath path = fResource.getFullPath();
146
147 /* Add all new analysis modules or refresh outputs of existing ones */
148 for (IAnalysisModuleHelper module : TmfAnalysisManager.getAnalysisModules(traceClass).values()) {
149
150 /* If the analysis is not a child of the trace, create it */
151 TmfAnalysisElement analysis = childrenMap.remove(module.getId());
152 if (analysis == null) {
153 /**
154 * No need for the resource to exist, nothing will be done with
155 * it
156 */
157 IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(path.append(module.getId()));
158 analysis = new TmfAnalysisElement(module.getName(), newresource, this, module.getId());
159 }
160 analysis.refreshChildren();
161 }
162
163 /* Remove analysis that are not children of this trace anymore */
164 for (TmfAnalysisElement analysis : childrenMap.values()) {
165 removeChild(analysis);
166 }
167 }
168
169 // ------------------------------------------------------------------------
170 // Operations
171 // ------------------------------------------------------------------------
172
173 /**
174 * Returns the trace type ID.
175 *
176 * @return trace type ID.
177 */
178 public String getTraceType() {
179 return fTraceTypeId;
180 }
181
182 /**
183 * Refreshes the trace type filed by reading the trace type persistent
184 * property of the resource referenece.
185 */
186 public void refreshTraceType() {
187 try {
188 fTraceTypeId = getResource().getPersistentProperty(TmfCommonConstants.TRACETYPE);
189 } catch (CoreException e) {
190 Activator.getDefault().logError(NLS.bind(Messages.TmfCommonProjectElement_ErrorRefreshingProperty, getName()), e);
191 }
99504bb8
GB
192 }
193
8f5221c2
GB
194 /**
195 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
196 * the corresponding extension.
197 *
198 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
199 */
200 public abstract ITmfTrace instantiateTrace();
201
99504bb8 202 /**
339d539c
PT
203 * Return the supplementary folder path for this element. The returned path
204 * is relative to the project's supplementary folder.
99504bb8 205 *
339d539c 206 * @return The supplementary folder path for this element
99504bb8 207 */
339d539c
PT
208 protected String getSupplementaryFolderPath() {
209 return getElementPath() + getSuffix();
99504bb8
GB
210 }
211
212 /**
339d539c
PT
213 * Return the element path relative to its common element (traces folder,
214 * experiments folder or experiment element).
215 * @return The element path
216 */
217 public String getElementPath() {
218 ITmfProjectModelElement parent = getParent();
219 while (!(parent instanceof TmfTracesFolder || parent instanceof TmfExperimentElement || parent instanceof TmfExperimentFolder)) {
220 parent = parent.getParent();
221 }
222 IPath path = fResource.getFullPath().makeRelativeTo(parent.getPath());
223 return path.toString();
224 }
225
226 /**
227 * @return The suffix for the supplementary folder
99504bb8
GB
228 */
229 protected String getSuffix() {
230 return ""; //$NON-NLS-1$
231 }
232
8f5221c2
GB
233 /**
234 * Returns a list of TmfTraceElements contained in project element.
235 *
236 * @return a list of TmfTraceElements, empty list if none
237 */
238 public List<TmfTraceElement> getTraces() {
239 return new ArrayList<>();
240 }
241
242 /**
243 * Get the instantiated trace associated with this element.
244 *
245 * @return The instantiated trace or null if trace is not (yet) available
246 */
247 public ITmfTrace getTrace() {
248 for (ITmfTrace trace : TmfTraceManager.getInstance().getOpenedTraces()) {
249 if (trace.getResource().equals(getResource())) {
250 return trace;
251 }
252 }
253 return null;
254 }
255
256 /**
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.
261 *
262 * @return the bookmarks file
263 * @throws CoreException
264 * if the bookmarks file cannot be created
265 */
266 public abstract IFile createBookmarksFile() throws CoreException;
267
268 /**
269 * Actually returns the bookmark file or creates it in the project element's
270 * folder
271 *
272 * @param bookmarksFolder
273 * Folder where to put the bookmark file
274 * @param traceType
275 * The canonical name to set as tracetype
276 * @return The bookmark file
277 * @throws CoreException
278 * if the bookmarks file cannot be created
279 */
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);
287 }
288 bookmarksFile.setHidden(true);
289 file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
290 file.setHidden(true);
291 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType);
292 }
293 return file;
294 }
295
296 /**
297 * Returns the optional editor ID from the trace type extension.
298 *
299 * @return the editor ID or <code>null</code> if not defined.
300 */
301 public abstract String getEditorId();
302
303 /**
304 * Returns the file resource used to store bookmarks. The file may not
305 * exist.
306 *
307 * @return the bookmarks file
308 */
309 public IFile getBookmarksFile() {
310 final IFolder folder = (IFolder) fResource;
311 IFile file = folder.getFile(getName() + '_');
312 return file;
313 }
314
315 /**
316 * Close open editors associated with this experiment.
317 */
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()) {
325 try {
326 if (editorReference.getEditorInput().equals(input)) {
327 wbPage.closeEditor(editorReference.getEditor(false), false);
328 }
329 } catch (PartInitException e) {
330 Activator.getDefault().logError(NLS.bind(Messages.TmfCommonProjectElement_ErrorClosingEditor, getName()), e);
331 }
332 }
333 }
334 }
335 }
336
337 /**
338 * Get a friendly name for the type of element this common project element
339 * is, to be displayed in UI messages.
340 *
341 * @return A string for the type of project element this object is, for
342 * example "trace" or "experiment"
343 */
344 public abstract String getTypeName();
345
346 /**
347 * Copy this model element
348 *
349 * @param newName
350 * The name of the new element
351 * @param copySuppFiles
352 * Whether to copy supplementary files or not
353 * @return the new Resource object
354 */
355 public IResource copy(final String newName, final boolean copySuppFiles) {
356
357 final IPath newPath = getParent().getResource().getFullPath().addTrailingSeparator().append(newName);
358
359 /* Copy supplementary files first, only if needed */
360 if (copySuppFiles) {
339d539c
PT
361 String newElementPath = new Path(getElementPath()).removeLastSegments(1).append(newName).toString();
362 copySupplementaryFolder(newElementPath);
8f5221c2
GB
363 }
364 /* Copy the trace */
365 try {
366 getResource().copy(newPath, IResource.FORCE | IResource.SHALLOW, null);
367
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);
376 }
377 }
378 }
379 return folder;
380 } catch (CoreException e) {
381
382 }
383 return null;
384 }
385
386 /**
387 * Get the list of analysis elements
388 *
389 * @return Array of analysis elements
390 */
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);
397 }
398 }
399 return analysis;
400 }
401
16036bc2
GB
402 // ------------------------------------------------------------------------
403 // Supplementary files operations
404 // ------------------------------------------------------------------------
405
99504bb8
GB
406 /**
407 * Deletes this element specific supplementary folder.
408 */
409 public void deleteSupplementaryFolder() {
339d539c
PT
410 IFolder supplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath());
411 try {
412 deleteFolder(supplFolder);
413 } catch (CoreException e) {
414 Activator.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$
415 }
416 }
417
418 private static void deleteFolder(IFolder folder) throws CoreException {
419 if (folder.exists()) {
420 folder.delete(true, new NullProgressMonitor());
421 }
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);
99504bb8
GB
426 }
427 }
428
429 /**
16036bc2 430 * Renames the element specific supplementary folder according to the new
339d539c 431 * element name or path.
99504bb8 432 *
339d539c
PT
433 * @param newElementPath
434 * The new element name or path
99504bb8 435 */
339d539c
PT
436 public void renameSupplementaryFolder(String newElementPath) {
437 IFolder oldSupplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath());
99504bb8
GB
438
439 // Rename supplementary folder
339d539c
PT
440 try {
441 if (oldSupplFolder.exists()) {
442 IFolder newSupplFolder = prepareTraceSupplementaryFolder(newElementPath + getSuffix(), false);
99504bb8 443 oldSupplFolder.move(newSupplFolder.getFullPath(), true, new NullProgressMonitor());
99504bb8 444 }
339d539c
PT
445 deleteFolder(oldSupplFolder);
446 } catch (CoreException e) {
447 Activator.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$
99504bb8
GB
448 }
449 }
450
451 /**
339d539c
PT
452 * Copies the element specific supplementary folder to the new element name
453 * or path.
99504bb8 454 *
339d539c
PT
455 * @param newElementPath
456 * The new element name or path
99504bb8 457 */
339d539c
PT
458 public void copySupplementaryFolder(String newElementPath) {
459 IFolder oldSupplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath());
99504bb8
GB
460
461 // copy supplementary folder
462 if (oldSupplFolder.exists()) {
463 try {
339d539c 464 IFolder newSupplFolder = prepareTraceSupplementaryFolder(newElementPath + getSuffix(), false);
99504bb8
GB
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$
468 }
469 }
470 }
471
472 /**
473 * Copies the element specific supplementary folder a new folder.
474 *
16036bc2
GB
475 * @param destination
476 * The destination folder to copy to.
99504bb8
GB
477 */
478 public void copySupplementaryFolder(IFolder destination) {
339d539c 479 IFolder oldSupplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath());
99504bb8
GB
480
481 // copy supplementary folder
482 if (oldSupplFolder.exists()) {
483 try {
c2f1c5b6 484 TraceUtils.createFolder((IFolder) destination.getParent(), new NullProgressMonitor());
99504bb8
GB
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$
488 }
489 }
490 }
491
99504bb8 492 /**
16036bc2
GB
493 * Refreshes the element specific supplementary folder information. It
494 * creates the folder if not exists. It sets the persistence property of the
495 * trace resource
99504bb8
GB
496 */
497 public void refreshSupplementaryFolder() {
339d539c 498 createSupplementaryFolder();
99504bb8
GB
499 }
500
501 /**
502 * Checks if supplementary resource exist or not.
503 *
16036bc2
GB
504 * @return <code>true</code> if one or more files are under the element
505 * supplementary folder
99504bb8
GB
506 */
507 public boolean hasSupplementaryResources() {
508 IResource[] resources = getSupplementaryResources();
509 return (resources.length > 0);
510 }
511
512 /**
513 * Returns the supplementary resources under the trace supplementary folder.
514 *
515 * @return array of resources under the trace supplementary folder.
516 */
517 public IResource[] getSupplementaryResources() {
339d539c 518 IFolder supplFolder = getTraceSupplementaryFolder(getSupplementaryFolderPath());
99504bb8
GB
519 if (supplFolder.exists()) {
520 try {
521 return supplFolder.members();
522 } catch (CoreException e) {
523 Activator.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$
524 }
525 }
526 return new IResource[0];
527 }
528
529 /**
530 * Deletes the given resources.
531 *
16036bc2
GB
532 * @param resources
533 * array of resources to delete.
99504bb8
GB
534 */
535 public void deleteSupplementaryResources(IResource[] resources) {
536
537 for (int i = 0; i < resources.length; i++) {
538 try {
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$
542 }
543 }
544 }
545
4726e963
BH
546 /**
547 * Deletes all supplementary resources in the supplementary directory
548 */
549 public void deleteSupplementaryResources() {
550 deleteSupplementaryResources(getSupplementaryResources());
551 }
552
339d539c
PT
553 private void createSupplementaryFolder() {
554 IFolder supplFolder = prepareTraceSupplementaryFolder(getSupplementaryFolderPath(), true);
99504bb8
GB
555
556 try {
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$
560 }
561
562 }
563
8f5221c2
GB
564 // -------------------------------------------------------
565 // Signal handlers
566 // -------------------------------------------------------
16036bc2 567
beb19106 568 /**
8f5221c2 569 * Handler for the Trace Opened signal
beb19106 570 *
8f5221c2
GB
571 * @param signal
572 * The incoming signal
beb19106 573 */
8f5221c2
GB
574 @TmfSignalHandler
575 public void traceOpened(TmfTraceOpenedSignal signal) {
576 IResource resource = signal.getTrace().getResource();
577 if ((resource == null) || !resource.equals(getResource())) {
578 return;
beb19106 579 }
beb19106 580
8f5221c2 581 getParent().refresh();
beb19106
GB
582 }
583
99504bb8 584}
This page took 0.090076 seconds and 5 git commands to generate.