1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson
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 * Patrick Tasse - Fix propagation to experiment traces
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.handlers
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Iterator
;
18 import java
.util
.List
;
20 import org
.eclipse
.core
.commands
.AbstractHandler
;
21 import org
.eclipse
.core
.commands
.ExecutionEvent
;
22 import org
.eclipse
.core
.commands
.ExecutionException
;
23 import org
.eclipse
.core
.resources
.IProject
;
24 import org
.eclipse
.core
.resources
.IResource
;
25 import org
.eclipse
.core
.runtime
.CoreException
;
26 import org
.eclipse
.core
.runtime
.IStatus
;
27 import org
.eclipse
.core
.runtime
.MultiStatus
;
28 import org
.eclipse
.core
.runtime
.Status
;
29 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
30 import org
.eclipse
.jface
.viewers
.ISelection
;
31 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
32 import org
.eclipse
.jface
.viewers
.TreeSelection
;
33 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
40 import org
.eclipse
.swt
.widgets
.Shell
;
41 import org
.eclipse
.ui
.IWorkbenchPage
;
42 import org
.eclipse
.ui
.IWorkbenchPart
;
43 import org
.eclipse
.ui
.IWorkbenchWindow
;
44 import org
.eclipse
.ui
.PlatformUI
;
47 * <b><u>SetTraceTypeHandler</u></b>
50 public class SelectTraceTypeHandler
extends AbstractHandler
{
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
56 private static final String BUNDLE_PARAMETER
= "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.bundle"; //$NON-NLS-1$
57 private static final String TYPE_PARAMETER
= "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.type"; //$NON-NLS-1$
58 private static final String ICON_PARAMETER
= "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.icon"; //$NON-NLS-1$
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
64 private TreeSelection fSelection
= null;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
71 public boolean isEnabled() {
73 // Check if we are closing down
74 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
80 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
81 IWorkbenchPart part
= page
.getActivePart();
85 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
86 if (selectionProvider
== null) {
89 ISelection selection
= selectionProvider
.getSelection();
91 // Make sure selection contains only traces
93 if (selection
instanceof TreeSelection
) {
94 fSelection
= (TreeSelection
) selection
;
95 Iterator
<Object
> iterator
= fSelection
.iterator();
96 while (iterator
.hasNext()) {
97 Object element
= iterator
.next();
98 if (!(element
instanceof TmfTraceElement
)) {
104 // If we get here, either nothing is selected or everything is a trace
105 return !selection
.isEmpty();
108 // ------------------------------------------------------------------------
110 // ------------------------------------------------------------------------
113 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
115 // Check if we are closing down
116 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
117 if (window
== null) {
120 List
<IStatus
> statuses
= new ArrayList
<>();
122 for (Object element
: fSelection
.toList()) {
123 TmfTraceElement trace
= (TmfTraceElement
) element
;
124 trace
= trace
.getElementUnderTraceFolder();
125 IResource resource
= trace
.getResource();
126 if (resource
!= null) {
128 // Set the properties for this resource
129 String bundleName
= event
.getParameter(BUNDLE_PARAMETER
);
130 String traceType
= event
.getParameter(TYPE_PARAMETER
);
131 String iconUrl
= event
.getParameter(ICON_PARAMETER
);
132 String previousTraceType
= trace
.getTraceType();
133 IStatus status
= propagateProperties(trace
, bundleName
, traceType
, iconUrl
);
137 if ((previousTraceType
!= null) && (!traceType
.equals(previousTraceType
))) {
138 // Close the trace if open
139 trace
.closeEditors();
140 // Delete all supplementary resources
141 trace
.deleteSupplementaryResources();
144 statuses
.add(status
);
146 } catch (CoreException e
) {
147 Activator
.getDefault().logError(Messages
.SelectTraceTypeHandler_ErrorSelectingTrace
+ trace
.getName(), e
);
151 ((ITmfProjectModelElement
) fSelection
.getFirstElement()).getProject().refresh();
154 final Shell shell
= window
.getShell();
155 MultiStatus info
= new MultiStatus(Activator
.PLUGIN_ID
, 1, Messages
.SelectTraceTypeHandler_TraceFailedValidation
, null);
156 if (statuses
.size() > 1)
158 info
= new MultiStatus(Activator
.PLUGIN_ID
, 1, Messages
.SelectTraceTypeHandler_TracesFailedValidation
, null);
160 for (IStatus status
: statuses
) {
163 ErrorDialog
.openError(shell
, Messages
.SelectTraceTypeHandler_Title
, Messages
.SelectTraceTypeHandler_InvalidTraceType
, info
);
168 private static IStatus
propagateProperties(TmfTraceElement trace
,
169 String bundleName
, String traceType
, String iconUrl
)
170 throws CoreException
{
172 IResource svResource
= trace
.getResource();
173 String svBundleName
= svResource
.getPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
);
174 String svTraceType
= svResource
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
175 String svIconUrl
= svResource
.getPersistentProperty(TmfCommonConstants
.TRACEICON
);
177 setProperties(trace
.getResource(), bundleName
, traceType
, iconUrl
);
178 trace
.refreshTraceType();
179 final IStatus validateTraceType
= validateTraceType(trace
);
180 if (!validateTraceType
.isOK()) {
181 setProperties(trace
.getResource(), svBundleName
, svTraceType
, svIconUrl
);
182 trace
.refreshTraceType();
183 return validateTraceType
;
186 trace
.refreshTraceType();
188 if (trace
.getParent() instanceof TmfTraceFolder
) {
189 TmfExperimentFolder experimentFolder
= trace
.getProject().getExperimentsFolder();
190 for (final ITmfProjectModelElement experiment
: experimentFolder
.getChildren()) {
191 for (final ITmfProjectModelElement child
: experiment
.getChildren()) {
192 if (child
instanceof TmfTraceElement
) {
193 TmfTraceElement linkedTrace
= (TmfTraceElement
) child
;
194 if (linkedTrace
.getName().equals(trace
.getName())) {
195 IResource resource
= linkedTrace
.getResource();
196 setProperties(resource
, bundleName
, traceType
, iconUrl
);
197 linkedTrace
.refreshTraceType();
204 return Status
.OK_STATUS
;
207 private static void setProperties(IResource resource
, String bundleName
,
208 String traceType
, String iconUrl
) throws CoreException
{
209 resource
.setPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
, bundleName
);
210 resource
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceType
);
211 resource
.setPersistentProperty(TmfCommonConstants
.TRACEICON
, iconUrl
);
214 private static IStatus
validateTraceType(TmfTraceElement trace
) {
215 IProject project
= trace
.getProject().getResource();
216 ITmfTrace tmfTrace
= null;
217 IStatus validate
= null;
219 tmfTrace
= trace
.instantiateTrace();
220 if (tmfTrace
!= null) {
221 validate
= tmfTrace
.validate(project
, trace
.getLocation().getPath());
224 validate
= new Status(IStatus
.ERROR
, trace
.getName(), "File does not exist : " + trace
.getLocation().getPath()); //$NON-NLS-1$
227 if (tmfTrace
!= null) {
231 if (validate
== null) {
232 validate
= new Status(IStatus
.ERROR
, "unknown", "unknown"); //$NON-NLS-1$ //$NON-NLS-2$