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
.HashSet
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
22 import org
.eclipse
.core
.commands
.AbstractHandler
;
23 import org
.eclipse
.core
.commands
.ExecutionEvent
;
24 import org
.eclipse
.core
.commands
.ExecutionException
;
25 import org
.eclipse
.core
.resources
.IProject
;
26 import org
.eclipse
.core
.resources
.IResource
;
27 import org
.eclipse
.core
.runtime
.CoreException
;
28 import org
.eclipse
.core
.runtime
.IStatus
;
29 import org
.eclipse
.core
.runtime
.MultiStatus
;
30 import org
.eclipse
.core
.runtime
.Status
;
31 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
32 import org
.eclipse
.jface
.viewers
.ISelection
;
33 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
34 import org
.eclipse
.jface
.viewers
.TreeSelection
;
35 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
43 import org
.eclipse
.swt
.widgets
.Shell
;
44 import org
.eclipse
.ui
.IWorkbenchPage
;
45 import org
.eclipse
.ui
.IWorkbenchPart
;
46 import org
.eclipse
.ui
.IWorkbenchWindow
;
47 import org
.eclipse
.ui
.PlatformUI
;
50 * <b><u>SetTraceTypeHandler</u></b>
53 public class SelectTraceTypeHandler
extends AbstractHandler
{
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 private static final String TYPE_PARAMETER
= "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.type"; //$NON-NLS-1$
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
65 private TreeSelection fSelection
= null;
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 public boolean isEnabled() {
74 // Check if we are closing down
75 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
81 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
82 IWorkbenchPart part
= page
.getActivePart();
86 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
87 if (selectionProvider
== null) {
90 ISelection selection
= selectionProvider
.getSelection();
92 // Make sure selection contains only traces
94 if (selection
instanceof TreeSelection
) {
95 fSelection
= (TreeSelection
) selection
;
96 Iterator
<Object
> iterator
= fSelection
.iterator();
97 while (iterator
.hasNext()) {
98 Object element
= iterator
.next();
99 if (!(element
instanceof TmfTraceElement
)) {
105 // If we get here, either nothing is selected or everything is a trace
106 return !selection
.isEmpty();
109 // ------------------------------------------------------------------------
111 // ------------------------------------------------------------------------
114 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
116 // Check if we are closing down
117 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
118 if (window
== null) {
121 List
<IStatus
> statuses
= new ArrayList
<>();
122 Set
<TmfProjectElement
> projects
= new HashSet
<>();
124 for (Object element
: fSelection
.toList()) {
125 TmfTraceElement trace
= (TmfTraceElement
) element
;
126 trace
= trace
.getElementUnderTraceFolder();
127 IResource resource
= trace
.getResource();
128 if (resource
!= null) {
130 // Set the properties for this resource
131 String traceType
= event
.getParameter(TYPE_PARAMETER
);
132 String previousTraceType
= trace
.getTraceType();
133 IStatus status
= propagateProperties(trace
, traceType
);
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 projects
.add(trace
.getProject());
147 } catch (CoreException e
) {
148 Activator
.getDefault().logError(Messages
.SelectTraceTypeHandler_ErrorSelectingTrace
+ trace
.getName(), e
);
153 for (TmfProjectElement project
: projects
) {
158 final Shell shell
= window
.getShell();
159 MultiStatus info
= new MultiStatus(Activator
.PLUGIN_ID
, 1, Messages
.SelectTraceTypeHandler_TraceFailedValidation
, null);
160 if (statuses
.size() > 1)
162 info
= new MultiStatus(Activator
.PLUGIN_ID
, 1, Messages
.SelectTraceTypeHandler_TracesFailedValidation
, null);
164 for (IStatus status
: statuses
) {
167 ErrorDialog
.openError(shell
, Messages
.SelectTraceTypeHandler_Title
, Messages
.SelectTraceTypeHandler_InvalidTraceType
, info
);
172 private static IStatus
propagateProperties(TmfTraceElement trace
, String traceType
)
173 throws CoreException
{
175 IResource svResource
= trace
.getResource();
176 String svTraceType
= svResource
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
);
178 setProperties(trace
.getResource(), traceType
);
179 trace
.refreshTraceType();
180 final IStatus validateTraceType
= validateTraceType(trace
);
181 if (!validateTraceType
.isOK()) {
182 setProperties(trace
.getResource(), svTraceType
);
183 trace
.refreshTraceType();
184 return validateTraceType
;
187 trace
.refreshTraceType();
189 if (trace
.getParent() instanceof TmfTraceFolder
) {
190 TmfExperimentFolder experimentFolder
= trace
.getProject().getExperimentsFolder();
191 for (final ITmfProjectModelElement experiment
: experimentFolder
.getChildren()) {
192 for (final ITmfProjectModelElement child
: experiment
.getChildren()) {
193 if (child
instanceof TmfTraceElement
) {
194 TmfTraceElement linkedTrace
= (TmfTraceElement
) child
;
195 if (linkedTrace
.getName().equals(trace
.getName())) {
196 IResource resource
= linkedTrace
.getResource();
197 setProperties(resource
, traceType
);
198 linkedTrace
.refreshTraceType();
205 return Status
.OK_STATUS
;
208 private static void setProperties(IResource resource
, String traceType
) throws CoreException
{
209 resource
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceType
);
212 private static IStatus
validateTraceType(TmfTraceElement trace
) {
213 IProject project
= trace
.getProject().getResource();
214 ITmfTrace tmfTrace
= null;
215 IStatus validate
= null;
217 tmfTrace
= trace
.instantiateTrace();
218 if (tmfTrace
!= null) {
219 validate
= tmfTrace
.validate(project
, trace
.getLocation().getPath());
222 validate
= new Status(IStatus
.ERROR
, trace
.getName(), "File does not exist : " + trace
.getLocation().getPath()); //$NON-NLS-1$
225 if (tmfTrace
!= null) {
229 if (validate
== null) {
230 validate
= new Status(IStatus
.ERROR
, "unknown", "unknown"); //$NON-NLS-1$ //$NON-NLS-2$