Commit | Line | Data |
---|---|---|
a0a88f65 | 1 | /******************************************************************************* |
f5cc6ed1 | 2 | * Copyright (c) 2010, 2016 Ericsson |
a0a88f65 AM |
3 | * |
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 | |
8 | * | |
9 | * Contributors: | |
f6aa55e2 | 10 | * Patrick Tasse - Initial API and implementation |
a0a88f65 AM |
11 | *******************************************************************************/ |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.internal.tmf.ui.parsers.wizards; |
6151d86c | 14 | |
7a0b1e3c PT |
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
16 | ||
6151d86c PT |
17 | import java.io.File; |
18 | import java.io.FileWriter; | |
19 | import java.io.IOException; | |
20 | import java.util.ArrayList; | |
21 | import java.util.Iterator; | |
22 | import java.util.List; | |
f5cc6ed1 | 23 | import java.util.Map.Entry; |
6151d86c PT |
24 | |
25 | import org.eclipse.jface.wizard.WizardPage; | |
6151d86c PT |
26 | import org.eclipse.swt.SWT; |
27 | import org.eclipse.swt.custom.SashForm; | |
28 | import org.eclipse.swt.custom.ScrolledComposite; | |
29 | import org.eclipse.swt.events.SelectionAdapter; | |
30 | import org.eclipse.swt.events.SelectionEvent; | |
31 | import org.eclipse.swt.graphics.Image; | |
32 | import org.eclipse.swt.layout.GridData; | |
33 | import org.eclipse.swt.layout.GridLayout; | |
34 | import org.eclipse.swt.widgets.Button; | |
35 | import org.eclipse.swt.widgets.Composite; | |
36 | import org.eclipse.swt.widgets.Text; | |
b04903a2 | 37 | import org.eclipse.tracecompass.internal.tmf.core.parsers.custom.CustomEventAspects; |
2bdf0193 AM |
38 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
39 | import org.eclipse.tracecompass.internal.tmf.ui.Messages; | |
2bdf0193 | 40 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; |
b04903a2 | 41 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.OutputColumn; |
f5cc6ed1 | 42 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.Tag; |
2bdf0193 AM |
43 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace; |
44 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition; | |
2bdf0193 AM |
45 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; |
46 | import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer; | |
47 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer; | |
48 | import org.eclipse.tracecompass.tmf.ui.viewers.events.TmfEventsTable; | |
6151d86c | 49 | |
a0a88f65 AM |
50 | /** |
51 | * Output wizard page for custom text trace parsers. | |
52 | * | |
f6aa55e2 | 53 | * @author Patrick Tasse |
a0a88f65 | 54 | */ |
6151d86c PT |
55 | public class CustomTxtParserOutputWizardPage extends WizardPage { |
56 | ||
f6aa55e2 PT |
57 | private static final Image UP_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$ |
58 | private static final Image DOWN_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$ | |
6151d86c PT |
59 | private final CustomTxtParserWizard wizard; |
60 | private CustomTxtTraceDefinition definition; | |
507b1336 | 61 | private List<Output> outputs = new ArrayList<>(); |
f6aa55e2 PT |
62 | private Composite container; |
63 | private SashForm sash; | |
64 | private ScrolledComposite outputsScrolledComposite; | |
65 | private Composite outputsContainer; | |
66 | private Composite tableContainer; | |
99d7adc6 | 67 | private TmfEventsTable previewTable; |
f6aa55e2 | 68 | private File tmpFile; |
6151d86c | 69 | |
a0a88f65 AM |
70 | /** |
71 | * Constructor | |
72 | * | |
73 | * @param wizard | |
74 | * The wizard to which this page belongs | |
75 | */ | |
6151d86c PT |
76 | protected CustomTxtParserOutputWizardPage(final CustomTxtParserWizard wizard) { |
77 | super("CustomParserOutputWizardPage"); //$NON-NLS-1$ | |
78 | setTitle(wizard.inputPage.getTitle()); | |
79 | setDescription(Messages.CustomTxtParserOutputWizardPage_description); | |
80 | this.wizard = wizard; | |
81 | setPageComplete(false); | |
82 | } | |
83 | ||
84 | @Override | |
85 | public void createControl(final Composite parent) { | |
86 | container = new Composite(parent, SWT.NULL); | |
87 | container.setLayout(new GridLayout()); | |
88 | ||
89 | sash = new SashForm(container, SWT.VERTICAL); | |
90 | sash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
91 | sash.setBackground(sash.getDisplay().getSystemColor(SWT.COLOR_GRAY)); | |
92 | ||
93 | outputsScrolledComposite = new ScrolledComposite(sash, SWT.V_SCROLL); | |
94 | outputsScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
95 | outputsContainer = new Composite(outputsScrolledComposite, SWT.NONE); | |
96 | final GridLayout outputsLayout = new GridLayout(4, false); | |
97 | outputsLayout.marginHeight = 10; | |
98 | outputsLayout.marginWidth = 0; | |
99 | outputsContainer.setLayout(outputsLayout); | |
100 | outputsScrolledComposite.setContent(outputsContainer); | |
101 | outputsScrolledComposite.setExpandHorizontal(true); | |
102 | outputsScrolledComposite.setExpandVertical(true); | |
103 | ||
104 | outputsContainer.layout(); | |
105 | ||
106 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
107 | ||
108 | tableContainer = new Composite(sash, SWT.NONE); | |
109 | final GridLayout tableLayout = new GridLayout(); | |
110 | tableLayout.marginHeight = 0; | |
111 | tableLayout.marginWidth = 0; | |
112 | tableContainer.setLayout(tableLayout); | |
b04903a2 | 113 | previewTable = new TmfEventsTable(tableContainer, 0, CustomEventAspects.generateAspects(new CustomTxtTraceDefinition())); |
6151d86c PT |
114 | previewTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
115 | ||
116 | if (wizard.definition != null) { | |
117 | loadDefinition(wizard.definition); | |
118 | } | |
119 | setControl(container); | |
120 | ||
121 | } | |
122 | ||
123 | @Override | |
124 | public void dispose() { | |
125 | previewTable.dispose(); | |
126 | super.dispose(); | |
127 | } | |
128 | ||
3dca7aa5 AM |
129 | private void loadDefinition(final CustomTxtTraceDefinition def) { |
130 | for (final OutputColumn outputColumn : def.outputs) { | |
f5cc6ed1 | 131 | final Output output = new Output(outputsContainer, outputColumn.tag, outputColumn.name); |
6151d86c PT |
132 | outputs.add(output); |
133 | } | |
134 | } | |
135 | ||
6151d86c PT |
136 | @Override |
137 | public void setVisible(final boolean visible) { | |
138 | if (visible) { | |
139 | this.definition = wizard.inputPage.getDefinition(); | |
f5cc6ed1 | 140 | final List<Entry<Tag, String>> inputs = wizard.inputPage.getInputs(); |
6151d86c PT |
141 | |
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; | |
f5cc6ed1 PT |
147 | for (final Entry<Tag, String> input : inputs) { |
148 | if (output.tag.equals(input.getKey()) && | |
149 | output.name.equals(input.getValue())) { | |
6151d86c PT |
150 | found = true; |
151 | break; | |
152 | } | |
153 | } | |
154 | if (!found) { | |
155 | output.dispose(); | |
156 | iter.remove(); | |
157 | } | |
158 | } | |
159 | ||
160 | // create outputs that have been added in the input page | |
f5cc6ed1 | 161 | for (final Entry<Tag, String> input : inputs) { |
6151d86c PT |
162 | boolean found = false; |
163 | for (final Output output : outputs) { | |
f5cc6ed1 PT |
164 | if (output.tag.equals(input.getKey()) && |
165 | output.name.equals(input.getValue())) { | |
6151d86c PT |
166 | found = true; |
167 | break; | |
168 | } | |
169 | } | |
170 | if (!found) { | |
f5cc6ed1 | 171 | outputs.add(new Output(outputsContainer, input.getKey(), input.getValue())); |
6151d86c PT |
172 | } |
173 | } | |
174 | ||
175 | outputsContainer.layout(); | |
176 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
177 | updatePreviewTable(); | |
178 | if (sash.getSize().y > outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y + previewTable.getTable().getItemHeight()) { | |
179 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sash.getSize().y - outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y}); | |
180 | } else { | |
181 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, previewTable.getTable().getItemHeight()}); | |
182 | } | |
183 | setPageComplete(true); | |
184 | } else { | |
185 | setPageComplete(false); | |
186 | } | |
187 | super.setVisible(visible); | |
188 | } | |
189 | ||
190 | private void moveBefore(final Output moved) { | |
191 | final int i = outputs.indexOf(moved); | |
192 | if (i > 0) { | |
193 | final Output previous = outputs.get(i-1); | |
194 | moved.enabledButton.moveAbove(previous.enabledButton); | |
195 | moved.nameLabel.moveBelow(moved.enabledButton); | |
196 | moved.upButton.moveBelow(moved.nameLabel); | |
197 | moved.downButton.moveBelow(moved.upButton); | |
198 | outputs.add(i-1, outputs.remove(i)); | |
199 | outputsContainer.layout(); | |
200 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
201 | container.layout(); | |
202 | updatePreviewTable(); | |
203 | } | |
204 | } | |
205 | ||
206 | private void moveAfter(final Output moved) { | |
207 | final int i = outputs.indexOf(moved); | |
208 | if (i+1 < outputs.size()) { | |
209 | final Output next = outputs.get(i+1); | |
210 | moved.enabledButton.moveBelow(next.downButton); | |
211 | moved.nameLabel.moveBelow(moved.enabledButton); | |
212 | moved.upButton.moveBelow(moved.nameLabel); | |
213 | moved.downButton.moveBelow(moved.upButton); | |
214 | outputs.add(i+1, outputs.remove(i)); | |
215 | outputsContainer.layout(); | |
216 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
217 | container.layout(); | |
218 | updatePreviewTable(); | |
219 | } | |
220 | } | |
221 | ||
222 | private void updatePreviewTable() { | |
223 | final int CACHE_SIZE = 50; | |
224 | definition.outputs = extractOutputs(); | |
507b1336 | 225 | tmpFile = Activator.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$ |
6151d86c | 226 | |
507b1336 | 227 | try (final FileWriter writer = new FileWriter(tmpFile);) { |
6151d86c | 228 | writer.write(wizard.inputPage.getInputText()); |
507b1336 AM |
229 | } catch (final IOException e) { |
230 | Activator.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ | |
231 | } | |
6151d86c | 232 | |
507b1336 | 233 | try { |
5ee05017 PT |
234 | final CustomTxtTrace trace = new CustomTxtTrace(null, definition, tmpFile.getAbsolutePath(), CACHE_SIZE) { |
235 | @Override | |
236 | protected ITmfTraceIndexer createIndexer(int interval) { | |
237 | return new TmfCheckpointIndexer(this, interval); | |
238 | } | |
239 | }; | |
b0422293 | 240 | trace.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, false); |
6151d86c | 241 | previewTable.dispose(); |
b04903a2 | 242 | previewTable = new TmfEventsTable(tableContainer, CACHE_SIZE, CustomEventAspects.generateAspects(definition)); |
6151d86c PT |
243 | previewTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
244 | previewTable.setTrace(trace, true); | |
245 | } catch (final TmfTraceException e) { | |
246 | Activator.getDefault().logError("Error creating CustomTxtTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ | |
6151d86c PT |
247 | } |
248 | ||
249 | tableContainer.layout(); | |
250 | container.layout(); | |
251 | } | |
252 | ||
a0a88f65 AM |
253 | /** |
254 | * Extract the list of output columns from the page's contents. | |
255 | * | |
256 | * @return The output columns | |
257 | */ | |
6151d86c | 258 | public List<OutputColumn> extractOutputs() { |
f5cc6ed1 PT |
259 | final List<OutputColumn> outputColumns = new ArrayList<>(); |
260 | for (Output output : outputs) { | |
6151d86c | 261 | if (output.enabledButton.getSelection()) { |
f5cc6ed1 | 262 | final OutputColumn column = new OutputColumn(checkNotNull(output.tag), checkNotNull(output.name)); |
6151d86c PT |
263 | outputColumns.add(column); |
264 | } | |
265 | } | |
266 | return outputColumns; | |
267 | } | |
268 | ||
269 | private class Output { | |
f5cc6ed1 | 270 | Tag tag; |
6151d86c PT |
271 | String name; |
272 | Button enabledButton; | |
273 | Text nameLabel; | |
274 | Button upButton; | |
275 | Button downButton; | |
276 | ||
f5cc6ed1 PT |
277 | public Output(final Composite parent, final Tag tag, final String name) { |
278 | this.tag = tag; | |
6151d86c PT |
279 | this.name = name; |
280 | ||
281 | enabledButton = new Button(parent, SWT.CHECK); | |
282 | enabledButton.setToolTipText(Messages.CustomTxtParserOutputWizardPage_visible); | |
283 | enabledButton.setSelection(true); | |
284 | enabledButton.addSelectionListener(new SelectionAdapter() { | |
285 | @Override | |
286 | public void widgetSelected(final SelectionEvent e) { | |
287 | updatePreviewTable(); | |
288 | } | |
289 | }); | |
290 | // if (messageOutput != null) { | |
291 | // enabledButton.moveAbove(messageOutput.enabledButton); | |
292 | // } | |
293 | ||
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); | |
298 | ||
299 | upButton = new Button(parent, SWT.PUSH); | |
f6aa55e2 | 300 | upButton.setImage(UP_IMAGE); |
6151d86c PT |
301 | upButton.setToolTipText(Messages.CustomTxtParserOutputWizardPage_moveBefore); |
302 | upButton.addSelectionListener(new SelectionAdapter() { | |
303 | @Override | |
304 | public void widgetSelected(final SelectionEvent e) { | |
305 | moveBefore(Output.this); | |
306 | } | |
307 | }); | |
308 | upButton.moveBelow(nameLabel); | |
309 | ||
310 | downButton = new Button(parent, SWT.PUSH); | |
f6aa55e2 | 311 | downButton.setImage(DOWN_IMAGE); |
6151d86c PT |
312 | downButton.setToolTipText(Messages.CustomTxtParserOutputWizardPage_moveAfter); |
313 | downButton.addSelectionListener(new SelectionAdapter() { | |
314 | @Override | |
315 | public void widgetSelected(final SelectionEvent e) { | |
316 | moveAfter(Output.this); | |
317 | } | |
318 | }); | |
319 | downButton.moveBelow(upButton); | |
320 | } | |
321 | ||
322 | private void dispose() { | |
323 | enabledButton.dispose(); | |
324 | nameLabel.dispose(); | |
325 | upButton.dispose(); | |
326 | downButton.dispose(); | |
327 | } | |
328 | } | |
329 | ||
a0a88f65 AM |
330 | /** |
331 | * Get the trace definition. | |
332 | * | |
333 | * @return The trace definition | |
334 | */ | |
6151d86c PT |
335 | public CustomTxtTraceDefinition getDefinition() { |
336 | return definition; | |
337 | } | |
338 | ||
339 | } |