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