Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
b544077e | 2 | * Copyright (c) 2010, 2012 Ericsson |
013a5f1c | 3 | * |
12c155f5 FC |
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 | |
013a5f1c | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
b544077e | 11 | * Bernd Hufmann - Added supplementary files/folder handling |
12c155f5 FC |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.tmf.ui.project.model; | |
15 | ||
16 | import java.net.URI; | |
17 | import java.util.ArrayList; | |
18 | import java.util.List; | |
19 | ||
e12ecd30 BH |
20 | import org.eclipse.core.resources.IFolder; |
21 | import org.eclipse.core.resources.IProject; | |
12c155f5 FC |
22 | import org.eclipse.core.resources.IResource; |
23 | import org.eclipse.core.resources.IResourceChangeEvent; | |
24 | import org.eclipse.core.resources.IResourceChangeListener; | |
25 | import org.eclipse.core.resources.ResourcesPlugin; | |
e12ecd30 | 26 | import org.eclipse.core.runtime.CoreException; |
12c155f5 | 27 | import org.eclipse.core.runtime.IPath; |
e12ecd30 | 28 | import org.eclipse.core.runtime.NullProgressMonitor; |
8fd82db5 | 29 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
e12ecd30 | 30 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
12c155f5 FC |
31 | |
32 | /** | |
013a5f1c AM |
33 | * The implementation of the base TMF project model element. It provides default implementation |
34 | * of the <code>ITmfProjectModelElement</code> interface. | |
12c155f5 | 35 | * <p> |
b544077e BH |
36 | * @version 1.0 |
37 | * @author Francois Chouinard | |
12c155f5 FC |
38 | */ |
39 | public abstract class TmfProjectModelElement implements ITmfProjectModelElement, IResourceChangeListener { | |
40 | ||
41 | // ------------------------------------------------------------------------ | |
42 | // Attributes | |
43 | // ------------------------------------------------------------------------ | |
44 | ||
45 | private final String fName; | |
b544077e BH |
46 | /** |
47 | * The project model element resource. | |
48 | */ | |
12c155f5 | 49 | protected final IResource fResource; |
b544077e BH |
50 | /** |
51 | * The project model resource location (URI) | |
52 | */ | |
12c155f5 | 53 | protected final URI fLocation; |
b544077e BH |
54 | /** |
55 | * The project model path of a resource. | |
56 | */ | |
12c155f5 FC |
57 | protected final IPath fPath; |
58 | private final ITmfProjectModelElement fParent; | |
b544077e BH |
59 | /** |
60 | * The list of children elements. | |
61 | */ | |
12c155f5 FC |
62 | protected final List<ITmfProjectModelElement> fChildren; |
63 | ||
64 | // ------------------------------------------------------------------------ | |
65 | // Constructor | |
66 | // ------------------------------------------------------------------------ | |
b544077e BH |
67 | /** |
68 | * Constructor. | |
013a5f1c | 69 | * |
b544077e BH |
70 | * Creates a base project model element. |
71 | * @param name The name of the element. | |
72 | * @param resource The element resource. | |
73 | * @param parent The parent model element. | |
74 | */ | |
12c155f5 FC |
75 | protected TmfProjectModelElement(String name, IResource resource, ITmfProjectModelElement parent) { |
76 | fName = name; | |
77 | fResource = resource; | |
78 | fPath = resource.getFullPath(); | |
79 | fLocation = resource.getLocationURI(); | |
80 | fParent = parent; | |
81 | fChildren = new ArrayList<ITmfProjectModelElement>(); | |
82 | ResourcesPlugin.getWorkspace().addResourceChangeListener(this); | |
83 | } | |
84 | ||
c4c81d91 PT |
85 | private void dispose() { |
86 | ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); | |
87 | } | |
88 | ||
12c155f5 FC |
89 | // ------------------------------------------------------------------------ |
90 | // ITmfProjectModelElement | |
91 | // ------------------------------------------------------------------------ | |
b544077e BH |
92 | /* |
93 | * (non-Javadoc) | |
94 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getName() | |
95 | */ | |
12c155f5 FC |
96 | @Override |
97 | public String getName() { | |
98 | return fName; | |
99 | } | |
b544077e BH |
100 | /* |
101 | * (non-Javadoc) | |
102 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getResource() | |
103 | */ | |
12c155f5 FC |
104 | @Override |
105 | public IResource getResource() { | |
106 | return fResource; | |
107 | } | |
b544077e BH |
108 | /* |
109 | * (non-Javadoc) | |
110 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getPath() | |
111 | */ | |
12c155f5 FC |
112 | @Override |
113 | public IPath getPath() { | |
114 | return fPath; | |
115 | } | |
b544077e BH |
116 | /* |
117 | * (non-Javadoc) | |
118 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getLocation() | |
119 | */ | |
12c155f5 FC |
120 | @Override |
121 | public URI getLocation() { | |
122 | return fLocation; | |
123 | } | |
b544077e BH |
124 | /* |
125 | * (non-Javadoc) | |
126 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getParent() | |
127 | */ | |
12c155f5 FC |
128 | @Override |
129 | public ITmfProjectModelElement getParent() { | |
130 | return fParent; | |
131 | } | |
b544077e BH |
132 | /* |
133 | * (non-Javadoc) | |
134 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#hasChildren() | |
135 | */ | |
12c155f5 FC |
136 | @Override |
137 | public boolean hasChildren() { | |
138 | return fChildren.size() > 0; | |
139 | } | |
b544077e BH |
140 | /* |
141 | * (non-Javadoc) | |
142 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getChildren() | |
143 | */ | |
12c155f5 FC |
144 | @Override |
145 | public List<ITmfProjectModelElement> getChildren() { | |
146 | return fChildren; | |
147 | } | |
b544077e BH |
148 | /* |
149 | * (non-Javadoc) | |
150 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#addChild(org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement) | |
151 | */ | |
12c155f5 FC |
152 | @Override |
153 | public void addChild(ITmfProjectModelElement child) { | |
154 | fChildren.add(child); | |
155 | } | |
b544077e BH |
156 | /* |
157 | * (non-Javadoc) | |
158 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#removeChild(org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement) | |
159 | */ | |
12c155f5 FC |
160 | @Override |
161 | public void removeChild(ITmfProjectModelElement child) { | |
162 | fChildren.remove(child); | |
c4c81d91 PT |
163 | if (child instanceof TmfProjectModelElement) { |
164 | ((TmfProjectModelElement) child).dispose(); | |
165 | } | |
12c155f5 FC |
166 | refresh(); |
167 | } | |
b544077e BH |
168 | /* |
169 | * (non-Javadoc) | |
170 | * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#refresh() | |
171 | */ | |
12c155f5 FC |
172 | @Override |
173 | public void refresh() { | |
174 | // Do nothing by default: sub-classes override this on an "as-needed" | |
175 | // basis. | |
176 | } | |
177 | ||
178 | // ------------------------------------------------------------------------ | |
179 | // IResourceChangeListener | |
180 | // ------------------------------------------------------------------------ | |
b544077e BH |
181 | /* |
182 | * (non-Javadoc) | |
183 | * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent) | |
184 | */ | |
12c155f5 FC |
185 | @Override |
186 | public void resourceChanged(IResourceChangeEvent event) { | |
187 | // Do nothing by default: sub-classes override this on an "as-needed" | |
188 | // basis. | |
189 | } | |
190 | ||
191 | // ------------------------------------------------------------------------ | |
192 | // Object | |
193 | // ------------------------------------------------------------------------ | |
b544077e BH |
194 | /* |
195 | * (non-Javadoc) | |
196 | * @see java.lang.Object#hashCode() | |
197 | */ | |
6e85c58d FC |
198 | @Override |
199 | public int hashCode() { | |
200 | final int prime = 31; | |
201 | int result = 1; | |
6e85c58d | 202 | result = prime * result + ((fPath == null) ? 0 : fPath.hashCode()); |
6e85c58d FC |
203 | return result; |
204 | } | |
b544077e BH |
205 | /* |
206 | * (non-Javadoc) | |
207 | * @see java.lang.Object#equals(java.lang.Object) | |
208 | */ | |
12c155f5 FC |
209 | @Override |
210 | public boolean equals(Object other) { | |
013a5f1c | 211 | if (this == other) { |
12c155f5 | 212 | return true; |
013a5f1c AM |
213 | } |
214 | if (other == null) { | |
6e85c58d | 215 | return false; |
013a5f1c AM |
216 | } |
217 | if (!(other instanceof TmfProjectModelElement)) { | |
12c155f5 | 218 | return false; |
013a5f1c | 219 | } |
6e85c58d | 220 | TmfProjectModelElement element = (TmfProjectModelElement) other; |
81fe3479 | 221 | return element.fPath.equals(fPath); |
12c155f5 | 222 | } |
013a5f1c | 223 | |
e12ecd30 BH |
224 | /** |
225 | * Returns the trace specific supplementary directory under the project's supplementary folder. | |
013a5f1c AM |
226 | * The folder will be created if it doesn't exist. |
227 | * | |
e12ecd30 BH |
228 | * @param supplFoldername - folder name. |
229 | * @return returns the trace specific supplementary directory | |
230 | */ | |
231 | public IFolder getTraceSupplementaryFolder(String supplFoldername) { | |
232 | IFolder supplFolderParent = getSupplementaryFolderParent(); | |
233 | return supplFolderParent.getFolder(supplFoldername); | |
234 | } | |
235 | ||
236 | /** | |
237 | * Returns the supplementary folder for this project | |
013a5f1c AM |
238 | * |
239 | * @return the supplementary folder for this project | |
e12ecd30 BH |
240 | */ |
241 | public IFolder getSupplementaryFolderParent() { | |
242 | TmfProjectElement project = getProject(); | |
013a5f1c | 243 | IProject projectResource = project.getResource(); |
e12ecd30 BH |
244 | IFolder supplFolderParent = projectResource.getFolder(TmfCommonConstants.TRACE_SUPPLEMENATARY_FOLDER_NAME); |
245 | ||
246 | if (!supplFolderParent.exists()) { | |
247 | try { | |
248 | supplFolderParent.create(true, true, new NullProgressMonitor()); | |
249 | } catch (CoreException e) { | |
8fd82db5 | 250 | Activator.getDefault().logError("Error creating project specific supplementary folder " + supplFolderParent, e); //$NON-NLS-1$ |
e12ecd30 BH |
251 | } |
252 | } | |
253 | return supplFolderParent; | |
254 | } | |
12c155f5 | 255 | } |