1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 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
.swt
.widgets
.Display
;
29 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
31 import org
.eclipse
.ui
.IViewPart
;
32 import org
.eclipse
.ui
.IViewReference
;
33 import org
.eclipse
.ui
.IWorkbench
;
34 import org
.eclipse
.ui
.IWorkbenchPage
;
35 import org
.eclipse
.ui
.IWorkbenchWindow
;
36 import org
.eclipse
.ui
.PlatformUI
;
37 import org
.eclipse
.ui
.navigator
.CommonNavigator
;
38 import org
.eclipse
.ui
.navigator
.CommonViewer
;
41 * The implementation of the base TMF project model element. It provides default implementation
42 * of the <code>ITmfProjectModelElement</code> interface.
45 * @author Francois Chouinard
47 public abstract class TmfProjectModelElement
implements ITmfProjectModelElement
{
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
53 private final String fName
;
55 * The project model element resource.
57 protected final IResource fResource
;
59 * The project model resource location (URI)
61 protected final URI fLocation
;
63 * The project model path of a resource.
65 protected final IPath fPath
;
66 private final ITmfProjectModelElement fParent
;
68 * The list of children elements.
70 protected final List
<ITmfProjectModelElement
> fChildren
;
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
78 * Creates a base project model element.
79 * @param name The name of the element.
80 * @param resource The element resource.
81 * @param parent The parent model element.
83 protected TmfProjectModelElement(String name
, IResource resource
, ITmfProjectModelElement parent
) {
86 fPath
= resource
.getFullPath();
87 fLocation
= new File(resource
.getLocationURI()).toURI();
89 fChildren
= new CopyOnWriteArrayList
<>();
92 // ------------------------------------------------------------------------
93 // ITmfProjectModelElement
94 // ------------------------------------------------------------------------
97 public String
getName() {
102 public IResource
getResource() {
107 public IPath
getPath() {
112 public URI
getLocation() {
117 public TmfProjectElement
getProject() {
118 return fParent
.getProject();
122 public ITmfProjectModelElement
getParent() {
127 public boolean hasChildren() {
128 return fChildren
.size() > 0;
132 public List
<ITmfProjectModelElement
> getChildren() {
137 public void addChild(ITmfProjectModelElement child
) {
138 fChildren
.add(child
);
142 public void removeChild(ITmfProjectModelElement child
) {
143 fChildren
.remove(child
);
147 public void refresh() {
148 // make sure the model is updated in the current thread
151 Display
.getDefault().asyncExec(new Runnable(){
154 IWorkbench wb
= PlatformUI
.getWorkbench();
155 IWorkbenchWindow wbWindow
= wb
.getActiveWorkbenchWindow();
156 if (wbWindow
== null) {
159 IWorkbenchPage activePage
= wbWindow
.getActivePage();
160 if (activePage
== null) {
164 for (IViewReference viewReference
: activePage
.getViewReferences()) {
165 IViewPart viewPart
= viewReference
.getView(false);
166 if (viewPart
instanceof CommonNavigator
) {
167 CommonViewer commonViewer
= ((CommonNavigator
) viewPart
).getCommonViewer();
168 Object element
= TmfProjectModelElement
.this;
169 if (element
instanceof TmfProjectElement
) {
170 // for the project element the viewer uses the IProject resource
171 element
= getResource();
173 commonViewer
.refresh(element
);
179 // ------------------------------------------------------------------------
181 // ------------------------------------------------------------------------
184 public int hashCode() {
185 final int prime
= 31;
187 result
= prime
* result
+ ((fPath
== null) ?
0 : fPath
.hashCode());
192 public boolean equals(Object other
) {
199 if (!(other
instanceof TmfProjectModelElement
)) {
202 TmfProjectModelElement element
= (TmfProjectModelElement
) other
;
203 return element
.fPath
.equals(fPath
);
206 // ------------------------------------------------------------------------
208 // ------------------------------------------------------------------------
211 * Refresh the children of this model element, adding new children and
212 * removing dangling children as necessary. The remaining children should
213 * also refresh their own children sub-tree.
215 void refreshChildren() {
216 // Sub-classes may override this method as needed
220 * Returns the trace specific supplementary folder under the project's
221 * supplementary folder. The returned folder and its parent folders may not
224 * @param supplFolderPath
225 * folder path relative to the project's supplementary folder
226 * @return the trace specific supplementary folder
228 public IFolder
getTraceSupplementaryFolder(String supplFolderPath
) {
229 TmfProjectElement project
= getProject();
230 IProject projectResource
= project
.getResource();
231 IFolder supplFolderParent
= projectResource
.getFolder(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER_NAME
);
232 IFolder folder
= supplFolderParent
.getFolder(supplFolderPath
);
237 * Returns the trace specific supplementary folder under the project's
238 * supplementary folder. Its parent folders will be created if they don't
239 * exist. If createFolder is true, the returned folder will be created,
240 * otherwise it may not exist.
242 * @param supplFolderPath
243 * folder path relative to the project's supplementary folder
244 * @param createFolder
245 * if true, the returned folder will be created
246 * @return the trace specific supplementary folder
248 public IFolder
prepareTraceSupplementaryFolder(String supplFolderPath
, boolean createFolder
) {
249 IFolder folder
= getTraceSupplementaryFolder(supplFolderPath
);
252 TraceUtils
.createFolder(folder
, new NullProgressMonitor());
254 TraceUtils
.createFolder((IFolder
) folder
.getParent(), new NullProgressMonitor());
256 } catch (CoreException e
) {
257 Activator
.getDefault().logError("Error creating supplementary folder " + folder
.getFullPath(), e
); //$NON-NLS-1$
263 public String
toString() {
264 return getClass().getSimpleName() + '(' + getPath() + ')';