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
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTrace
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.events
.TmfEventsTable
;
50 * Output wizard page for custom XML trace parsers.
52 * @author Patrick Tasse
54 public class CustomXmlParserOutputWizardPage
extends WizardPage
{
56 private static final Image UP_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$
57 private static final Image DOWN_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$
58 private final CustomXmlParserWizard wizard
;
59 private CustomXmlTraceDefinition definition
;
60 private List
<Output
> outputs
= new ArrayList
<>();
61 private Composite container
;
62 private SashForm sash
;
63 private ScrolledComposite outputsScrolledComposite
;
64 private Composite outputsContainer
;
65 private Composite tableContainer
;
66 private TmfEventsTable previewTable
;
73 * The wizard to which this page belongs
75 protected CustomXmlParserOutputWizardPage(final CustomXmlParserWizard wizard
) {
76 super("CustomParserOutputWizardPage"); //$NON-NLS-1$
77 setTitle(wizard
.inputPage
.getTitle());
78 setDescription(Messages
.CustomXmlParserOutputWizardPage_description
);
80 setPageComplete(false);
84 public void createControl(final Composite parent
) {
85 container
= new Composite(parent
, SWT
.NULL
);
86 container
.setLayout(new GridLayout());
88 sash
= new SashForm(container
, SWT
.VERTICAL
);
89 sash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
90 sash
.setBackground(sash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
92 outputsScrolledComposite
= new ScrolledComposite(sash
, SWT
.V_SCROLL
);
93 outputsScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
94 outputsContainer
= new Composite(outputsScrolledComposite
, SWT
.NONE
);
95 final GridLayout outputsLayout
= new GridLayout(4, false);
96 outputsLayout
.marginHeight
= 10;
97 outputsLayout
.marginWidth
= 0;
98 outputsContainer
.setLayout(outputsLayout
);
99 outputsScrolledComposite
.setContent(outputsContainer
);
100 outputsScrolledComposite
.setExpandHorizontal(true);
101 outputsScrolledComposite
.setExpandVertical(true);
103 outputsContainer
.layout();
105 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
107 tableContainer
= new Composite(sash
, SWT
.NONE
);
108 final GridLayout tableLayout
= new GridLayout();
109 tableLayout
.marginHeight
= 0;
110 tableLayout
.marginWidth
= 0;
111 tableContainer
.setLayout(tableLayout
);
112 previewTable
= new TmfEventsTable(tableContainer
, 0, CustomEventAspects
.generateAspects(new CustomXmlTraceDefinition()));
113 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
115 if (wizard
.definition
!= null) {
116 loadDefinition(wizard
.definition
);
118 setControl(container
);
123 public void dispose() {
124 previewTable
.dispose();
128 private void loadDefinition(final CustomTraceDefinition def
) {
129 for (final OutputColumn outputColumn
: def
.outputs
) {
130 final Output output
= new Output(outputsContainer
, outputColumn
.name
);
136 public void setVisible(final boolean visible
) {
138 this.definition
= wizard
.inputPage
.getDefinition();
139 final List
<String
> outputNames
= wizard
.inputPage
.getInputNames();
141 // dispose outputs that have been removed in the input page
142 final Iterator
<Output
> iter
= outputs
.iterator();
143 while (iter
.hasNext()) {
144 final Output output
= iter
.next();
145 boolean found
= false;
146 for (final String name
: outputNames
) {
147 if (output
.name
.equals(name
)) {
158 // create outputs that have been added in the input page
159 for (final String name
: outputNames
) {
160 boolean found
= false;
161 for (final Output output
: outputs
) {
162 if (output
.name
.equals(name
)) {
168 outputs
.add(new Output(outputsContainer
, name
));
172 outputsContainer
.layout();
173 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
174 updatePreviewTable();
175 if (sash
.getSize().y
> outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
+ previewTable
.getTable().getItemHeight()) {
176 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sash
.getSize().y
- outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
178 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, previewTable
.getTable().getItemHeight()});
180 setPageComplete(true);
182 setPageComplete(false);
184 super.setVisible(visible
);
187 private void moveBefore(final Output moved
) {
188 final int i
= outputs
.indexOf(moved
);
190 final Output previous
= outputs
.get(i
-1);
191 moved
.enabledButton
.moveAbove(previous
.enabledButton
);
192 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
193 moved
.upButton
.moveBelow(moved
.nameLabel
);
194 moved
.downButton
.moveBelow(moved
.upButton
);
195 outputs
.add(i
-1, outputs
.remove(i
));
196 outputsContainer
.layout();
197 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
199 updatePreviewTable();
203 private void moveAfter(final Output moved
) {
204 final int i
= outputs
.indexOf(moved
);
205 if (i
+1 < outputs
.size()) {
206 final Output next
= outputs
.get(i
+1);
207 moved
.enabledButton
.moveBelow(next
.downButton
);
208 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
209 moved
.upButton
.moveBelow(moved
.nameLabel
);
210 moved
.downButton
.moveBelow(moved
.upButton
);
211 outputs
.add(i
+1, outputs
.remove(i
));
212 outputsContainer
.layout();
213 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
215 updatePreviewTable();
219 private void updatePreviewTable() {
220 final int CACHE_SIZE
= 50;
221 definition
.outputs
= extractOutputs();
222 tmpFile
= Activator
.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
224 try (final FileWriter writer
= new FileWriter(tmpFile
);) {
225 writer
.write(wizard
.inputPage
.getInputText());
226 } catch (final IOException e
) {
227 Activator
.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
231 final CustomXmlTrace trace
= new CustomXmlTrace(null, definition
, tmpFile
.getAbsolutePath(), CACHE_SIZE
) {
233 protected ITmfTraceIndexer
createIndexer(int interval
) {
234 return new TmfCheckpointIndexer(this, interval
);
237 trace
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, false);
238 previewTable
.dispose();
239 previewTable
= new TmfEventsTable(tableContainer
, CACHE_SIZE
, CustomEventAspects
.generateAspects(definition
));
240 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
241 previewTable
.setTrace(trace
, true);
242 } catch (final TmfTraceException e
) {
243 Activator
.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
246 tableContainer
.layout();
251 * Extract the output columns from the page's current contents.
253 * @return The list of output columns
255 public List
<OutputColumn
> extractOutputs() {
257 for (int i
= 0; i
< outputs
.size(); i
++) {
258 if (outputs
.get(i
).enabledButton
.getSelection()) {
262 final List
<OutputColumn
> outputColumns
= new ArrayList
<>(numColumns
);
264 for (int i
= 0; i
< outputs
.size(); i
++) {
265 final Output output
= outputs
.get(i
);
266 if (output
.enabledButton
.getSelection()) {
267 final OutputColumn column
= new OutputColumn();
268 column
.name
= checkNotNull(output
.nameLabel
.getText());
269 outputColumns
.add(column
);
272 return outputColumns
;
275 private class Output
{
277 Button enabledButton
;
282 public Output(final Composite parent
, final String name
) {
285 enabledButton
= new Button(parent
, SWT
.CHECK
);
286 enabledButton
.setToolTipText(Messages
.CustomXmlParserOutputWizardPage_visible
);
287 enabledButton
.setSelection(true);
288 enabledButton
.addSelectionListener(new SelectionAdapter() {
290 public void widgetSelected(final SelectionEvent e
) {
291 updatePreviewTable();
294 // if (messageOutput != null) {
295 // enabledButton.moveAbove(messageOutput.enabledButton);
298 nameLabel
= new Text(parent
, SWT
.BORDER
| SWT
.READ_ONLY
| SWT
.SINGLE
);
299 nameLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
300 nameLabel
.setText(name
);
301 nameLabel
.moveBelow(enabledButton
);
303 upButton
= new Button(parent
, SWT
.PUSH
);
304 upButton
.setImage(UP_IMAGE
);
305 upButton
.setToolTipText(Messages
.CustomXmlParserOutputWizardPage_moveBefore
);
306 upButton
.addSelectionListener(new SelectionAdapter() {
308 public void widgetSelected(final SelectionEvent e
) {
309 moveBefore(Output
.this);
312 upButton
.moveBelow(nameLabel
);
314 downButton
= new Button(parent
, SWT
.PUSH
);
315 downButton
.setImage(DOWN_IMAGE
);
316 downButton
.setToolTipText(Messages
.CustomXmlParserOutputWizardPage_moveAfter
);
317 downButton
.addSelectionListener(new SelectionAdapter() {
319 public void widgetSelected(final SelectionEvent e
) {
320 moveAfter(Output
.this);
323 downButton
.moveBelow(upButton
);
326 private void dispose() {
327 enabledButton
.dispose();
330 downButton
.dispose();
335 * Get the trace definition.
337 * @return The trace definition
339 public CustomXmlTraceDefinition
getDefinition() {