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