1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 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
;
24 import org
.eclipse
.jface
.wizard
.WizardPage
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.swt
.custom
.SashForm
;
27 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
28 import org
.eclipse
.swt
.events
.SelectionAdapter
;
29 import org
.eclipse
.swt
.events
.SelectionEvent
;
30 import org
.eclipse
.swt
.graphics
.Image
;
31 import org
.eclipse
.swt
.layout
.GridData
;
32 import org
.eclipse
.swt
.layout
.GridLayout
;
33 import org
.eclipse
.swt
.widgets
.Button
;
34 import org
.eclipse
.swt
.widgets
.Composite
;
35 import org
.eclipse
.swt
.widgets
.Text
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.parsers
.custom
.CustomEventAspects
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Messages
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTxtTrace
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTxtTraceDefinition
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.events
.TmfEventsTable
;
49 * Output wizard page for custom text trace parsers.
51 * @author Patrick Tasse
53 public class CustomTxtParserOutputWizardPage
extends WizardPage
{
55 private static final Image UP_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$
56 private static final Image DOWN_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$
57 private final CustomTxtParserWizard wizard
;
58 private CustomTxtTraceDefinition definition
;
59 private List
<Output
> outputs
= new ArrayList
<>();
60 private Composite container
;
61 private SashForm sash
;
62 private ScrolledComposite outputsScrolledComposite
;
63 private Composite outputsContainer
;
64 private Composite tableContainer
;
65 private TmfEventsTable previewTable
;
72 * The wizard to which this page belongs
74 protected CustomTxtParserOutputWizardPage(final CustomTxtParserWizard wizard
) {
75 super("CustomParserOutputWizardPage"); //$NON-NLS-1$
76 setTitle(wizard
.inputPage
.getTitle());
77 setDescription(Messages
.CustomTxtParserOutputWizardPage_description
);
79 setPageComplete(false);
83 public void createControl(final Composite parent
) {
84 container
= new Composite(parent
, SWT
.NULL
);
85 container
.setLayout(new GridLayout());
87 sash
= new SashForm(container
, SWT
.VERTICAL
);
88 sash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
89 sash
.setBackground(sash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
91 outputsScrolledComposite
= new ScrolledComposite(sash
, SWT
.V_SCROLL
);
92 outputsScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
93 outputsContainer
= new Composite(outputsScrolledComposite
, SWT
.NONE
);
94 final GridLayout outputsLayout
= new GridLayout(4, false);
95 outputsLayout
.marginHeight
= 10;
96 outputsLayout
.marginWidth
= 0;
97 outputsContainer
.setLayout(outputsLayout
);
98 outputsScrolledComposite
.setContent(outputsContainer
);
99 outputsScrolledComposite
.setExpandHorizontal(true);
100 outputsScrolledComposite
.setExpandVertical(true);
102 outputsContainer
.layout();
104 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
106 tableContainer
= new Composite(sash
, SWT
.NONE
);
107 final GridLayout tableLayout
= new GridLayout();
108 tableLayout
.marginHeight
= 0;
109 tableLayout
.marginWidth
= 0;
110 tableContainer
.setLayout(tableLayout
);
111 previewTable
= new TmfEventsTable(tableContainer
, 0, CustomEventAspects
.generateAspects(new CustomTxtTraceDefinition()));
112 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
114 if (wizard
.definition
!= null) {
115 loadDefinition(wizard
.definition
);
117 setControl(container
);
122 public void dispose() {
123 previewTable
.dispose();
127 private void loadDefinition(final CustomTxtTraceDefinition def
) {
128 for (final OutputColumn outputColumn
: def
.outputs
) {
129 final Output output
= new Output(outputsContainer
, outputColumn
.name
);
135 public void setVisible(final boolean visible
) {
137 this.definition
= wizard
.inputPage
.getDefinition();
138 final List
<String
> outputNames
= wizard
.inputPage
.getInputNames();
140 // dispose outputs that have been removed in the input page
141 final Iterator
<Output
> iter
= outputs
.iterator();
142 while (iter
.hasNext()) {
143 final Output output
= iter
.next();
144 boolean found
= false;
145 for (final String name
: outputNames
) {
146 if (output
.name
.equals(name
)) {
157 // create outputs that have been added in the input page
158 for (final String name
: outputNames
) {
159 boolean found
= false;
160 for (final Output output
: outputs
) {
161 if (output
.name
.equals(name
)) {
167 outputs
.add(new Output(outputsContainer
, name
));
171 outputsContainer
.layout();
172 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
173 updatePreviewTable();
174 if (sash
.getSize().y
> outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
+ previewTable
.getTable().getItemHeight()) {
175 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sash
.getSize().y
- outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
177 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, previewTable
.getTable().getItemHeight()});
179 setPageComplete(true);
181 setPageComplete(false);
183 super.setVisible(visible
);
186 private void moveBefore(final Output moved
) {
187 final int i
= outputs
.indexOf(moved
);
189 final Output previous
= outputs
.get(i
-1);
190 moved
.enabledButton
.moveAbove(previous
.enabledButton
);
191 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
192 moved
.upButton
.moveBelow(moved
.nameLabel
);
193 moved
.downButton
.moveBelow(moved
.upButton
);
194 outputs
.add(i
-1, outputs
.remove(i
));
195 outputsContainer
.layout();
196 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
198 updatePreviewTable();
202 private void moveAfter(final Output moved
) {
203 final int i
= outputs
.indexOf(moved
);
204 if (i
+1 < outputs
.size()) {
205 final Output next
= outputs
.get(i
+1);
206 moved
.enabledButton
.moveBelow(next
.downButton
);
207 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
208 moved
.upButton
.moveBelow(moved
.nameLabel
);
209 moved
.downButton
.moveBelow(moved
.upButton
);
210 outputs
.add(i
+1, outputs
.remove(i
));
211 outputsContainer
.layout();
212 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
214 updatePreviewTable();
218 private void updatePreviewTable() {
219 final int CACHE_SIZE
= 50;
220 definition
.outputs
= extractOutputs();
221 tmpFile
= Activator
.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
223 try (final FileWriter writer
= new FileWriter(tmpFile
);) {
224 writer
.write(wizard
.inputPage
.getInputText());
225 } catch (final IOException e
) {
226 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
230 final CustomTxtTrace trace
= new CustomTxtTrace(null, definition
, tmpFile
.getAbsolutePath(), CACHE_SIZE
) {
232 protected ITmfTraceIndexer
createIndexer(int interval
) {
233 return new TmfCheckpointIndexer(this, interval
);
236 trace
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, false);
237 previewTable
.dispose();
238 previewTable
= new TmfEventsTable(tableContainer
, CACHE_SIZE
, CustomEventAspects
.generateAspects(definition
));
239 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
240 previewTable
.setTrace(trace
, true);
241 } catch (final TmfTraceException e
) {
242 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
245 tableContainer
.layout();
250 * Extract the list of output columns from the page's contents.
252 * @return The output columns
254 public List
<OutputColumn
> extractOutputs() {
256 for (int i
= 0; i
< outputs
.size(); i
++) {
257 if (outputs
.get(i
).enabledButton
.getSelection()) {
261 final List
<OutputColumn
> outputColumns
= new ArrayList
<>(numColumns
);
263 for (int i
= 0; i
< outputs
.size(); i
++) {
264 final Output output
= outputs
.get(i
);
265 if (output
.enabledButton
.getSelection()) {
266 final OutputColumn column
= new OutputColumn();
267 column
.name
= checkNotNull(output
.nameLabel
.getText());
268 outputColumns
.add(column
);
271 return outputColumns
;
274 private class Output
{
276 Button enabledButton
;
281 public Output(final Composite parent
, final String name
) {
284 enabledButton
= new Button(parent
, SWT
.CHECK
);
285 enabledButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_visible
);
286 enabledButton
.setSelection(true);
287 enabledButton
.addSelectionListener(new SelectionAdapter() {
289 public void widgetSelected(final SelectionEvent e
) {
290 updatePreviewTable();
293 // if (messageOutput != null) {
294 // enabledButton.moveAbove(messageOutput.enabledButton);
297 nameLabel
= new Text(parent
, SWT
.BORDER
| SWT
.READ_ONLY
| SWT
.SINGLE
);
298 nameLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
299 nameLabel
.setText(name
);
300 nameLabel
.moveBelow(enabledButton
);
302 upButton
= new Button(parent
, SWT
.PUSH
);
303 upButton
.setImage(UP_IMAGE
);
304 upButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_moveBefore
);
305 upButton
.addSelectionListener(new SelectionAdapter() {
307 public void widgetSelected(final SelectionEvent e
) {
308 moveBefore(Output
.this);
311 upButton
.moveBelow(nameLabel
);
313 downButton
= new Button(parent
, SWT
.PUSH
);
314 downButton
.setImage(DOWN_IMAGE
);
315 downButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_moveAfter
);
316 downButton
.addSelectionListener(new SelectionAdapter() {
318 public void widgetSelected(final SelectionEvent e
) {
319 moveAfter(Output
.this);
322 downButton
.moveBelow(upButton
);
325 private void dispose() {
326 enabledButton
.dispose();
329 downButton
.dispose();
334 * Get the trace definition.
336 * @return The trace definition
338 public CustomTxtTraceDefinition
getDefinition() {