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
.CustomEventsTable
;
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
.swt
.SWT
;
34 import org
.eclipse
.swt
.custom
.SashForm
;
35 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
36 import org
.eclipse
.swt
.events
.SelectionAdapter
;
37 import org
.eclipse
.swt
.events
.SelectionEvent
;
38 import org
.eclipse
.swt
.graphics
.Image
;
39 import org
.eclipse
.swt
.layout
.GridData
;
40 import org
.eclipse
.swt
.layout
.GridLayout
;
41 import org
.eclipse
.swt
.widgets
.Button
;
42 import org
.eclipse
.swt
.widgets
.Composite
;
43 import org
.eclipse
.swt
.widgets
.Text
;
46 * Output wizard page for custom text trace parsers.
48 * @author Patrick Tasse
50 public class CustomTxtParserOutputWizardPage
extends WizardPage
{
52 private static final Image UP_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$
53 private static final Image DOWN_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$
54 private final CustomTxtParserWizard wizard
;
55 private CustomTxtTraceDefinition definition
;
56 private List
<Output
> outputs
= new ArrayList
<>();
57 private Composite container
;
58 private SashForm sash
;
59 private ScrolledComposite outputsScrolledComposite
;
60 private Composite outputsContainer
;
61 private Composite tableContainer
;
62 private CustomEventsTable previewTable
;
69 * The wizard to which this page belongs
71 protected CustomTxtParserOutputWizardPage(final CustomTxtParserWizard wizard
) {
72 super("CustomParserOutputWizardPage"); //$NON-NLS-1$
73 setTitle(wizard
.inputPage
.getTitle());
74 setDescription(Messages
.CustomTxtParserOutputWizardPage_description
);
76 setPageComplete(false);
80 public void createControl(final Composite parent
) {
81 container
= new Composite(parent
, SWT
.NULL
);
82 container
.setLayout(new GridLayout());
84 sash
= new SashForm(container
, SWT
.VERTICAL
);
85 sash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
86 sash
.setBackground(sash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
88 outputsScrolledComposite
= new ScrolledComposite(sash
, SWT
.V_SCROLL
);
89 outputsScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
90 outputsContainer
= new Composite(outputsScrolledComposite
, SWT
.NONE
);
91 final GridLayout outputsLayout
= new GridLayout(4, false);
92 outputsLayout
.marginHeight
= 10;
93 outputsLayout
.marginWidth
= 0;
94 outputsContainer
.setLayout(outputsLayout
);
95 outputsScrolledComposite
.setContent(outputsContainer
);
96 outputsScrolledComposite
.setExpandHorizontal(true);
97 outputsScrolledComposite
.setExpandVertical(true);
99 outputsContainer
.layout();
101 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
103 tableContainer
= new Composite(sash
, SWT
.NONE
);
104 final GridLayout tableLayout
= new GridLayout();
105 tableLayout
.marginHeight
= 0;
106 tableLayout
.marginWidth
= 0;
107 tableContainer
.setLayout(tableLayout
);
108 previewTable
= new CustomEventsTable(new CustomTxtTraceDefinition(), tableContainer
, 0);
109 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
111 if (wizard
.definition
!= null) {
112 loadDefinition(wizard
.definition
);
114 setControl(container
);
119 public void dispose() {
120 previewTable
.dispose();
124 private void loadDefinition(final CustomTxtTraceDefinition def
) {
125 for (final OutputColumn outputColumn
: def
.outputs
) {
126 final Output output
= new Output(outputsContainer
, outputColumn
.name
);
132 public void setVisible(final boolean visible
) {
134 this.definition
= wizard
.inputPage
.getDefinition();
135 final List
<String
> outputNames
= wizard
.inputPage
.getInputNames();
137 // dispose outputs that have been removed in the input page
138 final Iterator
<Output
> iter
= outputs
.iterator();
139 while (iter
.hasNext()) {
140 final Output output
= iter
.next();
141 boolean found
= false;
142 for (final String name
: outputNames
) {
143 if (output
.name
.equals(name
)) {
154 // create outputs that have been added in the input page
155 for (final String name
: outputNames
) {
156 boolean found
= false;
157 for (final Output output
: outputs
) {
158 if (output
.name
.equals(name
)) {
164 outputs
.add(new Output(outputsContainer
, name
));
168 outputsContainer
.layout();
169 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
170 updatePreviewTable();
171 if (sash
.getSize().y
> outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
+ previewTable
.getTable().getItemHeight()) {
172 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sash
.getSize().y
- outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
174 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, previewTable
.getTable().getItemHeight()});
176 setPageComplete(true);
178 setPageComplete(false);
180 super.setVisible(visible
);
183 private void moveBefore(final Output moved
) {
184 final int i
= outputs
.indexOf(moved
);
186 final Output previous
= outputs
.get(i
-1);
187 moved
.enabledButton
.moveAbove(previous
.enabledButton
);
188 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
189 moved
.upButton
.moveBelow(moved
.nameLabel
);
190 moved
.downButton
.moveBelow(moved
.upButton
);
191 outputs
.add(i
-1, outputs
.remove(i
));
192 outputsContainer
.layout();
193 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
195 updatePreviewTable();
199 private void moveAfter(final Output moved
) {
200 final int i
= outputs
.indexOf(moved
);
201 if (i
+1 < outputs
.size()) {
202 final Output next
= outputs
.get(i
+1);
203 moved
.enabledButton
.moveBelow(next
.downButton
);
204 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
205 moved
.upButton
.moveBelow(moved
.nameLabel
);
206 moved
.downButton
.moveBelow(moved
.upButton
);
207 outputs
.add(i
+1, outputs
.remove(i
));
208 outputsContainer
.layout();
209 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
211 updatePreviewTable();
215 private void updatePreviewTable() {
216 final int CACHE_SIZE
= 50;
217 definition
.outputs
= extractOutputs();
218 tmpFile
= Activator
.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
220 try (final FileWriter writer
= new FileWriter(tmpFile
);) {
221 writer
.write(wizard
.inputPage
.getInputText());
222 } catch (final IOException e
) {
223 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
227 final CustomTxtTrace trace
= new CustomTxtTrace(null, definition
, tmpFile
.getAbsolutePath(), CACHE_SIZE
) {
229 protected ITmfTraceIndexer
createIndexer(int interval
) {
230 return new TmfCheckpointIndexer(this, interval
);
233 trace
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, false);
234 previewTable
.dispose();
235 previewTable
= new CustomEventsTable(definition
, tableContainer
, CACHE_SIZE
);
236 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
237 previewTable
.setTrace(trace
, true);
238 } catch (final TmfTraceException e
) {
239 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
242 tableContainer
.layout();
247 * Extract the list of output columns from the page's contents.
249 * @return The output columns
251 public List
<OutputColumn
> extractOutputs() {
253 for (int i
= 0; i
< outputs
.size(); i
++) {
254 if (outputs
.get(i
).enabledButton
.getSelection()) {
258 final List
<OutputColumn
> outputColumns
= new ArrayList
<>(numColumns
);
260 for (int i
= 0; i
< outputs
.size(); i
++) {
261 final Output output
= outputs
.get(i
);
262 if (output
.enabledButton
.getSelection()) {
263 final OutputColumn column
= new OutputColumn();
264 column
.name
= output
.nameLabel
.getText();
265 outputColumns
.add(column
);
268 return outputColumns
;
271 private class Output
{
273 Button enabledButton
;
278 public Output(final Composite parent
, 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() {