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