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 * Patrick Tasse - Close editors to release resources
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
;
17 import java
.io
.ByteArrayInputStream
;
18 import java
.io
.InputStream
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Arrays
;
21 import java
.util
.List
;
24 import org
.eclipse
.core
.resources
.IFile
;
25 import org
.eclipse
.core
.resources
.IFolder
;
26 import org
.eclipse
.core
.resources
.IResource
;
27 import org
.eclipse
.core
.resources
.IWorkspace
;
28 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
29 import org
.eclipse
.core
.runtime
.CoreException
;
30 import org
.eclipse
.core
.runtime
.IPath
;
31 import org
.eclipse
.core
.runtime
.QualifiedName
;
32 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.properties
.ReadOnlyTextPropertyDescriptor
;
36 import org
.eclipse
.ui
.IEditorReference
;
37 import org
.eclipse
.ui
.IWorkbench
;
38 import org
.eclipse
.ui
.IWorkbenchPage
;
39 import org
.eclipse
.ui
.IWorkbenchWindow
;
40 import org
.eclipse
.ui
.PartInitException
;
41 import org
.eclipse
.ui
.PlatformUI
;
42 import org
.eclipse
.ui
.part
.FileEditorInput
;
43 import org
.eclipse
.ui
.views
.properties
.IPropertyDescriptor
;
44 import org
.eclipse
.ui
.views
.properties
.IPropertySource2
;
47 * Implementation of TMF Experiment Model Element.
50 * @author Francois Chouinard
53 public class TmfExperimentElement
extends TmfWithFolderElement
implements IPropertySource2
{
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
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$
64 private static final String sfFolderSuffix
= "_exp"; //$NON-NLS-1$
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
,
71 private static final IPropertyDescriptor
[] sfDescriptors
= { sfNameDescriptor
, sfPathDescriptor
,
72 sfLocationDescriptor
};
75 sfNameDescriptor
.setCategory(sfInfoCategory
);
76 sfPathDescriptor
.setCategory(sfInfoCategory
);
77 sfLocationDescriptor
.setCategory(sfInfoCategory
);
80 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
82 // ------------------------------------------------------------------------
84 // ------------------------------------------------------------------------
87 * @param name The name of the experiment
88 * @param folder The folder reference
89 * @param parent The experiment folder reference.
91 public TmfExperimentElement(String name
, IFolder folder
, TmfExperimentFolder parent
) {
92 super(name
, folder
, parent
);
93 parent
.addChild(this);
96 // ------------------------------------------------------------------------
97 // TmfProjectModelElement
98 // ------------------------------------------------------------------------
101 public IFolder
getResource() {
102 return (IFolder
) fResource
;
106 public TmfProjectElement
getProject() {
107 return (TmfProjectElement
) getParent().getParent();
110 // ------------------------------------------------------------------------
112 // ------------------------------------------------------------------------
115 * Returns a list of TmfTraceElements contained in this experiment.
116 * @return a list of TmfTraceElements
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
);
131 * Adds a trace to the experiment
133 * @param trace The trace element to add
136 public void addTrace(TmfTraceElement trace
) {
138 * Create a link to the actual trace and set the trace type
140 IFolder experiment
= getResource();
141 IResource resource
= trace
.getResource();
142 IPath location
= resource
.getLocation();
143 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
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
);
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
);
157 Activator
.getDefault().logError("Error creating link. Invalid trace location " + location
); //$NON-NLS-1$
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
);
165 Activator
.getDefault().logError("Error creating link. Invalid trace location " + location
); //$NON-NLS-1$
168 } catch (CoreException e
) {
169 Activator
.getDefault().logError("Error creating link to location " + location
, e
); //$NON-NLS-1$
175 * Removes a trace from an experiment
177 * @param trace The trace to remove
178 * @throws CoreException exception
181 public void removeTrace(TmfTraceElement trace
) throws CoreException
{
183 // Close the experiment if open
186 /* Finally, remove the trace from experiment*/
188 trace
.getResource().delete(true, null);
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
);
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.
202 * @return the bookmarks file
203 * @throws CoreException if the bookmarks file cannot be created
206 public IFile
createBookmarksFile() throws CoreException
{
207 IFile file
= getBookmarksFile();
208 if (!file
.exists()) {
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);
214 bookmarksFile
.setHidden(true);
215 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
, null);
216 file
.setHidden(true);
217 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, TmfExperiment
.class.getCanonicalName());
223 * Returns the file resource used to store bookmarks.
224 * The file may not exist.
225 * @return the bookmarks file
228 public IFile
getBookmarksFile() {
229 final IFolder folder
= (IFolder
) fResource
;
230 IFile file
= folder
.getFile(getName() + '_');
234 // ------------------------------------------------------------------------
236 // ------------------------------------------------------------------------
239 public Object
getEditableValue() {
244 public IPropertyDescriptor
[] getPropertyDescriptors() {
245 return Arrays
.copyOf(sfDescriptors
, sfDescriptors
.length
);
249 public Object
getPropertyValue(Object id
) {
251 if (sfName
.equals(id
)) {
255 if (sfPath
.equals(id
)) {
256 return getPath().toString();
259 if (sfLocation
.equals(id
)) {
260 return getLocation().toString();
267 public void resetPropertyValue(Object id
) {
271 public void setPropertyValue(Object id
, Object value
) {
275 public boolean isPropertyResettable(Object id
) {
280 public boolean isPropertySet(Object id
) {
285 * Return the suffix for resource names
286 * @return The folder suffix
289 public String
getSuffix() {
290 return sfFolderSuffix
;
294 * Close open editors associated with this experiment.
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()) {
305 if (editorReference
.getEditorInput().equals(input
)) {
306 wbPage
.closeEditor(editorReference
.getEditor(false), false);
308 } catch (PartInitException e
) {
309 Activator
.getDefault().logError("Error closing editor for experiment " + getName(), e
); //$NON-NLS-1$