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