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