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
1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson
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:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.tmf.ui.parsers.wizards;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.File;
18 import java.io.FileWriter;
19 import java.io.IOException;
20 import java.util.AbstractMap.SimpleEntry;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map.Entry;
25
26 import org.eclipse.jface.wizard.WizardPage;
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;
38 import org.eclipse.tracecompass.internal.tmf.core.parsers.custom.CustomEventAspects;
39 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
40 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
41 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
42 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.OutputColumn;
43 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.Tag;
44 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace;
45 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition;
46 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
47 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
48 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
49 import org.eclipse.tracecompass.tmf.ui.viewers.events.TmfEventsTable;
50
51 /**
52 * Output wizard page for custom text trace parsers.
53 *
54 * @author Patrick Tasse
55 */
56 public class CustomTxtParserOutputWizardPage extends WizardPage {
57
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$
60 private final CustomTxtParserWizard wizard;
61 private CustomTxtTraceDefinition definition;
62 private List<Output> outputs = new ArrayList<>();
63 private Composite container;
64 private SashForm sash;
65 private ScrolledComposite outputsScrolledComposite;
66 private Composite outputsContainer;
67 private Composite tableContainer;
68 private TmfEventsTable previewTable;
69 private File tmpFile;
70
71 /**
72 * Constructor
73 *
74 * @param wizard
75 * The wizard to which this page belongs
76 */
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);
114 previewTable = new TmfEventsTable(tableContainer, 0, CustomEventAspects.generateAspects(new CustomTxtTraceDefinition()));
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
130 private void loadDefinition(final CustomTxtTraceDefinition def) {
131 for (final OutputColumn outputColumn : def.outputs) {
132 final Output output = new Output(outputsContainer, outputColumn.tag, outputColumn.name);
133 outputs.add(output);
134 }
135 }
136
137 @Override
138 public void setVisible(final boolean visible) {
139 if (visible) {
140 this.definition = wizard.inputPage.getDefinition();
141 final List<Entry<Tag, String>> inputs = wizard.inputPage.getInputs();
142
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
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;
163 for (final Entry<Tag, String> input : inputs) {
164 if (output.tag.equals(input.getKey()) &&
165 output.name.equals(input.getValue())) {
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
177 for (final Entry<Tag, String> input : inputs) {
178 boolean found = false;
179 for (final Output output : outputs) {
180 if (output.tag.equals(input.getKey()) &&
181 output.name.equals(input.getValue())) {
182 found = true;
183 break;
184 }
185 }
186 if (!found) {
187 outputs.add(new Output(outputsContainer, input.getKey(), input.getValue()));
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();
241 tmpFile = Activator.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
242
243 try (final FileWriter writer = new FileWriter(tmpFile);) {
244 writer.write(wizard.inputPage.getInputText());
245 } catch (final IOException e) {
246 Activator.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$
247 }
248
249 try {
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 };
256 trace.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, false);
257 previewTable.dispose();
258 previewTable = new TmfEventsTable(tableContainer, CACHE_SIZE, CustomEventAspects.generateAspects(definition));
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$
263 }
264
265 tableContainer.layout();
266 container.layout();
267 }
268
269 /**
270 * Extract the list of output columns from the page's contents.
271 *
272 * @return The output columns
273 */
274 public List<OutputColumn> extractOutputs() {
275 final List<OutputColumn> outputColumns = new ArrayList<>();
276 for (Output output : outputs) {
277 if (output.enabledButton.getSelection()) {
278 final OutputColumn column = new OutputColumn(checkNotNull(output.tag), checkNotNull(output.name));
279 outputColumns.add(column);
280 }
281 }
282 return outputColumns;
283 }
284
285 private class Output {
286 Tag tag;
287 String name;
288 Button enabledButton;
289 Text nameLabel;
290 Button upButton;
291 Button downButton;
292
293 public Output(final Composite parent, final Tag tag, final String name) {
294 this.tag = tag;
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);
316 upButton.setImage(UP_IMAGE);
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);
327 downButton.setImage(DOWN_IMAGE);
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
346 /**
347 * Get the trace definition.
348 *
349 * @return The trace definition
350 */
351 public CustomTxtTraceDefinition getDefinition() {
352 return definition;
353 }
354
355 }
This page took 0.038111 seconds and 5 git commands to generate.