tmf: Fix TmfProjectModelElement.equals()
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / project / model / TmfProjectModelElement.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 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 * Bernd Hufmann - Added supplementary files/folder handling
12 * Patrick Tasse - Refactor resource change listener
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.ui.project.model;
16
17 import java.io.File;
18 import java.net.URI;
19 import java.util.List;
20 import java.util.concurrent.CopyOnWriteArrayList;
21
22 import org.eclipse.core.resources.IFolder;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IResource;
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.jdt.annotation.NonNull;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
31 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
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;
40
41 import com.google.common.collect.ImmutableList;
42
43 /**
44 * The implementation of the base TMF project model element. It provides default implementation
45 * of the <code>ITmfProjectModelElement</code> interface.
46 * <p>
47 * @version 1.0
48 * @author Francois Chouinard
49 */
50 public abstract class TmfProjectModelElement implements ITmfProjectModelElement {
51
52 // ------------------------------------------------------------------------
53 // Attributes
54 // ------------------------------------------------------------------------
55
56 private final String fName;
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
67 private final ITmfProjectModelElement fParent;
68
69 /** The list of children elements */
70 private final @NonNull List<ITmfProjectModelElement> fChildren;
71
72 // ------------------------------------------------------------------------
73 // Constructor
74 // ------------------------------------------------------------------------
75
76 /**
77 * Constructor.
78 *
79 * Creates a base project model element.
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.
87 */
88 protected TmfProjectModelElement(String name, IResource resource, ITmfProjectModelElement parent) {
89 fName = name;
90 fResource = resource;
91 fPath = resource.getFullPath();
92 fLocation = new File(resource.getLocationURI()).toURI();
93 fParent = parent;
94 fChildren = new CopyOnWriteArrayList<>();
95 }
96
97 // ------------------------------------------------------------------------
98 // ITmfProjectModelElement
99 // ------------------------------------------------------------------------
100
101 @Override
102 public String getName() {
103 return fName;
104 }
105
106 @Override
107 public IResource getResource() {
108 return fResource;
109 }
110
111 @Override
112 public IPath getPath() {
113 return fPath;
114 }
115
116 @Override
117 public URI getLocation() {
118 return fLocation;
119 }
120
121 @Override
122 public TmfProjectElement getProject() {
123 return fParent.getProject();
124 }
125
126 @Override
127 public ITmfProjectModelElement getParent() {
128 return fParent;
129 }
130
131 @Override
132 public List<ITmfProjectModelElement> getChildren() {
133 return ImmutableList.copyOf(fChildren);
134 }
135
136 @Override
137 public void refresh() {
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 }});
167 }
168
169 // ------------------------------------------------------------------------
170 // Object
171 // ------------------------------------------------------------------------
172
173 @Override
174 public int hashCode() {
175 final int prime = 31;
176 int result = 1;
177 result = prime * result + ((fPath == null) ? 0 : fPath.hashCode());
178 return result;
179 }
180
181 @Override
182 public boolean equals(Object other) {
183 if (this == other) {
184 return true;
185 }
186 if (other == null) {
187 return false;
188 }
189 if (!(other.getClass().equals(this.getClass()))) {
190 return false;
191 }
192 TmfProjectModelElement element = (TmfProjectModelElement) other;
193 return element.fPath.equals(fPath);
194 }
195
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.
204 *
205 * @since 2.0
206 */
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);
227 }
228
229 /**
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.
233 *
234 * @param supplFolderPath
235 * folder path relative to the project's supplementary folder
236 * @return the trace specific supplementary folder
237 */
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;
244 }
245
246 /**
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.
251 *
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
257 */
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());
265 }
266 } catch (CoreException e) {
267 Activator.getDefault().logError("Error creating supplementary folder " + folder.getFullPath(), e); //$NON-NLS-1$
268 }
269 return folder;
270 }
271
272 @Override
273 public String toString() {
274 return getClass().getSimpleName() + '(' + getPath() + ')';
275 }
276
277 }
This page took 0.037556 seconds and 5 git commands to generate.