tmf: Drop generics from ITmfTrace and TmfExperiment
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / OpenTraceHandler.java
CommitLineData
12c155f5
FC
1/*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011 Ericsson
ce2388e0 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
ce2388e0 8 *
12c155f5
FC
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
d34665f9 13package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
12c155f5 14
828e5592 15import java.io.ByteArrayInputStream;
828e5592 16import java.io.InputStream;
12c155f5
FC
17
18import org.eclipse.core.commands.AbstractHandler;
19import org.eclipse.core.commands.ExecutionEvent;
20import org.eclipse.core.commands.ExecutionException;
4ba871ae 21import org.eclipse.core.resources.IFile;
828e5592 22import org.eclipse.core.resources.IFolder;
12c155f5 23import org.eclipse.core.resources.IResource;
828e5592 24import org.eclipse.core.runtime.CoreException;
12c155f5 25import org.eclipse.jface.viewers.ISelection;
1595249b 26import org.eclipse.jface.viewers.ISelectionProvider;
12c155f5 27import org.eclipse.jface.viewers.TreeSelection;
8fd82db5 28import org.eclipse.linuxtools.internal.tmf.ui.Activator;
e12ecd30 29import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
ce2388e0 30import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
b4f71e4a 31import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
09d11238
PT
32import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
33import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
6c13869b 34import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
9e0640dc 35import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
828e5592 36import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
09d11238 37import org.eclipse.linuxtools.tmf.ui.editors.EventsViewEditor;
12c155f5
FC
38import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput;
39import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
40import org.eclipse.swt.widgets.MessageBox;
41import org.eclipse.ui.IEditorInput;
42import org.eclipse.ui.IEditorPart;
43import org.eclipse.ui.IReusableEditor;
44import org.eclipse.ui.IWorkbench;
45import org.eclipse.ui.IWorkbenchPage;
46import org.eclipse.ui.IWorkbenchPart;
47import org.eclipse.ui.IWorkbenchWindow;
48import org.eclipse.ui.PartInitException;
49import org.eclipse.ui.PlatformUI;
4ba871ae 50import org.eclipse.ui.ide.IDE;
a1091415 51import org.eclipse.ui.part.FileEditorInput;
12c155f5
FC
52
53/**
54 * <b><u>OpenTraceHandler</u></b>
55 * <p>
56 * TODO: Add support for multiple trace selection
57 */
58public class OpenTraceHandler extends AbstractHandler {
59
828e5592
PT
60 private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
61
12c155f5
FC
62 // ------------------------------------------------------------------------
63 // Attributes
64 // ------------------------------------------------------------------------
65
66 private TmfTraceElement fTrace = null;
67
68 // ------------------------------------------------------------------------
69 // Validation
70 // ------------------------------------------------------------------------
71
72 @Override
73 public boolean isEnabled() {
74
75 // Check if we are closing down
25e48683 76 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
abbdd66a 77 if (window == null) {
12c155f5 78 return false;
abbdd66a 79 }
12c155f5
FC
80
81 // Get the selection
25e48683
FC
82 final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
83 final IWorkbenchPart part = page.getActivePart();
84 final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
abbdd66a 85 if (selectionProvider == null) {
1595249b 86 return false;
abbdd66a 87 }
25e48683 88 final ISelection selection = selectionProvider.getSelection();
12c155f5
FC
89
90 // Make sure there is only one selection and that it is a trace
91 fTrace = null;
92 if (selection instanceof TreeSelection) {
25e48683 93 final TreeSelection sel = (TreeSelection) selection;
12c155f5 94 // There should be only one item selected as per the plugin.xml
25e48683 95 final Object element = sel.getFirstElement();
abbdd66a 96 if (element instanceof TmfTraceElement) {
12c155f5 97 fTrace = (TmfTraceElement) element;
abbdd66a 98 }
12c155f5
FC
99 }
100
101 // We only enable opening from the Traces folder for now
102 return (fTrace != null);
103 }
104
105 // ------------------------------------------------------------------------
106 // Execution
107 // ------------------------------------------------------------------------
108
109 @Override
25e48683 110 public Object execute(final ExecutionEvent event) throws ExecutionException {
12c155f5
FC
111
112 // Check if we are closing down
25e48683 113 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
abbdd66a 114 if (window == null) {
12c155f5 115 return null;
abbdd66a 116 }
12c155f5
FC
117
118 // Check that the trace is valid
abbdd66a 119 if (fTrace == null) {
12c155f5 120 return null;
abbdd66a 121 }
12c155f5 122
828e5592 123 // If trace is under an experiment, use the original trace from the traces folder
e12ecd30 124 fTrace = fTrace.getElementUnderTraceFolder();
828e5592 125
25e48683
FC
126 final ITmfTrace trace = fTrace.instantiateTrace();
127 final ITmfEvent traceEvent = fTrace.instantiateEvent();
ce2388e0 128 if ((trace == null) || (traceEvent == null)) {
12c155f5
FC
129 displayErrorMsg(Messages.OpenTraceHandler_NoTraceType);
130 return null;
131 }
132
133 // Get the editor_id from the extension point
25e48683
FC
134 final String editorId = fTrace.getEditorId();
135 final boolean usesEditor = (editorId != null) && (editorId.length() > 0);
12c155f5
FC
136
137 try {
25e48683 138 trace.initTrace(fTrace.getResource(), fTrace.getLocation().getPath(), traceEvent.getClass());
b4f71e4a 139 } catch (final TmfTraceException e) {
e12ecd30 140 displayErrorMsg(Messages.OpenTraceHandler_NoTrace + "\n\n" + e); //$NON-NLS-1$
12c155f5
FC
141 return null;
142 }
143
25e48683 144 final IResource resource = fTrace.getResource();
09d11238 145 IFile file = null;
abbdd66a 146 if (resource instanceof IFile) {
09d11238 147 file = (IFile) resource;
abbdd66a 148 } else if (resource instanceof IFolder) {
09d11238 149 try {
25e48683 150 final IFile bookmarksFile = fTrace.getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
09d11238 151 if (!bookmarksFile.exists()) {
25e48683 152 final InputStream source = new ByteArrayInputStream(new byte[0]);
09d11238
PT
153 bookmarksFile.create(source, true, null);
154 }
155 bookmarksFile.setHidden(true);
156
25e48683 157 final IFolder folder = (IFolder) resource;
09d11238 158 file = folder.getFile(fTrace.getName() + '_');
abbdd66a 159 if (!file.exists()) {
09d11238 160 file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
abbdd66a 161 }
09d11238 162 file.setHidden(true);
abbdd66a 163 if (usesEditor) {
e12ecd30 164 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, fTrace.getTraceType());
abbdd66a 165 } else {
e12ecd30 166 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfTrace.class.getCanonicalName());
abbdd66a 167 }
25e48683 168 } catch (final CoreException e) {
8fd82db5 169 Activator.getDefault().logError("Error opening trace " + fTrace.getName(), e); //$NON-NLS-1$
09d11238 170 }
abbdd66a 171 }
09d11238 172
abbdd66a 173 if (usesEditor) {
12c155f5 174 try {
25e48683
FC
175 final IEditorInput editorInput = new TmfEditorInput(file, trace);
176 final IWorkbench wb = PlatformUI.getWorkbench();
177 final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
12c155f5 178
25e48683 179 final IEditorPart editor = activePage.findEditor(new FileEditorInput(file));
ce2388e0 180 if ((editor != null) && (editor instanceof IReusableEditor)) {
12c155f5
FC
181 activePage.reuseEditor((IReusableEditor) editor, editorInput);
182 activePage.activate(editor);
183 } else {
5a5c2fc7 184 activePage.openEditor(editorInput, editorId);
abbdd66a 185 if (resource instanceof IFile) {
4ba871ae 186 IDE.setDefaultEditor((IFile) resource, editorId);
abbdd66a 187 }
12c155f5 188 }
25e48683 189 } catch (final PartInitException e) {
8fd82db5 190 Activator.getDefault().logError("Error opening trace " + fTrace.getName(), e); //$NON-NLS-1$
12c155f5 191 }
abbdd66a 192 } else {
09d11238 193 // Create the experiment
25e48683 194 final ITmfTrace[] traces = new ITmfTrace[] { trace };
20658947 195 final TmfExperiment experiment = new TmfExperiment(traceEvent.getClass(), fTrace.getName(), traces, trace.getCacheSize());
a1091415 196 experiment.setBookmarksFile(file);
ce2388e0 197
09d11238
PT
198 TmfExperiment.setCurrentExperiment(experiment);
199 TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment));
200 IDE.setDefaultEditor(file, EventsViewEditor.ID);
12c155f5
FC
201 }
202 return null;
203 }
204
abbdd66a 205 private static void displayErrorMsg(final String errorMsg) {
25e48683 206 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
12c155f5
FC
207 mb.setText(Messages.OpenTraceHandler_Title);
208 mb.setMessage(errorMsg);
209 mb.open();
210 }
211
212}
This page took 0.051059 seconds and 5 git commands to generate.