Fix NLS-related Javadoc warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfExperimentElement.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 Ericsson, École Polytechnique de Montréal
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 * Geneviève Bastien - Copied code to add/remove traces in this class
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.project.model;
15
16 import java.io.ByteArrayInputStream;
17 import java.io.InputStream;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IFolder;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.IWorkspace;
27 import org.eclipse.core.resources.ResourcesPlugin;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.core.runtime.IPath;
30 import org.eclipse.core.runtime.QualifiedName;
31 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
32 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
33 import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
34 import org.eclipse.linuxtools.tmf.core.util.ReadOnlyTextPropertyDescriptor;
35 import org.eclipse.ui.IEditorReference;
36 import org.eclipse.ui.IWorkbench;
37 import org.eclipse.ui.IWorkbenchPage;
38 import org.eclipse.ui.IWorkbenchWindow;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.part.FileEditorInput;
41 import org.eclipse.ui.views.properties.IPropertyDescriptor;
42 import org.eclipse.ui.views.properties.IPropertySource2;
43
44 /**
45 * Implementation of TMF Experiment Model Element.
46 * <p>
47 * @version 1.0
48 * @author Francois Chouinard
49 *
50 */
51 public class TmfExperimentElement extends TmfWithFolderElement implements IPropertySource2 {
52
53 // ------------------------------------------------------------------------
54 // Constants
55 // ------------------------------------------------------------------------
56
57 // Property View stuff
58 private static final String sfInfoCategory = "Info"; //$NON-NLS-1$
59 private static final String sfName = "name"; //$NON-NLS-1$
60 private static final String sfPath = "path"; //$NON-NLS-1$
61 private static final String sfLocation = "location"; //$NON-NLS-1$
62 private static final String sfFolderSuffix = "_exp"; //$NON-NLS-1$
63
64 private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName);
65 private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath);
66 private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfLocation,
67 sfLocation);
68
69 private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor,
70 sfLocationDescriptor };
71
72 static {
73 sfNameDescriptor.setCategory(sfInfoCategory);
74 sfPathDescriptor.setCategory(sfInfoCategory);
75 sfLocationDescriptor.setCategory(sfInfoCategory);
76 }
77
78 private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
79
80 // ------------------------------------------------------------------------
81 // Constructors
82 // ------------------------------------------------------------------------
83 /**
84 * Constructor
85 * @param name The name of the experiment
86 * @param folder The folder reference
87 * @param parent The experiment folder reference.
88 */
89 public TmfExperimentElement(String name, IFolder folder, TmfExperimentFolder parent) {
90 super(name, folder, parent);
91 parent.addChild(this);
92 }
93
94 // ------------------------------------------------------------------------
95 // TmfProjectModelElement
96 // ------------------------------------------------------------------------
97
98 /*
99 * (non-Javadoc)
100 * @see org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectModelElement#getResource()
101 */
102 @Override
103 public IFolder getResource() {
104 return (IFolder) fResource;
105 }
106
107 /*
108 * (non-Javadoc)
109 * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getProject()
110 */
111 @Override
112 public TmfProjectElement getProject() {
113 return (TmfProjectElement) getParent().getParent();
114 }
115
116 // ------------------------------------------------------------------------
117 // Operations
118 // ------------------------------------------------------------------------
119 /**
120 * Returns a list of TmfTraceElements contained in this experiment.
121 * @return a list of TmfTraceElements
122 */
123 public List<TmfTraceElement> getTraces() {
124 List<ITmfProjectModelElement> children = getChildren();
125 List<TmfTraceElement> traces = new ArrayList<TmfTraceElement>();
126 for (ITmfProjectModelElement child : children) {
127 if (child instanceof TmfTraceElement) {
128 traces.add((TmfTraceElement) child);
129 }
130 }
131 return traces;
132 }
133
134
135 /**
136 * Adds a trace to the experiment
137 *
138 * @param trace The trace element to add
139 * @since 2.0
140 */
141 public void addTrace(TmfTraceElement trace) {
142 /**
143 * Create a link to the actual trace and set the trace type
144 */
145 IFolder experiment = getResource();
146 IResource resource = trace.getResource();
147 IPath location = resource.getLocation();
148 IWorkspace workspace = ResourcesPlugin.getWorkspace();
149 try {
150 Map<QualifiedName, String> properties = trace.getResource().getPersistentProperties();
151 String bundleName = properties.get(TmfCommonConstants.TRACEBUNDLE);
152 String traceType = properties.get(TmfCommonConstants.TRACETYPE);
153 String iconUrl = properties.get(TmfCommonConstants.TRACEICON);
154
155 if (resource instanceof IFolder) {
156 IFolder folder = experiment.getFolder(trace.getName());
157 if (workspace.validateLinkLocation(folder, location).isOK()) {
158 folder.createLink(location, IResource.REPLACE, null);
159 setProperties(folder, bundleName, traceType, iconUrl);
160
161 } else {
162 Activator.getDefault().logError("Error creating link. Invalid trace location " + location); //$NON-NLS-1$
163 }
164 } else {
165 IFile file = experiment.getFile(trace.getName());
166 if (workspace.validateLinkLocation(file, location).isOK()) {
167 file.createLink(location, IResource.REPLACE, null);
168 setProperties(file, bundleName, traceType, iconUrl);
169 } else {
170 Activator.getDefault().logError("Error creating link. Invalid trace location " + location); //$NON-NLS-1$
171 }
172 }
173 } catch (CoreException e) {
174 Activator.getDefault().logError("Error creating link to location " + location, e); //$NON-NLS-1$
175 }
176
177 }
178
179 /**
180 * Removes a trace from an experiment
181 *
182 * @param trace The trace to remove
183 * @throws CoreException exception
184 * @since 2.0
185 */
186 public void removeTrace(TmfTraceElement trace) throws CoreException {
187
188 // Close the experiment if open
189 IFile file = getBookmarksFile();
190 FileEditorInput input = new FileEditorInput(file);
191 IWorkbench wb = PlatformUI.getWorkbench();
192 for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
193 for (IWorkbenchPage wbPage : wbWindow.getPages()) {
194 for (IEditorReference editorReference : wbPage.getEditorReferences()) {
195 if (editorReference.getEditorInput().equals(input)) {
196 wbPage.closeEditor(editorReference.getEditor(false), false);
197 }
198 }
199 }
200 }
201
202 /* Finally, remove the trace from experiment*/
203 removeChild(trace);
204 trace.getResource().delete(true, null);
205
206 }
207
208 private static void setProperties(IResource resource, String bundleName,
209 String traceType, String iconUrl) throws CoreException {
210 resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, bundleName);
211 resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType);
212 resource.setPersistentProperty(TmfCommonConstants.TRACEICON, iconUrl);
213 }
214
215 /**
216 * Returns the file resource used to store bookmarks after creating it if necessary.
217 * The file will be created if it does not exist.
218 * @return the bookmarks file
219 * @throws CoreException if the bookmarks file cannot be created
220 * @since 2.0
221 */
222 public IFile createBookmarksFile() throws CoreException {
223 IFile file = getBookmarksFile();
224 if (!file.exists()) {
225 final IFile bookmarksFile = getProject().getExperimentsFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
226 if (!bookmarksFile.exists()) {
227 final InputStream source = new ByteArrayInputStream(new byte[0]);
228 bookmarksFile.create(source, true, null);
229 }
230 bookmarksFile.setHidden(true);
231 file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
232 file.setHidden(true);
233 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfExperiment.class.getCanonicalName());
234 }
235 return file;
236 }
237
238 /**
239 * Returns the file resource used to store bookmarks.
240 * The file may not exist.
241 * @return the bookmarks file
242 * @since 2.0
243 */
244 public IFile getBookmarksFile() {
245 final IFolder folder = (IFolder) fResource;
246 IFile file = folder.getFile(getName() + '_');
247 return file;
248 }
249
250 // ------------------------------------------------------------------------
251 // IPropertySource2
252 // ------------------------------------------------------------------------
253
254 /*
255 * (non-Javadoc)
256 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
257 */
258 @Override
259 public Object getEditableValue() {
260 return null;
261 }
262
263 /*
264 * (non-Javadoc)
265 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
266 */
267 @Override
268 public IPropertyDescriptor[] getPropertyDescriptors() {
269 return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
270 }
271
272 /*
273 * (non-Javadoc)
274 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
275 */
276 @Override
277 public Object getPropertyValue(Object id) {
278
279 if (sfName.equals(id)) {
280 return getName();
281 }
282
283 if (sfPath.equals(id)) {
284 return getPath().toString();
285 }
286
287 if (sfLocation.equals(id)) {
288 return getLocation().toString();
289 }
290
291 return null;
292 }
293
294 /*
295 * (non-Javadoc)
296 * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
297 */
298 @Override
299 public void resetPropertyValue(Object id) {
300 }
301
302 /*
303 * (non-Javadoc)
304 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
305 */
306 @Override
307 public void setPropertyValue(Object id, Object value) {
308 }
309
310 /*
311 * (non-Javadoc)
312 * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertyResettable(java.lang.Object)
313 */
314 @Override
315 public boolean isPropertyResettable(Object id) {
316 return false;
317 }
318
319 /*
320 * (non-Javadoc)
321 * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertySet(java.lang.Object)
322 */
323 @Override
324 public boolean isPropertySet(Object id) {
325 return false;
326 }
327
328 /**
329 * Return the suffix for resource names
330 * @return The folder suffix
331 */
332 @Override
333 public String getSuffix() {
334 return sfFolderSuffix;
335 }
336
337 }
This page took 0.037419 seconds and 5 git commands to generate.