1 /*******************************************************************************
2 * Copyright (c) 2010, 2012 Ericsson
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 * Francois Chouinard - Initial API and implementation
11 * Bernd Hufmann - Added supplementary files/folder handling
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
17 import java
.util
.ArrayList
;
18 import java
.util
.List
;
20 import org
.eclipse
.core
.resources
.IFolder
;
21 import org
.eclipse
.core
.resources
.IProject
;
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
;
26 import org
.eclipse
.core
.runtime
.CoreException
;
27 import org
.eclipse
.core
.runtime
.IPath
;
28 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
29 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
33 * The implementation of the base TMF project model element. It provides default implementation
34 * of the <code>ITmfProjectModelElement</code> interface.
37 * @author Francois Chouinard
39 public abstract class TmfProjectModelElement
implements ITmfProjectModelElement
, IResourceChangeListener
{
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
45 private final String fName
;
47 * The project model element resource.
49 protected final IResource fResource
;
51 * The project model resource location (URI)
53 protected final URI fLocation
;
55 * The project model path of a resource.
57 protected final IPath fPath
;
58 private final ITmfProjectModelElement fParent
;
60 * The list of children elements.
62 protected final List
<ITmfProjectModelElement
> fChildren
;
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
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.
75 protected TmfProjectModelElement(String name
, IResource resource
, ITmfProjectModelElement parent
) {
78 fPath
= resource
.getFullPath();
79 fLocation
= resource
.getLocationURI();
81 fChildren
= new ArrayList
<ITmfProjectModelElement
>();
82 ResourcesPlugin
.getWorkspace().addResourceChangeListener(this);
85 private void dispose() {
86 ResourcesPlugin
.getWorkspace().removeResourceChangeListener(this);
89 // ------------------------------------------------------------------------
90 // ITmfProjectModelElement
91 // ------------------------------------------------------------------------
94 * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getName()
97 public String
getName() {
102 * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getResource()
105 public IResource
getResource() {
110 * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getPath()
113 public IPath
getPath() {
118 * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getLocation()
121 public URI
getLocation() {
126 * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getParent()
129 public ITmfProjectModelElement
getParent() {
134 * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#hasChildren()
137 public boolean hasChildren() {
138 return fChildren
.size() > 0;
142 * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getChildren()
145 public List
<ITmfProjectModelElement
> getChildren() {
150 * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#addChild(org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement)
153 public void addChild(ITmfProjectModelElement child
) {
154 fChildren
.add(child
);
158 * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#removeChild(org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement)
161 public void removeChild(ITmfProjectModelElement child
) {
162 fChildren
.remove(child
);
163 if (child
instanceof TmfProjectModelElement
) {
164 ((TmfProjectModelElement
) child
).dispose();
170 * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#refresh()
173 public void refresh() {
174 // Do nothing by default: sub-classes override this on an "as-needed"
178 // ------------------------------------------------------------------------
179 // IResourceChangeListener
180 // ------------------------------------------------------------------------
183 * @see org.eclipse.core.resources.IResourceChangeListener#resourceChanged(org.eclipse.core.resources.IResourceChangeEvent)
186 public void resourceChanged(IResourceChangeEvent event
) {
187 // Do nothing by default: sub-classes override this on an "as-needed"
191 // ------------------------------------------------------------------------
193 // ------------------------------------------------------------------------
196 * @see java.lang.Object#hashCode()
199 public int hashCode() {
200 final int prime
= 31;
202 result
= prime
* result
+ ((fLocation
== null) ?
0 : fLocation
.hashCode());
203 result
= prime
* result
+ ((fName
== null) ?
0 : fName
.hashCode());
204 result
= prime
* result
+ ((fPath
== null) ?
0 : fPath
.hashCode());
209 * @see java.lang.Object#equals(java.lang.Object)
212 public boolean equals(Object other
) {
219 if (!(other
instanceof TmfProjectModelElement
)) {
222 TmfProjectModelElement element
= (TmfProjectModelElement
) other
;
223 return element
.fName
.equals(fName
) && element
.fLocation
.equals(fLocation
);
227 * Returns the trace specific supplementary directory under the project's supplementary folder.
228 * The folder will be created if it doesn't exist.
230 * @param supplFoldername - folder name.
231 * @return returns the trace specific supplementary directory
233 public IFolder
getTraceSupplementaryFolder(String supplFoldername
) {
234 IFolder supplFolderParent
= getSupplementaryFolderParent();
235 return supplFolderParent
.getFolder(supplFoldername
);
239 * Returns the supplementary folder for this project
241 * @return the supplementary folder for this project
243 public IFolder
getSupplementaryFolderParent() {
244 TmfProjectElement project
= getProject();
245 IProject projectResource
= project
.getResource();
246 IFolder supplFolderParent
= projectResource
.getFolder(TmfCommonConstants
.TRACE_SUPPLEMENATARY_FOLDER_NAME
);
248 if (!supplFolderParent
.exists()) {
250 supplFolderParent
.create(true, true, new NullProgressMonitor());
251 } catch (CoreException e
) {
252 Activator
.getDefault().logError("Error creating project specific supplementary folder " + supplFolderParent
, e
); //$NON-NLS-1$
255 return supplFolderParent
;