Merge master in TmfTraceModel
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / editors / EventsViewEditor.java
1 /*******************************************************************************
2 * Copyright (c) 2012 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 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.editors;
14
15 import java.io.FileNotFoundException;
16 import java.util.List;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IMarker;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.InvalidRegistryObjectException;
23 import org.eclipse.linuxtools.internal.tmf.ui.project.handlers.Messages;
24 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
25 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
26 import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment;
27 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
28 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
29 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
30 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
31 import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement;
32 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
33 import org.eclipse.linuxtools.tmf.ui.project.model.TmfNavigatorContentProvider;
34 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
35 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
36 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
37 import org.eclipse.linuxtools.tmf.ui.views.events.TmfEventsView;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.ui.IEditorInput;
41 import org.eclipse.ui.IEditorSite;
42 import org.eclipse.ui.IFileEditorInput;
43 import org.eclipse.ui.IViewPart;
44 import org.eclipse.ui.IWorkbenchPage;
45 import org.eclipse.ui.PartInitException;
46 import org.eclipse.ui.ide.IGotoMarker;
47 import org.eclipse.ui.part.EditorPart;
48
49 /**
50 *
51 * This editor is used to open a trace in the Events view
52 * and set the trace as the current experiment.
53 * It intercepts the IGotoMarker adapter and dispatches
54 * the handling to the Events view' events table.
55 * The editor then closes itself and hides the
56 * editor area if no other editor is open.
57 *
58 */
59 public class EventsViewEditor extends TmfEditor {
60
61 /**
62 * The editor ID
63 */
64 public static final String ID = "org.eclipse.linuxtools.tmf.ui.editors.eventsView"; //$NON-NLS-1$
65
66 private IFile fFile;
67 @SuppressWarnings("rawtypes")
68 private ITmfTrace fTrace;
69 private IMarker fGotoMarker;
70 private boolean fEditorAreaVisible;
71
72 /*
73 * (non-Javadoc)
74 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
75 */
76 @Override
77 public void doSave(IProgressMonitor monitor) {
78 }
79
80 @Override
81 public void doSaveAs() {
82 }
83
84 /*
85 * (non-Javadoc)
86 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
87 */
88 @SuppressWarnings({ "unchecked", "rawtypes" })
89 @Override
90 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
91 fEditorAreaVisible = site.getPage().isEditorAreaVisible();
92 if (input instanceof TmfEditorInput) {
93 fFile = ((TmfEditorInput) input).getFile();
94 fTrace = ((TmfEditorInput) input).getTrace();
95 } else if (input instanceof IFileEditorInput) {
96 fFile = ((IFileEditorInput) input).getFile();
97 if (fFile == null) {
98 throw new PartInitException("Invalid IFileEditorInput: " + input); //$NON-NLS-1$
99 }
100 TmfExperiment currentExperiment = TmfExperiment.getCurrentExperiment();
101 if ((currentExperiment != null) && fFile.equals(currentExperiment.getBookmarksFile())) {
102 fTrace = currentExperiment;
103 super.setSite(site);
104 super.setInput(input);
105 return;
106 }
107 try {
108 String traceTypeId = fFile.getPersistentProperty(TmfTraceElement.TRACETYPE);
109 if (traceTypeId == null) {
110 throw new PartInitException(Messages.OpenTraceHandler_NoTraceType);
111 }
112 if (traceTypeId.equals(TmfExperiment.class.getCanonicalName())) {
113 // Special case: experiment bookmark resource
114 TmfNavigatorContentProvider ncp = new TmfNavigatorContentProvider();
115 ncp.getChildren(fFile.getProject()); // force the model to be populated
116 TmfProjectElement project = TmfProjectRegistry.getProject(fFile.getProject());
117 if (project == null) {
118 throw new PartInitException(Messages.OpenExperimentHandler_NoTraceType);
119 }
120 for (ITmfProjectModelElement projectElement : project.getExperimentsFolder().getChildren()) {
121 String traceName = fFile.getParent().getName();
122 if (projectElement.getName().equals(traceName)) {
123 TmfExperimentElement experimentElement = (TmfExperimentElement) projectElement;
124 // Instantiate the experiment's traces
125 List<TmfTraceElement> traceEntries = experimentElement.getTraces();
126 int nbTraces = traceEntries.size();
127 int cacheSize = Integer.MAX_VALUE;
128 ITmfTrace<?>[] traces = new ITmfTrace[nbTraces];
129 for (int i = 0; i < nbTraces; i++) {
130 TmfTraceElement traceElement = traceEntries.get(i);
131 ITmfTrace trace = traceElement.instantiateTrace();
132 ITmfEvent traceEvent = traceElement.instantiateEvent();
133 if ((trace == null) || (traceEvent == null)) {
134 for (int j = 0; j < i; j++) {
135 traces[j].dispose();
136 }
137 throw new PartInitException(Messages.OpenExperimentHandler_NoTraceType);
138 }
139 try {
140 trace.initTrace(traceElement.getName(), traceElement.getLocation().getPath(), traceEvent.getClass());
141 } catch (FileNotFoundException e) {
142 }
143 trace.setResource(traceElement.getResource());
144 cacheSize = Math.min(cacheSize, trace.getIndexPageSize());
145 traces[i] = trace;
146 }
147 TmfExperiment experiment = new TmfExperiment(TmfEvent.class, experimentElement.getName(), traces, cacheSize);
148 experiment.setBookmarksFile(fFile);
149 fTrace = experiment;
150 TmfExperiment.setCurrentExperiment(experiment);
151 TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment));
152 break;
153 }
154 }
155 } else if (traceTypeId.equals(TmfTrace.class.getCanonicalName())) {
156 // Special case: trace bookmark resource
157 TmfNavigatorContentProvider ncp = new TmfNavigatorContentProvider();
158 ncp.getChildren(fFile.getProject()); // force the model to be populated
159 TmfProjectElement project = TmfProjectRegistry.getProject(fFile.getProject());
160 for (ITmfProjectModelElement projectElement : project.getTracesFolder().getChildren()) {
161 String traceName = fFile.getParent().getName();
162 if (projectElement.getName().equals(traceName)) {
163 TmfTraceElement traceElement = (TmfTraceElement) projectElement;
164 // Instantiate the experiment trace
165 ITmfTrace trace = traceElement.instantiateTrace();
166 ITmfEvent traceEvent = traceElement.instantiateEvent();
167 if ((trace == null) || (traceEvent == null)) {
168 throw new PartInitException(Messages.OpenTraceHandler_NoTraceType);
169 }
170 try {
171 trace.initTrace(traceElement.getName(), traceElement.getLocation().getPath(), traceEvent.getClass());
172 } catch (FileNotFoundException e) {
173 }
174 trace.setResource(traceElement.getResource());
175 ITmfTrace[] traces = new ITmfTrace[] { trace };
176 TmfExperiment experiment = new TmfExperiment(traceEvent.getClass(), traceElement.getName(), traces, trace.getIndexPageSize());
177 experiment.setBookmarksFile(fFile);
178 fTrace = experiment;
179 TmfExperiment.setCurrentExperiment(experiment);
180 TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment));
181 break;
182 }
183 }
184 } else {
185 TmfNavigatorContentProvider ncp = new TmfNavigatorContentProvider();
186 ncp.getChildren(fFile.getProject()); // force the model to be populated
187 TmfProjectElement project = TmfProjectRegistry.getProject(fFile.getProject());
188 for (ITmfProjectModelElement projectElement : project.getTracesFolder().getChildren()) {
189 if (projectElement.getResource().equals(fFile)) {
190 TmfTraceElement traceElement = (TmfTraceElement) projectElement;
191 // Instantiate the experiment trace
192 ITmfTrace trace = traceElement.instantiateTrace();
193 ITmfEvent traceEvent = traceElement.instantiateEvent();
194 if ((trace == null) || (traceEvent == null)) {
195 throw new PartInitException(Messages.OpenTraceHandler_NoTraceType);
196 }
197 try {
198 trace.initTrace(traceElement.getName(), traceElement.getLocation().getPath(), traceEvent.getClass());
199 trace.indexTrace(false);
200 } catch (FileNotFoundException e) {
201 }
202 if (trace instanceof TmfTrace) {
203 ((TmfTrace) trace).setResource(traceElement.getResource());
204 }
205 ITmfTrace[] traces = new ITmfTrace[] { trace };
206 TmfExperiment experiment = new TmfExperiment(traceEvent.getClass(), traceElement.getName(), traces, trace.getIndexPageSize());
207 experiment.setBookmarksFile(fFile);
208 fTrace = experiment;
209 TmfExperiment.setCurrentExperiment(experiment);
210 TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment));
211 break;
212 }
213 }
214 }
215 } catch (InvalidRegistryObjectException e) {
216 e.printStackTrace();
217 } catch (PartInitException e) {
218 throw e;
219 } catch (CoreException e) {
220 e.printStackTrace();
221 }
222 input = new TmfEditorInput(fFile, fTrace);
223 } else {
224 throw new PartInitException("Invalid IEditorInput: " + input.getClass()); //$NON-NLS-1$
225 }
226 if (fTrace == null) {
227 throw new PartInitException("Invalid IEditorInput: " + fFile.getName()); //$NON-NLS-1$
228 }
229 super.setSite(site);
230 super.setInput(input);
231 }
232
233 /*
234 * (non-Javadoc)
235 * @see org.eclipse.ui.part.EditorPart#isDirty()
236 */
237 @Override
238 public boolean isDirty() {
239 return false;
240 }
241
242 /*
243 * (non-Javadoc)
244 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
245 */
246 @Override
247 public boolean isSaveAsAllowed() {
248 return false;
249 }
250
251 /*
252 * (non-Javadoc)
253 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
254 */
255 @Override
256 public void createPartControl(Composite parent) {
257 setPartName(getEditorInput().getName());
258 }
259
260 /*
261 * (non-Javadoc)
262 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
263 */
264 @Override
265 public void setFocus() {
266 Display.getDefault().asyncExec(new Runnable() {
267 @Override
268 public void run() {
269 EditorPart editorPart = EventsViewEditor.this;
270 IWorkbenchPage page = editorPart.getEditorSite().getPage();
271 page.closeEditor(editorPart, false);
272 if (page.getEditorReferences().length == 0) {
273 page.setEditorAreaVisible(fEditorAreaVisible);
274 }
275 try {
276 IViewPart viewPart = page.showView(TmfEventsView.ID);
277 if (fGotoMarker != null) {
278 IGotoMarker adapter = (IGotoMarker) viewPart.getAdapter(IGotoMarker.class);
279 if (adapter != null) {
280 adapter.gotoMarker(fGotoMarker);
281 }
282 }
283 } catch (PartInitException e) {
284 e.printStackTrace();
285 }
286 }
287 });
288 }
289
290 /* (non-Javadoc)
291 * @see org.eclipse.ui.part.WorkbenchPart#getAdapter(java.lang.Class)
292 */
293 @SuppressWarnings("rawtypes")
294 @Override
295 public Object getAdapter(Class adapter) {
296 if (IGotoMarker.class.equals(adapter)) {
297 return new IGotoMarker() {
298 @Override
299 public void gotoMarker(IMarker marker) {
300 fGotoMarker = marker;
301 }
302 };
303 }
304 return super.getAdapter(adapter);
305 }
306
307 }
This page took 0.039852 seconds and 5 git commands to generate.