1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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
.CustomTxtTrace
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTxtTraceDefinition
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
31 import org
.eclipse
.swt
.SWT
;
32 import org
.eclipse
.swt
.custom
.SashForm
;
33 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
34 import org
.eclipse
.swt
.events
.SelectionAdapter
;
35 import org
.eclipse
.swt
.events
.SelectionEvent
;
36 import org
.eclipse
.swt
.graphics
.Image
;
37 import org
.eclipse
.swt
.layout
.GridData
;
38 import org
.eclipse
.swt
.layout
.GridLayout
;
39 import org
.eclipse
.swt
.widgets
.Button
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.swt
.widgets
.Text
;
44 * Output wizard page for custom text trace parsers.
46 * @author Patrick Tasse
48 public class CustomTxtParserOutputWizardPage
extends WizardPage
{
50 private static final Image UP_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$
51 private static final Image DOWN_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$
52 private final CustomTxtParserWizard wizard
;
53 private CustomTxtTraceDefinition definition
;
54 private List
<Output
> outputs
= new ArrayList
<>();
55 private Composite container
;
56 private SashForm sash
;
57 private ScrolledComposite outputsScrolledComposite
;
58 private Composite outputsContainer
;
59 private Composite tableContainer
;
60 private CustomEventsTable previewTable
;
67 * The wizard to which this page belongs
69 protected CustomTxtParserOutputWizardPage(final CustomTxtParserWizard wizard
) {
70 super("CustomParserOutputWizardPage"); //$NON-NLS-1$
71 setTitle(wizard
.inputPage
.getTitle());
72 setDescription(Messages
.CustomTxtParserOutputWizardPage_description
);
74 setPageComplete(false);
78 public void createControl(final Composite parent
) {
79 container
= new Composite(parent
, SWT
.NULL
);
80 container
.setLayout(new GridLayout());
82 sash
= new SashForm(container
, SWT
.VERTICAL
);
83 sash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
84 sash
.setBackground(sash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
86 outputsScrolledComposite
= new ScrolledComposite(sash
, SWT
.V_SCROLL
);
87 outputsScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
88 outputsContainer
= new Composite(outputsScrolledComposite
, SWT
.NONE
);
89 final GridLayout outputsLayout
= new GridLayout(4, false);
90 outputsLayout
.marginHeight
= 10;
91 outputsLayout
.marginWidth
= 0;
92 outputsContainer
.setLayout(outputsLayout
);
93 outputsScrolledComposite
.setContent(outputsContainer
);
94 outputsScrolledComposite
.setExpandHorizontal(true);
95 outputsScrolledComposite
.setExpandVertical(true);
97 outputsContainer
.layout();
99 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
101 tableContainer
= new Composite(sash
, SWT
.NONE
);
102 final GridLayout tableLayout
= new GridLayout();
103 tableLayout
.marginHeight
= 0;
104 tableLayout
.marginWidth
= 0;
105 tableContainer
.setLayout(tableLayout
);
106 previewTable
= new CustomEventsTable(new CustomTxtTraceDefinition(), tableContainer
, 0);
107 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
109 if (wizard
.definition
!= null) {
110 loadDefinition(wizard
.definition
);
112 setControl(container
);
117 public void dispose() {
118 previewTable
.dispose();
122 private void loadDefinition(final CustomTxtTraceDefinition def
) {
123 for (final OutputColumn outputColumn
: def
.outputs
) {
124 final Output output
= new Output(outputsContainer
, outputColumn
.name
);
130 public void setVisible(final boolean visible
) {
132 this.definition
= wizard
.inputPage
.getDefinition();
133 final List
<String
> outputNames
= wizard
.inputPage
.getInputNames();
135 // dispose outputs that have been removed in the input page
136 final Iterator
<Output
> iter
= outputs
.iterator();
137 while (iter
.hasNext()) {
138 final Output output
= iter
.next();
139 boolean found
= false;
140 for (final String name
: outputNames
) {
141 if (output
.name
.equals(name
)) {
152 // create outputs that have been added in the input page
153 for (final String name
: outputNames
) {
154 boolean found
= false;
155 for (final Output output
: outputs
) {
156 if (output
.name
.equals(name
)) {
162 outputs
.add(new Output(outputsContainer
, name
));
166 outputsContainer
.layout();
167 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
168 updatePreviewTable();
169 if (sash
.getSize().y
> outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
+ previewTable
.getTable().getItemHeight()) {
170 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sash
.getSize().y
- outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
172 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, previewTable
.getTable().getItemHeight()});
174 setPageComplete(true);
176 setPageComplete(false);
178 super.setVisible(visible
);
181 private void moveBefore(final Output moved
) {
182 final int i
= outputs
.indexOf(moved
);
184 final Output previous
= outputs
.get(i
-1);
185 moved
.enabledButton
.moveAbove(previous
.enabledButton
);
186 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
187 moved
.upButton
.moveBelow(moved
.nameLabel
);
188 moved
.downButton
.moveBelow(moved
.upButton
);
189 outputs
.add(i
-1, outputs
.remove(i
));
190 outputsContainer
.layout();
191 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
193 updatePreviewTable();
197 private void moveAfter(final Output moved
) {
198 final int i
= outputs
.indexOf(moved
);
199 if (i
+1 < outputs
.size()) {
200 final Output next
= outputs
.get(i
+1);
201 moved
.enabledButton
.moveBelow(next
.downButton
);
202 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
203 moved
.upButton
.moveBelow(moved
.nameLabel
);
204 moved
.downButton
.moveBelow(moved
.upButton
);
205 outputs
.add(i
+1, outputs
.remove(i
));
206 outputsContainer
.layout();
207 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
209 updatePreviewTable();
213 private void updatePreviewTable() {
214 final int CACHE_SIZE
= 50;
215 definition
.outputs
= extractOutputs();
216 tmpFile
= Activator
.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
218 try (final FileWriter writer
= new FileWriter(tmpFile
);) {
219 writer
.write(wizard
.inputPage
.getInputText());
220 } catch (final IOException e
) {
221 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
225 final CustomTxtTrace trace
= new CustomTxtTrace(null, definition
, tmpFile
.getAbsolutePath(), CACHE_SIZE
);
226 trace
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, false);
227 previewTable
.dispose();
228 previewTable
= new CustomEventsTable(definition
, tableContainer
, CACHE_SIZE
);
229 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
230 previewTable
.setTrace(trace
, true);
231 } catch (final TmfTraceException e
) {
232 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
235 tableContainer
.layout();
240 * Extract the list of output columns from the page's contents.
242 * @return The output columns
244 public List
<OutputColumn
> extractOutputs() {
246 for (int i
= 0; i
< outputs
.size(); i
++) {
247 if (outputs
.get(i
).enabledButton
.getSelection()) {
251 final List
<OutputColumn
> outputColumns
= new ArrayList
<>(numColumns
);
253 for (int i
= 0; i
< outputs
.size(); i
++) {
254 final Output output
= outputs
.get(i
);
255 if (output
.enabledButton
.getSelection()) {
256 final OutputColumn column
= new OutputColumn();
257 column
.name
= output
.nameLabel
.getText();
258 outputColumns
.add(column
);
261 return outputColumns
;
264 private class Output
{
266 Button enabledButton
;
271 public Output(final Composite parent
, final String name
) {
274 enabledButton
= new Button(parent
, SWT
.CHECK
);
275 enabledButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_visible
);
276 enabledButton
.setSelection(true);
277 enabledButton
.addSelectionListener(new SelectionAdapter() {
279 public void widgetSelected(final SelectionEvent e
) {
280 updatePreviewTable();
283 // if (messageOutput != null) {
284 // enabledButton.moveAbove(messageOutput.enabledButton);
287 nameLabel
= new Text(parent
, SWT
.BORDER
| SWT
.READ_ONLY
| SWT
.SINGLE
);
288 nameLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
289 nameLabel
.setText(name
);
290 nameLabel
.moveBelow(enabledButton
);
292 upButton
= new Button(parent
, SWT
.PUSH
);
293 upButton
.setImage(UP_IMAGE
);
294 upButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_moveBefore
);
295 upButton
.addSelectionListener(new SelectionAdapter() {
297 public void widgetSelected(final SelectionEvent e
) {
298 moveBefore(Output
.this);
301 upButton
.moveBelow(nameLabel
);
303 downButton
= new Button(parent
, SWT
.PUSH
);
304 downButton
.setImage(DOWN_IMAGE
);
305 downButton
.setToolTipText(Messages
.CustomTxtParserOutputWizardPage_moveAfter
);
306 downButton
.addSelectionListener(new SelectionAdapter() {
308 public void widgetSelected(final SelectionEvent e
) {
309 moveAfter(Output
.this);
312 downButton
.moveBelow(upButton
);
315 private void dispose() {
316 enabledButton
.dispose();
319 downButton
.dispose();
324 * Get the trace definition.
326 * @return The trace definition
328 public CustomTxtTraceDefinition
getDefinition() {