Commit | Line | Data |
---|---|---|
99504bb8 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2010-2013 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 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.linuxtools.tmf.ui.project.model; | |
15 | ||
16 | ||
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.NullProgressMonitor; | |
21 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
22 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; | |
23 | ||
24 | ||
25 | /** | |
26 | * Base class for project elements who will have folder elements | |
27 | * under them to store supplementary files. | |
28 | * | |
29 | * @author gbastien | |
30 | * @since 2.0 | |
31 | */ | |
32 | public abstract class TmfWithFolderElement extends TmfProjectModelElement { | |
33 | ||
34 | // ------------------------------------------------------------------------ | |
35 | // Constructors | |
36 | // ------------------------------------------------------------------------ | |
37 | ||
38 | ||
39 | /** | |
40 | * Constructor. | |
41 | * Creates model element. | |
42 | * @param name The name of the element | |
43 | * @param resource The resource. | |
44 | * @param parent The parent element | |
45 | */ | |
46 | public TmfWithFolderElement(String name, IResource resource, TmfProjectModelElement parent) { | |
47 | super(name, resource, parent); | |
48 | } | |
49 | ||
50 | /** | |
51 | * Return the resource name for this element | |
52 | * | |
53 | * @return The name of the resource for this element | |
54 | */ | |
55 | protected String getResourceName() { | |
56 | return fResource.getName() + getSuffix(); | |
57 | } | |
58 | ||
59 | /** | |
60 | * @return The suffix for resource names | |
61 | */ | |
62 | protected String getSuffix() { | |
63 | return ""; //$NON-NLS-1$ | |
64 | } | |
65 | ||
66 | /** | |
67 | * Deletes this element specific supplementary folder. | |
68 | */ | |
69 | public void deleteSupplementaryFolder() { | |
70 | IFolder supplFolder = getTraceSupplementaryFolder(getResourceName()); | |
71 | if (supplFolder.exists()) { | |
72 | try { | |
73 | supplFolder.delete(true, new NullProgressMonitor()); | |
74 | } catch (CoreException e) { | |
75 | Activator.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$ | |
76 | } | |
77 | } | |
78 | } | |
79 | ||
80 | /** | |
81 | * Renames the element specific supplementary folder according to the new element name. | |
82 | * | |
83 | * @param newName The new element name | |
84 | */ | |
85 | public void renameSupplementaryFolder(String newName) { | |
86 | IFolder oldSupplFolder = getTraceSupplementaryFolder(getResourceName()); | |
87 | IFolder newSupplFolder = getTraceSupplementaryFolder(newName + getSuffix()); | |
88 | ||
89 | // Rename supplementary folder | |
90 | if (oldSupplFolder.exists()) { | |
91 | try { | |
92 | oldSupplFolder.move(newSupplFolder.getFullPath(), true, new NullProgressMonitor()); | |
93 | } catch (CoreException e) { | |
94 | Activator.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$ | |
95 | } | |
96 | } | |
97 | } | |
98 | ||
99 | /** | |
100 | * Copies the element specific supplementary folder to the new element name. | |
101 | * | |
102 | * @param newName The new element name | |
103 | */ | |
104 | public void copySupplementaryFolder(String newName) { | |
105 | IFolder oldSupplFolder = getTraceSupplementaryFolder(getResourceName()); | |
106 | IFolder newSupplFolder = getTraceSupplementaryFolder(newName + getSuffix()); | |
107 | ||
108 | // copy supplementary folder | |
109 | if (oldSupplFolder.exists()) { | |
110 | try { | |
111 | oldSupplFolder.copy(newSupplFolder.getFullPath(), true, new NullProgressMonitor()); | |
112 | } catch (CoreException e) { | |
113 | Activator.getDefault().logError("Error renaming supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$ | |
114 | } | |
115 | } | |
116 | } | |
117 | ||
118 | /** | |
119 | * Copies the element specific supplementary folder a new folder. | |
120 | * | |
121 | * @param destination The destination folder to copy to. | |
122 | */ | |
123 | public void copySupplementaryFolder(IFolder destination) { | |
124 | IFolder oldSupplFolder = getTraceSupplementaryFolder(getResourceName()); | |
125 | ||
126 | // copy supplementary folder | |
127 | if (oldSupplFolder.exists()) { | |
128 | try { | |
129 | oldSupplFolder.copy(destination.getFullPath(), true, new NullProgressMonitor()); | |
130 | } catch (CoreException e) { | |
131 | Activator.getDefault().logError("Error copying supplementary folder " + oldSupplFolder, e); //$NON-NLS-1$ | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | ||
137 | /** | |
138 | * Refreshes the element specific supplementary folder information. It creates the folder if not exists. | |
139 | * It sets the persistence property of the trace resource | |
140 | */ | |
141 | public void refreshSupplementaryFolder() { | |
142 | createSupplementaryDirectory(); | |
143 | } | |
144 | ||
145 | /** | |
146 | * Checks if supplementary resource exist or not. | |
147 | * | |
148 | * @return <code>true</code> if one or more files are under the element supplementary folder | |
149 | */ | |
150 | public boolean hasSupplementaryResources() { | |
151 | IResource[] resources = getSupplementaryResources(); | |
152 | return (resources.length > 0); | |
153 | } | |
154 | ||
155 | /** | |
156 | * Returns the supplementary resources under the trace supplementary folder. | |
157 | * | |
158 | * @return array of resources under the trace supplementary folder. | |
159 | */ | |
160 | public IResource[] getSupplementaryResources() { | |
161 | IFolder supplFolder = getTraceSupplementaryFolder(getResourceName()); | |
162 | if (supplFolder.exists()) { | |
163 | try { | |
164 | return supplFolder.members(); | |
165 | } catch (CoreException e) { | |
166 | Activator.getDefault().logError("Error deleting supplementary folder " + supplFolder, e); //$NON-NLS-1$ | |
167 | } | |
168 | } | |
169 | return new IResource[0]; | |
170 | } | |
171 | ||
172 | /** | |
173 | * Deletes the given resources. | |
174 | * | |
175 | * @param resources array of resources to delete. | |
176 | */ | |
177 | public void deleteSupplementaryResources(IResource[] resources) { | |
178 | ||
179 | for (int i = 0; i < resources.length; i++) { | |
180 | try { | |
181 | resources[i].delete(true, new NullProgressMonitor()); | |
182 | } catch (CoreException e) { | |
183 | Activator.getDefault().logError("Error deleting supplementary resource " + resources[i], e); //$NON-NLS-1$ | |
184 | } | |
185 | } | |
186 | } | |
187 | ||
188 | private void createSupplementaryDirectory() { | |
189 | IFolder supplFolder = getTraceSupplementaryFolder(getResourceName()); | |
190 | if (!supplFolder.exists()) { | |
191 | try { | |
192 | supplFolder.create(true, true, new NullProgressMonitor()); | |
193 | } catch (CoreException e) { | |
194 | Activator.getDefault().logError("Error creating resource supplementary file " + supplFolder, e); //$NON-NLS-1$ | |
195 | } | |
196 | } | |
197 | ||
198 | try { | |
199 | fResource.setPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, supplFolder.getLocationURI().getPath()); | |
200 | } catch (CoreException e) { | |
201 | Activator.getDefault().logError("Error setting persistant property " + TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, e); //$NON-NLS-1$ | |
202 | } | |
203 | ||
204 | } | |
205 | ||
206 | } |