tmf: Bug 497038: Custom parser field names conflict with built-in tags
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / parsers / wizards / CustomXmlParserOutputWizardPage.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;
20import java.util.ArrayList;
21import java.util.Iterator;
22import java.util.List;
f5cc6ed1 23import java.util.Map.Entry;
6151d86c
PT
24
25import org.eclipse.jface.wizard.WizardPage;
6151d86c
PT
26import org.eclipse.swt.SWT;
27import org.eclipse.swt.custom.SashForm;
28import org.eclipse.swt.custom.ScrolledComposite;
29import org.eclipse.swt.events.SelectionAdapter;
30import org.eclipse.swt.events.SelectionEvent;
31import org.eclipse.swt.graphics.Image;
32import org.eclipse.swt.layout.GridData;
33import org.eclipse.swt.layout.GridLayout;
34import org.eclipse.swt.widgets.Button;
35import org.eclipse.swt.widgets.Composite;
36import org.eclipse.swt.widgets.Text;
b04903a2 37import org.eclipse.tracecompass.internal.tmf.core.parsers.custom.CustomEventAspects;
2bdf0193
AM
38import org.eclipse.tracecompass.internal.tmf.ui.Activator;
39import org.eclipse.tracecompass.internal.tmf.ui.Messages;
2bdf0193
AM
40import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
41import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition;
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.CustomXmlTrace;
45import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition;
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 XML trace parsers.
53 *
f6aa55e2 54 * @author Patrick Tasse
a0a88f65 55 */
6151d86c
PT
56public class CustomXmlParserOutputWizardPage 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 CustomXmlParserWizard wizard;
61 private CustomXmlTraceDefinition 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 CustomXmlParserOutputWizardPage(final CustomXmlParserWizard wizard) {
78 super("CustomParserOutputWizardPage"); //$NON-NLS-1$
79 setTitle(wizard.inputPage.getTitle());
80 setDescription(Messages.CustomXmlParserOutputWizardPage_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 CustomXmlTraceDefinition()));
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 CustomTraceDefinition 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
PT
142
143 // dispose outputs that have been removed in the input page
144 final Iterator<Output> iter = outputs.iterator();
145 while (iter.hasNext()) {
146 final Output output = iter.next();
147 boolean found = false;
f5cc6ed1
PT
148 for (final Entry<Tag, String> input : inputs) {
149 if (output.tag.equals(input.getKey()) &&
150 output.name.equals(input.getValue())) {
6151d86c
PT
151 found = true;
152 break;
153 }
154 }
155 if (!found) {
156 output.dispose();
157 iter.remove();
158 }
159 }
160
161 // create outputs that have been added in the input page
f5cc6ed1 162 for (final Entry<Tag, String> input : inputs) {
6151d86c
PT
163 boolean found = false;
164 for (final Output output : outputs) {
f5cc6ed1
PT
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) {
f5cc6ed1 172 outputs.add(new Output(outputsContainer, input.getKey(), input.getValue()));
6151d86c
PT
173 }
174 }
175
176 outputsContainer.layout();
177 outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5);
178 updatePreviewTable();
179 if (sash.getSize().y > outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y + previewTable.getTable().getItemHeight()) {
180 sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sash.getSize().y - outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y});
181 } else {
182 sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, previewTable.getTable().getItemHeight()});
183 }
184 setPageComplete(true);
185 } else {
186 setPageComplete(false);
187 }
188 super.setVisible(visible);
189 }
190
191 private void moveBefore(final Output moved) {
192 final int i = outputs.indexOf(moved);
193 if (i > 0) {
194 final Output previous = outputs.get(i-1);
195 moved.enabledButton.moveAbove(previous.enabledButton);
196 moved.nameLabel.moveBelow(moved.enabledButton);
197 moved.upButton.moveBelow(moved.nameLabel);
198 moved.downButton.moveBelow(moved.upButton);
199 outputs.add(i-1, outputs.remove(i));
200 outputsContainer.layout();
201 outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5);
202 container.layout();
203 updatePreviewTable();
204 }
205 }
206
207 private void moveAfter(final Output moved) {
208 final int i = outputs.indexOf(moved);
209 if (i+1 < outputs.size()) {
210 final Output next = outputs.get(i+1);
211 moved.enabledButton.moveBelow(next.downButton);
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 updatePreviewTable() {
224 final int CACHE_SIZE = 50;
225 definition.outputs = extractOutputs();
507b1336 226 tmpFile = Activator.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
6151d86c 227
507b1336 228 try (final FileWriter writer = new FileWriter(tmpFile);) {
6151d86c 229 writer.write(wizard.inputPage.getInputText());
507b1336
AM
230 } catch (final IOException e) {
231 Activator.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$
232 }
6151d86c 233
507b1336 234 try {
5ee05017
PT
235 final CustomXmlTrace trace = new CustomXmlTrace(null, definition, tmpFile.getAbsolutePath(), CACHE_SIZE) {
236 @Override
237 protected ITmfTraceIndexer createIndexer(int interval) {
238 return new TmfCheckpointIndexer(this, interval);
239 }
240 };
b0422293 241 trace.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, false);
6151d86c 242 previewTable.dispose();
b04903a2 243 previewTable = new TmfEventsTable(tableContainer, CACHE_SIZE, CustomEventAspects.generateAspects(definition));
6151d86c
PT
244 previewTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
245 previewTable.setTrace(trace, true);
246 } catch (final TmfTraceException e) {
247 Activator.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$
6151d86c
PT
248 }
249
250 tableContainer.layout();
251 container.layout();
252 }
253
a0a88f65
AM
254 /**
255 * Extract the output columns from the page's current contents.
256 *
257 * @return The list of output columns
258 */
6151d86c 259 public List<OutputColumn> extractOutputs() {
f5cc6ed1
PT
260 final List<OutputColumn> outputColumns = new ArrayList<>();
261 for (Output output : outputs) {
6151d86c 262 if (output.enabledButton.getSelection()) {
f5cc6ed1 263 final OutputColumn column = new OutputColumn(checkNotNull(output.tag), checkNotNull(output.name));
6151d86c
PT
264 outputColumns.add(column);
265 }
266 }
267 return outputColumns;
268 }
269
270 private class Output {
f5cc6ed1 271 Tag tag;
6151d86c
PT
272 String name;
273 Button enabledButton;
274 Text nameLabel;
275 Button upButton;
276 Button downButton;
277
f5cc6ed1
PT
278 public Output(final Composite parent, final Tag tag, final String name) {
279 this.tag = tag;
6151d86c
PT
280 this.name = name;
281
282 enabledButton = new Button(parent, SWT.CHECK);
283 enabledButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_visible);
284 enabledButton.setSelection(true);
285 enabledButton.addSelectionListener(new SelectionAdapter() {
286 @Override
287 public void widgetSelected(final SelectionEvent e) {
288 updatePreviewTable();
289 }
290 });
291 // if (messageOutput != null) {
292 // enabledButton.moveAbove(messageOutput.enabledButton);
293 // }
294
295 nameLabel = new Text(parent, SWT.BORDER | SWT.READ_ONLY | SWT.SINGLE);
296 nameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
297 nameLabel.setText(name);
298 nameLabel.moveBelow(enabledButton);
299
300 upButton = new Button(parent, SWT.PUSH);
f6aa55e2 301 upButton.setImage(UP_IMAGE);
6151d86c
PT
302 upButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_moveBefore);
303 upButton.addSelectionListener(new SelectionAdapter() {
304 @Override
305 public void widgetSelected(final SelectionEvent e) {
306 moveBefore(Output.this);
307 }
308 });
309 upButton.moveBelow(nameLabel);
310
311 downButton = new Button(parent, SWT.PUSH);
f6aa55e2 312 downButton.setImage(DOWN_IMAGE);
6151d86c
PT
313 downButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_moveAfter);
314 downButton.addSelectionListener(new SelectionAdapter() {
315 @Override
316 public void widgetSelected(final SelectionEvent e) {
317 moveAfter(Output.this);
318 }
319 });
320 downButton.moveBelow(upButton);
321 }
322
323 private void dispose() {
324 enabledButton.dispose();
325 nameLabel.dispose();
326 upButton.dispose();
327 downButton.dispose();
328 }
329 }
330
a0a88f65
AM
331 /**
332 * Get the trace definition.
333 *
334 * @return The trace definition
335 */
6151d86c
PT
336 public CustomXmlTraceDefinition getDefinition() {
337 return definition;
338 }
339
340}
This page took 0.116878 seconds and 5 git commands to generate.