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
.NullProgressMonitor
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.TmfUiPlugin
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
32 * <b><u>TmfProjectModelElement</u></b>
35 public abstract class TmfProjectModelElement
implements ITmfProjectModelElement
, IResourceChangeListener
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
41 private final String fName
;
42 protected final IResource fResource
;
43 protected final URI fLocation
;
44 protected final IPath fPath
;
45 private final ITmfProjectModelElement fParent
;
46 protected final List
<ITmfProjectModelElement
> fChildren
;
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 protected TmfProjectModelElement(String name
, IResource resource
, ITmfProjectModelElement parent
) {
55 fPath
= resource
.getFullPath();
56 fLocation
= resource
.getLocationURI();
58 fChildren
= new ArrayList
<ITmfProjectModelElement
>();
59 ResourcesPlugin
.getWorkspace().addResourceChangeListener(this);
62 // ------------------------------------------------------------------------
63 // ITmfProjectModelElement
64 // ------------------------------------------------------------------------
67 public String
getName() {
72 public IResource
getResource() {
77 public IPath
getPath() {
82 public URI
getLocation() {
87 public ITmfProjectModelElement
getParent() {
92 public boolean hasChildren() {
93 return fChildren
.size() > 0;
97 public List
<ITmfProjectModelElement
> getChildren() {
102 public void addChild(ITmfProjectModelElement child
) {
103 fChildren
.add(child
);
107 public void removeChild(ITmfProjectModelElement child
) {
108 fChildren
.remove(child
);
113 public void refresh() {
114 // Do nothing by default: sub-classes override this on an "as-needed"
118 // ------------------------------------------------------------------------
119 // IResourceChangeListener
120 // ------------------------------------------------------------------------
123 public void resourceChanged(IResourceChangeEvent event
) {
124 // Do nothing by default: sub-classes override this on an "as-needed"
128 // ------------------------------------------------------------------------
130 // ------------------------------------------------------------------------
133 public int hashCode() {
134 final int prime
= 31;
136 result
= prime
* result
+ ((fLocation
== null) ?
0 : fLocation
.hashCode());
137 result
= prime
* result
+ ((fName
== null) ?
0 : fName
.hashCode());
138 result
= prime
* result
+ ((fPath
== null) ?
0 : fPath
.hashCode());
143 public boolean equals(Object other
) {
148 if (!(other
instanceof TmfProjectModelElement
))
150 TmfProjectModelElement element
= (TmfProjectModelElement
) other
;
151 return element
.fName
.equals(fName
) && element
.fLocation
.equals(fLocation
);
156 * Returns the trace specific supplementary directory under the project's supplementary folder.
157 * The folder will be created if it doesn't exist.
159 * @param supplFoldername - folder name.
160 * @return returns the trace specific supplementary directory
162 public IFolder
getTraceSupplementaryFolder(String supplFoldername
) {
163 IFolder supplFolderParent
= getSupplementaryFolderParent();
164 return supplFolderParent
.getFolder(supplFoldername
);
168 * Returns the supplementary folder for this project
170 * @return the supplementary folder for this project
172 public IFolder
getSupplementaryFolderParent() {
173 TmfProjectElement project
= getProject();
174 IProject projectResource
= (IProject
)project
.getResource();
175 IFolder supplFolderParent
= projectResource
.getFolder(TmfCommonConstants
.TRACE_SUPPLEMENATARY_FOLDER_NAME
);
177 if (!supplFolderParent
.exists()) {
179 supplFolderParent
.create(true, true, new NullProgressMonitor());
180 } catch (CoreException e
) {
181 TmfUiPlugin
.getDefault().logError("Error creating project specific supplementary folder " + supplFolderParent
, e
); //$NON-NLS-1$
184 return supplFolderParent
;