Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2010, 2014 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 |
f537c959 | 12 | * Patrick Tasse - Refactor resource change listener |
12c155f5 FC |
13 | *******************************************************************************/ |
14 | ||
2bdf0193 | 15 | package org.eclipse.tracecompass.tmf.ui.project.model; |
12c155f5 | 16 | |
89730b51 | 17 | import java.io.File; |
12c155f5 | 18 | import java.net.URI; |
12c155f5 | 19 | import java.util.List; |
9e619b5e | 20 | import java.util.concurrent.CopyOnWriteArrayList; |
12c155f5 | 21 | |
e12ecd30 BH |
22 | import org.eclipse.core.resources.IFolder; |
23 | import org.eclipse.core.resources.IProject; | |
12c155f5 | 24 | import org.eclipse.core.resources.IResource; |
e12ecd30 | 25 | import org.eclipse.core.runtime.CoreException; |
12c155f5 | 26 | import org.eclipse.core.runtime.IPath; |
e12ecd30 | 27 | import org.eclipse.core.runtime.NullProgressMonitor; |
b3e4798c | 28 | import org.eclipse.jdt.annotation.NonNull; |
f537c959 | 29 | import org.eclipse.swt.widgets.Display; |
2bdf0193 AM |
30 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
31 | import org.eclipse.tracecompass.tmf.core.TmfCommonConstants; | |
f537c959 PT |
32 | import org.eclipse.ui.IViewPart; |
33 | import org.eclipse.ui.IViewReference; | |
34 | import org.eclipse.ui.IWorkbench; | |
35 | import org.eclipse.ui.IWorkbenchPage; | |
36 | import org.eclipse.ui.IWorkbenchWindow; | |
37 | import org.eclipse.ui.PlatformUI; | |
38 | import org.eclipse.ui.navigator.CommonNavigator; | |
39 | import org.eclipse.ui.navigator.CommonViewer; | |
12c155f5 | 40 | |
b3e4798c AM |
41 | import com.google.common.collect.ImmutableList; |
42 | ||
12c155f5 | 43 | /** |
013a5f1c AM |
44 | * The implementation of the base TMF project model element. It provides default implementation |
45 | * of the <code>ITmfProjectModelElement</code> interface. | |
12c155f5 | 46 | * <p> |
b544077e BH |
47 | * @version 1.0 |
48 | * @author Francois Chouinard | |
12c155f5 | 49 | */ |
f537c959 | 50 | public abstract class TmfProjectModelElement implements ITmfProjectModelElement { |
12c155f5 FC |
51 | |
52 | // ------------------------------------------------------------------------ | |
53 | // Attributes | |
54 | // ------------------------------------------------------------------------ | |
55 | ||
56 | private final String fName; | |
b3e4798c AM |
57 | |
58 | /** The project model element resource */ | |
59 | private final IResource fResource; | |
60 | ||
61 | /** The project model resource location (URI) */ | |
62 | private final URI fLocation; | |
63 | ||
64 | /** The project model path of a resource */ | |
65 | private final IPath fPath; | |
66 | ||
12c155f5 | 67 | private final ITmfProjectModelElement fParent; |
b3e4798c AM |
68 | |
69 | /** The list of children elements */ | |
70 | private final @NonNull List<ITmfProjectModelElement> fChildren; | |
12c155f5 FC |
71 | |
72 | // ------------------------------------------------------------------------ | |
73 | // Constructor | |
74 | // ------------------------------------------------------------------------ | |
b3e4798c | 75 | |
b544077e BH |
76 | /** |
77 | * Constructor. | |
013a5f1c | 78 | * |
b544077e | 79 | * Creates a base project model element. |
b3e4798c AM |
80 | * |
81 | * @param name | |
82 | * The name of the element. | |
83 | * @param resource | |
84 | * The element resource. | |
85 | * @param parent | |
86 | * The parent model element. | |
b544077e | 87 | */ |
12c155f5 FC |
88 | protected TmfProjectModelElement(String name, IResource resource, ITmfProjectModelElement parent) { |
89 | fName = name; | |
90 | fResource = resource; | |
91 | fPath = resource.getFullPath(); | |
89730b51 | 92 | fLocation = new File(resource.getLocationURI()).toURI(); |
12c155f5 | 93 | fParent = parent; |
9e619b5e | 94 | fChildren = new CopyOnWriteArrayList<>(); |
c4c81d91 PT |
95 | } |
96 | ||
12c155f5 FC |
97 | // ------------------------------------------------------------------------ |
98 | // ITmfProjectModelElement | |
99 | // ------------------------------------------------------------------------ | |
11252342 | 100 | |
12c155f5 FC |
101 | @Override |
102 | public String getName() { | |
103 | return fName; | |
104 | } | |
11252342 | 105 | |
12c155f5 FC |
106 | @Override |
107 | public IResource getResource() { | |
108 | return fResource; | |
109 | } | |
11252342 | 110 | |
12c155f5 FC |
111 | @Override |
112 | public IPath getPath() { | |
113 | return fPath; | |
114 | } | |
11252342 | 115 | |
12c155f5 FC |
116 | @Override |
117 | public URI getLocation() { | |
118 | return fLocation; | |
119 | } | |
11252342 | 120 | |
339d539c PT |
121 | @Override |
122 | public TmfProjectElement getProject() { | |
123 | return fParent.getProject(); | |
124 | } | |
125 | ||
12c155f5 FC |
126 | @Override |
127 | public ITmfProjectModelElement getParent() { | |
128 | return fParent; | |
129 | } | |
11252342 | 130 | |
12c155f5 FC |
131 | @Override |
132 | public List<ITmfProjectModelElement> getChildren() { | |
b3e4798c | 133 | return ImmutableList.copyOf(fChildren); |
12c155f5 | 134 | } |
11252342 | 135 | |
12c155f5 FC |
136 | @Override |
137 | public void refresh() { | |
f537c959 PT |
138 | // make sure the model is updated in the current thread |
139 | refreshChildren(); | |
140 | ||
141 | Display.getDefault().asyncExec(new Runnable(){ | |
142 | @Override | |
143 | public void run() { | |
144 | IWorkbench wb = PlatformUI.getWorkbench(); | |
145 | IWorkbenchWindow wbWindow = wb.getActiveWorkbenchWindow(); | |
146 | if (wbWindow == null) { | |
147 | return; | |
148 | } | |
149 | IWorkbenchPage activePage = wbWindow.getActivePage(); | |
150 | if (activePage == null) { | |
151 | return; | |
152 | } | |
153 | ||
154 | for (IViewReference viewReference : activePage.getViewReferences()) { | |
155 | IViewPart viewPart = viewReference.getView(false); | |
156 | if (viewPart instanceof CommonNavigator) { | |
157 | CommonViewer commonViewer = ((CommonNavigator) viewPart).getCommonViewer(); | |
158 | Object element = TmfProjectModelElement.this; | |
159 | if (element instanceof TmfProjectElement) { | |
160 | // for the project element the viewer uses the IProject resource | |
161 | element = getResource(); | |
162 | } | |
163 | commonViewer.refresh(element); | |
164 | } | |
165 | } | |
166 | }}); | |
12c155f5 FC |
167 | } |
168 | ||
169 | // ------------------------------------------------------------------------ | |
170 | // Object | |
171 | // ------------------------------------------------------------------------ | |
11252342 | 172 | |
6e85c58d FC |
173 | @Override |
174 | public int hashCode() { | |
175 | final int prime = 31; | |
176 | int result = 1; | |
6e85c58d | 177 | result = prime * result + ((fPath == null) ? 0 : fPath.hashCode()); |
6e85c58d FC |
178 | return result; |
179 | } | |
11252342 | 180 | |
12c155f5 FC |
181 | @Override |
182 | public boolean equals(Object other) { | |
013a5f1c | 183 | if (this == other) { |
12c155f5 | 184 | return true; |
013a5f1c AM |
185 | } |
186 | if (other == null) { | |
6e85c58d | 187 | return false; |
013a5f1c AM |
188 | } |
189 | if (!(other instanceof TmfProjectModelElement)) { | |
12c155f5 | 190 | return false; |
013a5f1c | 191 | } |
6e85c58d | 192 | TmfProjectModelElement element = (TmfProjectModelElement) other; |
81fe3479 | 193 | return element.fPath.equals(fPath); |
12c155f5 | 194 | } |
013a5f1c | 195 | |
f537c959 PT |
196 | // ------------------------------------------------------------------------ |
197 | // Operations | |
198 | // ------------------------------------------------------------------------ | |
199 | ||
200 | /** | |
201 | * Refresh the children of this model element, adding new children and | |
202 | * removing dangling children as necessary. The remaining children should | |
203 | * also refresh their own children sub-tree. | |
b3e4798c AM |
204 | * |
205 | * @since 2.0 | |
f537c959 | 206 | */ |
b3e4798c AM |
207 | protected abstract void refreshChildren(); |
208 | ||
209 | /** | |
210 | * Add a new child element to this element. | |
211 | * | |
212 | * @param child | |
213 | * The child to add | |
214 | */ | |
215 | protected void addChild(ITmfProjectModelElement child) { | |
216 | fChildren.add(child); | |
217 | } | |
218 | ||
219 | /** | |
220 | * Remove an element from the current child elements. | |
221 | * | |
222 | * @param child | |
223 | * The child to remove | |
224 | */ | |
225 | protected void removeChild(ITmfProjectModelElement child) { | |
226 | fChildren.remove(child); | |
f537c959 PT |
227 | } |
228 | ||
e12ecd30 | 229 | /** |
339d539c PT |
230 | * Returns the trace specific supplementary folder under the project's |
231 | * supplementary folder. The returned folder and its parent folders may not | |
232 | * exist. | |
013a5f1c | 233 | * |
339d539c PT |
234 | * @param supplFolderPath |
235 | * folder path relative to the project's supplementary folder | |
236 | * @return the trace specific supplementary folder | |
e12ecd30 | 237 | */ |
339d539c PT |
238 | public IFolder getTraceSupplementaryFolder(String supplFolderPath) { |
239 | TmfProjectElement project = getProject(); | |
240 | IProject projectResource = project.getResource(); | |
241 | IFolder supplFolderParent = projectResource.getFolder(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER_NAME); | |
242 | IFolder folder = supplFolderParent.getFolder(supplFolderPath); | |
243 | return folder; | |
e12ecd30 BH |
244 | } |
245 | ||
246 | /** | |
339d539c PT |
247 | * Returns the trace specific supplementary folder under the project's |
248 | * supplementary folder. Its parent folders will be created if they don't | |
249 | * exist. If createFolder is true, the returned folder will be created, | |
250 | * otherwise it may not exist. | |
013a5f1c | 251 | * |
339d539c PT |
252 | * @param supplFolderPath |
253 | * folder path relative to the project's supplementary folder | |
254 | * @param createFolder | |
255 | * if true, the returned folder will be created | |
256 | * @return the trace specific supplementary folder | |
e12ecd30 | 257 | */ |
339d539c PT |
258 | public IFolder prepareTraceSupplementaryFolder(String supplFolderPath, boolean createFolder) { |
259 | IFolder folder = getTraceSupplementaryFolder(supplFolderPath); | |
260 | try { | |
261 | if (createFolder) { | |
262 | TraceUtils.createFolder(folder, new NullProgressMonitor()); | |
263 | } else { | |
264 | TraceUtils.createFolder((IFolder) folder.getParent(), new NullProgressMonitor()); | |
e12ecd30 | 265 | } |
339d539c PT |
266 | } catch (CoreException e) { |
267 | Activator.getDefault().logError("Error creating supplementary folder " + folder.getFullPath(), e); //$NON-NLS-1$ | |
e12ecd30 | 268 | } |
339d539c | 269 | return folder; |
e12ecd30 | 270 | } |
ceacc1e3 PT |
271 | |
272 | @Override | |
273 | public String toString() { | |
274 | return getClass().getSimpleName() + '(' + getPath() + ')'; | |
275 | } | |
276 | ||
12c155f5 | 277 | } |