1 /*******************************************************************************
2 * Copyright (c) 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 Tassé - 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
.event
.TmfTimeRange
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
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 Tassé
49 public class CustomXmlParserOutputWizardPage
extends WizardPage
{
51 private static final Image upImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$
52 private static final Image downImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$
53 private final CustomXmlParserWizard wizard
;
54 private CustomXmlTraceDefinition definition
;
55 List
<Output
> outputs
= new ArrayList
<Output
>();
56 // Output messageOutput;
59 // Text timestampFormatText;
60 // Text timestampPreviewText;
61 ScrolledComposite outputsScrolledComposite
;
62 Composite outputsContainer
;
63 // ScrolledComposite inputScrolledComposite;
64 Composite tableContainer
;
65 CustomEventsTable previewTable
;
72 * The wizard to which this page belongs
74 protected CustomXmlParserOutputWizardPage(final CustomXmlParserWizard wizard
) {
75 super("CustomParserOutputWizardPage"); //$NON-NLS-1$
76 setTitle(wizard
.inputPage
.getTitle());
77 setDescription(Messages
.CustomXmlParserOutputWizardPage_description
);
79 setPageComplete(false);
83 public void createControl(final Composite parent
) {
84 container
= new Composite(parent
, SWT
.NULL
);
85 container
.setLayout(new GridLayout());
87 sash
= new SashForm(container
, SWT
.VERTICAL
);
88 sash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
89 sash
.setBackground(sash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
91 outputsScrolledComposite
= new ScrolledComposite(sash
, SWT
.V_SCROLL
);
92 outputsScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
93 outputsContainer
= new Composite(outputsScrolledComposite
, SWT
.NONE
);
94 final GridLayout outputsLayout
= new GridLayout(4, false);
95 outputsLayout
.marginHeight
= 10;
96 outputsLayout
.marginWidth
= 0;
97 outputsContainer
.setLayout(outputsLayout
);
98 outputsScrolledComposite
.setContent(outputsContainer
);
99 outputsScrolledComposite
.setExpandHorizontal(true);
100 outputsScrolledComposite
.setExpandVertical(true);
102 outputsContainer
.layout();
104 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
106 tableContainer
= new Composite(sash
, SWT
.NONE
);
107 final GridLayout tableLayout
= new GridLayout();
108 tableLayout
.marginHeight
= 0;
109 tableLayout
.marginWidth
= 0;
110 tableContainer
.setLayout(tableLayout
);
111 previewTable
= new CustomEventsTable(new CustomXmlTraceDefinition(), tableContainer
, 0);
112 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
114 if (wizard
.definition
!= null) {
115 loadDefinition(wizard
.definition
);
117 setControl(container
);
122 public void dispose() {
123 previewTable
.dispose();
127 private void loadDefinition(final CustomTraceDefinition definition
) {
128 for (final OutputColumn outputColumn
: definition
.outputs
) {
129 final Output output
= new Output(outputsContainer
, outputColumn
.name
);
135 * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
138 public void setVisible(final boolean visible
) {
140 this.definition
= wizard
.inputPage
.getDefinition();
141 final List
<String
> outputNames
= wizard
.inputPage
.getInputNames();
143 // dispose outputs that have been removed in the input page
144 final Iterator
<Output
> iter
= outputs
.iterator();
145 while (iter
.hasNext()) {
146 final Output output
= iter
.next();
147 boolean found
= false;
148 for (final String name
: outputNames
) {
149 if (output
.name
.equals(name
)) {
160 // create outputs that have been added in the input page
161 for (final String name
: outputNames
) {
162 boolean found
= false;
163 for (final Output output
: outputs
) {
164 if (output
.name
.equals(name
)) {
170 outputs
.add(new Output(outputsContainer
, name
));
174 outputsContainer
.layout();
175 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
176 updatePreviewTable();
177 if (sash
.getSize().y
> outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
+ previewTable
.getTable().getItemHeight()) {
178 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sash
.getSize().y
- outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
180 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, previewTable
.getTable().getItemHeight()});
182 setPageComplete(true);
184 setPageComplete(false);
186 super.setVisible(visible
);
189 private void moveBefore(final Output moved
) {
190 final int i
= outputs
.indexOf(moved
);
192 final Output previous
= outputs
.get(i
-1);
193 moved
.enabledButton
.moveAbove(previous
.enabledButton
);
194 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
195 moved
.upButton
.moveBelow(moved
.nameLabel
);
196 moved
.downButton
.moveBelow(moved
.upButton
);
197 outputs
.add(i
-1, outputs
.remove(i
));
198 outputsContainer
.layout();
199 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
201 updatePreviewTable();
205 private void moveAfter(final Output moved
) {
206 final int i
= outputs
.indexOf(moved
);
207 if (i
+1 < outputs
.size()) {
208 final Output next
= outputs
.get(i
+1);
209 moved
.enabledButton
.moveBelow(next
.downButton
);
210 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
211 moved
.upButton
.moveBelow(moved
.nameLabel
);
212 moved
.downButton
.moveBelow(moved
.upButton
);
213 outputs
.add(i
+1, outputs
.remove(i
));
214 outputsContainer
.layout();
215 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
217 updatePreviewTable();
221 private void updatePreviewTable() {
222 final int CACHE_SIZE
= 50;
223 definition
.outputs
= extractOutputs();
226 tmpFile
= Activator
.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
227 final FileWriter writer
= new FileWriter(tmpFile
);
228 writer
.write(wizard
.inputPage
.getInputText());
231 final CustomXmlTrace trace
= new CustomXmlTrace(null, definition
, tmpFile
.getAbsolutePath(), CACHE_SIZE
);
232 trace
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, false);
233 previewTable
.dispose();
234 previewTable
= new CustomEventsTable(definition
, tableContainer
, CACHE_SIZE
);
235 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
236 previewTable
.setTrace(trace
, true);
237 } catch (final TmfTraceException e
) {
238 Activator
.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
239 } catch (final IOException e
) {
240 Activator
.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
243 tableContainer
.layout();
248 * Extract the output columns from the page's current contents.
250 * @return The list of 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
<OutputColumn
>(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
.CustomXmlParserOutputWizardPage_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(upImage
);
302 upButton
.setToolTipText(Messages
.CustomXmlParserOutputWizardPage_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(downImage
);
313 downButton
.setToolTipText(Messages
.CustomXmlParserOutputWizardPage_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 CustomXmlTraceDefinition
getDefinition() {