1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 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 * Bernd Hufmann - Added supplementary files/folder handling
12 * Patrick Tasse - Refactor resource change listener
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
;
19 import java
.util
.List
;
20 import java
.util
.concurrent
.CopyOnWriteArrayList
;
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
;
41 import com
.google
.common
.collect
.ImmutableList
;
44 * The implementation of the base TMF project model element. It provides default implementation
45 * of the <code>ITmfProjectModelElement</code> interface.
48 * @author Francois Chouinard
50 public abstract class TmfProjectModelElement
implements ITmfProjectModelElement
{
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
56 private final String fName
;
58 /** The project model element resource */
59 private final IResource fResource
;
61 /** The project model resource location (URI) */
62 private final URI fLocation
;
64 /** The project model path of a resource */
65 private final IPath fPath
;
67 private final ITmfProjectModelElement fParent
;
69 /** The list of children elements */
70 private final @NonNull List
<ITmfProjectModelElement
> fChildren
;
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
79 * Creates a base project model element.
82 * The name of the element.
84 * The element resource.
86 * The parent model element.
88 protected TmfProjectModelElement(String name
, IResource resource
, ITmfProjectModelElement parent
) {
91 fPath
= resource
.getFullPath();
92 fLocation
= new File(resource
.getLocationURI()).toURI();
94 fChildren
= new CopyOnWriteArrayList
<>();
97 // ------------------------------------------------------------------------
98 // ITmfProjectModelElement
99 // ------------------------------------------------------------------------
102 public String
getName() {
107 public IResource
getResource() {
112 public IPath
getPath() {
117 public URI
getLocation() {
122 public TmfProjectElement
getProject() {
123 return fParent
.getProject();
127 public ITmfProjectModelElement
getParent() {
132 public List
<ITmfProjectModelElement
> getChildren() {
133 return ImmutableList
.copyOf(fChildren
);
137 public void refresh() {
138 // make sure the model is updated in the current thread
141 Display
.getDefault().asyncExec(new Runnable(){
144 IWorkbench wb
= PlatformUI
.getWorkbench();
145 IWorkbenchWindow wbWindow
= wb
.getActiveWorkbenchWindow();
146 if (wbWindow
== null) {
149 IWorkbenchPage activePage
= wbWindow
.getActivePage();
150 if (activePage
== null) {
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();
163 commonViewer
.refresh(element
);
169 // ------------------------------------------------------------------------
171 // ------------------------------------------------------------------------
174 public int hashCode() {
175 final int prime
= 31;
177 result
= prime
* result
+ ((fPath
== null) ?
0 : fPath
.hashCode());
182 public boolean equals(Object other
) {
189 if (!(other
.getClass().equals(this.getClass()))) {
192 TmfProjectModelElement element
= (TmfProjectModelElement
) other
;
193 return element
.fPath
.equals(fPath
);
196 // ------------------------------------------------------------------------
198 // ------------------------------------------------------------------------
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.
207 protected abstract void refreshChildren();
210 * Add a new child element to this element.
215 protected void addChild(ITmfProjectModelElement child
) {
216 fChildren
.add(child
);
220 * Remove an element from the current child elements.
223 * The child to remove
225 protected void removeChild(ITmfProjectModelElement child
) {
226 fChildren
.remove(child
);
230 * Returns the trace specific supplementary folder under the project's
231 * supplementary folder. The returned folder and its parent folders may not
234 * @param supplFolderPath
235 * folder path relative to the project's supplementary folder
236 * @return the trace specific supplementary folder
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
);
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.
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
258 public IFolder
prepareTraceSupplementaryFolder(String supplFolderPath
, boolean createFolder
) {
259 IFolder folder
= getTraceSupplementaryFolder(supplFolderPath
);
262 TraceUtils
.createFolder(folder
, new NullProgressMonitor());
264 TraceUtils
.createFolder((IFolder
) folder
.getParent(), new NullProgressMonitor());
266 } catch (CoreException e
) {
267 Activator
.getDefault().logError("Error creating supplementary folder " + folder
.getFullPath(), e
); //$NON-NLS-1$
273 public String
toString() {
274 return getClass().getSimpleName() + '(' + getPath() + ')';