Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2010 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.project.model; | |
14 | ||
15 | import java.net.URI; | |
16 | import java.util.ArrayList; | |
17 | import java.util.List; | |
18 | ||
e12ecd30 BH |
19 | import org.eclipse.core.resources.IFolder; |
20 | import org.eclipse.core.resources.IProject; | |
12c155f5 FC |
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; | |
e12ecd30 | 25 | import org.eclipse.core.runtime.CoreException; |
12c155f5 | 26 | import org.eclipse.core.runtime.IPath; |
e12ecd30 | 27 | import org.eclipse.core.runtime.NullProgressMonitor; |
8fd82db5 | 28 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
e12ecd30 | 29 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
12c155f5 FC |
30 | |
31 | /** | |
32 | * <b><u>TmfProjectModelElement</u></b> | |
33 | * <p> | |
34 | */ | |
35 | public abstract class TmfProjectModelElement implements ITmfProjectModelElement, IResourceChangeListener { | |
36 | ||
37 | // ------------------------------------------------------------------------ | |
38 | // Attributes | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
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; | |
47 | ||
48 | // ------------------------------------------------------------------------ | |
49 | // Constructor | |
50 | // ------------------------------------------------------------------------ | |
51 | ||
52 | protected TmfProjectModelElement(String name, IResource resource, ITmfProjectModelElement parent) { | |
53 | fName = name; | |
54 | fResource = resource; | |
55 | fPath = resource.getFullPath(); | |
56 | fLocation = resource.getLocationURI(); | |
57 | fParent = parent; | |
58 | fChildren = new ArrayList<ITmfProjectModelElement>(); | |
59 | ResourcesPlugin.getWorkspace().addResourceChangeListener(this); | |
60 | } | |
61 | ||
62 | // ------------------------------------------------------------------------ | |
63 | // ITmfProjectModelElement | |
64 | // ------------------------------------------------------------------------ | |
65 | ||
66 | @Override | |
67 | public String getName() { | |
68 | return fName; | |
69 | } | |
70 | ||
71 | @Override | |
72 | public IResource getResource() { | |
73 | return fResource; | |
74 | } | |
75 | ||
76 | @Override | |
77 | public IPath getPath() { | |
78 | return fPath; | |
79 | } | |
80 | ||
81 | @Override | |
82 | public URI getLocation() { | |
83 | return fLocation; | |
84 | } | |
85 | ||
86 | @Override | |
87 | public ITmfProjectModelElement getParent() { | |
88 | return fParent; | |
89 | } | |
90 | ||
91 | @Override | |
92 | public boolean hasChildren() { | |
93 | return fChildren.size() > 0; | |
94 | } | |
95 | ||
96 | @Override | |
97 | public List<ITmfProjectModelElement> getChildren() { | |
98 | return fChildren; | |
99 | } | |
100 | ||
101 | @Override | |
102 | public void addChild(ITmfProjectModelElement child) { | |
103 | fChildren.add(child); | |
104 | } | |
105 | ||
106 | @Override | |
107 | public void removeChild(ITmfProjectModelElement child) { | |
108 | fChildren.remove(child); | |
109 | refresh(); | |
110 | } | |
111 | ||
112 | @Override | |
113 | public void refresh() { | |
114 | // Do nothing by default: sub-classes override this on an "as-needed" | |
115 | // basis. | |
116 | } | |
117 | ||
118 | // ------------------------------------------------------------------------ | |
119 | // IResourceChangeListener | |
120 | // ------------------------------------------------------------------------ | |
121 | ||
122 | @Override | |
123 | public void resourceChanged(IResourceChangeEvent event) { | |
124 | // Do nothing by default: sub-classes override this on an "as-needed" | |
125 | // basis. | |
126 | } | |
127 | ||
128 | // ------------------------------------------------------------------------ | |
129 | // Object | |
130 | // ------------------------------------------------------------------------ | |
131 | ||
6e85c58d FC |
132 | @Override |
133 | public int hashCode() { | |
134 | final int prime = 31; | |
135 | int result = 1; | |
6e85c58d FC |
136 | result = prime * result + ((fLocation == null) ? 0 : fLocation.hashCode()); |
137 | result = prime * result + ((fName == null) ? 0 : fName.hashCode()); | |
6e85c58d | 138 | result = prime * result + ((fPath == null) ? 0 : fPath.hashCode()); |
6e85c58d FC |
139 | return result; |
140 | } | |
141 | ||
12c155f5 FC |
142 | @Override |
143 | public boolean equals(Object other) { | |
144 | if (this == other) | |
145 | return true; | |
6e85c58d FC |
146 | if (other == null) |
147 | return false; | |
12c155f5 FC |
148 | if (!(other instanceof TmfProjectModelElement)) |
149 | return false; | |
6e85c58d FC |
150 | TmfProjectModelElement element = (TmfProjectModelElement) other; |
151 | return element.fName.equals(fName) && element.fLocation.equals(fLocation); | |
12c155f5 FC |
152 | } |
153 | ||
e12ecd30 BH |
154 | |
155 | /** | |
156 | * Returns the trace specific supplementary directory under the project's supplementary folder. | |
157 | * The folder will be created if it doesn't exist. | |
158 | * | |
159 | * @param supplFoldername - folder name. | |
160 | * @return returns the trace specific supplementary directory | |
161 | */ | |
162 | public IFolder getTraceSupplementaryFolder(String supplFoldername) { | |
163 | IFolder supplFolderParent = getSupplementaryFolderParent(); | |
164 | return supplFolderParent.getFolder(supplFoldername); | |
165 | } | |
166 | ||
167 | /** | |
168 | * Returns the supplementary folder for this project | |
169 | * | |
170 | * @return the supplementary folder for this project | |
171 | */ | |
172 | public IFolder getSupplementaryFolderParent() { | |
173 | TmfProjectElement project = getProject(); | |
174 | IProject projectResource = (IProject)project.getResource(); | |
175 | IFolder supplFolderParent = projectResource.getFolder(TmfCommonConstants.TRACE_SUPPLEMENATARY_FOLDER_NAME); | |
176 | ||
177 | if (!supplFolderParent.exists()) { | |
178 | try { | |
179 | supplFolderParent.create(true, true, new NullProgressMonitor()); | |
180 | } catch (CoreException e) { | |
8fd82db5 | 181 | Activator.getDefault().logError("Error creating project specific supplementary folder " + supplFolderParent, e); //$NON-NLS-1$ |
e12ecd30 BH |
182 | } |
183 | } | |
184 | return supplFolderParent; | |
185 | } | |
12c155f5 | 186 | } |