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