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; |
e12ecd30 | 28 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
ce2388e0 | 29 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
b4f71e4a | 30 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
6c13869b | 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 | 120 | file.setHidden(true); |
e12ecd30 | 121 | file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfExperiment.class.getCanonicalName()); |
828e5592 PT |
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++) { |
5e4bf87d BH |
131 | TmfTraceElement element = traceEntries.get(i); |
132 | ||
133 | // Since trace is under an experiment, use the original trace from the traces folder | |
134 | element = element.getElementUnderTraceFolder(); | |
135 | ||
25e48683 FC |
136 | final ITmfTrace trace = element.instantiateTrace(); |
137 | final ITmfEvent traceEvent = element.instantiateEvent(); | |
ce2388e0 | 138 | if ((trace == null) || (traceEvent == null)) { |
828e5592 | 139 | displayErrorMsg(Messages.OpenExperimentHandler_NoTraceType); |
25e48683 | 140 | for (int j = 0; j < i; j++) |
828e5592 | 141 | traces[j].dispose(); |
828e5592 PT |
142 | return null; |
143 | } | |
144 | try { | |
25e48683 | 145 | trace.initTrace(element.getResource(), element.getLocation().getPath(), traceEvent.getClass()); |
b4f71e4a | 146 | } catch (final TmfTraceException e) { |
828e5592 PT |
147 | displayErrorMsg(""); //$NON-NLS-1$ |
148 | } | |
20658947 | 149 | cacheSize = Math.min(cacheSize, trace.getCacheSize()); |
25e48683 | 150 | final String editorId = element.getEditorId(); |
09d11238 PT |
151 | if (editorId == null) { |
152 | useEditor = false; | |
153 | experimentEditorId = null; | |
25e48683 FC |
154 | } else if (useEditor) |
155 | if (experimentEditorId == null) | |
09d11238 | 156 | experimentEditorId = editorId; |
25e48683 | 157 | else if (!editorId.equals(experimentEditorId)) |
09d11238 | 158 | useEditor = false; |
828e5592 | 159 | traces[i] = trace; |
12c155f5 | 160 | } |
12c155f5 | 161 | |
09d11238 | 162 | // Create the experiment |
e6a4cf1b | 163 | final TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, fExperiment.getName(), traces, cacheSize); |
a1091415 | 164 | experiment.setBookmarksFile(file); |
828e5592 | 165 | |
09d11238 | 166 | if (useEditor) { |
25e48683 FC |
167 | final IEditorInput editorInput = new TmfEditorInput(file, experiment); |
168 | final IWorkbench wb = PlatformUI.getWorkbench(); | |
169 | final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage(); | |
ce2388e0 | 170 | |
25e48683 FC |
171 | final String editorId = TmfEventsEditor.ID; |
172 | final IEditorPart editor = activePage.findEditor(new FileEditorInput(file)); | |
ce2388e0 | 173 | if ((editor != null) && (editor instanceof IReusableEditor)) { |
09d11238 PT |
174 | activePage.reuseEditor((IReusableEditor) editor, editorInput); |
175 | activePage.activate(editor); | |
25e48683 | 176 | } else |
5a5c2fc7 | 177 | activePage.openEditor(editorInput, editorId); |
3118edf1 | 178 | experiment.initTrace(null, null, null); |
828e5592 | 179 | IDE.setDefaultEditor(file, editorId); |
09d11238 PT |
180 | // editor should dispose the experiment on close |
181 | } else { | |
182 | TmfExperiment.setCurrentExperiment(experiment); | |
183 | TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment)); | |
184 | IDE.setDefaultEditor(file, EventsViewEditor.ID); | |
828e5592 | 185 | } |
25e48683 | 186 | } catch (final CoreException e) { |
828e5592 PT |
187 | displayErrorMsg(e.getMessage()); |
188 | } | |
12c155f5 FC |
189 | |
190 | return null; | |
191 | } | |
192 | ||
25e48683 FC |
193 | private void displayErrorMsg(final String errorMsg) { |
194 | final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); | |
828e5592 | 195 | mb.setText(Messages.OpenExperimentHandler_Title); |
12c155f5 FC |
196 | mb.setMessage(errorMsg); |
197 | mb.open(); | |
198 | } | |
199 | ||
200 | } |