Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
8a917424 | 2 | * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal |
6256d8ad | 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 | |
6256d8ad | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
ab37ff41 | 11 | * Patrick Tasse - Fix propagation to experiment traces |
8a917424 | 12 | * Geneviève Bastien - Add support of experiment types |
12c155f5 FC |
13 | *******************************************************************************/ |
14 | ||
d34665f9 | 15 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
12c155f5 | 16 | |
a94410d9 | 17 | import java.util.ArrayList; |
f537c959 | 18 | import java.util.HashSet; |
12c155f5 | 19 | import java.util.Iterator; |
a94410d9 | 20 | import java.util.List; |
f537c959 | 21 | import java.util.Set; |
12c155f5 FC |
22 | |
23 | import org.eclipse.core.commands.AbstractHandler; | |
24 | import org.eclipse.core.commands.ExecutionEvent; | |
25 | import org.eclipse.core.commands.ExecutionException; | |
12c155f5 FC |
26 | import org.eclipse.core.resources.IResource; |
27 | import org.eclipse.core.runtime.CoreException; | |
a94410d9 MK |
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; | |
12c155f5 | 32 | import org.eclipse.jface.viewers.ISelection; |
1595249b | 33 | import org.eclipse.jface.viewers.ISelectionProvider; |
12c155f5 | 34 | import org.eclipse.jface.viewers.TreeSelection; |
8fd82db5 | 35 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
a6e37e4c PT |
36 | import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType; |
37 | import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper; | |
8a917424 | 38 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfCommonProjectElement; |
a6e37e4c | 39 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
12c155f5 | 40 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; |
f537c959 | 41 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement; |
12c155f5 | 42 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
a6e37e4c | 43 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceTypeUIUtils; |
a94410d9 | 44 | import org.eclipse.swt.widgets.Shell; |
12c155f5 FC |
45 | import org.eclipse.ui.IWorkbenchPage; |
46 | import org.eclipse.ui.IWorkbenchPart; | |
47 | import org.eclipse.ui.IWorkbenchWindow; | |
48 | import org.eclipse.ui.PlatformUI; | |
49 | ||
50 | /** | |
51 | * <b><u>SetTraceTypeHandler</u></b> | |
52 | * <p> | |
53 | */ | |
54 | public class SelectTraceTypeHandler extends AbstractHandler { | |
55 | ||
56 | // ------------------------------------------------------------------------ | |
57 | // Constants | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
37d150dd | 60 | private static final String TYPE_PARAMETER = "org.eclipse.linuxtools.tmf.ui.commandparameter.select_trace_type.type"; //$NON-NLS-1$ |
12c155f5 FC |
61 | |
62 | // ------------------------------------------------------------------------ | |
63 | // Attributes | |
64 | // ------------------------------------------------------------------------ | |
65 | ||
66 | private TreeSelection fSelection = null; | |
67 | ||
68 | // ------------------------------------------------------------------------ | |
69 | // Validation | |
70 | // ------------------------------------------------------------------------ | |
71 | ||
72 | @Override | |
73 | public boolean isEnabled() { | |
74 | ||
75 | // Check if we are closing down | |
76 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
6256d8ad | 77 | if (window == null) { |
12c155f5 | 78 | return false; |
6256d8ad | 79 | } |
12c155f5 FC |
80 | |
81 | // Get the selection | |
82 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
83 | IWorkbenchPart part = page.getActivePart(); | |
d5210347 PT |
84 | if (part == null) { |
85 | return false; | |
86 | } | |
1595249b | 87 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
6256d8ad | 88 | if (selectionProvider == null) { |
1595249b | 89 | return false; |
6256d8ad | 90 | } |
1595249b | 91 | ISelection selection = selectionProvider.getSelection(); |
12c155f5 FC |
92 | |
93 | // Make sure selection contains only traces | |
94 | fSelection = null; | |
95 | if (selection instanceof TreeSelection) { | |
96 | fSelection = (TreeSelection) selection; | |
12c155f5 FC |
97 | Iterator<Object> iterator = fSelection.iterator(); |
98 | while (iterator.hasNext()) { | |
99 | Object element = iterator.next(); | |
8a917424 | 100 | if (!(element instanceof TmfCommonProjectElement)) { |
12c155f5 FC |
101 | return false; |
102 | } | |
103 | } | |
104 | } | |
105 | ||
106 | // If we get here, either nothing is selected or everything is a trace | |
107 | return !selection.isEmpty(); | |
108 | } | |
109 | ||
110 | // ------------------------------------------------------------------------ | |
111 | // Execution | |
112 | // ------------------------------------------------------------------------ | |
113 | ||
114 | @Override | |
115 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
116 | ||
117 | // Check if we are closing down | |
118 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
6256d8ad | 119 | if (window == null) { |
12c155f5 | 120 | return null; |
6256d8ad | 121 | } |
507b1336 | 122 | List<IStatus> statuses = new ArrayList<>(); |
f537c959 | 123 | Set<TmfProjectElement> projects = new HashSet<>(); |
12c155f5 FC |
124 | boolean ok = true; |
125 | for (Object element : fSelection.toList()) { | |
8a917424 GB |
126 | TmfCommonProjectElement trace = (TmfCommonProjectElement) element; |
127 | if (trace instanceof TmfTraceElement) { | |
128 | trace = ((TmfTraceElement) trace).getElementUnderTraceFolder(); | |
129 | } | |
12c155f5 FC |
130 | IResource resource = trace.getResource(); |
131 | if (resource != null) { | |
132 | try { | |
a6e37e4c | 133 | // Set the trace type for this resource |
12c155f5 | 134 | String traceType = event.getParameter(TYPE_PARAMETER); |
4726e963 | 135 | String previousTraceType = trace.getTraceType(); |
2d64a788 | 136 | IStatus status = propagateProperties(trace, traceType); |
a94410d9 | 137 | ok &= status.isOK(); |
4726e963 BH |
138 | |
139 | if (status.isOK()) { | |
140 | if ((previousTraceType != null) && (!traceType.equals(previousTraceType))) { | |
a72a6830 PT |
141 | // Close the trace if open |
142 | trace.closeEditors(); | |
4726e963 BH |
143 | // Delete all supplementary resources |
144 | trace.deleteSupplementaryResources(); | |
145 | } | |
146 | } else { | |
a94410d9 MK |
147 | statuses.add(status); |
148 | } | |
f537c959 | 149 | projects.add(trace.getProject()); |
12c155f5 | 150 | } catch (CoreException e) { |
a94410d9 | 151 | Activator.getDefault().logError(Messages.SelectTraceTypeHandler_ErrorSelectingTrace + trace.getName(), e); |
12c155f5 FC |
152 | } |
153 | } | |
f537c959 PT |
154 | trace.getProject(); |
155 | } | |
156 | for (TmfProjectElement project : projects) { | |
157 | project.refresh(); | |
12c155f5 | 158 | } |
12c155f5 FC |
159 | |
160 | if (!ok) { | |
a94410d9 MK |
161 | final Shell shell = window.getShell(); |
162 | MultiStatus info = new MultiStatus(Activator.PLUGIN_ID, 1, Messages.SelectTraceTypeHandler_TraceFailedValidation, null); | |
163 | if (statuses.size() > 1) | |
164 | { | |
165 | info = new MultiStatus(Activator.PLUGIN_ID, 1, Messages.SelectTraceTypeHandler_TracesFailedValidation, null); | |
166 | } | |
167 | for (IStatus status : statuses) { | |
168 | info.add(status); | |
169 | } | |
170 | ErrorDialog.openError(shell, Messages.SelectTraceTypeHandler_Title, Messages.SelectTraceTypeHandler_InvalidTraceType, info); | |
12c155f5 | 171 | } |
12c155f5 FC |
172 | return null; |
173 | } | |
174 | ||
8a917424 | 175 | private static IStatus propagateProperties(TmfCommonProjectElement element, String traceType) |
abbdd66a | 176 | throws CoreException { |
12c155f5 | 177 | |
a4a116c3 | 178 | TraceTypeHelper traceTypeHelper = TmfTraceType.getTraceType(traceType); |
0621dbae PT |
179 | if (traceTypeHelper == null) { |
180 | return Status.CANCEL_STATUS; | |
181 | } | |
f06ca6d0 | 182 | final IStatus validateTraceType = traceTypeHelper.validate(element.getResource().getLocation().toOSString()); |
a94410d9 | 183 | if (!validateTraceType.isOK()) { |
a94410d9 | 184 | return validateTraceType; |
12c155f5 FC |
185 | } |
186 | ||
a8ddd783 GB |
187 | IResource resource = element.getResource(); |
188 | TmfTraceTypeUIUtils.setTraceType(resource, traceTypeHelper); | |
189 | ||
8a917424 | 190 | TmfExperimentFolder experimentFolder = element.getProject().getExperimentsFolder(); |
a6e37e4c PT |
191 | for (final TmfExperimentElement experiment : experimentFolder.getExperiments()) { |
192 | for (final TmfTraceElement child : experiment.getTraces()) { | |
8a917424 | 193 | if (child.getName().equals(element.getName())) { |
a6e37e4c PT |
194 | TmfTraceTypeUIUtils.setTraceType(child.getResource(), traceTypeHelper); |
195 | break; | |
12c155f5 FC |
196 | } |
197 | } | |
198 | } | |
c50b1d3b | 199 | |
a94410d9 | 200 | return Status.OK_STATUS; |
12c155f5 FC |
201 | } |
202 | ||
12c155f5 | 203 | } |