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