Commit | Line | Data |
---|---|---|
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 | ||
15 | package org.eclipse.linuxtools.tmf.ui.project.model; | |
16 | ||
89730b51 | 17 | import java.io.File; |
12c155f5 | 18 | import java.net.URI; |
12c155f5 | 19 | import java.util.List; |
9e619b5e | 20 | import java.util.concurrent.CopyOnWriteArrayList; |
12c155f5 | 21 | |
e12ecd30 BH |
22 | import org.eclipse.core.resources.IFolder; |
23 | import org.eclipse.core.resources.IProject; | |
12c155f5 | 24 | import org.eclipse.core.resources.IResource; |
e12ecd30 | 25 | import org.eclipse.core.runtime.CoreException; |
12c155f5 | 26 | import org.eclipse.core.runtime.IPath; |
e12ecd30 | 27 | import org.eclipse.core.runtime.NullProgressMonitor; |
8fd82db5 | 28 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
e12ecd30 | 29 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
f537c959 PT |
30 | import org.eclipse.swt.widgets.Display; |
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; | |
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 | 47 | public 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 | |
12c155f5 FC |
116 | @Override |
117 | public ITmfProjectModelElement getParent() { | |
118 | return fParent; | |
119 | } | |
11252342 | 120 | |
12c155f5 FC |
121 | @Override |
122 | public boolean hasChildren() { | |
123 | return fChildren.size() > 0; | |
124 | } | |
11252342 | 125 | |
12c155f5 FC |
126 | @Override |
127 | public List<ITmfProjectModelElement> getChildren() { | |
128 | return fChildren; | |
129 | } | |
11252342 | 130 | |
12c155f5 FC |
131 | @Override |
132 | public void addChild(ITmfProjectModelElement child) { | |
133 | fChildren.add(child); | |
134 | } | |
11252342 | 135 | |
12c155f5 FC |
136 | @Override |
137 | public void removeChild(ITmfProjectModelElement child) { | |
138 | fChildren.remove(child); | |
12c155f5 | 139 | } |
11252342 | 140 | |
12c155f5 FC |
141 | @Override |
142 | public void refresh() { | |
f537c959 PT |
143 | // make sure the model is updated in the current thread |
144 | refreshChildren(); | |
145 | ||
146 | Display.getDefault().asyncExec(new Runnable(){ | |
147 | @Override | |
148 | public void run() { | |
149 | IWorkbench wb = PlatformUI.getWorkbench(); | |
150 | IWorkbenchWindow wbWindow = wb.getActiveWorkbenchWindow(); | |
151 | if (wbWindow == null) { | |
152 | return; | |
153 | } | |
154 | IWorkbenchPage activePage = wbWindow.getActivePage(); | |
155 | if (activePage == null) { | |
156 | return; | |
157 | } | |
158 | ||
159 | for (IViewReference viewReference : activePage.getViewReferences()) { | |
160 | IViewPart viewPart = viewReference.getView(false); | |
161 | if (viewPart instanceof CommonNavigator) { | |
162 | CommonViewer commonViewer = ((CommonNavigator) viewPart).getCommonViewer(); | |
163 | Object element = TmfProjectModelElement.this; | |
164 | if (element instanceof TmfProjectElement) { | |
165 | // for the project element the viewer uses the IProject resource | |
166 | element = getResource(); | |
167 | } | |
168 | commonViewer.refresh(element); | |
169 | } | |
170 | } | |
171 | }}); | |
12c155f5 FC |
172 | } |
173 | ||
174 | // ------------------------------------------------------------------------ | |
175 | // Object | |
176 | // ------------------------------------------------------------------------ | |
11252342 | 177 | |
6e85c58d FC |
178 | @Override |
179 | public int hashCode() { | |
180 | final int prime = 31; | |
181 | int result = 1; | |
6e85c58d | 182 | result = prime * result + ((fPath == null) ? 0 : fPath.hashCode()); |
6e85c58d FC |
183 | return result; |
184 | } | |
11252342 | 185 | |
12c155f5 FC |
186 | @Override |
187 | public boolean equals(Object other) { | |
013a5f1c | 188 | if (this == other) { |
12c155f5 | 189 | return true; |
013a5f1c AM |
190 | } |
191 | if (other == null) { | |
6e85c58d | 192 | return false; |
013a5f1c AM |
193 | } |
194 | if (!(other instanceof TmfProjectModelElement)) { | |
12c155f5 | 195 | return false; |
013a5f1c | 196 | } |
6e85c58d | 197 | TmfProjectModelElement element = (TmfProjectModelElement) other; |
81fe3479 | 198 | return element.fPath.equals(fPath); |
12c155f5 | 199 | } |
013a5f1c | 200 | |
f537c959 PT |
201 | // ------------------------------------------------------------------------ |
202 | // Operations | |
203 | // ------------------------------------------------------------------------ | |
204 | ||
205 | /** | |
206 | * Refresh the children of this model element, adding new children and | |
207 | * removing dangling children as necessary. The remaining children should | |
208 | * also refresh their own children sub-tree. | |
209 | */ | |
210 | void refreshChildren() { | |
211 | // Sub-classes may override this method as needed | |
212 | } | |
213 | ||
e12ecd30 BH |
214 | /** |
215 | * Returns the trace specific supplementary directory under the project's supplementary folder. | |
013a5f1c AM |
216 | * The folder will be created if it doesn't exist. |
217 | * | |
e12ecd30 BH |
218 | * @param supplFoldername - folder name. |
219 | * @return returns the trace specific supplementary directory | |
220 | */ | |
221 | public IFolder getTraceSupplementaryFolder(String supplFoldername) { | |
222 | IFolder supplFolderParent = getSupplementaryFolderParent(); | |
223 | return supplFolderParent.getFolder(supplFoldername); | |
224 | } | |
225 | ||
226 | /** | |
227 | * Returns the supplementary folder for this project | |
013a5f1c AM |
228 | * |
229 | * @return the supplementary folder for this project | |
e12ecd30 BH |
230 | */ |
231 | public IFolder getSupplementaryFolderParent() { | |
232 | TmfProjectElement project = getProject(); | |
013a5f1c | 233 | IProject projectResource = project.getResource(); |
b5e8ee95 | 234 | IFolder supplFolderParent = projectResource.getFolder(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER_NAME); |
e12ecd30 BH |
235 | |
236 | if (!supplFolderParent.exists()) { | |
237 | try { | |
238 | supplFolderParent.create(true, true, new NullProgressMonitor()); | |
239 | } catch (CoreException e) { | |
8fd82db5 | 240 | Activator.getDefault().logError("Error creating project specific supplementary folder " + supplFolderParent, e); //$NON-NLS-1$ |
e12ecd30 BH |
241 | } |
242 | } | |
243 | return supplFolderParent; | |
244 | } | |
ceacc1e3 PT |
245 | |
246 | @Override | |
247 | public String toString() { | |
248 | return getClass().getSimpleName() + '(' + getPath() + ')'; | |
249 | } | |
250 | ||
12c155f5 | 251 | } |