1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.parsers
.wizards
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
18 import java
.io
.FileWriter
;
19 import java
.io
.IOException
;
20 import java
.util
.AbstractMap
.SimpleEntry
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Iterator
;
23 import java
.util
.List
;
24 import java
.util
.Map
.Entry
;
26 import org
.eclipse
.jface
.wizard
.WizardPage
;
27 import org
.eclipse
.swt
.SWT
;
28 import org
.eclipse
.swt
.custom
.SashForm
;
29 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
30 import org
.eclipse
.swt
.events
.SelectionAdapter
;
31 import org
.eclipse
.swt
.events
.SelectionEvent
;
32 import org
.eclipse
.swt
.graphics
.Image
;
33 import org
.eclipse
.swt
.layout
.GridData
;
34 import org
.eclipse
.swt
.layout
.GridLayout
;
35 import org
.eclipse
.swt
.widgets
.Button
;
36 import org
.eclipse
.swt
.widgets
.Composite
;
37 import org
.eclipse
.swt
.widgets
.Text
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.parsers
.custom
.CustomEventAspects
;
39 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
40 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Messages
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.Tag
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTxtTrace
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTxtTraceDefinition
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.events
.TmfEventsTable
;
52 * Output wizard page for custom text trace parsers.
54 * @author Patrick Tasse
56 public class CustomTxtParserOutputWizardPage
extends WizardPage
{
58 private static final Image UP_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$
59 private static final Image DOWN_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$
60 private final CustomTxtParserWizard wizard
;
61 private CustomTxtTraceDefinition definition
;
62 private List
<Output
> outputs
= new ArrayList
<>();
63 private Composite container
;
64 private SashForm sash
;
65 private ScrolledComposite outputsScrolledComposite
;
66 private Composite outputsContainer
;
67 private Composite tableContainer
;
68 private TmfEventsTable previewTable
;
75 * The wizard to which this page belongs
77 protected CustomTxtParserOutputWizardPage(final CustomTxtParserWizard wizard
) {
78 super("CustomParserOutputWizardPage"); //$NON-NLS-1$
79 setTitle(wizard
.inputPage
.getTitle());
80 setDescription(Messages
.CustomTxtParserOutputWizardPage_description
);
82 setPageComplete(false);
86 public void createControl(final Composite parent
) {
87 container
= new Composite(parent
, SWT
.NULL
);
88 container
.setLayout(new GridLayout());
90 sash
= new SashForm(container
, SWT
.VERTICAL
);
91 sash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
92 sash
.setBackground(sash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
94 outputsScrolledComposite
= new ScrolledComposite(sash
, SWT
.V_SCROLL
);
95 outputsScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
96 outputsContainer
= new Composite(outputsScrolledComposite
, SWT
.NONE
);
97 final GridLayout outputsLayout
= new GridLayout(4, false);
98 outputsLayout
.marginHeight
= 10;
99 outputsLayout
.marginWidth
= 0;
100 outputsContainer
.setLayout(outputsLayout
);
101 outputsScrolledComposite
.setContent(outputsContainer
);
102 outputsScrolledComposite
.setExpandHorizontal(true);
103 outputsScrolledComposite
.setExpandVertical(true);
105 outputsContainer
.layout();
107 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
109 tableContainer
= new Composite(sash
, SWT
.NONE
);
110 final GridLayout tableLayout
= new GridLayout();
111 tableLayout
.marginHeight
= 0;
112 tableLayout
.marginWidth
= 0;
113 tableContainer
.setLayout(tableLayout
);
114 previewTable
= new TmfEventsTable(tableContainer
, 0, CustomEventAspects
.generateAspects(new CustomTxtTraceDefinition()));
115 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
117 if (wizard
.definition
!= null) {
118 loadDefinition(wizard
.definition
);
120 setControl(container
);
125 public void dispose() {
126 previewTable
.dispose();
130 private void loadDefinition(final CustomTxtTraceDefinition def
) {
131 for (final OutputColumn outputColumn
: def
.outputs
) {
132 final Output output
= new Output(outputsContainer
, outputColumn
.tag
, outputColumn
.name
);
138 public void setVisible(final boolean visible
) {
140 this.definition
= wizard
.inputPage
.getDefinition();
141 final List
<Entry
<Tag
, String
>> inputs
= wizard
.inputPage
.getInputs();
143 // substitute extra field name/value with extra fields tag
144 Iterator
<Entry
<Tag
, String
>> iterator
= inputs
.iterator();
145 boolean addExtraFields
= false;
146 while (iterator
.hasNext()) {
147 Entry
<Tag
, String
> entry
= iterator
.next();
148 if (entry
.getKey().equals(Tag
.EXTRA_FIELD_NAME
) ||
149 entry
.getKey().equals(Tag
.EXTRA_FIELD_VALUE
)) {
151 addExtraFields
= true;
154 if (addExtraFields
) {
155 inputs
.add(new SimpleEntry
<>(Tag
.EXTRA_FIELDS
, Tag
.EXTRA_FIELDS
.toString()));
158 // dispose outputs that have been removed in the input page
159 final Iterator
<Output
> iter
= outputs
.iterator();
160 while (iter
.hasNext()) {
161 final Output output
= iter
.next();
162 boolean found
= false;
163 for (final Entry
<Tag
, String
> input
: inputs
) {
164 if (output
.tag
.equals(input
.getKey()) &&
165 output
.name
.equals(input
.getValue())) {
176 // create outputs that have been added in the input page
177 for (final Entry
<Tag
, String
> input
: inputs
) {
178 boolean found
= false;
179 for (final Output output
: outputs
) {
180 if (output
.tag
.equals(input
.getKey()) &&
181 output
.name
.equals(input
.getValue())) {
187 outputs
.add(new Output(outputsContainer
, input
.getKey(), input
.getValue()));
191 outputsContainer
.layout();
192 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
193 updatePreviewTable();
194 if (sash
.getSize().y
> outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
+ previewTable
.getTable().getItemHeight()) {
195 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sash
.getSize().y
- outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
197 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, previewTable
.getTable().getItemHeight()});
199 setPageComplete(true);
201 setPageComplete(false);
203 super.setVisible(visible
);
206 private void moveBefore(final Output moved
) {
207 final int i
= outputs
.indexOf(moved
);
209 final Output previous
= outputs
.get(i
-1);
210 moved
.enabledButton
.moveAbove(previous
.enabledButton
);
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);
218 updatePreviewTable();
222 private void moveAfter(final Output moved
) {
223 final int i
= outputs
.indexOf(moved
);
224 if (i
+1 < outputs
.size()) {
225 final Output next
= outputs
.get(i
+1);
226 moved
.enabledButton
.moveBelow(next
.downButton
);
227 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
228 moved
.upButton
.moveBelow(moved
.nameLabel
);
229 moved
.downButton
.moveBelow(moved
.upButton
);
230 outputs
.add(i
+1, outputs
.remove(i
));
231 outputsContainer
.layout();
232 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
234 updatePreviewTable();
238 private void updatePreviewTable() {
239 final int CACHE_SIZE
= 50;
240 definition
.outputs
= extractOutputs();
241 tmpFile
= Activator
.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
243 try (final FileWriter writer
= new FileWriter(tmpFile
);) {
244 writer
.write(wizard
.inputPage
.getInputText());
245 } catch (final IOException e
) {
246 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
250 final CustomTxtTrace trace
= new CustomTxtTrace(null, definition
, tmpFile
.getAbsolutePath(), CACHE_SIZE
) {
252 protected ITmfTraceIndexer
createIndexer(int interval
) {
253 return new TmfCheckpointIndexer(this, interval
);
256 trace
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, false);
257 previewTable
.dispose();
258 previewTable
= new TmfEventsTable(tableContainer
, CACHE_SIZE
, CustomEventAspects
.generateAspects(definition
));
259 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
260 previewTable
.setTrace(trace
, true);
261 } catch (final TmfTraceException e
) {
262 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
265 tableContainer
.layout();
270 * Extract the list of output columns from the page's contents.
272 * @return The output columns
274 public List
<OutputColumn
> extractOutputs() {
275 final List
<OutputColumn
> outputColumns
= new ArrayList
<>();
276 for (Output output
: outputs
) {
277 if (output
.enabledButton
.getSelection()) {
278 final OutputColumn column
= new OutputColumn(checkNotNull(output
.tag
), checkNotNull(output
.name
));
279 outputColumns
.add(column
);
282 return outputColumns
;
285 private class Output
{
288 Button enabledButton
;
293 public Output(final Composite parent
, final Tag tag
, final String name
) {
297 enabledButton
= new Button(parent
, SWT
.CHECK
);
298 enabledButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_visible
);
299 enabledButton
.setSelection(true);
300 enabledButton
.addSelectionListener(new SelectionAdapter() {
302 public void widgetSelected(final SelectionEvent e
) {
303 updatePreviewTable();
306 // if (messageOutput != null) {
307 // enabledButton.moveAbove(messageOutput.enabledButton);
310 nameLabel
= new Text(parent
, SWT
.BORDER
| SWT
.READ_ONLY
| SWT
.SINGLE
);
311 nameLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
312 nameLabel
.setText(name
);
313 nameLabel
.moveBelow(enabledButton
);
315 upButton
= new Button(parent
, SWT
.PUSH
);
316 upButton
.setImage(UP_IMAGE
);
317 upButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_moveBefore
);
318 upButton
.addSelectionListener(new SelectionAdapter() {
320 public void widgetSelected(final SelectionEvent e
) {
321 moveBefore(Output
.this);
324 upButton
.moveBelow(nameLabel
);
326 downButton
= new Button(parent
, SWT
.PUSH
);
327 downButton
.setImage(DOWN_IMAGE
);
328 downButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_moveAfter
);
329 downButton
.addSelectionListener(new SelectionAdapter() {
331 public void widgetSelected(final SelectionEvent e
) {
332 moveAfter(Output
.this);
335 downButton
.moveBelow(upButton
);
338 private void dispose() {
339 enabledButton
.dispose();
342 downButton
.dispose();
347 * Get the trace definition.
349 * @return The trace definition
351 public CustomTxtTraceDefinition
getDefinition() {