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 | import java.util.List; |
18 | ||
19 | import org.eclipse.core.commands.AbstractHandler; | |
20 | import org.eclipse.core.commands.ExecutionEvent; | |
21 | import org.eclipse.core.commands.ExecutionException; | |
828e5592 PT |
22 | import org.eclipse.core.resources.IFile; |
23 | import org.eclipse.core.resources.IResource; | |
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; |
ce2388e0 | 28 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
b4f71e4a | 29 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
6c13869b | 30 | import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment; |
09d11238 PT |
31 | import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal; |
32 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; | |
6c13869b | 33 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
09d11238 | 34 | import org.eclipse.linuxtools.tmf.ui.editors.EventsViewEditor; |
828e5592 PT |
35 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput; |
36 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor; | |
12c155f5 FC |
37 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
38 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
39 | import org.eclipse.swt.widgets.MessageBox; | |
828e5592 PT |
40 | import org.eclipse.ui.IEditorInput; |
41 | import org.eclipse.ui.IEditorPart; | |
42 | import org.eclipse.ui.IReusableEditor; | |
43 | import org.eclipse.ui.IWorkbench; | |
12c155f5 FC |
44 | import org.eclipse.ui.IWorkbenchPage; |
45 | import org.eclipse.ui.IWorkbenchPart; | |
46 | import org.eclipse.ui.IWorkbenchWindow; | |
47 | import org.eclipse.ui.PlatformUI; | |
828e5592 | 48 | import org.eclipse.ui.ide.IDE; |
a1091415 | 49 | import org.eclipse.ui.part.FileEditorInput; |
12c155f5 FC |
50 | |
51 | /** | |
52 | * <b><u>OpenExperimentHandler</u></b> | |
53 | * <p> | |
12c155f5 FC |
54 | */ |
55 | public class OpenExperimentHandler extends AbstractHandler { | |
56 | ||
828e5592 PT |
57 | private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$ |
58 | ||
12c155f5 FC |
59 | private TmfExperimentElement fExperiment = null; |
60 | ||
61 | // ------------------------------------------------------------------------ | |
62 | // Validation | |
63 | // ------------------------------------------------------------------------ | |
64 | ||
65 | @Override | |
66 | public boolean isEnabled() { | |
67 | ||
68 | // Check if we are closing down | |
25e48683 FC |
69 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
70 | if (window == null) | |
12c155f5 FC |
71 | return false; |
72 | ||
73 | // Get the selection | |
25e48683 FC |
74 | final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
75 | final IWorkbenchPart part = page.getActivePart(); | |
76 | final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); | |
77 | if (selectionProvider == null) | |
1595249b | 78 | return false; |
25e48683 | 79 | final ISelection selection = selectionProvider.getSelection(); |
12c155f5 FC |
80 | |
81 | // Make sure there is only one selection and that it is an experiment | |
82 | fExperiment = null; | |
83 | if (selection instanceof TreeSelection) { | |
25e48683 | 84 | final TreeSelection sel = (TreeSelection) selection; |
12c155f5 | 85 | // There should be only one item selected as per the plugin.xml |
25e48683 FC |
86 | final Object element = sel.getFirstElement(); |
87 | if (element instanceof TmfExperimentElement) | |
12c155f5 | 88 | fExperiment = (TmfExperimentElement) element; |
12c155f5 FC |
89 | } |
90 | ||
91 | // We only enable opening from the Traces folder for now | |
92 | return (fExperiment != null); | |
93 | } | |
94 | ||
95 | // ------------------------------------------------------------------------ | |
96 | // Execution | |
97 | // ------------------------------------------------------------------------ | |
98 | ||
99 | @SuppressWarnings({ "rawtypes", "unchecked" }) | |
100 | @Override | |
25e48683 | 101 | public Object execute(final ExecutionEvent event) throws ExecutionException { |
12c155f5 FC |
102 | |
103 | // Check if we are closing down | |
25e48683 FC |
104 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
105 | if (window == null) | |
12c155f5 FC |
106 | return false; |
107 | ||
828e5592 | 108 | try { |
25e48683 | 109 | final IFile bookmarksFile = fExperiment.getProject().getExperimentsFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE); |
828e5592 | 110 | if (!bookmarksFile.exists()) { |
25e48683 | 111 | final InputStream source = new ByteArrayInputStream(new byte[0]); |
828e5592 PT |
112 | bookmarksFile.create(source, true, null); |
113 | } | |
114 | bookmarksFile.setHidden(true); | |
12c155f5 | 115 | |
25e48683 FC |
116 | final IFile file = fExperiment.getResource().getFile(fExperiment.getName() + '_'); |
117 | if (!file.exists()) | |
828e5592 | 118 | file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null); |
828e5592 PT |
119 | file.setHidden(true); |
120 | file.setPersistentProperty(TmfTraceElement.TRACETYPE, TmfExperiment.class.getCanonicalName()); | |
121 | ||
122 | // Instantiate the experiment's traces | |
25e48683 FC |
123 | final List<TmfTraceElement> traceEntries = fExperiment.getTraces(); |
124 | final int nbTraces = traceEntries.size(); | |
828e5592 | 125 | int cacheSize = Integer.MAX_VALUE; |
09d11238 PT |
126 | boolean useEditor = true; |
127 | String experimentEditorId = null; | |
25e48683 | 128 | final ITmfTrace<?>[] traces = new ITmfTrace[nbTraces]; |
828e5592 | 129 | for (int i = 0; i < nbTraces; i++) { |
25e48683 FC |
130 | final TmfTraceElement element = traceEntries.get(i); |
131 | final ITmfTrace trace = element.instantiateTrace(); | |
132 | final ITmfEvent traceEvent = element.instantiateEvent(); | |
ce2388e0 | 133 | if ((trace == null) || (traceEvent == null)) { |
828e5592 | 134 | displayErrorMsg(Messages.OpenExperimentHandler_NoTraceType); |
25e48683 | 135 | for (int j = 0; j < i; j++) |
828e5592 | 136 | traces[j].dispose(); |
828e5592 PT |
137 | return null; |
138 | } | |
139 | try { | |
25e48683 | 140 | trace.initTrace(element.getResource(), element.getLocation().getPath(), traceEvent.getClass()); |
b4f71e4a | 141 | } catch (final TmfTraceException e) { |
828e5592 PT |
142 | displayErrorMsg(""); //$NON-NLS-1$ |
143 | } | |
20658947 | 144 | cacheSize = Math.min(cacheSize, trace.getCacheSize()); |
25e48683 | 145 | final String editorId = element.getEditorId(); |
09d11238 PT |
146 | if (editorId == null) { |
147 | useEditor = false; | |
148 | experimentEditorId = null; | |
25e48683 FC |
149 | } else if (useEditor) |
150 | if (experimentEditorId == null) | |
09d11238 | 151 | experimentEditorId = editorId; |
25e48683 | 152 | else if (!editorId.equals(experimentEditorId)) |
09d11238 | 153 | useEditor = false; |
828e5592 | 154 | traces[i] = trace; |
12c155f5 | 155 | } |
12c155f5 | 156 | |
09d11238 | 157 | // Create the experiment |
e6a4cf1b | 158 | final TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, fExperiment.getName(), traces, cacheSize); |
a1091415 | 159 | experiment.setBookmarksFile(file); |
828e5592 | 160 | |
09d11238 | 161 | if (useEditor) { |
25e48683 FC |
162 | final IEditorInput editorInput = new TmfEditorInput(file, experiment); |
163 | final IWorkbench wb = PlatformUI.getWorkbench(); | |
164 | final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage(); | |
ce2388e0 | 165 | |
25e48683 FC |
166 | final String editorId = TmfEventsEditor.ID; |
167 | final IEditorPart editor = activePage.findEditor(new FileEditorInput(file)); | |
ce2388e0 | 168 | if ((editor != null) && (editor instanceof IReusableEditor)) { |
09d11238 PT |
169 | activePage.reuseEditor((IReusableEditor) editor, editorInput); |
170 | activePage.activate(editor); | |
25e48683 | 171 | } else |
5a5c2fc7 | 172 | activePage.openEditor(editorInput, editorId); |
3118edf1 | 173 | experiment.initTrace(null, null, null); |
828e5592 | 174 | IDE.setDefaultEditor(file, editorId); |
09d11238 PT |
175 | // editor should dispose the experiment on close |
176 | } else { | |
177 | TmfExperiment.setCurrentExperiment(experiment); | |
178 | TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment)); | |
179 | IDE.setDefaultEditor(file, EventsViewEditor.ID); | |
828e5592 | 180 | } |
25e48683 | 181 | } catch (final CoreException e) { |
828e5592 PT |
182 | displayErrorMsg(e.getMessage()); |
183 | } | |
12c155f5 FC |
184 | |
185 | return null; | |
186 | } | |
187 | ||
25e48683 FC |
188 | private void displayErrorMsg(final String errorMsg) { |
189 | final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); | |
828e5592 | 190 | mb.setText(Messages.OpenExperimentHandler_Title); |
12c155f5 FC |
191 | mb.setMessage(errorMsg); |
192 | mb.open(); | |
193 | } | |
194 | ||
195 | } |