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