| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2013 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 | * Francois Chouinard - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.internal.tmf.ui.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.IFile; |
| 19 | import org.eclipse.core.runtime.CoreException; |
| 20 | import org.eclipse.jface.viewers.ISelection; |
| 21 | import org.eclipse.jface.viewers.ISelectionProvider; |
| 22 | import org.eclipse.jface.viewers.TreeSelection; |
| 23 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
| 24 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 25 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
| 26 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 27 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput; |
| 28 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor; |
| 29 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
| 30 | import org.eclipse.swt.widgets.Display; |
| 31 | import org.eclipse.swt.widgets.MessageBox; |
| 32 | import org.eclipse.ui.IEditorInput; |
| 33 | import org.eclipse.ui.IEditorPart; |
| 34 | import org.eclipse.ui.IReusableEditor; |
| 35 | import org.eclipse.ui.IWorkbench; |
| 36 | import org.eclipse.ui.IWorkbenchPage; |
| 37 | import org.eclipse.ui.IWorkbenchPart; |
| 38 | import org.eclipse.ui.IWorkbenchWindow; |
| 39 | import org.eclipse.ui.PartInitException; |
| 40 | import org.eclipse.ui.PlatformUI; |
| 41 | import org.eclipse.ui.ide.IDE; |
| 42 | import org.eclipse.ui.part.FileEditorInput; |
| 43 | |
| 44 | /** |
| 45 | * <b><u>OpenTraceHandler</u></b> |
| 46 | * <p> |
| 47 | * TODO: Add support for multiple trace selection |
| 48 | */ |
| 49 | public class OpenTraceHandler extends AbstractHandler { |
| 50 | |
| 51 | // ------------------------------------------------------------------------ |
| 52 | // Attributes |
| 53 | // ------------------------------------------------------------------------ |
| 54 | |
| 55 | private TmfTraceElement fTrace = null; |
| 56 | |
| 57 | // ------------------------------------------------------------------------ |
| 58 | // Validation |
| 59 | // ------------------------------------------------------------------------ |
| 60 | |
| 61 | @Override |
| 62 | public boolean isEnabled() { |
| 63 | |
| 64 | // Check if we are closing down |
| 65 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 66 | if (window == null) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | // Get the selection |
| 71 | final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
| 72 | final IWorkbenchPart part = page.getActivePart(); |
| 73 | if (part == null) { |
| 74 | return false; |
| 75 | } |
| 76 | final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
| 77 | if (selectionProvider == null) { |
| 78 | return false; |
| 79 | } |
| 80 | final ISelection selection = selectionProvider.getSelection(); |
| 81 | |
| 82 | // Make sure there is only one selection and that it is a trace |
| 83 | fTrace = null; |
| 84 | if (selection instanceof TreeSelection) { |
| 85 | final TreeSelection sel = (TreeSelection) selection; |
| 86 | // There should be only one item selected as per the plugin.xml |
| 87 | final Object element = sel.getFirstElement(); |
| 88 | if (element instanceof TmfTraceElement) { |
| 89 | fTrace = (TmfTraceElement) element; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // We only enable opening from the Traces folder for now |
| 94 | return (fTrace != null); |
| 95 | } |
| 96 | |
| 97 | // ------------------------------------------------------------------------ |
| 98 | // Execution |
| 99 | // ------------------------------------------------------------------------ |
| 100 | |
| 101 | @Override |
| 102 | public Object execute(final ExecutionEvent event) throws ExecutionException { |
| 103 | |
| 104 | // Check if we are closing down |
| 105 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 106 | if (window == null) { |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | // Check that the trace is valid |
| 111 | if (fTrace == null) { |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | // If trace is under an experiment, use the original trace from the traces folder |
| 116 | final TmfTraceElement traceElement = fTrace.getElementUnderTraceFolder(); |
| 117 | |
| 118 | Thread thread = new Thread() { |
| 119 | @Override |
| 120 | public void run() { |
| 121 | |
| 122 | final ITmfTrace trace = traceElement.instantiateTrace(); |
| 123 | final ITmfEvent traceEvent = traceElement.instantiateEvent(); |
| 124 | if ((trace == null) || (traceEvent == null)) { |
| 125 | displayErrorMsg(Messages.OpenTraceHandler_NoTraceType); |
| 126 | if (trace != null) { |
| 127 | trace.dispose(); |
| 128 | } |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | // Get the editor_id from the extension point |
| 133 | String traceEditorId = traceElement.getEditorId(); |
| 134 | final String editorId = (traceEditorId != null) ? traceEditorId : TmfEventsEditor.ID; |
| 135 | |
| 136 | try { |
| 137 | trace.initTrace(traceElement.getResource(), traceElement.getLocation().getPath(), traceEvent.getClass()); |
| 138 | } catch (final TmfTraceException e) { |
| 139 | displayErrorMsg(Messages.OpenTraceHandler_InitError + "\n\n" + e); //$NON-NLS-1$ |
| 140 | trace.dispose(); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | final IFile file; |
| 145 | try { |
| 146 | file = traceElement.createBookmarksFile(); |
| 147 | } catch (final CoreException e) { |
| 148 | Activator.getDefault().logError("Error opening trace " + traceElement.getName(), e); //$NON-NLS-1$ |
| 149 | displayErrorMsg(Messages.OpenTraceHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$ |
| 150 | trace.dispose(); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | Display.getDefault().asyncExec(new Runnable() { |
| 155 | @Override |
| 156 | public void run() { |
| 157 | try { |
| 158 | final IEditorInput editorInput = new TmfEditorInput(file, trace); |
| 159 | final IWorkbench wb = PlatformUI.getWorkbench(); |
| 160 | final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage(); |
| 161 | |
| 162 | final IEditorPart editor = activePage.findEditor(new FileEditorInput(file)); |
| 163 | if ((editor != null) && (editor instanceof IReusableEditor)) { |
| 164 | activePage.reuseEditor((IReusableEditor) editor, editorInput); |
| 165 | activePage.activate(editor); |
| 166 | } else { |
| 167 | activePage.openEditor(editorInput, editorId); |
| 168 | IDE.setDefaultEditor(file, editorId); |
| 169 | // editor should dispose the trace on close |
| 170 | } |
| 171 | } catch (final PartInitException e) { |
| 172 | displayErrorMsg(Messages.OpenTraceHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$ |
| 173 | Activator.getDefault().logError("Error opening trace " + traceElement.getName(), e); //$NON-NLS-1$ |
| 174 | trace.dispose(); |
| 175 | } |
| 176 | } |
| 177 | }); |
| 178 | |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | thread.start(); |
| 183 | return null; |
| 184 | } |
| 185 | |
| 186 | private static void displayErrorMsg(final String errorMsg) { |
| 187 | Display.getDefault().asyncExec(new Runnable() { |
| 188 | @Override |
| 189 | public void run() { |
| 190 | final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
| 191 | mb.setText(Messages.OpenTraceHandler_Title); |
| 192 | mb.setMessage(errorMsg); |
| 193 | mb.open(); |
| 194 | } |
| 195 | }); |
| 196 | } |
| 197 | |
| 198 | } |