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