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
.internal
.tmf
.ui
.parsers
.custom
.CustomTraceDefinition
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTraceDefinition
.OutputColumn
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTrace
;
29 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomXmlTraceDefinition
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
32 import org
.eclipse
.swt
.SWT
;
33 import org
.eclipse
.swt
.custom
.SashForm
;
34 import org
.eclipse
.swt
.custom
.ScrolledComposite
;
35 import org
.eclipse
.swt
.events
.SelectionAdapter
;
36 import org
.eclipse
.swt
.events
.SelectionEvent
;
37 import org
.eclipse
.swt
.graphics
.Image
;
38 import org
.eclipse
.swt
.layout
.GridData
;
39 import org
.eclipse
.swt
.layout
.GridLayout
;
40 import org
.eclipse
.swt
.widgets
.Button
;
41 import org
.eclipse
.swt
.widgets
.Composite
;
42 import org
.eclipse
.swt
.widgets
.Text
;
45 * Output wizard page for custom XML trace parsers.
47 * @author Patrick Tasse
49 public class CustomXmlParserOutputWizardPage
extends WizardPage
{
51 private static final Image UP_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$
52 private static final Image DOWN_IMAGE
= Activator
.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$
53 private final CustomXmlParserWizard wizard
;
54 private CustomXmlTraceDefinition definition
;
55 private List
<Output
> outputs
= new ArrayList
<>();
56 private Composite container
;
57 private SashForm sash
;
58 private ScrolledComposite outputsScrolledComposite
;
59 private Composite outputsContainer
;
60 private Composite tableContainer
;
61 private CustomEventsTable previewTable
;
68 * The wizard to which this page belongs
70 protected CustomXmlParserOutputWizardPage(final CustomXmlParserWizard wizard
) {
71 super("CustomParserOutputWizardPage"); //$NON-NLS-1$
72 setTitle(wizard
.inputPage
.getTitle());
73 setDescription(Messages
.CustomXmlParserOutputWizardPage_description
);
75 setPageComplete(false);
79 public void createControl(final Composite parent
) {
80 container
= new Composite(parent
, SWT
.NULL
);
81 container
.setLayout(new GridLayout());
83 sash
= new SashForm(container
, SWT
.VERTICAL
);
84 sash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
85 sash
.setBackground(sash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
87 outputsScrolledComposite
= new ScrolledComposite(sash
, SWT
.V_SCROLL
);
88 outputsScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
89 outputsContainer
= new Composite(outputsScrolledComposite
, SWT
.NONE
);
90 final GridLayout outputsLayout
= new GridLayout(4, false);
91 outputsLayout
.marginHeight
= 10;
92 outputsLayout
.marginWidth
= 0;
93 outputsContainer
.setLayout(outputsLayout
);
94 outputsScrolledComposite
.setContent(outputsContainer
);
95 outputsScrolledComposite
.setExpandHorizontal(true);
96 outputsScrolledComposite
.setExpandVertical(true);
98 outputsContainer
.layout();
100 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
102 tableContainer
= new Composite(sash
, SWT
.NONE
);
103 final GridLayout tableLayout
= new GridLayout();
104 tableLayout
.marginHeight
= 0;
105 tableLayout
.marginWidth
= 0;
106 tableContainer
.setLayout(tableLayout
);
107 previewTable
= new CustomEventsTable(new CustomXmlTraceDefinition(), tableContainer
, 0);
108 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
110 if (wizard
.definition
!= null) {
111 loadDefinition(wizard
.definition
);
113 setControl(container
);
118 public void dispose() {
119 previewTable
.dispose();
123 private void loadDefinition(final CustomTraceDefinition def
) {
124 for (final OutputColumn outputColumn
: def
.outputs
) {
125 final Output output
= new Output(outputsContainer
, outputColumn
.name
);
131 public void setVisible(final boolean visible
) {
133 this.definition
= wizard
.inputPage
.getDefinition();
134 final List
<String
> outputNames
= wizard
.inputPage
.getInputNames();
136 // dispose outputs that have been removed in the input page
137 final Iterator
<Output
> iter
= outputs
.iterator();
138 while (iter
.hasNext()) {
139 final Output output
= iter
.next();
140 boolean found
= false;
141 for (final String name
: outputNames
) {
142 if (output
.name
.equals(name
)) {
153 // create outputs that have been added in the input page
154 for (final String name
: outputNames
) {
155 boolean found
= false;
156 for (final Output output
: outputs
) {
157 if (output
.name
.equals(name
)) {
163 outputs
.add(new Output(outputsContainer
, name
));
167 outputsContainer
.layout();
168 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
169 updatePreviewTable();
170 if (sash
.getSize().y
> outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
+ previewTable
.getTable().getItemHeight()) {
171 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sash
.getSize().y
- outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
173 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, previewTable
.getTable().getItemHeight()});
175 setPageComplete(true);
177 setPageComplete(false);
179 super.setVisible(visible
);
182 private void moveBefore(final Output moved
) {
183 final int i
= outputs
.indexOf(moved
);
185 final Output previous
= outputs
.get(i
-1);
186 moved
.enabledButton
.moveAbove(previous
.enabledButton
);
187 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
188 moved
.upButton
.moveBelow(moved
.nameLabel
);
189 moved
.downButton
.moveBelow(moved
.upButton
);
190 outputs
.add(i
-1, outputs
.remove(i
));
191 outputsContainer
.layout();
192 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
194 updatePreviewTable();
198 private void moveAfter(final Output moved
) {
199 final int i
= outputs
.indexOf(moved
);
200 if (i
+1 < outputs
.size()) {
201 final Output next
= outputs
.get(i
+1);
202 moved
.enabledButton
.moveBelow(next
.downButton
);
203 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
204 moved
.upButton
.moveBelow(moved
.nameLabel
);
205 moved
.downButton
.moveBelow(moved
.upButton
);
206 outputs
.add(i
+1, outputs
.remove(i
));
207 outputsContainer
.layout();
208 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
210 updatePreviewTable();
214 private void updatePreviewTable() {
215 final int CACHE_SIZE
= 50;
216 definition
.outputs
= extractOutputs();
217 tmpFile
= Activator
.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
219 try (final FileWriter writer
= new FileWriter(tmpFile
);) {
220 writer
.write(wizard
.inputPage
.getInputText());
221 } catch (final IOException e
) {
222 Activator
.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
226 final CustomXmlTrace trace
= new CustomXmlTrace(null, definition
, tmpFile
.getAbsolutePath(), CACHE_SIZE
);
227 trace
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, false);
228 previewTable
.dispose();
229 previewTable
= new CustomEventsTable(definition
, tableContainer
, CACHE_SIZE
);
230 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
231 previewTable
.setTrace(trace
, true);
232 } catch (final TmfTraceException e
) {
233 Activator
.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
236 tableContainer
.layout();
241 * Extract the output columns from the page's current contents.
243 * @return The list of output columns
245 public List
<OutputColumn
> extractOutputs() {
247 for (int i
= 0; i
< outputs
.size(); i
++) {
248 if (outputs
.get(i
).enabledButton
.getSelection()) {
252 final List
<OutputColumn
> outputColumns
= new ArrayList
<>(numColumns
);
254 for (int i
= 0; i
< outputs
.size(); i
++) {
255 final Output output
= outputs
.get(i
);
256 if (output
.enabledButton
.getSelection()) {
257 final OutputColumn column
= new OutputColumn();
258 column
.name
= output
.nameLabel
.getText();
259 outputColumns
.add(column
);
262 return outputColumns
;
265 private class Output
{
267 Button enabledButton
;
272 public Output(final Composite parent
, final String name
) {
275 enabledButton
= new Button(parent
, SWT
.CHECK
);
276 enabledButton
.setToolTipText(Messages
.CustomXmlParserOutputWizardPage_visible
);
277 enabledButton
.setSelection(true);
278 enabledButton
.addSelectionListener(new SelectionAdapter() {
280 public void widgetSelected(final SelectionEvent e
) {
281 updatePreviewTable();
284 // if (messageOutput != null) {
285 // enabledButton.moveAbove(messageOutput.enabledButton);
288 nameLabel
= new Text(parent
, SWT
.BORDER
| SWT
.READ_ONLY
| SWT
.SINGLE
);
289 nameLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
290 nameLabel
.setText(name
);
291 nameLabel
.moveBelow(enabledButton
);
293 upButton
= new Button(parent
, SWT
.PUSH
);
294 upButton
.setImage(UP_IMAGE
);
295 upButton
.setToolTipText(Messages
.CustomXmlParserOutputWizardPage_moveBefore
);
296 upButton
.addSelectionListener(new SelectionAdapter() {
298 public void widgetSelected(final SelectionEvent e
) {
299 moveBefore(Output
.this);
302 upButton
.moveBelow(nameLabel
);
304 downButton
= new Button(parent
, SWT
.PUSH
);
305 downButton
.setImage(DOWN_IMAGE
);
306 downButton
.setToolTipText(Messages
.CustomXmlParserOutputWizardPage_moveAfter
);
307 downButton
.addSelectionListener(new SelectionAdapter() {
309 public void widgetSelected(final SelectionEvent e
) {
310 moveAfter(Output
.this);
313 downButton
.moveBelow(upButton
);
316 private void dispose() {
317 enabledButton
.dispose();
320 downButton
.dispose();
325 * Get the trace definition.
327 * @return The trace definition
329 public CustomXmlTraceDefinition
getDefinition() {