tmf: Move plugins to their own sub-directory
[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, 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.tracecompass.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.swt.SWT;
24 import org.eclipse.swt.custom.SashForm;
25 import org.eclipse.swt.custom.ScrolledComposite;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Text;
34 import org.eclipse.tracecompass.internal.tmf.core.parsers.custom.CustomEventAspects;
35 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
36 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
37 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
38 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.OutputColumn;
39 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace;
40 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition;
41 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
42 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
43 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
44 import org.eclipse.tracecompass.tmf.ui.viewers.events.TmfEventsTable;
45
46 /**
47 * Output wizard page for custom text trace parsers.
48 *
49 * @author Patrick Tasse
50 */
51 public class CustomTxtParserOutputWizardPage extends WizardPage {
52
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$
55 private final CustomTxtParserWizard wizard;
56 private CustomTxtTraceDefinition definition;
57 private List<Output> outputs = new ArrayList<>();
58 private Composite container;
59 private SashForm sash;
60 private ScrolledComposite outputsScrolledComposite;
61 private Composite outputsContainer;
62 private Composite tableContainer;
63 private TmfEventsTable previewTable;
64 private File tmpFile;
65
66 /**
67 * Constructor
68 *
69 * @param wizard
70 * The wizard to which this page belongs
71 */
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);
109 previewTable = new TmfEventsTable(tableContainer, 0, CustomEventAspects.generateAspects(new CustomTxtTraceDefinition()));
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
125 private void loadDefinition(final CustomTxtTraceDefinition def) {
126 for (final OutputColumn outputColumn : def.outputs) {
127 final Output output = new Output(outputsContainer, outputColumn.name);
128 outputs.add(output);
129 }
130 }
131
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();
219 tmpFile = Activator.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
220
221 try (final FileWriter writer = new FileWriter(tmpFile);) {
222 writer.write(wizard.inputPage.getInputText());
223 } catch (final IOException e) {
224 Activator.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$
225 }
226
227 try {
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 };
234 trace.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, false);
235 previewTable.dispose();
236 previewTable = new TmfEventsTable(tableContainer, CACHE_SIZE, CustomEventAspects.generateAspects(definition));
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$
241 }
242
243 tableContainer.layout();
244 container.layout();
245 }
246
247 /**
248 * Extract the list of output columns from the page's contents.
249 *
250 * @return The output columns
251 */
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 }
259 final List<OutputColumn> outputColumns = new ArrayList<>(numColumns);
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);
301 upButton.setImage(UP_IMAGE);
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);
312 downButton.setImage(DOWN_IMAGE);
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
331 /**
332 * Get the trace definition.
333 *
334 * @return The trace definition
335 */
336 public CustomTxtTraceDefinition getDefinition() {
337 return definition;
338 }
339
340 }
This page took 0.0375 seconds and 5 git commands to generate.