1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 Ericsson, École Polytechnique de Montréal
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
10 * Francois Chouinard - Initial API and implementation
11 * Geneviève Bastien - Copied code to add/remove traces in this class
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
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
;
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
;
45 * Implementation of TMF Experiment Model Element.
48 * @author Francois Chouinard
51 public class TmfExperimentElement
extends TmfWithFolderElement
implements IPropertySource2
{
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
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$
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
,
69 private static final IPropertyDescriptor
[] sfDescriptors
= { sfNameDescriptor
, sfPathDescriptor
,
70 sfLocationDescriptor
};
73 sfNameDescriptor
.setCategory(sfInfoCategory
);
74 sfPathDescriptor
.setCategory(sfInfoCategory
);
75 sfLocationDescriptor
.setCategory(sfInfoCategory
);
78 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
80 // ------------------------------------------------------------------------
82 // ------------------------------------------------------------------------
85 * @param name The name of the experiment
86 * @param folder The folder reference
87 * @param parent The experiment folder reference.
89 public TmfExperimentElement(String name
, IFolder folder
, TmfExperimentFolder parent
) {
90 super(name
, folder
, parent
);
91 parent
.addChild(this);
94 // ------------------------------------------------------------------------
95 // TmfProjectModelElement
96 // ------------------------------------------------------------------------
100 * @see org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectModelElement#getResource()
103 public IFolder
getResource() {
104 return (IFolder
) fResource
;
109 * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getProject()
112 public TmfProjectElement
getProject() {
113 return (TmfProjectElement
) getParent().getParent();
116 // ------------------------------------------------------------------------
118 // ------------------------------------------------------------------------
120 * Returns a list of TmfTraceElements contained in this experiment.
121 * @return a list of TmfTraceElements
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
);
136 * Adds a trace to the experiment
138 * @param trace The trace element to add
141 public void addTrace(TmfTraceElement trace
) {
143 * Create a link to the actual trace and set the trace type
145 IFolder experiment
= getResource();
146 IResource resource
= trace
.getResource();
147 IPath location
= resource
.getLocation();
148 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
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
);
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
);
162 Activator
.getDefault().logError("Error creating link. Invalid trace location " + location
); //$NON-NLS-1$
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
);
170 Activator
.getDefault().logError("Error creating link. Invalid trace location " + location
); //$NON-NLS-1$
173 } catch (CoreException e
) {
174 Activator
.getDefault().logError("Error creating link to location " + location
, e
); //$NON-NLS-1$
180 * Removes a trace from an experiment
182 * @param trace The trace to remove
183 * @throws CoreException exception
186 public void removeTrace(TmfTraceElement trace
) throws CoreException
{
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);
202 /* Finally, remove the trace from experiment*/
204 trace
.getResource().delete(true, null);
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
);
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
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);
230 bookmarksFile
.setHidden(true);
231 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
, null);
232 file
.setHidden(true);
233 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, TmfExperiment
.class.getCanonicalName());
239 * Returns the file resource used to store bookmarks.
240 * The file may not exist.
241 * @return the bookmarks file
244 public IFile
getBookmarksFile() {
245 final IFolder folder
= (IFolder
) fResource
;
246 IFile file
= folder
.getFile(getName() + '_');
250 // ------------------------------------------------------------------------
252 // ------------------------------------------------------------------------
256 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
259 public Object
getEditableValue() {
265 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
268 public IPropertyDescriptor
[] getPropertyDescriptors() {
269 return Arrays
.copyOf(sfDescriptors
, sfDescriptors
.length
);
274 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
277 public Object
getPropertyValue(Object id
) {
279 if (sfName
.equals(id
)) {
283 if (sfPath
.equals(id
)) {
284 return getPath().toString();
287 if (sfLocation
.equals(id
)) {
288 return getLocation().toString();
296 * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
299 public void resetPropertyValue(Object id
) {
304 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
307 public void setPropertyValue(Object id
, Object value
) {
312 * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertyResettable(java.lang.Object)
315 public boolean isPropertyResettable(Object id
) {
321 * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertySet(java.lang.Object)
324 public boolean isPropertySet(Object id
) {
329 * Return the suffix for resource names
330 * @return The folder suffix
333 public String
getSuffix() {
334 return sfFolderSuffix
;