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