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