Commit | Line | Data |
---|---|---|
a0a88f65 | 1 | /******************************************************************************* |
7a0b1e3c | 2 | * Copyright (c) 2010, 2015 Ericsson |
a0a88f65 AM |
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: | |
f6aa55e2 | 10 | * Patrick Tasse - Initial API and implementation |
a0a88f65 AM |
11 | *******************************************************************************/ |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.internal.tmf.ui.parsers.wizards; |
6151d86c | 14 | |
7a0b1e3c PT |
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
16 | ||
6151d86c PT |
17 | import java.io.File; |
18 | import java.io.FileWriter; | |
19 | import java.io.IOException; | |
20 | import java.util.ArrayList; | |
21 | import java.util.Iterator; | |
22 | import java.util.List; | |
23 | ||
24 | import org.eclipse.jface.wizard.WizardPage; | |
6151d86c PT |
25 | import org.eclipse.swt.SWT; |
26 | import org.eclipse.swt.custom.SashForm; | |
27 | import org.eclipse.swt.custom.ScrolledComposite; | |
28 | import org.eclipse.swt.events.SelectionAdapter; | |
29 | import org.eclipse.swt.events.SelectionEvent; | |
30 | import org.eclipse.swt.graphics.Image; | |
31 | import org.eclipse.swt.layout.GridData; | |
32 | import org.eclipse.swt.layout.GridLayout; | |
33 | import org.eclipse.swt.widgets.Button; | |
34 | import org.eclipse.swt.widgets.Composite; | |
35 | import org.eclipse.swt.widgets.Text; | |
b04903a2 | 36 | import org.eclipse.tracecompass.internal.tmf.core.parsers.custom.CustomEventAspects; |
2bdf0193 AM |
37 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
38 | import org.eclipse.tracecompass.internal.tmf.ui.Messages; | |
2bdf0193 AM |
39 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; |
40 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition; | |
b04903a2 | 41 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.OutputColumn; |
2bdf0193 AM |
42 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace; |
43 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition; | |
2bdf0193 AM |
44 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; |
45 | import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer; | |
46 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer; | |
47 | import org.eclipse.tracecompass.tmf.ui.viewers.events.TmfEventsTable; | |
6151d86c | 48 | |
a0a88f65 AM |
49 | /** |
50 | * Output wizard page for custom XML trace parsers. | |
51 | * | |
f6aa55e2 | 52 | * @author Patrick Tasse |
a0a88f65 | 53 | */ |
6151d86c PT |
54 | public class CustomXmlParserOutputWizardPage extends WizardPage { |
55 | ||
f6aa55e2 PT |
56 | private static final Image UP_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$ |
57 | private static final Image DOWN_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$ | |
6151d86c PT |
58 | private final CustomXmlParserWizard wizard; |
59 | private CustomXmlTraceDefinition definition; | |
507b1336 | 60 | private List<Output> outputs = new ArrayList<>(); |
f6aa55e2 PT |
61 | private Composite container; |
62 | private SashForm sash; | |
63 | private ScrolledComposite outputsScrolledComposite; | |
64 | private Composite outputsContainer; | |
65 | private Composite tableContainer; | |
99d7adc6 | 66 | private TmfEventsTable previewTable; |
f6aa55e2 | 67 | private File tmpFile; |
6151d86c | 68 | |
a0a88f65 AM |
69 | /** |
70 | * Constructor | |
71 | * | |
72 | * @param wizard | |
73 | * The wizard to which this page belongs | |
74 | */ | |
6151d86c PT |
75 | protected CustomXmlParserOutputWizardPage(final CustomXmlParserWizard wizard) { |
76 | super("CustomParserOutputWizardPage"); //$NON-NLS-1$ | |
77 | setTitle(wizard.inputPage.getTitle()); | |
78 | setDescription(Messages.CustomXmlParserOutputWizardPage_description); | |
79 | this.wizard = wizard; | |
80 | setPageComplete(false); | |
81 | } | |
82 | ||
83 | @Override | |
84 | public void createControl(final Composite parent) { | |
85 | container = new Composite(parent, SWT.NULL); | |
86 | container.setLayout(new GridLayout()); | |
87 | ||
88 | sash = new SashForm(container, SWT.VERTICAL); | |
89 | sash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
90 | sash.setBackground(sash.getDisplay().getSystemColor(SWT.COLOR_GRAY)); | |
91 | ||
92 | outputsScrolledComposite = new ScrolledComposite(sash, SWT.V_SCROLL); | |
93 | outputsScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
94 | outputsContainer = new Composite(outputsScrolledComposite, SWT.NONE); | |
95 | final GridLayout outputsLayout = new GridLayout(4, false); | |
96 | outputsLayout.marginHeight = 10; | |
97 | outputsLayout.marginWidth = 0; | |
98 | outputsContainer.setLayout(outputsLayout); | |
99 | outputsScrolledComposite.setContent(outputsContainer); | |
100 | outputsScrolledComposite.setExpandHorizontal(true); | |
101 | outputsScrolledComposite.setExpandVertical(true); | |
102 | ||
103 | outputsContainer.layout(); | |
104 | ||
105 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
106 | ||
107 | tableContainer = new Composite(sash, SWT.NONE); | |
108 | final GridLayout tableLayout = new GridLayout(); | |
109 | tableLayout.marginHeight = 0; | |
110 | tableLayout.marginWidth = 0; | |
111 | tableContainer.setLayout(tableLayout); | |
b04903a2 | 112 | previewTable = new TmfEventsTable(tableContainer, 0, CustomEventAspects.generateAspects(new CustomXmlTraceDefinition())); |
6151d86c PT |
113 | previewTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
114 | ||
115 | if (wizard.definition != null) { | |
116 | loadDefinition(wizard.definition); | |
117 | } | |
118 | setControl(container); | |
119 | ||
120 | } | |
121 | ||
122 | @Override | |
123 | public void dispose() { | |
124 | previewTable.dispose(); | |
125 | super.dispose(); | |
126 | } | |
127 | ||
3dca7aa5 AM |
128 | private void loadDefinition(final CustomTraceDefinition def) { |
129 | for (final OutputColumn outputColumn : def.outputs) { | |
6151d86c PT |
130 | final Output output = new Output(outputsContainer, outputColumn.name); |
131 | outputs.add(output); | |
132 | } | |
133 | } | |
134 | ||
6151d86c PT |
135 | @Override |
136 | public void setVisible(final boolean visible) { | |
137 | if (visible) { | |
138 | this.definition = wizard.inputPage.getDefinition(); | |
139 | final List<String> outputNames = wizard.inputPage.getInputNames(); | |
140 | ||
141 | // dispose outputs that have been removed in the input page | |
142 | final Iterator<Output> iter = outputs.iterator(); | |
143 | while (iter.hasNext()) { | |
144 | final Output output = iter.next(); | |
145 | boolean found = false; | |
146 | for (final String name : outputNames) { | |
147 | if (output.name.equals(name)) { | |
148 | found = true; | |
149 | break; | |
150 | } | |
151 | } | |
152 | if (!found) { | |
153 | output.dispose(); | |
154 | iter.remove(); | |
155 | } | |
156 | } | |
157 | ||
158 | // create outputs that have been added in the input page | |
159 | for (final String name : outputNames) { | |
160 | boolean found = false; | |
161 | for (final Output output : outputs) { | |
162 | if (output.name.equals(name)) { | |
163 | found = true; | |
164 | break; | |
165 | } | |
166 | } | |
167 | if (!found) { | |
168 | outputs.add(new Output(outputsContainer, name)); | |
169 | } | |
170 | } | |
171 | ||
172 | outputsContainer.layout(); | |
173 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
174 | updatePreviewTable(); | |
175 | if (sash.getSize().y > outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y + previewTable.getTable().getItemHeight()) { | |
176 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sash.getSize().y - outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y}); | |
177 | } else { | |
178 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, previewTable.getTable().getItemHeight()}); | |
179 | } | |
180 | setPageComplete(true); | |
181 | } else { | |
182 | setPageComplete(false); | |
183 | } | |
184 | super.setVisible(visible); | |
185 | } | |
186 | ||
187 | private void moveBefore(final Output moved) { | |
188 | final int i = outputs.indexOf(moved); | |
189 | if (i > 0) { | |
190 | final Output previous = outputs.get(i-1); | |
191 | moved.enabledButton.moveAbove(previous.enabledButton); | |
192 | moved.nameLabel.moveBelow(moved.enabledButton); | |
193 | moved.upButton.moveBelow(moved.nameLabel); | |
194 | moved.downButton.moveBelow(moved.upButton); | |
195 | outputs.add(i-1, outputs.remove(i)); | |
196 | outputsContainer.layout(); | |
197 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
198 | container.layout(); | |
199 | updatePreviewTable(); | |
200 | } | |
201 | } | |
202 | ||
203 | private void moveAfter(final Output moved) { | |
204 | final int i = outputs.indexOf(moved); | |
205 | if (i+1 < outputs.size()) { | |
206 | final Output next = outputs.get(i+1); | |
207 | moved.enabledButton.moveBelow(next.downButton); | |
208 | moved.nameLabel.moveBelow(moved.enabledButton); | |
209 | moved.upButton.moveBelow(moved.nameLabel); | |
210 | moved.downButton.moveBelow(moved.upButton); | |
211 | outputs.add(i+1, outputs.remove(i)); | |
212 | outputsContainer.layout(); | |
213 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
214 | container.layout(); | |
215 | updatePreviewTable(); | |
216 | } | |
217 | } | |
218 | ||
219 | private void updatePreviewTable() { | |
220 | final int CACHE_SIZE = 50; | |
221 | definition.outputs = extractOutputs(); | |
507b1336 | 222 | tmpFile = Activator.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$ |
6151d86c | 223 | |
507b1336 | 224 | try (final FileWriter writer = new FileWriter(tmpFile);) { |
6151d86c | 225 | writer.write(wizard.inputPage.getInputText()); |
507b1336 AM |
226 | } catch (final IOException e) { |
227 | Activator.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ | |
228 | } | |
6151d86c | 229 | |
507b1336 | 230 | try { |
5ee05017 PT |
231 | final CustomXmlTrace trace = new CustomXmlTrace(null, definition, tmpFile.getAbsolutePath(), CACHE_SIZE) { |
232 | @Override | |
233 | protected ITmfTraceIndexer createIndexer(int interval) { | |
234 | return new TmfCheckpointIndexer(this, interval); | |
235 | } | |
236 | }; | |
b0422293 | 237 | trace.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, false); |
6151d86c | 238 | previewTable.dispose(); |
b04903a2 | 239 | previewTable = new TmfEventsTable(tableContainer, CACHE_SIZE, CustomEventAspects.generateAspects(definition)); |
6151d86c PT |
240 | previewTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
241 | previewTable.setTrace(trace, true); | |
242 | } catch (final TmfTraceException e) { | |
243 | Activator.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ | |
6151d86c PT |
244 | } |
245 | ||
246 | tableContainer.layout(); | |
247 | container.layout(); | |
248 | } | |
249 | ||
a0a88f65 AM |
250 | /** |
251 | * Extract the output columns from the page's current contents. | |
252 | * | |
253 | * @return The list of output columns | |
254 | */ | |
6151d86c PT |
255 | public List<OutputColumn> extractOutputs() { |
256 | int numColumns = 0; | |
257 | for (int i = 0; i < outputs.size(); i++) { | |
258 | if (outputs.get(i).enabledButton.getSelection()) { | |
259 | numColumns++; | |
260 | } | |
261 | } | |
507b1336 | 262 | final List<OutputColumn> outputColumns = new ArrayList<>(numColumns); |
6151d86c PT |
263 | numColumns = 0; |
264 | for (int i = 0; i < outputs.size(); i++) { | |
265 | final Output output = outputs.get(i); | |
266 | if (output.enabledButton.getSelection()) { | |
267 | final OutputColumn column = new OutputColumn(); | |
7a0b1e3c | 268 | column.name = checkNotNull(output.nameLabel.getText()); |
6151d86c PT |
269 | outputColumns.add(column); |
270 | } | |
271 | } | |
272 | return outputColumns; | |
273 | } | |
274 | ||
275 | private class Output { | |
276 | String name; | |
277 | Button enabledButton; | |
278 | Text nameLabel; | |
279 | Button upButton; | |
280 | Button downButton; | |
281 | ||
282 | public Output(final Composite parent, final String name) { | |
283 | this.name = name; | |
284 | ||
285 | enabledButton = new Button(parent, SWT.CHECK); | |
286 | enabledButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_visible); | |
287 | enabledButton.setSelection(true); | |
288 | enabledButton.addSelectionListener(new SelectionAdapter() { | |
289 | @Override | |
290 | public void widgetSelected(final SelectionEvent e) { | |
291 | updatePreviewTable(); | |
292 | } | |
293 | }); | |
294 | // if (messageOutput != null) { | |
295 | // enabledButton.moveAbove(messageOutput.enabledButton); | |
296 | // } | |
297 | ||
298 | nameLabel = new Text(parent, SWT.BORDER | SWT.READ_ONLY | SWT.SINGLE); | |
299 | nameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
300 | nameLabel.setText(name); | |
301 | nameLabel.moveBelow(enabledButton); | |
302 | ||
303 | upButton = new Button(parent, SWT.PUSH); | |
f6aa55e2 | 304 | upButton.setImage(UP_IMAGE); |
6151d86c PT |
305 | upButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_moveBefore); |
306 | upButton.addSelectionListener(new SelectionAdapter() { | |
307 | @Override | |
308 | public void widgetSelected(final SelectionEvent e) { | |
309 | moveBefore(Output.this); | |
310 | } | |
311 | }); | |
312 | upButton.moveBelow(nameLabel); | |
313 | ||
314 | downButton = new Button(parent, SWT.PUSH); | |
f6aa55e2 | 315 | downButton.setImage(DOWN_IMAGE); |
6151d86c PT |
316 | downButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_moveAfter); |
317 | downButton.addSelectionListener(new SelectionAdapter() { | |
318 | @Override | |
319 | public void widgetSelected(final SelectionEvent e) { | |
320 | moveAfter(Output.this); | |
321 | } | |
322 | }); | |
323 | downButton.moveBelow(upButton); | |
324 | } | |
325 | ||
326 | private void dispose() { | |
327 | enabledButton.dispose(); | |
328 | nameLabel.dispose(); | |
329 | upButton.dispose(); | |
330 | downButton.dispose(); | |
331 | } | |
332 | } | |
333 | ||
a0a88f65 AM |
334 | /** |
335 | * Get the trace definition. | |
336 | * | |
337 | * @return The trace definition | |
338 | */ | |
6151d86c PT |
339 | public CustomXmlTraceDefinition getDefinition() { |
340 | return definition; | |
341 | } | |
342 | ||
343 | } |