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
.OutputColumn
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTrace
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
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 Tassé
48 public class CustomTxtParserOutputWizardPage
extends WizardPage
{
50 private static final Image upImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$
51 private static final Image downImage
= Activator
.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$
52 private final CustomTxtParserWizard wizard
;
53 private CustomTxtTraceDefinition definition
;
54 List
<Output
> outputs
= new ArrayList
<Output
>();
55 // Output messageOutput;
58 // Text timestampFormatText;
59 // Text timestampPreviewText;
60 ScrolledComposite outputsScrolledComposite
;
61 Composite outputsContainer
;
62 // ScrolledComposite inputScrolledComposite;
63 Composite tableContainer
;
64 CustomEventsTable previewTable
;
71 * The wizard to which this page belongs
73 protected CustomTxtParserOutputWizardPage(final CustomTxtParserWizard wizard
) {
74 super("CustomParserOutputWizardPage"); //$NON-NLS-1$
75 setTitle(wizard
.inputPage
.getTitle());
76 setDescription(Messages
.CustomTxtParserOutputWizardPage_description
);
78 setPageComplete(false);
82 public void createControl(final Composite parent
) {
83 container
= new Composite(parent
, SWT
.NULL
);
84 container
.setLayout(new GridLayout());
86 sash
= new SashForm(container
, SWT
.VERTICAL
);
87 sash
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
88 sash
.setBackground(sash
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
));
90 outputsScrolledComposite
= new ScrolledComposite(sash
, SWT
.V_SCROLL
);
91 outputsScrolledComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
92 outputsContainer
= new Composite(outputsScrolledComposite
, SWT
.NONE
);
93 final GridLayout outputsLayout
= new GridLayout(4, false);
94 outputsLayout
.marginHeight
= 10;
95 outputsLayout
.marginWidth
= 0;
96 outputsContainer
.setLayout(outputsLayout
);
97 outputsScrolledComposite
.setContent(outputsContainer
);
98 outputsScrolledComposite
.setExpandHorizontal(true);
99 outputsScrolledComposite
.setExpandVertical(true);
101 outputsContainer
.layout();
103 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
105 tableContainer
= new Composite(sash
, SWT
.NONE
);
106 final GridLayout tableLayout
= new GridLayout();
107 tableLayout
.marginHeight
= 0;
108 tableLayout
.marginWidth
= 0;
109 tableContainer
.setLayout(tableLayout
);
110 previewTable
= new CustomEventsTable(new CustomTxtTraceDefinition(), tableContainer
, 0);
111 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
113 if (wizard
.definition
!= null) {
114 loadDefinition(wizard
.definition
);
116 setControl(container
);
121 public void dispose() {
122 previewTable
.dispose();
126 private void loadDefinition(final CustomTxtTraceDefinition def
) {
127 for (final OutputColumn outputColumn
: def
.outputs
) {
128 final Output output
= new Output(outputsContainer
, outputColumn
.name
);
134 * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
137 public void setVisible(final boolean visible
) {
139 this.definition
= wizard
.inputPage
.getDefinition();
140 final List
<String
> outputNames
= wizard
.inputPage
.getInputNames();
142 // dispose outputs that have been removed in the input page
143 final Iterator
<Output
> iter
= outputs
.iterator();
144 while (iter
.hasNext()) {
145 final Output output
= iter
.next();
146 boolean found
= false;
147 for (final String name
: outputNames
) {
148 if (output
.name
.equals(name
)) {
159 // create outputs that have been added in the input page
160 for (final String name
: outputNames
) {
161 boolean found
= false;
162 for (final Output output
: outputs
) {
163 if (output
.name
.equals(name
)) {
169 outputs
.add(new Output(outputsContainer
, name
));
173 outputsContainer
.layout();
174 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
175 updatePreviewTable();
176 if (sash
.getSize().y
> outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
+ previewTable
.getTable().getItemHeight()) {
177 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, sash
.getSize().y
- outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
});
179 sash
.setWeights(new int[] {outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
, previewTable
.getTable().getItemHeight()});
181 setPageComplete(true);
183 setPageComplete(false);
185 super.setVisible(visible
);
188 private void moveBefore(final Output moved
) {
189 final int i
= outputs
.indexOf(moved
);
191 final Output previous
= outputs
.get(i
-1);
192 moved
.enabledButton
.moveAbove(previous
.enabledButton
);
193 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
194 moved
.upButton
.moveBelow(moved
.nameLabel
);
195 moved
.downButton
.moveBelow(moved
.upButton
);
196 outputs
.add(i
-1, outputs
.remove(i
));
197 outputsContainer
.layout();
198 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
200 updatePreviewTable();
204 private void moveAfter(final Output moved
) {
205 final int i
= outputs
.indexOf(moved
);
206 if (i
+1 < outputs
.size()) {
207 final Output next
= outputs
.get(i
+1);
208 moved
.enabledButton
.moveBelow(next
.downButton
);
209 moved
.nameLabel
.moveBelow(moved
.enabledButton
);
210 moved
.upButton
.moveBelow(moved
.nameLabel
);
211 moved
.downButton
.moveBelow(moved
.upButton
);
212 outputs
.add(i
+1, outputs
.remove(i
));
213 outputsContainer
.layout();
214 outputsScrolledComposite
.setMinSize(outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).x
, outputsContainer
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
-5);
216 updatePreviewTable();
220 private void updatePreviewTable() {
221 final int CACHE_SIZE
= 50;
222 definition
.outputs
= extractOutputs();
225 tmpFile
= Activator
.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
226 final FileWriter writer
= new FileWriter(tmpFile
);
227 writer
.write(wizard
.inputPage
.getInputText());
230 final CustomTxtTrace trace
= new CustomTxtTrace(null, definition
, tmpFile
.getAbsolutePath(), CACHE_SIZE
);
231 trace
.getIndexer().buildIndex(0, TmfTimeRange
.ETERNITY
, false);
232 previewTable
.dispose();
233 previewTable
= new CustomEventsTable(definition
, tableContainer
, CACHE_SIZE
);
234 previewTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
235 previewTable
.setTrace(trace
, true);
236 } catch (final TmfTraceException e
) {
237 Activator
.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile
.getAbsolutePath(), e
); //$NON-NLS-1$
238 } catch (final IOException 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
<OutputColumn
>(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(upImage
);
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(downImage
);
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() {