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
.linuxtools
.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
.linuxtools
.internal
.tmf
.ui
.Activator
;
24 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomEventTableColumns
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTxtTrace
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTxtTraceDefinition
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.events
.TmfEventsTable
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.custom
.SashForm
;
36 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
37 import org
.eclipse
.swt
.events
.SelectionAdapter
;
38 import org
.eclipse
.swt
.events
.SelectionEvent
;
39 import org
.eclipse
.swt
.graphics
.Image
;
40 import org
.eclipse
.swt
.layout
.GridData
;
41 import org
.eclipse
.swt
.layout
.GridLayout
;
42 import org
.eclipse
.swt
.widgets
.Button
;
43 import org
.eclipse
.swt
.widgets
.Composite
;
44 import org
.eclipse
.swt
.widgets
.Text
;
47 * Output wizard page for custom text trace parsers.
49 * @author Patrick Tasse
51 public class CustomTxtParserOutputWizardPage
extends WizardPage
{
53 private static final Image UP_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$
54 private static final Image DOWN_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$
55 private final CustomTxtParserWizard wizard
;
56 private CustomTxtTraceDefinition definition
;
57 private List
<Output
> outputs
= new ArrayList
<>();
58 private Composite container
;
59 private SashForm sash
;
60 private ScrolledComposite outputsScrolledComposite
;
61 private Composite outputsContainer
;
62 private Composite tableContainer
;
63 private TmfEventsTable previewTable
;
70 * The wizard to which this page belongs
72 protected CustomTxtParserOutputWizardPage(final CustomTxtParserWizard wizard
) {
73 super("CustomParserOutputWizardPage"); //$NON-NLS-1$
74 setTitle(wizard
.inputPage
.getTitle());
75 setDescription(Messages
.CustomTxtParserOutputWizardPage_description
);
77 setPageComplete(false);
81 public void createControl(final Composite parent
) {
82 container
= new Composite(parent
, SWT
.NULL
);
83 container
.setLayout(new GridLayout());
85 sash
= new SashForm(container
, SWT
.VERTICAL
);
86 sash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
87 sash
.setBackground(sash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
89 outputsScrolledComposite
= new ScrolledComposite(sash
, SWT
.V_SCROLL
);
90 outputsScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
91 outputsContainer
= new Composite(outputsScrolledComposite
, SWT
.NONE
);
92 final GridLayout outputsLayout
= new GridLayout(4, false);
93 outputsLayout
.marginHeight
= 10;
94 outputsLayout
.marginWidth
= 0;
95 outputsContainer
.setLayout(outputsLayout
);
96 outputsScrolledComposite
.setContent(outputsContainer
);
97 outputsScrolledComposite
.setExpandHorizontal(true);
98 outputsScrolledComposite
.setExpandVertical(true);
100 outputsContainer
.layout();
102 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
104 tableContainer
= new Composite(sash
, SWT
.NONE
);
105 final GridLayout tableLayout
= new GridLayout();
106 tableLayout
.marginHeight
= 0;
107 tableLayout
.marginWidth
= 0;
108 tableContainer
.setLayout(tableLayout
);
109 previewTable
= new TmfEventsTable(tableContainer
, 0, CustomEventTableColumns
.generateColumns(new CustomTxtTraceDefinition()));
110 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
112 if (wizard
.definition
!= null) {
113 loadDefinition(wizard
.definition
);
115 setControl(container
);
120 public void dispose() {
121 previewTable
.dispose();
125 private void loadDefinition(final CustomTxtTraceDefinition def
) {
126 for (final OutputColumn outputColumn
: def
.outputs
) {
127 final Output output
= new Output(outputsContainer
, outputColumn
.name
);
133 public void setVisible(final boolean visible
) {
135 this.definition
= wizard
.inputPage
.getDefinition();
136 final List
<String
> outputNames
= wizard
.inputPage
.getInputNames();
138 // dispose outputs that have been removed in the input page
139 final Iterator
<Output
> iter
= outputs
.iterator();
140 while (iter
.hasNext()) {
141 final Output output
= iter
.next();
142 boolean found
= false;
143 for (final String name
: outputNames
) {
144 if (output
.name
.equals(name
)) {
155 // create outputs that have been added in the input page
156 for (final String name
: outputNames
) {
157 boolean found
= false;
158 for (final Output output
: outputs
) {
159 if (output
.name
.equals(name
)) {
165 outputs
.add(new Output(outputsContainer
, name
));
169 outputsContainer
.layout();
170 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
171 updatePreviewTable();
172 if (sash
.getSize().y
> outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
+ previewTable
.getTable().getItemHeight()) {
173 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sash
.getSize().y
- outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
175 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, previewTable
.getTable().getItemHeight()});
177 setPageComplete(true);
179 setPageComplete(false);
181 super.setVisible(visible
);
184 private void moveBefore(final Output moved
) {
185 final int i
= outputs
.indexOf(moved
);
187 final Output previous
= outputs
.get(i
-1);
188 moved
.enabledButton
.moveAbove(previous
.enabledButton
);
189 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
190 moved
.upButton
.moveBelow(moved
.nameLabel
);
191 moved
.downButton
.moveBelow(moved
.upButton
);
192 outputs
.add(i
-1, outputs
.remove(i
));
193 outputsContainer
.layout();
194 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
196 updatePreviewTable();
200 private void moveAfter(final Output moved
) {
201 final int i
= outputs
.indexOf(moved
);
202 if (i
+1 < outputs
.size()) {
203 final Output next
= outputs
.get(i
+1);
204 moved
.enabledButton
.moveBelow(next
.downButton
);
205 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
206 moved
.upButton
.moveBelow(moved
.nameLabel
);
207 moved
.downButton
.moveBelow(moved
.upButton
);
208 outputs
.add(i
+1, outputs
.remove(i
));
209 outputsContainer
.layout();
210 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
212 updatePreviewTable();
216 private void updatePreviewTable() {
217 final int CACHE_SIZE
= 50;
218 definition
.outputs
= extractOutputs();
219 tmpFile
= Activator
.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
221 try (final FileWriter writer
= new FileWriter(tmpFile
);) {
222 writer
.write(wizard
.inputPage
.getInputText());
223 } catch (final IOException e
) {
224 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
228 final CustomTxtTrace trace
= new CustomTxtTrace(null, definition
, tmpFile
.getAbsolutePath(), CACHE_SIZE
) {
230 protected ITmfTraceIndexer
createIndexer(int interval
) {
231 return new TmfCheckpointIndexer(this, interval
);
234 trace
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, false);
235 previewTable
.dispose();
236 previewTable
= new TmfEventsTable(tableContainer
, CACHE_SIZE
, CustomEventTableColumns
.generateColumns(definition
));
237 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
238 previewTable
.setTrace(trace
, true);
239 } catch (final TmfTraceException e
) {
240 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
243 tableContainer
.layout();
248 * Extract the list of output columns from the page's contents.
250 * @return The output columns
252 public List
<OutputColumn
> extractOutputs() {
254 for (int i
= 0; i
< outputs
.size(); i
++) {
255 if (outputs
.get(i
).enabledButton
.getSelection()) {
259 final List
<OutputColumn
> outputColumns
= new ArrayList
<>(numColumns
);
261 for (int i
= 0; i
< outputs
.size(); i
++) {
262 final Output output
= outputs
.get(i
);
263 if (output
.enabledButton
.getSelection()) {
264 final OutputColumn column
= new OutputColumn();
265 column
.name
= output
.nameLabel
.getText();
266 outputColumns
.add(column
);
269 return outputColumns
;
272 private class Output
{
274 Button enabledButton
;
279 public Output(final Composite parent
, final String name
) {
282 enabledButton
= new Button(parent
, SWT
.CHECK
);
283 enabledButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_visible
);
284 enabledButton
.setSelection(true);
285 enabledButton
.addSelectionListener(new SelectionAdapter() {
287 public void widgetSelected(final SelectionEvent e
) {
288 updatePreviewTable();
291 // if (messageOutput != null) {
292 // enabledButton.moveAbove(messageOutput.enabledButton);
295 nameLabel
= new Text(parent
, SWT
.BORDER
| SWT
.READ_ONLY
| SWT
.SINGLE
);
296 nameLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
297 nameLabel
.setText(name
);
298 nameLabel
.moveBelow(enabledButton
);
300 upButton
= new Button(parent
, SWT
.PUSH
);
301 upButton
.setImage(UP_IMAGE
);
302 upButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_moveBefore
);
303 upButton
.addSelectionListener(new SelectionAdapter() {
305 public void widgetSelected(final SelectionEvent e
) {
306 moveBefore(Output
.this);
309 upButton
.moveBelow(nameLabel
);
311 downButton
= new Button(parent
, SWT
.PUSH
);
312 downButton
.setImage(DOWN_IMAGE
);
313 downButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_moveAfter
);
314 downButton
.addSelectionListener(new SelectionAdapter() {
316 public void widgetSelected(final SelectionEvent e
) {
317 moveAfter(Output
.this);
320 downButton
.moveBelow(upButton
);
323 private void dispose() {
324 enabledButton
.dispose();
327 downButton
.dispose();
332 * Get the trace definition.
334 * @return The trace definition
336 public CustomTxtTraceDefinition
getDefinition() {