tmf: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / project / model / TmfProjectModelElement.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 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.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;
39
40 /**
41 * The implementation of the base TMF project model element. It provides default implementation
42 * of the <code>ITmfProjectModelElement</code> interface.
43 * <p>
44 * @version 1.0
45 * @author Francois Chouinard
46 */
47 public abstract class TmfProjectModelElement implements ITmfProjectModelElement {
48
49 // ------------------------------------------------------------------------
50 // Attributes
51 // ------------------------------------------------------------------------
52
53 private final String fName;
54 /**
55 * The project model element resource.
56 */
57 protected final IResource fResource;
58 /**
59 * The project model resource location (URI)
60 */
61 protected final URI fLocation;
62 /**
63 * The project model path of a resource.
64 */
65 protected final IPath fPath;
66 private final ITmfProjectModelElement fParent;
67 /**
68 * The list of children elements.
69 */
70 protected final List<ITmfProjectModelElement> fChildren;
71
72 // ------------------------------------------------------------------------
73 // Constructor
74 // ------------------------------------------------------------------------
75 /**
76 * Constructor.
77 *
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 */
83 protected TmfProjectModelElement(String name, IResource resource, ITmfProjectModelElement parent) {
84 fName = name;
85 fResource = resource;
86 fPath = resource.getFullPath();
87 fLocation = new File(resource.getLocationURI()).toURI();
88 fParent = parent;
89 fChildren = new CopyOnWriteArrayList<>();
90 }
91
92 // ------------------------------------------------------------------------
93 // ITmfProjectModelElement
94 // ------------------------------------------------------------------------
95
96 @Override
97 public String getName() {
98 return fName;
99 }
100
101 @Override
102 public IResource getResource() {
103 return fResource;
104 }
105
106 @Override
107 public IPath getPath() {
108 return fPath;
109 }
110
111 @Override
112 public URI getLocation() {
113 return fLocation;
114 }
115
116 /**
117 * @since 3.0
118 */
119 @Override
120 public TmfProjectElement getProject() {
121 return fParent.getProject();
122 }
123
124 @Override
125 public ITmfProjectModelElement getParent() {
126 return fParent;
127 }
128
129 @Override
130 public boolean hasChildren() {
131 return fChildren.size() > 0;
132 }
133
134 @Override
135 public List<ITmfProjectModelElement> getChildren() {
136 return fChildren;
137 }
138
139 @Override
140 public void addChild(ITmfProjectModelElement child) {
141 fChildren.add(child);
142 }
143
144 @Override
145 public void removeChild(ITmfProjectModelElement child) {
146 fChildren.remove(child);
147 }
148
149 @Override
150 public void refresh() {
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 }});
180 }
181
182 // ------------------------------------------------------------------------
183 // Object
184 // ------------------------------------------------------------------------
185
186 @Override
187 public int hashCode() {
188 final int prime = 31;
189 int result = 1;
190 result = prime * result + ((fPath == null) ? 0 : fPath.hashCode());
191 return result;
192 }
193
194 @Override
195 public boolean equals(Object other) {
196 if (this == other) {
197 return true;
198 }
199 if (other == null) {
200 return false;
201 }
202 if (!(other instanceof TmfProjectModelElement)) {
203 return false;
204 }
205 TmfProjectModelElement element = (TmfProjectModelElement) other;
206 return element.fPath.equals(fPath);
207 }
208
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
222 /**
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.
226 *
227 * @param supplFolderPath
228 * folder path relative to the project's supplementary folder
229 * @return the trace specific supplementary folder
230 */
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;
237 }
238
239 /**
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.
244 *
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
251 */
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());
259 }
260 } catch (CoreException e) {
261 Activator.getDefault().logError("Error creating supplementary folder " + folder.getFullPath(), e); //$NON-NLS-1$
262 }
263 return folder;
264 }
265
266 @Override
267 public String toString() {
268 return getClass().getSimpleName() + '(' + getPath() + ')';
269 }
270
271 }
This page took 0.038534 seconds and 5 git commands to generate.