1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 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
;
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
;
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
.ui
.Activator
;
35 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Messages
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.parsers
.custom
.CustomEventTableColumns
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTrace
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
45 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.events
.TmfEventsTable
;
48 * Output wizard page for custom XML trace parsers.
50 * @author Patrick Tasse
52 public class CustomXmlParserOutputWizardPage
extends WizardPage
{
54 private static final Image UP_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$
55 private static final Image DOWN_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$
56 private final CustomXmlParserWizard wizard
;
57 private CustomXmlTraceDefinition definition
;
58 private List
<Output
> outputs
= new ArrayList
<>();
59 private Composite container
;
60 private SashForm sash
;
61 private ScrolledComposite outputsScrolledComposite
;
62 private Composite outputsContainer
;
63 private Composite tableContainer
;
64 private TmfEventsTable previewTable
;
71 * The wizard to which this page belongs
73 protected CustomXmlParserOutputWizardPage(final CustomXmlParserWizard wizard
) {
74 super("CustomParserOutputWizardPage"); //$NON-NLS-1$
75 setTitle(wizard
.inputPage
.getTitle());
76 setDescription(Messages
.CustomXmlParserOutputWizardPage_description
);
78 setPageComplete(false);
82 public void createControl(final Composite parent
) {
83 container
= new Composite(parent
, SWT
.NULL
);
84 container
.setLayout(new GridLayout());
86 sash
= new SashForm(container
, SWT
.VERTICAL
);
87 sash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
88 sash
.setBackground(sash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
90 outputsScrolledComposite
= new ScrolledComposite(sash
, SWT
.V_SCROLL
);
91 outputsScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
92 outputsContainer
= new Composite(outputsScrolledComposite
, SWT
.NONE
);
93 final GridLayout outputsLayout
= new GridLayout(4, false);
94 outputsLayout
.marginHeight
= 10;
95 outputsLayout
.marginWidth
= 0;
96 outputsContainer
.setLayout(outputsLayout
);
97 outputsScrolledComposite
.setContent(outputsContainer
);
98 outputsScrolledComposite
.setExpandHorizontal(true);
99 outputsScrolledComposite
.setExpandVertical(true);
101 outputsContainer
.layout();
103 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
105 tableContainer
= new Composite(sash
, SWT
.NONE
);
106 final GridLayout tableLayout
= new GridLayout();
107 tableLayout
.marginHeight
= 0;
108 tableLayout
.marginWidth
= 0;
109 tableContainer
.setLayout(tableLayout
);
110 previewTable
= new TmfEventsTable(tableContainer
, 0, CustomEventTableColumns
.generateColumns(new CustomXmlTraceDefinition()));
111 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
113 if (wizard
.definition
!= null) {
114 loadDefinition(wizard
.definition
);
116 setControl(container
);
121 public void dispose() {
122 previewTable
.dispose();
126 private void loadDefinition(final CustomTraceDefinition def
) {
127 for (final OutputColumn outputColumn
: def
.outputs
) {
128 final Output output
= new Output(outputsContainer
, outputColumn
.name
);
134 public void setVisible(final boolean visible
) {
136 this.definition
= wizard
.inputPage
.getDefinition();
137 final List
<String
> outputNames
= wizard
.inputPage
.getInputNames();
139 // dispose outputs that have been removed in the input page
140 final Iterator
<Output
> iter
= outputs
.iterator();
141 while (iter
.hasNext()) {
142 final Output output
= iter
.next();
143 boolean found
= false;
144 for (final String name
: outputNames
) {
145 if (output
.name
.equals(name
)) {
156 // create outputs that have been added in the input page
157 for (final String name
: outputNames
) {
158 boolean found
= false;
159 for (final Output output
: outputs
) {
160 if (output
.name
.equals(name
)) {
166 outputs
.add(new Output(outputsContainer
, name
));
170 outputsContainer
.layout();
171 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
172 updatePreviewTable();
173 if (sash
.getSize().y
> outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
+ previewTable
.getTable().getItemHeight()) {
174 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sash
.getSize().y
- outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
176 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, previewTable
.getTable().getItemHeight()});
178 setPageComplete(true);
180 setPageComplete(false);
182 super.setVisible(visible
);
185 private void moveBefore(final Output moved
) {
186 final int i
= outputs
.indexOf(moved
);
188 final Output previous
= outputs
.get(i
-1);
189 moved
.enabledButton
.moveAbove(previous
.enabledButton
);
190 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
191 moved
.upButton
.moveBelow(moved
.nameLabel
);
192 moved
.downButton
.moveBelow(moved
.upButton
);
193 outputs
.add(i
-1, outputs
.remove(i
));
194 outputsContainer
.layout();
195 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
197 updatePreviewTable();
201 private void moveAfter(final Output moved
) {
202 final int i
= outputs
.indexOf(moved
);
203 if (i
+1 < outputs
.size()) {
204 final Output next
= outputs
.get(i
+1);
205 moved
.enabledButton
.moveBelow(next
.downButton
);
206 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
207 moved
.upButton
.moveBelow(moved
.nameLabel
);
208 moved
.downButton
.moveBelow(moved
.upButton
);
209 outputs
.add(i
+1, outputs
.remove(i
));
210 outputsContainer
.layout();
211 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
213 updatePreviewTable();
217 private void updatePreviewTable() {
218 final int CACHE_SIZE
= 50;
219 definition
.outputs
= extractOutputs();
220 tmpFile
= Activator
.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
222 try (final FileWriter writer
= new FileWriter(tmpFile
);) {
223 writer
.write(wizard
.inputPage
.getInputText());
224 } catch (final IOException e
) {
225 Activator
.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
229 final CustomXmlTrace trace
= new CustomXmlTrace(null, definition
, tmpFile
.getAbsolutePath(), CACHE_SIZE
) {
231 protected ITmfTraceIndexer
createIndexer(int interval
) {
232 return new TmfCheckpointIndexer(this, interval
);
235 trace
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, false);
236 previewTable
.dispose();
237 previewTable
= new TmfEventsTable(tableContainer
, CACHE_SIZE
, CustomEventTableColumns
.generateColumns(definition
));
238 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
239 previewTable
.setTrace(trace
, true);
240 } catch (final TmfTraceException e
) {
241 Activator
.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
244 tableContainer
.layout();
249 * Extract the output columns from the page's current contents.
251 * @return The list of output columns
253 public List
<OutputColumn
> extractOutputs() {
255 for (int i
= 0; i
< outputs
.size(); i
++) {
256 if (outputs
.get(i
).enabledButton
.getSelection()) {
260 final List
<OutputColumn
> outputColumns
= new ArrayList
<>(numColumns
);
262 for (int i
= 0; i
< outputs
.size(); i
++) {
263 final Output output
= outputs
.get(i
);
264 if (output
.enabledButton
.getSelection()) {
265 final OutputColumn column
= new OutputColumn();
266 column
.name
= output
.nameLabel
.getText();
267 outputColumns
.add(column
);
270 return outputColumns
;
273 private class Output
{
275 Button enabledButton
;
280 public Output(final Composite parent
, final String name
) {
283 enabledButton
= new Button(parent
, SWT
.CHECK
);
284 enabledButton
.setToolTipText(Messages
.CustomXmlParserOutputWizardPage_visible
);
285 enabledButton
.setSelection(true);
286 enabledButton
.addSelectionListener(new SelectionAdapter() {
288 public void widgetSelected(final SelectionEvent e
) {
289 updatePreviewTable();
292 // if (messageOutput != null) {
293 // enabledButton.moveAbove(messageOutput.enabledButton);
296 nameLabel
= new Text(parent
, SWT
.BORDER
| SWT
.READ_ONLY
| SWT
.SINGLE
);
297 nameLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
298 nameLabel
.setText(name
);
299 nameLabel
.moveBelow(enabledButton
);
301 upButton
= new Button(parent
, SWT
.PUSH
);
302 upButton
.setImage(UP_IMAGE
);
303 upButton
.setToolTipText(Messages
.CustomXmlParserOutputWizardPage_moveBefore
);
304 upButton
.addSelectionListener(new SelectionAdapter() {
306 public void widgetSelected(final SelectionEvent e
) {
307 moveBefore(Output
.this);
310 upButton
.moveBelow(nameLabel
);
312 downButton
= new Button(parent
, SWT
.PUSH
);
313 downButton
.setImage(DOWN_IMAGE
);
314 downButton
.setToolTipText(Messages
.CustomXmlParserOutputWizardPage_moveAfter
);
315 downButton
.addSelectionListener(new SelectionAdapter() {
317 public void widgetSelected(final SelectionEvent e
) {
318 moveAfter(Output
.this);
321 downButton
.moveBelow(upButton
);
324 private void dispose() {
325 enabledButton
.dispose();
328 downButton
.dispose();
333 * Get the trace definition.
335 * @return The trace definition
337 public CustomXmlTraceDefinition
getDefinition() {