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
.ArrayList
;
21 import java
.util
.Iterator
;
22 import java
.util
.List
;
23 import java
.util
.Map
.Entry
;
25 import org
.eclipse
.jface
.wizard
.WizardPage
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.custom
.SashForm
;
28 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
29 import org
.eclipse
.swt
.events
.SelectionAdapter
;
30 import org
.eclipse
.swt
.events
.SelectionEvent
;
31 import org
.eclipse
.swt
.graphics
.Image
;
32 import org
.eclipse
.swt
.layout
.GridData
;
33 import org
.eclipse
.swt
.layout
.GridLayout
;
34 import org
.eclipse
.swt
.widgets
.Button
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Text
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.parsers
.custom
.CustomEventAspects
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
39 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Messages
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.Tag
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTxtTrace
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTxtTraceDefinition
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.events
.TmfEventsTable
;
51 * Output wizard page for custom text trace parsers.
53 * @author Patrick Tasse
55 public class CustomTxtParserOutputWizardPage
extends WizardPage
{
57 private static final Image UP_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$
58 private static final Image DOWN_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$
59 private final CustomTxtParserWizard wizard
;
60 private CustomTxtTraceDefinition definition
;
61 private List
<Output
> outputs
= new ArrayList
<>();
62 private Composite container
;
63 private SashForm sash
;
64 private ScrolledComposite outputsScrolledComposite
;
65 private Composite outputsContainer
;
66 private Composite tableContainer
;
67 private TmfEventsTable previewTable
;
74 * The wizard to which this page belongs
76 protected CustomTxtParserOutputWizardPage(final CustomTxtParserWizard wizard
) {
77 super("CustomParserOutputWizardPage"); //$NON-NLS-1$
78 setTitle(wizard
.inputPage
.getTitle());
79 setDescription(Messages
.CustomTxtParserOutputWizardPage_description
);
81 setPageComplete(false);
85 public void createControl(final Composite parent
) {
86 container
= new Composite(parent
, SWT
.NULL
);
87 container
.setLayout(new GridLayout());
89 sash
= new SashForm(container
, SWT
.VERTICAL
);
90 sash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
91 sash
.setBackground(sash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
93 outputsScrolledComposite
= new ScrolledComposite(sash
, SWT
.V_SCROLL
);
94 outputsScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
95 outputsContainer
= new Composite(outputsScrolledComposite
, SWT
.NONE
);
96 final GridLayout outputsLayout
= new GridLayout(4, false);
97 outputsLayout
.marginHeight
= 10;
98 outputsLayout
.marginWidth
= 0;
99 outputsContainer
.setLayout(outputsLayout
);
100 outputsScrolledComposite
.setContent(outputsContainer
);
101 outputsScrolledComposite
.setExpandHorizontal(true);
102 outputsScrolledComposite
.setExpandVertical(true);
104 outputsContainer
.layout();
106 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
108 tableContainer
= new Composite(sash
, SWT
.NONE
);
109 final GridLayout tableLayout
= new GridLayout();
110 tableLayout
.marginHeight
= 0;
111 tableLayout
.marginWidth
= 0;
112 tableContainer
.setLayout(tableLayout
);
113 previewTable
= new TmfEventsTable(tableContainer
, 0, CustomEventAspects
.generateAspects(new CustomTxtTraceDefinition()));
114 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
116 if (wizard
.definition
!= null) {
117 loadDefinition(wizard
.definition
);
119 setControl(container
);
124 public void dispose() {
125 previewTable
.dispose();
129 private void loadDefinition(final CustomTxtTraceDefinition def
) {
130 for (final OutputColumn outputColumn
: def
.outputs
) {
131 final Output output
= new Output(outputsContainer
, outputColumn
.tag
, outputColumn
.name
);
137 public void setVisible(final boolean visible
) {
139 this.definition
= wizard
.inputPage
.getDefinition();
140 final List
<Entry
<Tag
, String
>> inputs
= wizard
.inputPage
.getInputs();
142 // dispose outputs that have been removed in the input page
143 final Iterator
<Output
> iter
= outputs
.iterator();
144 while (iter
.hasNext()) {
145 final Output output
= iter
.next();
146 boolean found
= false;
147 for (final Entry
<Tag
, String
> input
: inputs
) {
148 if (output
.tag
.equals(input
.getKey()) &&
149 output
.name
.equals(input
.getValue())) {
160 // create outputs that have been added in the input page
161 for (final Entry
<Tag
, String
> input
: inputs
) {
162 boolean found
= false;
163 for (final Output output
: outputs
) {
164 if (output
.tag
.equals(input
.getKey()) &&
165 output
.name
.equals(input
.getValue())) {
171 outputs
.add(new Output(outputsContainer
, input
.getKey(), input
.getValue()));
175 outputsContainer
.layout();
176 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
177 updatePreviewTable();
178 if (sash
.getSize().y
> outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
+ previewTable
.getTable().getItemHeight()) {
179 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sash
.getSize().y
- outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
181 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, previewTable
.getTable().getItemHeight()});
183 setPageComplete(true);
185 setPageComplete(false);
187 super.setVisible(visible
);
190 private void moveBefore(final Output moved
) {
191 final int i
= outputs
.indexOf(moved
);
193 final Output previous
= outputs
.get(i
-1);
194 moved
.enabledButton
.moveAbove(previous
.enabledButton
);
195 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
196 moved
.upButton
.moveBelow(moved
.nameLabel
);
197 moved
.downButton
.moveBelow(moved
.upButton
);
198 outputs
.add(i
-1, outputs
.remove(i
));
199 outputsContainer
.layout();
200 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
202 updatePreviewTable();
206 private void moveAfter(final Output moved
) {
207 final int i
= outputs
.indexOf(moved
);
208 if (i
+1 < outputs
.size()) {
209 final Output next
= outputs
.get(i
+1);
210 moved
.enabledButton
.moveBelow(next
.downButton
);
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 updatePreviewTable() {
223 final int CACHE_SIZE
= 50;
224 definition
.outputs
= extractOutputs();
225 tmpFile
= Activator
.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
227 try (final FileWriter writer
= new FileWriter(tmpFile
);) {
228 writer
.write(wizard
.inputPage
.getInputText());
229 } catch (final IOException e
) {
230 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
234 final CustomTxtTrace trace
= new CustomTxtTrace(null, definition
, tmpFile
.getAbsolutePath(), CACHE_SIZE
) {
236 protected ITmfTraceIndexer
createIndexer(int interval
) {
237 return new TmfCheckpointIndexer(this, interval
);
240 trace
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, false);
241 previewTable
.dispose();
242 previewTable
= new TmfEventsTable(tableContainer
, CACHE_SIZE
, CustomEventAspects
.generateAspects(definition
));
243 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
244 previewTable
.setTrace(trace
, true);
245 } catch (final TmfTraceException e
) {
246 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
249 tableContainer
.layout();
254 * Extract the list of output columns from the page's contents.
256 * @return The output columns
258 public List
<OutputColumn
> extractOutputs() {
259 final List
<OutputColumn
> outputColumns
= new ArrayList
<>();
260 for (Output output
: outputs
) {
261 if (output
.enabledButton
.getSelection()) {
262 final OutputColumn column
= new OutputColumn(checkNotNull(output
.tag
), checkNotNull(output
.name
));
263 outputColumns
.add(column
);
266 return outputColumns
;
269 private class Output
{
272 Button enabledButton
;
277 public Output(final Composite parent
, final Tag tag
, final String name
) {
281 enabledButton
= new Button(parent
, SWT
.CHECK
);
282 enabledButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_visible
);
283 enabledButton
.setSelection(true);
284 enabledButton
.addSelectionListener(new SelectionAdapter() {
286 public void widgetSelected(final SelectionEvent e
) {
287 updatePreviewTable();
290 // if (messageOutput != null) {
291 // enabledButton.moveAbove(messageOutput.enabledButton);
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
);
299 upButton
= new Button(parent
, SWT
.PUSH
);
300 upButton
.setImage(UP_IMAGE
);
301 upButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_moveBefore
);
302 upButton
.addSelectionListener(new SelectionAdapter() {
304 public void widgetSelected(final SelectionEvent e
) {
305 moveBefore(Output
.this);
308 upButton
.moveBelow(nameLabel
);
310 downButton
= new Button(parent
, SWT
.PUSH
);
311 downButton
.setImage(DOWN_IMAGE
);
312 downButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_moveAfter
);
313 downButton
.addSelectionListener(new SelectionAdapter() {
315 public void widgetSelected(final SelectionEvent e
) {
316 moveAfter(Output
.this);
319 downButton
.moveBelow(upButton
);
322 private void dispose() {
323 enabledButton
.dispose();
326 downButton
.dispose();
331 * Get the trace definition.
333 * @return The trace definition
335 public CustomTxtTraceDefinition
getDefinition() {