1 /*******************************************************************************
2 * Copyright (c) 2010 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
16 import java
.util
.ArrayList
;
17 import java
.util
.List
;
19 import org
.eclipse
.core
.resources
.IFolder
;
20 import org
.eclipse
.core
.resources
.IProject
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.resources
.IResourceChangeEvent
;
23 import org
.eclipse
.core
.resources
.IResourceChangeListener
;
24 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
25 import org
.eclipse
.core
.runtime
.CoreException
;
26 import org
.eclipse
.core
.runtime
.IPath
;
27 import org
.eclipse
.core
.runtime
.IStatus
;
28 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
29 import org
.eclipse
.core
.runtime
.Status
;
30 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.TmfUiPlugin
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
34 * <b><u>TmfProjectModelElement</u></b>
37 public abstract class TmfProjectModelElement
implements ITmfProjectModelElement
, IResourceChangeListener
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
43 private final String fName
;
44 protected final IResource fResource
;
45 protected final URI fLocation
;
46 protected final IPath fPath
;
47 private final ITmfProjectModelElement fParent
;
48 protected final List
<ITmfProjectModelElement
> fChildren
;
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 protected TmfProjectModelElement(String name
, IResource resource
, ITmfProjectModelElement parent
) {
57 fPath
= resource
.getFullPath();
58 fLocation
= resource
.getLocationURI();
60 fChildren
= new ArrayList
<ITmfProjectModelElement
>();
61 ResourcesPlugin
.getWorkspace().addResourceChangeListener(this);
64 // ------------------------------------------------------------------------
65 // ITmfProjectModelElement
66 // ------------------------------------------------------------------------
69 public String
getName() {
74 public IResource
getResource() {
79 public IPath
getPath() {
84 public URI
getLocation() {
89 public ITmfProjectModelElement
getParent() {
94 public boolean hasChildren() {
95 return fChildren
.size() > 0;
99 public List
<ITmfProjectModelElement
> getChildren() {
104 public void addChild(ITmfProjectModelElement child
) {
105 fChildren
.add(child
);
109 public void removeChild(ITmfProjectModelElement child
) {
110 fChildren
.remove(child
);
115 public void refresh() {
116 // Do nothing by default: sub-classes override this on an "as-needed"
120 // ------------------------------------------------------------------------
121 // IResourceChangeListener
122 // ------------------------------------------------------------------------
125 public void resourceChanged(IResourceChangeEvent event
) {
126 // Do nothing by default: sub-classes override this on an "as-needed"
130 // ------------------------------------------------------------------------
132 // ------------------------------------------------------------------------
135 public int hashCode() {
136 final int prime
= 31;
138 result
= prime
* result
+ ((fLocation
== null) ?
0 : fLocation
.hashCode());
139 result
= prime
* result
+ ((fName
== null) ?
0 : fName
.hashCode());
140 result
= prime
* result
+ ((fPath
== null) ?
0 : fPath
.hashCode());
145 public boolean equals(Object other
) {
150 if (!(other
instanceof TmfProjectModelElement
))
152 TmfProjectModelElement element
= (TmfProjectModelElement
) other
;
153 return element
.fName
.equals(fName
) && element
.fLocation
.equals(fLocation
);
158 * Returns the trace specific supplementary directory under the project's supplementary folder.
159 * The folder will be created if it doesn't exist.
161 * @param supplFoldername - folder name.
162 * @return returns the trace specific supplementary directory
164 public IFolder
getTraceSupplementaryFolder(String supplFoldername
) {
165 IFolder supplFolderParent
= getSupplementaryFolderParent();
166 return supplFolderParent
.getFolder(supplFoldername
);
170 * Returns the supplementary folder for this project
172 * @return the supplementary folder for this project
174 public IFolder
getSupplementaryFolderParent() {
175 TmfProjectElement project
= getProject();
176 IProject projectResource
= (IProject
)project
.getResource();
177 IFolder supplFolderParent
= projectResource
.getFolder(TmfCommonConstants
.TRACE_SUPPLEMENATARY_FOLDER_NAME
);
179 if (!supplFolderParent
.exists()) {
181 supplFolderParent
.create(true, true, new NullProgressMonitor());
182 } catch (CoreException e
) {
183 TmfUiPlugin
.getDefault().getLog().log(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, "Error creating project specific supplementary folder " + supplFolderParent
, e
)); //$NON-NLS-1$
186 return supplFolderParent
;