tmf: Delete supplementary files when changing trace type
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / SelectTraceTypeHandler.java
CommitLineData
12c155f5 1/*******************************************************************************
ab37ff41 2 * Copyright (c) 2011, 2013 Ericsson
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
12c155f5
FC
12 *******************************************************************************/
13
d34665f9 14package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
12c155f5 15
a94410d9 16import java.util.ArrayList;
12c155f5 17import java.util.Iterator;
a94410d9 18import java.util.List;
12c155f5
FC
19
20import org.eclipse.core.commands.AbstractHandler;
21import org.eclipse.core.commands.ExecutionEvent;
22import org.eclipse.core.commands.ExecutionException;
23import org.eclipse.core.resources.IProject;
24import org.eclipse.core.resources.IResource;
25import org.eclipse.core.runtime.CoreException;
a94410d9
MK
26import org.eclipse.core.runtime.IStatus;
27import org.eclipse.core.runtime.MultiStatus;
28import org.eclipse.core.runtime.Status;
29import org.eclipse.jface.dialogs.ErrorDialog;
12c155f5 30import org.eclipse.jface.viewers.ISelection;
1595249b 31import org.eclipse.jface.viewers.ISelectionProvider;
12c155f5 32import org.eclipse.jface.viewers.TreeSelection;
8fd82db5 33import org.eclipse.linuxtools.internal.tmf.ui.Activator;
e12ecd30 34import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
6c13869b 35import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
12c155f5
FC
36import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement;
37import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder;
38import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
39import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
a94410d9 40import org.eclipse.swt.widgets.Shell;
12c155f5
FC
41import org.eclipse.ui.IWorkbenchPage;
42import org.eclipse.ui.IWorkbenchPart;
43import org.eclipse.ui.IWorkbenchWindow;
44import org.eclipse.ui.PlatformUI;
45
46/**
47 * <b><u>SetTraceTypeHandler</u></b>
48 * <p>
49 */
50public class SelectTraceTypeHandler extends AbstractHandler {
51
52 // ------------------------------------------------------------------------
53 // Constants
54 // ------------------------------------------------------------------------
55
37d150dd
FC
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$
12c155f5
FC
59
60 // ------------------------------------------------------------------------
61 // Attributes
62 // ------------------------------------------------------------------------
63
64 private TreeSelection fSelection = null;
65
66 // ------------------------------------------------------------------------
67 // Validation
68 // ------------------------------------------------------------------------
69
70 @Override
71 public boolean isEnabled() {
72
73 // Check if we are closing down
74 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
6256d8ad 75 if (window == null) {
12c155f5 76 return false;
6256d8ad 77 }
12c155f5
FC
78
79 // Get the selection
80 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
81 IWorkbenchPart part = page.getActivePart();
d5210347
PT
82 if (part == null) {
83 return false;
84 }
1595249b 85 ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
6256d8ad 86 if (selectionProvider == null) {
1595249b 87 return false;
6256d8ad 88 }
1595249b 89 ISelection selection = selectionProvider.getSelection();
12c155f5
FC
90
91 // Make sure selection contains only traces
92 fSelection = null;
93 if (selection instanceof TreeSelection) {
94 fSelection = (TreeSelection) selection;
12c155f5
FC
95 Iterator<Object> iterator = fSelection.iterator();
96 while (iterator.hasNext()) {
97 Object element = iterator.next();
98 if (!(element instanceof TmfTraceElement)) {
99 return false;
100 }
101 }
102 }
103
104 // If we get here, either nothing is selected or everything is a trace
105 return !selection.isEmpty();
106 }
107
108 // ------------------------------------------------------------------------
109 // Execution
110 // ------------------------------------------------------------------------
111
112 @Override
113 public Object execute(ExecutionEvent event) throws ExecutionException {
114
115 // Check if we are closing down
116 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
6256d8ad 117 if (window == null) {
12c155f5 118 return null;
6256d8ad 119 }
a94410d9 120 List<IStatus> statuses = new ArrayList<IStatus>();
12c155f5
FC
121 boolean ok = true;
122 for (Object element : fSelection.toList()) {
123 TmfTraceElement trace = (TmfTraceElement) element;
e12ecd30 124 trace = trace.getElementUnderTraceFolder();
12c155f5
FC
125 IResource resource = trace.getResource();
126 if (resource != null) {
127 try {
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);
4726e963 132 String previousTraceType = trace.getTraceType();
a94410d9
MK
133 IStatus status = propagateProperties(trace, bundleName, traceType, iconUrl);
134 ok &= status.isOK();
4726e963
BH
135
136 if (status.isOK()) {
137 if ((previousTraceType != null) && (!traceType.equals(previousTraceType))) {
138 // Delete all supplementary resources
139 trace.deleteSupplementaryResources();
140 }
141 } else {
a94410d9
MK
142 statuses.add(status);
143 }
12c155f5 144 } catch (CoreException e) {
a94410d9 145 Activator.getDefault().logError(Messages.SelectTraceTypeHandler_ErrorSelectingTrace + trace.getName(), e);
12c155f5
FC
146 }
147 }
148 }
149 ((ITmfProjectModelElement) fSelection.getFirstElement()).getProject().refresh();
150
151 if (!ok) {
a94410d9
MK
152 final Shell shell = window.getShell();
153 MultiStatus info = new MultiStatus(Activator.PLUGIN_ID, 1, Messages.SelectTraceTypeHandler_TraceFailedValidation, null);
154 if (statuses.size() > 1)
155 {
156 info = new MultiStatus(Activator.PLUGIN_ID, 1, Messages.SelectTraceTypeHandler_TracesFailedValidation, null);
157 }
158 for (IStatus status : statuses) {
159 info.add(status);
160 }
161 ErrorDialog.openError(shell, Messages.SelectTraceTypeHandler_Title, Messages.SelectTraceTypeHandler_InvalidTraceType, info);
12c155f5 162 }
12c155f5
FC
163 return null;
164 }
165
a94410d9 166 private static IStatus propagateProperties(TmfTraceElement trace,
abbdd66a
AM
167 String bundleName, String traceType, String iconUrl)
168 throws CoreException {
12c155f5
FC
169
170 IResource svResource = trace.getResource();
e12ecd30
BH
171 String svBundleName = svResource.getPersistentProperty(TmfCommonConstants.TRACEBUNDLE);
172 String svTraceType = svResource.getPersistentProperty(TmfCommonConstants.TRACETYPE);
173 String svIconUrl = svResource.getPersistentProperty(TmfCommonConstants.TRACEICON);
12c155f5
FC
174
175 setProperties(trace.getResource(), bundleName, traceType, iconUrl);
176 trace.refreshTraceType();
a94410d9
MK
177 final IStatus validateTraceType = validateTraceType(trace);
178 if (!validateTraceType.isOK()) {
12c155f5
FC
179 setProperties(trace.getResource(), svBundleName, svTraceType, svIconUrl);
180 trace.refreshTraceType();
a94410d9 181 return validateTraceType;
12c155f5
FC
182 }
183
184 trace.refreshTraceType();
185
186 if (trace.getParent() instanceof TmfTraceFolder) {
187 TmfExperimentFolder experimentFolder = trace.getProject().getExperimentsFolder();
188 for (final ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
189 for (final ITmfProjectModelElement child : experiment.getChildren()) {
190 if (child instanceof TmfTraceElement) {
191 TmfTraceElement linkedTrace = (TmfTraceElement) child;
ab37ff41 192 if (linkedTrace.getName().equals(trace.getName())) {
12c155f5
FC
193 IResource resource = linkedTrace.getResource();
194 setProperties(resource, bundleName, traceType, iconUrl);
195 linkedTrace.refreshTraceType();
196 }
197 }
198 }
199 }
200 }
c50b1d3b 201
a94410d9 202 return Status.OK_STATUS;
12c155f5
FC
203 }
204
abbdd66a
AM
205 private static void setProperties(IResource resource, String bundleName,
206 String traceType, String iconUrl) throws CoreException {
e12ecd30
BH
207 resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, bundleName);
208 resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType);
209 resource.setPersistentProperty(TmfCommonConstants.TRACEICON, iconUrl);
12c155f5
FC
210 }
211
a94410d9 212 private static IStatus validateTraceType(TmfTraceElement trace) {
12c155f5 213 IProject project = trace.getProject().getResource();
6256d8ad 214 ITmfTrace tmfTrace = null;
a94410d9 215 IStatus validate = null;
12c155f5
FC
216 try {
217 tmfTrace = trace.instantiateTrace();
a94410d9
MK
218 if (tmfTrace != null) {
219 validate = tmfTrace.validate(project, trace.getLocation().getPath());
220 }
221 else{
222 validate = new Status(IStatus.ERROR, trace.getName(), "File does not exist : " + trace.getLocation().getPath()); //$NON-NLS-1$
223 }
12c155f5 224 } finally {
6256d8ad 225 if (tmfTrace != null) {
12c155f5 226 tmfTrace.dispose();
6256d8ad 227 }
12c155f5 228 }
a94410d9
MK
229 if (validate == null) {
230 validate = new Status(IStatus.ERROR, "unknown", "unknown"); //$NON-NLS-1$ //$NON-NLS-2$
231 }
232 return validate;
12c155f5
FC
233 }
234
235}
This page took 0.050937 seconds and 5 git commands to generate.