2010-10-26 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug309042
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / project / handlers / SelectParserHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.views.project.handlers;
14
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.linuxtools.tmf.event.TmfEvent;
23 import org.eclipse.linuxtools.tmf.experiment.TmfExperiment;
24 import org.eclipse.linuxtools.tmf.signal.TmfExperimentSelectedSignal;
25 import org.eclipse.linuxtools.tmf.signal.TmfTraceParserUpdatedSignal;
26 import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
27 import org.eclipse.linuxtools.tmf.ui.parsers.ParserProviderManager;
28 import org.eclipse.linuxtools.tmf.ui.views.project.ProjectView;
29 import org.eclipse.linuxtools.tmf.ui.views.project.model.TmfExperimentNode;
30 import org.eclipse.linuxtools.tmf.ui.views.project.model.TmfTraceNode;
31 import org.eclipse.ui.IWorkbenchPage;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.PlatformUI;
34
35 /**
36 * <b><u>OpenTraceHandler</u></b>
37 * <p>
38 */
39 public class SelectParserHandler extends AbstractHandler {
40
41 private TmfTraceNode fTrace = null;
42 private ProjectView fProjectView = null;
43
44 // ------------------------------------------------------------------------
45 // Validation
46 // ------------------------------------------------------------------------
47
48 @Override
49 public boolean isEnabled() {
50
51 // Check if we are closing down
52 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
53 if (window == null)
54 return false;
55
56 // Check if a trace is selected
57 IWorkbenchPage page = window.getActivePage();
58 if (!(page.getActivePart() instanceof ProjectView))
59 return false;
60 fProjectView = (ProjectView) page.getActivePart();
61
62 // Check if a trace is selected
63 ISelection selection = page.getSelection(ProjectView.ID);
64 if (selection instanceof StructuredSelection) {
65 Object element = ((StructuredSelection) selection).getFirstElement();
66 fTrace = (element instanceof TmfTraceNode) ? (TmfTraceNode) element : null;
67 }
68
69 return (fTrace != null);
70 }
71
72 // ------------------------------------------------------------------------
73 // Execution
74 // ------------------------------------------------------------------------
75
76 @Override
77 @SuppressWarnings("unchecked")
78 public Object execute(ExecutionEvent event) throws ExecutionException {
79 IResource resource = fTrace.getResource();
80 if (fTrace.getParent() instanceof TmfExperimentNode) {
81 resource = fTrace.getProject().getTracesFolder().getTraceForLocation(resource.getLocation()).getResource();
82 }
83 String parser = event.getParameter("org.eclipse.linuxtools.tmf.ui.commandparameter.project.trace.selectparser.parser");
84 try {
85 resource.setPersistentProperty(ParserProviderManager.PARSER_PROPERTY, parser);
86 fProjectView.broadcast(new TmfTraceParserUpdatedSignal(fProjectView, resource));
87 fTrace.getProject().refresh();
88
89 TmfExperiment<TmfEvent> currentExperiment = (TmfExperiment<TmfEvent>) TmfExperiment.getCurrentExperiment();
90 if (currentExperiment != null) {
91 for (int i = 0; i < currentExperiment.getTraces().length; i++) {
92 ITmfTrace trace = currentExperiment.getTraces()[i];
93 if (resource.getLocation().toOSString().equals(trace.getPath())) {
94 ITmfTrace newTrace = ParserProviderManager.getTrace(resource);
95 if (newTrace != null) {
96 currentExperiment.getTraces()[i] = newTrace;
97 fProjectView.broadcast(new TmfExperimentSelectedSignal<TmfEvent>(new Object(), currentExperiment));
98 }
99 }
100 }
101 }
102 } catch (CoreException e) {
103 e.printStackTrace();
104 }
105 return null;
106 }
107
108 }
This page took 0.031948 seconds and 5 git commands to generate.