tmf: Switch tmf.ui to Java 7 + fix warnings
[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 }
507b1336 120 List<IStatus> statuses = new ArrayList<>();
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))) {
a72a6830
PT
138 // Close the trace if open
139 trace.closeEditors();
4726e963
BH
140 // Delete all supplementary resources
141 trace.deleteSupplementaryResources();
142 }
143 } else {
a94410d9
MK
144 statuses.add(status);
145 }
12c155f5 146 } catch (CoreException e) {
a94410d9 147 Activator.getDefault().logError(Messages.SelectTraceTypeHandler_ErrorSelectingTrace + trace.getName(), e);
12c155f5
FC
148 }
149 }
150 }
151 ((ITmfProjectModelElement) fSelection.getFirstElement()).getProject().refresh();
152
153 if (!ok) {
a94410d9
MK
154 final Shell shell = window.getShell();
155 MultiStatus info = new MultiStatus(Activator.PLUGIN_ID, 1, Messages.SelectTraceTypeHandler_TraceFailedValidation, null);
156 if (statuses.size() > 1)
157 {
158 info = new MultiStatus(Activator.PLUGIN_ID, 1, Messages.SelectTraceTypeHandler_TracesFailedValidation, null);
159 }
160 for (IStatus status : statuses) {
161 info.add(status);
162 }
163 ErrorDialog.openError(shell, Messages.SelectTraceTypeHandler_Title, Messages.SelectTraceTypeHandler_InvalidTraceType, info);
12c155f5 164 }
12c155f5
FC
165 return null;
166 }
167
a94410d9 168 private static IStatus propagateProperties(TmfTraceElement trace,
abbdd66a
AM
169 String bundleName, String traceType, String iconUrl)
170 throws CoreException {
12c155f5
FC
171
172 IResource svResource = trace.getResource();
e12ecd30
BH
173 String svBundleName = svResource.getPersistentProperty(TmfCommonConstants.TRACEBUNDLE);
174 String svTraceType = svResource.getPersistentProperty(TmfCommonConstants.TRACETYPE);
175 String svIconUrl = svResource.getPersistentProperty(TmfCommonConstants.TRACEICON);
12c155f5
FC
176
177 setProperties(trace.getResource(), bundleName, traceType, iconUrl);
178 trace.refreshTraceType();
a94410d9
MK
179 final IStatus validateTraceType = validateTraceType(trace);
180 if (!validateTraceType.isOK()) {
12c155f5
FC
181 setProperties(trace.getResource(), svBundleName, svTraceType, svIconUrl);
182 trace.refreshTraceType();
a94410d9 183 return validateTraceType;
12c155f5
FC
184 }
185
186 trace.refreshTraceType();
187
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;
ab37ff41 194 if (linkedTrace.getName().equals(trace.getName())) {
12c155f5
FC
195 IResource resource = linkedTrace.getResource();
196 setProperties(resource, bundleName, traceType, iconUrl);
197 linkedTrace.refreshTraceType();
198 }
199 }
200 }
201 }
202 }
c50b1d3b 203
a94410d9 204 return Status.OK_STATUS;
12c155f5
FC
205 }
206
abbdd66a
AM
207 private static void setProperties(IResource resource, String bundleName,
208 String traceType, String iconUrl) throws CoreException {
e12ecd30
BH
209 resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, bundleName);
210 resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType);
211 resource.setPersistentProperty(TmfCommonConstants.TRACEICON, iconUrl);
12c155f5
FC
212 }
213
a94410d9 214 private static IStatus validateTraceType(TmfTraceElement trace) {
12c155f5 215 IProject project = trace.getProject().getResource();
6256d8ad 216 ITmfTrace tmfTrace = null;
a94410d9 217 IStatus validate = null;
12c155f5
FC
218 try {
219 tmfTrace = trace.instantiateTrace();
a94410d9
MK
220 if (tmfTrace != null) {
221 validate = tmfTrace.validate(project, trace.getLocation().getPath());
222 }
223 else{
224 validate = new Status(IStatus.ERROR, trace.getName(), "File does not exist : " + trace.getLocation().getPath()); //$NON-NLS-1$
225 }
12c155f5 226 } finally {
6256d8ad 227 if (tmfTrace != null) {
12c155f5 228 tmfTrace.dispose();
6256d8ad 229 }
12c155f5 230 }
a94410d9
MK
231 if (validate == null) {
232 validate = new Status(IStatus.ERROR, "unknown", "unknown"); //$NON-NLS-1$ //$NON-NLS-2$
233 }
234 return validate;
12c155f5
FC
235 }
236
237}
This page took 0.062062 seconds and 5 git commands to generate.