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