Implement dispose of CTF traces
[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;
6c13869b 32import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
828e5592 33import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
12c155f5 34import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput;
faa38350 35import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
12c155f5 36import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
9c8e0546 37import org.eclipse.swt.widgets.Display;
12c155f5
FC
38import org.eclipse.swt.widgets.MessageBox;
39import org.eclipse.ui.IEditorInput;
40import org.eclipse.ui.IEditorPart;
41import org.eclipse.ui.IReusableEditor;
42import org.eclipse.ui.IWorkbench;
43import org.eclipse.ui.IWorkbenchPage;
44import org.eclipse.ui.IWorkbenchPart;
45import org.eclipse.ui.IWorkbenchWindow;
46import org.eclipse.ui.PartInitException;
47import org.eclipse.ui.PlatformUI;
4ba871ae 48import org.eclipse.ui.ide.IDE;
a1091415 49import org.eclipse.ui.part.FileEditorInput;
12c155f5
FC
50
51/**
52 * <b><u>OpenTraceHandler</u></b>
53 * <p>
54 * TODO: Add support for multiple trace selection
55 */
56public class OpenTraceHandler extends AbstractHandler {
57
828e5592
PT
58 private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
59
12c155f5
FC
60 // ------------------------------------------------------------------------
61 // Attributes
62 // ------------------------------------------------------------------------
63
64 private TmfTraceElement fTrace = null;
65
66 // ------------------------------------------------------------------------
67 // Validation
68 // ------------------------------------------------------------------------
69
70 @Override
71 public boolean isEnabled() {
72
73 // Check if we are closing down
25e48683 74 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
abbdd66a 75 if (window == null) {
12c155f5 76 return false;
abbdd66a 77 }
12c155f5
FC
78
79 // Get the selection
25e48683
FC
80 final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
81 final IWorkbenchPart part = page.getActivePart();
82 final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
abbdd66a 83 if (selectionProvider == null) {
1595249b 84 return false;
abbdd66a 85 }
25e48683 86 final ISelection selection = selectionProvider.getSelection();
12c155f5
FC
87
88 // Make sure there is only one selection and that it is a trace
89 fTrace = null;
90 if (selection instanceof TreeSelection) {
25e48683 91 final TreeSelection sel = (TreeSelection) selection;
12c155f5 92 // There should be only one item selected as per the plugin.xml
25e48683 93 final Object element = sel.getFirstElement();
abbdd66a 94 if (element instanceof TmfTraceElement) {
12c155f5 95 fTrace = (TmfTraceElement) element;
abbdd66a 96 }
12c155f5
FC
97 }
98
99 // We only enable opening from the Traces folder for now
100 return (fTrace != null);
101 }
102
103 // ------------------------------------------------------------------------
104 // Execution
105 // ------------------------------------------------------------------------
106
107 @Override
25e48683 108 public Object execute(final ExecutionEvent event) throws ExecutionException {
12c155f5
FC
109
110 // Check if we are closing down
25e48683 111 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
abbdd66a 112 if (window == null) {
12c155f5 113 return null;
abbdd66a 114 }
12c155f5
FC
115
116 // Check that the trace is valid
abbdd66a 117 if (fTrace == null) {
12c155f5 118 return null;
abbdd66a 119 }
12c155f5 120
828e5592 121 // If trace is under an experiment, use the original trace from the traces folder
9c8e0546
BH
122 final TmfTraceElement traceElement = fTrace.getElementUnderTraceFolder();
123
124 Thread thread = new Thread() {
125 @Override
126 public void run() {
127
128 final ITmfTrace trace = traceElement.instantiateTrace();
129 final ITmfEvent traceEvent = traceElement.instantiateEvent();
130 if ((trace == null) || (traceEvent == null)) {
131 displayErrorMsg(Messages.OpenTraceHandler_NoTraceType);
132 if (trace != null) {
133 trace.dispose();
134 }
135 return;
09d11238 136 }
09d11238 137
9c8e0546
BH
138 // Get the editor_id from the extension point
139 String traceEditorId = traceElement.getEditorId();
140 final String editorId = (traceEditorId != null) ? traceEditorId : TmfEventsEditor.ID;
141
142 try {
143 trace.initTrace(traceElement.getResource(), traceElement.getLocation().getPath(), traceEvent.getClass());
144 } catch (final TmfTraceException e) {
145 displayErrorMsg(Messages.OpenTraceHandler_InitError + "\n\n" + e); //$NON-NLS-1$
146 trace.dispose();
147 return;
abbdd66a 148 }
09d11238 149
9c8e0546
BH
150 final IResource resource = traceElement.getResource();
151 IFile file = null;
faa38350 152 if (resource instanceof IFile) {
9c8e0546
BH
153 file = (IFile) resource;
154 } else if (resource instanceof IFolder) {
155 try {
156 final IFile bookmarksFile = traceElement.getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
157 if (!bookmarksFile.exists()) {
158 final InputStream source = new ByteArrayInputStream(new byte[0]);
159 bookmarksFile.create(source, true, null);
160 }
161 bookmarksFile.setHidden(true);
162
163 final IFolder folder = (IFolder) resource;
164 file = folder.getFile(traceElement.getName() + '_');
165 if (!file.exists()) {
166 file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
167 }
168 file.setHidden(true);
169 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfTrace.class.getCanonicalName());
170 IDE.setDefaultEditor(file, editorId);
8a95ce5a 171 // editor should dispose the trace on close
9c8e0546
BH
172 } catch (final CoreException e) {
173 Activator.getDefault().logError("Error opening trace " + traceElement.getName(), e); //$NON-NLS-1$
174 displayErrorMsg(Messages.OpenTraceHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$
175 trace.dispose();
176 return;
177 }
12c155f5 178 }
9c8e0546
BH
179
180 final IFile editorFile = file;
181 Display.getDefault().asyncExec(new Runnable() {
182 @Override
183 public void run() {
184 try {
185 final IEditorInput editorInput = new TmfEditorInput(editorFile, trace);
186 final IWorkbench wb = PlatformUI.getWorkbench();
187 final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
188
189 final IEditorPart editor = activePage.findEditor(new FileEditorInput(editorFile));
190 if ((editor != null) && (editor instanceof IReusableEditor)) {
191 activePage.reuseEditor((IReusableEditor) editor, editorInput);
192 activePage.activate(editor);
193 } else {
194 activePage.openEditor(editorInput, editorId);
195 if (resource instanceof IFile) {
196 IDE.setDefaultEditor((IFile) resource, editorId);
197 }
198 }
199 } catch (final PartInitException e) {
200 displayErrorMsg(Messages.OpenTraceHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$
201 Activator.getDefault().logError("Error opening trace " + traceElement.getName(), e); //$NON-NLS-1$
202 trace.dispose();
203 }
204 }
205 });
206
12c155f5 207 }
9c8e0546
BH
208 };
209
210 thread.start();
12c155f5
FC
211 return null;
212 }
213
abbdd66a 214 private static void displayErrorMsg(final String errorMsg) {
9c8e0546
BH
215 Display.getDefault().asyncExec(new Runnable() {
216 @Override
217 public void run() {
218 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
219 mb.setText(Messages.OpenTraceHandler_Title);
220 mb.setMessage(errorMsg);
221 mb.open();
222 }
223 });
12c155f5
FC
224 }
225
226}
This page took 0.04556 seconds and 5 git commands to generate.