Commit | Line | Data |
---|---|---|
99504bb8 | 1 | /******************************************************************************* |
beb19106 | 2 | * Copyright (c) 2010, 2013 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 | |
beb19106 | 12 | * Moved to this class code to copy a model element |
99504bb8 GB |
13 | *******************************************************************************/ |
14 | ||
15 | package org.eclipse.linuxtools.tmf.ui.project.model; | |
16 | ||
99504bb8 GB |
17 | import org.eclipse.core.resources.IFolder; |
18 | import org.eclipse.core.resources.IResource; | |
19 | import org.eclipse.core.runtime.CoreException; | |
beb19106 | 20 | import org.eclipse.core.runtime.IPath; |
99504bb8 GB |
21 | import org.eclipse.core.runtime.NullProgressMonitor; |
22 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
23 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; | |
beb19106 GB |
24 | import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment; |
25 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; | |
99504bb8 GB |
26 | |
27 | ||
28 | /** | |
29 | * Base class for project elements who will have folder elements | |
30 | * under them to store supplementary files. | |
31 | * | |
e73a4ba5 | 32 | * @author Geneviève Bastien |
99504bb8 GB |
33 | * @since 2.0 |
34 | */ | |
35 | public abstract class TmfWithFolderElement extends TmfProjectModelElement { | |
36 | ||
37 | // ------------------------------------------------------------------------ | |
38 | // Constructors | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
41 | ||
42 | /** | |
43 | * Constructor. | |
44 | * Creates model element. | |
45 | * @param name The name of the element | |
46 | * @param resource The resource. | |
47 | * @param parent The parent element | |
48 | */ | |
49 | public TmfWithFolderElement(String name, IResource resource, TmfProjectModelElement parent) { | |
50 | super(name, resource, parent); | |
51 | } | |
52 | ||
53 | /** | |
54 | * Return the resource name for this element | |
55 | * | |
56 | * @return The name of the resource for this element | |
57 | */ | |
58 | protected String getResourceName() { | |
59 | return fResource.getName() + getSuffix(); | |
60 | } | |
61 | ||
62 | /** | |
63 | * @return The suffix for resource names | |
64 | */ | |
65 | protected String getSuffix() { | |
66 | return ""; //$NON-NLS-1$ | |
67 | } | |
68 | ||
69 | /** | |
70 | * Deletes this element specific supplementary folder. | |
71 | */ | |
72 | public void deleteSupplementaryFolder() { | |
73 | IFolder supplFolder = getTraceSupplementaryFolder(getResourceName()); | |
74 | if (supplFolder.exists()) { | |
75 | try { | |
76 | supplFolder.delete(true, new NullProgressMonitor()); | |
77 | } catch (CoreException e) { | |
78 | Activator.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$ | |
79 | } | |
80 | } | |
81 | } | |
82 | ||
83 | /** | |
84 | * Renames the element specific supplementary folder according to the new element name. | |
85 | * | |
86 | * @param newName The new element name | |
87 | */ | |
88 | public void renameSupplementaryFolder(String newName) { | |
89 | IFolder oldSupplFolder = getTraceSupplementaryFolder(getResourceName()); | |
90 | IFolder newSupplFolder = getTraceSupplementaryFolder(newName + getSuffix()); | |
91 | ||
92 | // Rename supplementary folder | |
93 | if (oldSupplFolder.exists()) { | |
94 | try { | |
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$ | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
102 | /** | |
103 | * Copies the element specific supplementary folder to the new element name. | |
104 | * | |
105 | * @param newName The new element name | |
106 | */ | |
107 | public void copySupplementaryFolder(String newName) { | |
108 | IFolder oldSupplFolder = getTraceSupplementaryFolder(getResourceName()); | |
109 | IFolder newSupplFolder = getTraceSupplementaryFolder(newName + getSuffix()); | |
110 | ||
111 | // copy supplementary folder | |
112 | if (oldSupplFolder.exists()) { | |
113 | try { | |
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$ | |
117 | } | |
118 | } | |
119 | } | |
120 | ||
121 | /** | |
122 | * Copies the element specific supplementary folder a new folder. | |
123 | * | |
124 | * @param destination The destination folder to copy to. | |
125 | */ | |
126 | public void copySupplementaryFolder(IFolder destination) { | |
127 | IFolder oldSupplFolder = getTraceSupplementaryFolder(getResourceName()); | |
128 | ||
129 | // copy supplementary folder | |
130 | if (oldSupplFolder.exists()) { | |
131 | try { | |
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$ | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | ||
140 | /** | |
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 | |
143 | */ | |
144 | public void refreshSupplementaryFolder() { | |
145 | createSupplementaryDirectory(); | |
146 | } | |
147 | ||
148 | /** | |
149 | * Checks if supplementary resource exist or not. | |
150 | * | |
151 | * @return <code>true</code> if one or more files are under the element supplementary folder | |
152 | */ | |
153 | public boolean hasSupplementaryResources() { | |
154 | IResource[] resources = getSupplementaryResources(); | |
155 | return (resources.length > 0); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Returns the supplementary resources under the trace supplementary folder. | |
160 | * | |
161 | * @return array of resources under the trace supplementary folder. | |
162 | */ | |
163 | public IResource[] getSupplementaryResources() { | |
164 | IFolder supplFolder = getTraceSupplementaryFolder(getResourceName()); | |
165 | if (supplFolder.exists()) { | |
166 | try { | |
167 | return supplFolder.members(); | |
168 | } catch (CoreException e) { | |
169 | Activator.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$ | |
170 | } | |
171 | } | |
172 | return new IResource[0]; | |
173 | } | |
174 | ||
175 | /** | |
176 | * Deletes the given resources. | |
177 | * | |
178 | * @param resources array of resources to delete. | |
179 | */ | |
180 | public void deleteSupplementaryResources(IResource[] resources) { | |
181 | ||
182 | for (int i = 0; i < resources.length; i++) { | |
183 | try { | |
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$ | |
187 | } | |
188 | } | |
189 | } | |
190 | ||
4726e963 BH |
191 | /** |
192 | * Deletes all supplementary resources in the supplementary directory | |
193 | */ | |
194 | public void deleteSupplementaryResources() { | |
195 | deleteSupplementaryResources(getSupplementaryResources()); | |
196 | } | |
197 | ||
99504bb8 GB |
198 | private void createSupplementaryDirectory() { |
199 | IFolder supplFolder = getTraceSupplementaryFolder(getResourceName()); | |
200 | if (!supplFolder.exists()) { | |
201 | try { | |
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$ | |
205 | } | |
206 | } | |
207 | ||
208 | try { | |
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$ | |
212 | } | |
213 | ||
214 | } | |
215 | ||
beb19106 GB |
216 | /** |
217 | * Copy this model element | |
218 | * | |
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 | |
222 | */ | |
223 | public IResource copy(final String newName, final boolean copySuppFiles) { | |
224 | ||
225 | final IPath newPath = getParent().getResource().getFullPath().addTrailingSeparator().append(newName); | |
226 | ||
227 | /* Copy supplementary files first, only if needed */ | |
228 | if (copySuppFiles) { | |
229 | copySupplementaryFolder(newName); | |
230 | } | |
231 | /* Copy the trace */ | |
232 | try { | |
233 | getResource().copy(newPath, IResource.FORCE | IResource.SHALLOW, null); | |
234 | ||
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); | |
241 | } | |
e73a4ba5 | 242 | else if (TmfExperiment.class.getCanonicalName().equals(member.getPersistentProperty(TmfCommonConstants.TRACETYPE))) { |
beb19106 GB |
243 | member.delete(true, null); |
244 | } | |
245 | } | |
246 | } | |
247 | return folder; | |
248 | } catch (CoreException e) { | |
249 | ||
250 | } | |
251 | ||
252 | return null; | |
253 | ||
254 | } | |
255 | ||
99504bb8 | 256 | } |