[Tmf][Ctf] Add descriptive fail to import messages.
[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);
a94410d9
MK
132 IStatus status = propagateProperties(trace, bundleName, traceType, iconUrl);
133 ok &= status.isOK();
134 if (!status.isOK()) {
135 statuses.add(status);
136 }
12c155f5 137 } catch (CoreException e) {
a94410d9 138 Activator.getDefault().logError(Messages.SelectTraceTypeHandler_ErrorSelectingTrace + trace.getName(), e);
12c155f5
FC
139 }
140 }
141 }
142 ((ITmfProjectModelElement) fSelection.getFirstElement()).getProject().refresh();
143
144 if (!ok) {
a94410d9
MK
145 final Shell shell = window.getShell();
146 MultiStatus info = new MultiStatus(Activator.PLUGIN_ID, 1, Messages.SelectTraceTypeHandler_TraceFailedValidation, null);
147 if (statuses.size() > 1)
148 {
149 info = new MultiStatus(Activator.PLUGIN_ID, 1, Messages.SelectTraceTypeHandler_TracesFailedValidation, null);
150 }
151 for (IStatus status : statuses) {
152 info.add(status);
153 }
154 ErrorDialog.openError(shell, Messages.SelectTraceTypeHandler_Title, Messages.SelectTraceTypeHandler_InvalidTraceType, info);
12c155f5 155 }
12c155f5
FC
156 return null;
157 }
158
a94410d9 159 private static IStatus propagateProperties(TmfTraceElement trace,
abbdd66a
AM
160 String bundleName, String traceType, String iconUrl)
161 throws CoreException {
12c155f5
FC
162
163 IResource svResource = trace.getResource();
e12ecd30
BH
164 String svBundleName = svResource.getPersistentProperty(TmfCommonConstants.TRACEBUNDLE);
165 String svTraceType = svResource.getPersistentProperty(TmfCommonConstants.TRACETYPE);
166 String svIconUrl = svResource.getPersistentProperty(TmfCommonConstants.TRACEICON);
12c155f5
FC
167
168 setProperties(trace.getResource(), bundleName, traceType, iconUrl);
169 trace.refreshTraceType();
a94410d9
MK
170 final IStatus validateTraceType = validateTraceType(trace);
171 if (!validateTraceType.isOK()) {
12c155f5
FC
172 setProperties(trace.getResource(), svBundleName, svTraceType, svIconUrl);
173 trace.refreshTraceType();
a94410d9 174 return validateTraceType;
12c155f5
FC
175 }
176
177 trace.refreshTraceType();
178
179 if (trace.getParent() instanceof TmfTraceFolder) {
180 TmfExperimentFolder experimentFolder = trace.getProject().getExperimentsFolder();
181 for (final ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
182 for (final ITmfProjectModelElement child : experiment.getChildren()) {
183 if (child instanceof TmfTraceElement) {
184 TmfTraceElement linkedTrace = (TmfTraceElement) child;
ab37ff41 185 if (linkedTrace.getName().equals(trace.getName())) {
12c155f5
FC
186 IResource resource = linkedTrace.getResource();
187 setProperties(resource, bundleName, traceType, iconUrl);
188 linkedTrace.refreshTraceType();
189 }
190 }
191 }
192 }
193 }
c50b1d3b 194
a94410d9 195 return Status.OK_STATUS;
12c155f5
FC
196 }
197
abbdd66a
AM
198 private static void setProperties(IResource resource, String bundleName,
199 String traceType, String iconUrl) throws CoreException {
e12ecd30
BH
200 resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, bundleName);
201 resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType);
202 resource.setPersistentProperty(TmfCommonConstants.TRACEICON, iconUrl);
12c155f5
FC
203 }
204
a94410d9 205 private static IStatus validateTraceType(TmfTraceElement trace) {
12c155f5 206 IProject project = trace.getProject().getResource();
6256d8ad 207 ITmfTrace tmfTrace = null;
a94410d9 208 IStatus validate = null;
12c155f5
FC
209 try {
210 tmfTrace = trace.instantiateTrace();
a94410d9
MK
211 if (tmfTrace != null) {
212 validate = tmfTrace.validate(project, trace.getLocation().getPath());
213 }
214 else{
215 validate = new Status(IStatus.ERROR, trace.getName(), "File does not exist : " + trace.getLocation().getPath()); //$NON-NLS-1$
216 }
12c155f5 217 } finally {
6256d8ad 218 if (tmfTrace != null) {
12c155f5 219 tmfTrace.dispose();
6256d8ad 220 }
12c155f5 221 }
a94410d9
MK
222 if (validate == null) {
223 validate = new Status(IStatus.ERROR, "unknown", "unknown"); //$NON-NLS-1$ //$NON-NLS-2$
224 }
225 return validate;
12c155f5
FC
226 }
227
228}
This page took 0.050959 seconds and 5 git commands to generate.