1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
17 import org
.eclipse
.core
.resources
.IFolder
;
18 import org
.eclipse
.core
.resources
.IResource
;
19 import org
.eclipse
.core
.runtime
.CoreException
;
20 import org
.eclipse
.core
.runtime
.IPath
;
21 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
22 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
29 * Base class for project elements who will have folder elements
30 * under them to store supplementary files.
32 * @author Geneviève Bastien
35 public abstract class TmfWithFolderElement
extends TmfProjectModelElement
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
44 * Creates model element.
45 * @param name The name of the element
46 * @param resource The resource.
47 * @param parent The parent element
49 public TmfWithFolderElement(String name
, IResource resource
, TmfProjectModelElement parent
) {
50 super(name
, resource
, parent
);
54 * Return the resource name for this element
56 * @return The name of the resource for this element
58 protected String
getResourceName() {
59 return fResource
.getName() + getSuffix();
63 * @return The suffix for resource names
65 protected String
getSuffix() {
66 return ""; //$NON-NLS-1$
70 * Deletes this element specific supplementary folder.
72 public void deleteSupplementaryFolder() {
73 IFolder supplFolder
= getTraceSupplementaryFolder(getResourceName());
74 if (supplFolder
.exists()) {
76 supplFolder
.delete(true, new NullProgressMonitor());
77 } catch (CoreException e
) {
78 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
84 * Renames the element specific supplementary folder according to the new element name.
86 * @param newName The new element name
88 public void renameSupplementaryFolder(String newName
) {
89 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getResourceName());
90 IFolder newSupplFolder
= getTraceSupplementaryFolder(newName
+ getSuffix());
92 // Rename supplementary folder
93 if (oldSupplFolder
.exists()) {
95 oldSupplFolder
.move(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
96 } catch (CoreException e
) {
97 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
103 * Copies the element specific supplementary folder to the new element name.
105 * @param newName The new element name
107 public void copySupplementaryFolder(String newName
) {
108 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getResourceName());
109 IFolder newSupplFolder
= getTraceSupplementaryFolder(newName
+ getSuffix());
111 // copy supplementary folder
112 if (oldSupplFolder
.exists()) {
114 oldSupplFolder
.copy(newSupplFolder
.getFullPath(), true, new NullProgressMonitor());
115 } catch (CoreException e
) {
116 Activator
.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
122 * Copies the element specific supplementary folder a new folder.
124 * @param destination The destination folder to copy to.
126 public void copySupplementaryFolder(IFolder destination
) {
127 IFolder oldSupplFolder
= getTraceSupplementaryFolder(getResourceName());
129 // copy supplementary folder
130 if (oldSupplFolder
.exists()) {
132 oldSupplFolder
.copy(destination
.getFullPath(), true, new NullProgressMonitor());
133 } catch (CoreException e
) {
134 Activator
.getDefault().logError("Error copying supplementary folder " + oldSupplFolder
, e
); //$NON-NLS-1$
141 * Refreshes the element specific supplementary folder information. It creates the folder if not exists.
142 * It sets the persistence property of the trace resource
144 public void refreshSupplementaryFolder() {
145 createSupplementaryDirectory();
149 * Checks if supplementary resource exist or not.
151 * @return <code>true</code> if one or more files are under the element supplementary folder
153 public boolean hasSupplementaryResources() {
154 IResource
[] resources
= getSupplementaryResources();
155 return (resources
.length
> 0);
159 * Returns the supplementary resources under the trace supplementary folder.
161 * @return array of resources under the trace supplementary folder.
163 public IResource
[] getSupplementaryResources() {
164 IFolder supplFolder
= getTraceSupplementaryFolder(getResourceName());
165 if (supplFolder
.exists()) {
167 return supplFolder
.members();
168 } catch (CoreException e
) {
169 Activator
.getDefault().logError("Error deleting supplementary folder " + supplFolder
, e
); //$NON-NLS-1$
172 return new IResource
[0];
176 * Deletes the given resources.
178 * @param resources array of resources to delete.
180 public void deleteSupplementaryResources(IResource
[] resources
) {
182 for (int i
= 0; i
< resources
.length
; i
++) {
184 resources
[i
].delete(true, new NullProgressMonitor());
185 } catch (CoreException e
) {
186 Activator
.getDefault().logError("Error deleting supplementary resource " + resources
[i
], e
); //$NON-NLS-1$
192 * Deletes all supplementary resources in the supplementary directory
194 public void deleteSupplementaryResources() {
195 deleteSupplementaryResources(getSupplementaryResources());
198 private void createSupplementaryDirectory() {
199 IFolder supplFolder
= getTraceSupplementaryFolder(getResourceName());
200 if (!supplFolder
.exists()) {
202 supplFolder
.create(true, true, new NullProgressMonitor());
203 } catch (CoreException e
) {
204 Activator
.getDefault().logError("Error creating resource supplementary file " + supplFolder
, e
); //$NON-NLS-1$
209 fResource
.setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, supplFolder
.getLocationURI().getPath());
210 } catch (CoreException e
) {
211 Activator
.getDefault().logError("Error setting persistant property " + TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, e
); //$NON-NLS-1$
217 * Copy this model element
219 * @param newName The name of the new element
220 * @param copySuppFiles Whether to copy supplementary files or not
221 * @return the new Resource object
223 public IResource
copy(final String newName
, final boolean copySuppFiles
) {
225 final IPath newPath
= getParent().getResource().getFullPath().addTrailingSeparator().append(newName
);
227 /* Copy supplementary files first, only if needed */
229 copySupplementaryFolder(newName
);
233 getResource().copy(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
235 /* Delete any bookmarks file found in copied trace folder */
236 IFolder folder
= ((IFolder
)getParent().getResource()).getFolder(newName
);
237 if (folder
.exists()) {
238 for (IResource member
: folder
.members()) {
239 if (TmfTrace
.class.getCanonicalName().equals(member
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
))) {
240 member
.delete(true, null);
242 else if (TmfExperiment
.class.getCanonicalName().equals(member
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
))) {
243 member
.delete(true, null);
248 } catch (CoreException e
) {