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