/*******************************************************************************
- * Copyright (c) 2009, 2010, 2011 Ericsson
- *
+ * Copyright (c) 2009, 2013 Ericsson
+ *
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
- *
+ *
* Contributors:
* Francois Chouinard - Initial API and implementation
*******************************************************************************/
package org.eclipse.linuxtools.internal.tmf.ui.project.handlers;
-import java.io.ByteArrayInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
-
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.TreeSelection;
-import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
-import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment;
-import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
-import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
+import org.eclipse.linuxtools.internal.tmf.ui.Activator;
+import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
+import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
-import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
-import org.eclipse.linuxtools.tmf.ui.editors.EventsViewEditor;
import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput;
-import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
+import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
+import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
*/
public class OpenTraceHandler extends AbstractHandler {
- private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
-
// ------------------------------------------------------------------------
// Attributes
// ------------------------------------------------------------------------
public boolean isEnabled() {
// Check if we are closing down
- IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
- if (window == null)
+ final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if (window == null) {
return false;
+ }
// Get the selection
- IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
- IWorkbenchPart part = page.getActivePart();
- ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
- if (selectionProvider == null)
+ final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+ final IWorkbenchPart part = page.getActivePart();
+ if (part == null) {
return false;
- ISelection selection = selectionProvider.getSelection();
+ }
+ final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
+ if (selectionProvider == null) {
+ return false;
+ }
+ final ISelection selection = selectionProvider.getSelection();
// Make sure there is only one selection and that it is a trace
fTrace = null;
if (selection instanceof TreeSelection) {
- TreeSelection sel = (TreeSelection) selection;
+ final TreeSelection sel = (TreeSelection) selection;
// There should be only one item selected as per the plugin.xml
- Object element = sel.getFirstElement();
+ final Object element = sel.getFirstElement();
if (element instanceof TmfTraceElement) {
fTrace = (TmfTraceElement) element;
}
// ------------------------------------------------------------------------
@Override
- @SuppressWarnings({ "rawtypes", "unchecked" })
- public Object execute(ExecutionEvent event) throws ExecutionException {
+ public Object execute(final ExecutionEvent event) throws ExecutionException {
// Check if we are closing down
- IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
- if (window == null)
+ final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if (window == null) {
return null;
+ }
// Check that the trace is valid
if (fTrace == null) {
}
// If trace is under an experiment, use the original trace from the traces folder
- if (fTrace.getParent() instanceof TmfExperimentElement) {
- for (TmfTraceElement trace : fTrace.getProject().getTracesFolder().getTraces()) {
- if (trace.getName().equals(fTrace.getName())) {
- fTrace = trace;
- break;
+ final TmfTraceElement traceElement = fTrace.getElementUnderTraceFolder();
+
+ Thread thread = new Thread() {
+ @Override
+ public void run() {
+
+ final ITmfTrace trace = traceElement.instantiateTrace();
+ final ITmfEvent traceEvent = traceElement.instantiateEvent();
+ if ((trace == null) || (traceEvent == null)) {
+ displayErrorMsg(Messages.OpenTraceHandler_NoTraceType);
+ if (trace != null) {
+ trace.dispose();
+ }
+ return;
}
- }
- }
- ITmfTrace trace = fTrace.instantiateTrace();
- TmfEvent traceEvent = fTrace.instantiateEvent();
- if (trace == null || traceEvent == null) {
- displayErrorMsg(Messages.OpenTraceHandler_NoTraceType);
- return null;
- }
+ // Get the editor_id from the extension point
+ String traceEditorId = traceElement.getEditorId();
+ final String editorId = (traceEditorId != null) ? traceEditorId : TmfEventsEditor.ID;
- // Get the editor_id from the extension point
- String editorId = fTrace.getEditorId();
- boolean usesEditor = editorId != null && editorId.length() > 0;
-
- try {
- trace.initTrace(fTrace.getName(), fTrace.getLocation().getPath(), traceEvent.getClass());
- if (usesEditor)
- trace.indexTrace(false);
- } catch (FileNotFoundException e) {
- displayErrorMsg(Messages.OpenTraceHandler_NoTrace);
- return null;
- }
- trace.setResource(fTrace.getResource());
-
- IResource resource = fTrace.getResource();
- IFile file = null;
- if (resource instanceof IFile) {
- file = (IFile) resource;
- } else if (resource instanceof IFolder){
- try {
- IFile bookmarksFile = fTrace.getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
- if (!bookmarksFile.exists()) {
- InputStream source = new ByteArrayInputStream(new byte[0]);
- bookmarksFile.create(source, true, null);
+ try {
+ trace.initTrace(traceElement.getResource(), traceElement.getLocation().getPath(), traceEvent.getClass());
+ } catch (final TmfTraceException e) {
+ displayErrorMsg(Messages.OpenTraceHandler_InitError + "\n\n" + e); //$NON-NLS-1$
+ trace.dispose();
+ return;
}
- bookmarksFile.setHidden(true);
- IFolder folder = (IFolder) resource;
- file = folder.getFile(fTrace.getName() + '_');
- if (!file.exists()) {
- file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
+ final IFile file;
+ try {
+ file = traceElement.createBookmarksFile();
+ } catch (final CoreException e) {
+ Activator.getDefault().logError("Error opening trace " + traceElement.getName(), e); //$NON-NLS-1$
+ displayErrorMsg(Messages.OpenTraceHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$
+ trace.dispose();
+ return;
}
- file.setHidden(true);
- if (usesEditor) {
- file.setPersistentProperty(TmfTraceElement.TRACETYPE, fTrace.getTraceType());
- } else {
- file.setPersistentProperty(TmfTraceElement.TRACETYPE, TmfTrace.class.getCanonicalName());
- }
- } catch (CoreException e) {
- e.printStackTrace();
- }
- }
- if (usesEditor) {
- try {
- IEditorInput editorInput = new TmfEditorInput(file, trace);
- IWorkbench wb = PlatformUI.getWorkbench();
- IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
-
- IEditorPart editor = activePage.findEditor(new FileEditorInput(file));
- if (editor != null && editor instanceof IReusableEditor) {
- activePage.reuseEditor((IReusableEditor) editor, editorInput);
- activePage.activate(editor);
- } else {
- activePage.openEditor(editorInput, editorId);
- if (resource instanceof IFile) {
- IDE.setDefaultEditor((IFile) resource, editorId);
+ Display.getDefault().asyncExec(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ final IEditorInput editorInput = new TmfEditorInput(file, trace);
+ final IWorkbench wb = PlatformUI.getWorkbench();
+ final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
+
+ final IEditorPart editor = activePage.findEditor(new FileEditorInput(file));
+ if ((editor != null) && (editor instanceof IReusableEditor)) {
+ activePage.reuseEditor((IReusableEditor) editor, editorInput);
+ activePage.activate(editor);
+ } else {
+ activePage.openEditor(editorInput, editorId);
+ IDE.setDefaultEditor(file, editorId);
+ // editor should dispose the trace on close
+ }
+ } catch (final PartInitException e) {
+ displayErrorMsg(Messages.OpenTraceHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$
+ Activator.getDefault().logError("Error opening trace " + traceElement.getName(), e); //$NON-NLS-1$
+ trace.dispose();
+ }
}
- }
- } catch (PartInitException e) {
- e.printStackTrace();
+ });
+
}
- } else {
- // Create the experiment
- ITmfTrace[] traces = new ITmfTrace[] { trace };
- TmfExperiment experiment = new TmfExperiment(traceEvent.getClass(), fTrace.getName(), traces, trace.getIndexPageSize());
- experiment.setBookmarksFile(file);
-
- TmfExperiment.setCurrentExperiment(experiment);
- TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment));
- IDE.setDefaultEditor(file, EventsViewEditor.ID);
- }
+ };
+
+ thread.start();
return null;
}
- private void displayErrorMsg(String errorMsg) {
- MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
- mb.setText(Messages.OpenTraceHandler_Title);
- mb.setMessage(errorMsg);
- mb.open();
+ private static void displayErrorMsg(final String errorMsg) {
+ Display.getDefault().asyncExec(new Runnable() {
+ @Override
+ public void run() {
+ final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
+ mb.setText(Messages.OpenTraceHandler_Title);
+ mb.setMessage(errorMsg);
+ mb.open();
+ }
+ });
}
}