1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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 public String
getName() {
99 public IResource
getResource() {
104 public IPath
getPath() {
109 public URI
getLocation() {
114 public ITmfProjectModelElement
getParent() {
119 public boolean hasChildren() {
120 return fChildren
.size() > 0;
124 public List
<ITmfProjectModelElement
> getChildren() {
129 public void addChild(ITmfProjectModelElement child
) {
130 fChildren
.add(child
);
134 public void removeChild(ITmfProjectModelElement child
) {
135 fChildren
.remove(child
);
136 if (child
instanceof TmfProjectModelElement
) {
137 ((TmfProjectModelElement
) child
).dispose();
143 public void refresh() {
144 // Do nothing by default: sub-classes override this on an "as-needed"
148 // ------------------------------------------------------------------------
149 // IResourceChangeListener
150 // ------------------------------------------------------------------------
153 public void resourceChanged(IResourceChangeEvent event
) {
154 // Do nothing by default: sub-classes override this on an "as-needed"
158 // ------------------------------------------------------------------------
160 // ------------------------------------------------------------------------
163 public int hashCode() {
164 final int prime
= 31;
166 result
= prime
* result
+ ((fPath
== null) ?
0 : fPath
.hashCode());
171 public boolean equals(Object other
) {
178 if (!(other
instanceof TmfProjectModelElement
)) {
181 TmfProjectModelElement element
= (TmfProjectModelElement
) other
;
182 return element
.fPath
.equals(fPath
);
186 * Returns the trace specific supplementary directory under the project's supplementary folder.
187 * The folder will be created if it doesn't exist.
189 * @param supplFoldername - folder name.
190 * @return returns the trace specific supplementary directory
192 public IFolder
getTraceSupplementaryFolder(String supplFoldername
) {
193 IFolder supplFolderParent
= getSupplementaryFolderParent();
194 return supplFolderParent
.getFolder(supplFoldername
);
198 * Returns the supplementary folder for this project
200 * @return the supplementary folder for this project
202 public IFolder
getSupplementaryFolderParent() {
203 TmfProjectElement project
= getProject();
204 IProject projectResource
= project
.getResource();
205 IFolder supplFolderParent
= projectResource
.getFolder(TmfCommonConstants
.TRACE_SUPPLEMENATARY_FOLDER_NAME
);
207 if (!supplFolderParent
.exists()) {
209 supplFolderParent
.create(true, true, new NullProgressMonitor());
210 } catch (CoreException e
) {
211 Activator
.getDefault().logError("Error creating project specific supplementary folder " + supplFolderParent
, e
); //$NON-NLS-1$
214 return supplFolderParent
;