Commit | Line | Data |
---|---|---|
a0a88f65 | 1 | /******************************************************************************* |
f6aa55e2 | 2 | * Copyright (c) 2010, 2013 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; | |
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; | |
3bd46eef | 31 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; |
6151d86c PT |
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; | |
43 | ||
a0a88f65 AM |
44 | /** |
45 | * Output wizard page for custom XML trace parsers. | |
46 | * | |
f6aa55e2 | 47 | * @author Patrick Tasse |
a0a88f65 | 48 | */ |
6151d86c PT |
49 | public class CustomXmlParserOutputWizardPage extends WizardPage { |
50 | ||
f6aa55e2 PT |
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$ | |
6151d86c PT |
53 | private final CustomXmlParserWizard wizard; |
54 | private CustomXmlTraceDefinition definition; | |
507b1336 | 55 | private List<Output> outputs = new ArrayList<>(); |
f6aa55e2 PT |
56 | private Composite container; |
57 | private SashForm sash; | |
58 | private ScrolledComposite outputsScrolledComposite; | |
59 | private Composite outputsContainer; | |
60 | private Composite tableContainer; | |
61 | private CustomEventsTable previewTable; | |
62 | private File tmpFile; | |
6151d86c | 63 | |
a0a88f65 AM |
64 | /** |
65 | * Constructor | |
66 | * | |
67 | * @param wizard | |
68 | * The wizard to which this page belongs | |
69 | */ | |
6151d86c PT |
70 | protected CustomXmlParserOutputWizardPage(final CustomXmlParserWizard wizard) { |
71 | super("CustomParserOutputWizardPage"); //$NON-NLS-1$ | |
72 | setTitle(wizard.inputPage.getTitle()); | |
73 | setDescription(Messages.CustomXmlParserOutputWizardPage_description); | |
74 | this.wizard = wizard; | |
75 | setPageComplete(false); | |
76 | } | |
77 | ||
78 | @Override | |
79 | public void createControl(final Composite parent) { | |
80 | container = new Composite(parent, SWT.NULL); | |
81 | container.setLayout(new GridLayout()); | |
82 | ||
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)); | |
86 | ||
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); | |
97 | ||
98 | outputsContainer.layout(); | |
99 | ||
100 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); | |
101 | ||
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)); | |
109 | ||
110 | if (wizard.definition != null) { | |
111 | loadDefinition(wizard.definition); | |
112 | } | |
113 | setControl(container); | |
114 | ||
115 | } | |
116 | ||
117 | @Override | |
118 | public void dispose() { | |
119 | previewTable.dispose(); | |
120 | super.dispose(); | |
121 | } | |
122 | ||
3dca7aa5 AM |
123 | private void loadDefinition(final CustomTraceDefinition def) { |
124 | for (final OutputColumn outputColumn : def.outputs) { | |
6151d86c PT |
125 | final Output output = new Output(outputsContainer, outputColumn.name); |
126 | outputs.add(output); | |
127 | } | |
128 | } | |
129 | ||
6151d86c PT |
130 | @Override |
131 | public void setVisible(final boolean visible) { | |
132 | if (visible) { | |
133 | this.definition = wizard.inputPage.getDefinition(); | |
134 | final List<String> outputNames = wizard.inputPage.getInputNames(); | |
135 | ||
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)) { | |
143 | found = true; | |
144 | break; | |
145 | } | |
146 | } | |
147 | if (!found) { | |
148 | output.dispose(); | |
149 | iter.remove(); | |
150 | } | |
151 | } | |
152 | ||
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)) { | |
158 | found = true; | |
159 | break; | |
160 | } | |
161 | } | |
162 | if (!found) { | |
163 | outputs.add(new Output(outputsContainer, name)); | |
164 | } | |
165 | } | |
166 | ||
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}); | |
172 | } else { | |
173 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, previewTable.getTable().getItemHeight()}); | |
174 | } | |
175 | setPageComplete(true); | |
176 | } else { | |
177 | setPageComplete(false); | |
178 | } | |
179 | super.setVisible(visible); | |
180 | } | |
181 | ||
182 | private void moveBefore(final Output moved) { | |
183 | final int i = outputs.indexOf(moved); | |
184 | if (i > 0) { | |
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); | |
193 | container.layout(); | |
194 | updatePreviewTable(); | |
195 | } | |
196 | } | |
197 | ||
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); | |
209 | container.layout(); | |
210 | updatePreviewTable(); | |
211 | } | |
212 | } | |
213 | ||
214 | private void updatePreviewTable() { | |
215 | final int CACHE_SIZE = 50; | |
216 | definition.outputs = extractOutputs(); | |
507b1336 | 217 | tmpFile = Activator.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$ |
6151d86c | 218 | |
507b1336 | 219 | try (final FileWriter writer = new FileWriter(tmpFile);) { |
6151d86c | 220 | writer.write(wizard.inputPage.getInputText()); |
507b1336 AM |
221 | } catch (final IOException e) { |
222 | Activator.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ | |
223 | } | |
6151d86c | 224 | |
507b1336 | 225 | try { |
b0422293 PT |
226 | final CustomXmlTrace trace = new CustomXmlTrace(null, definition, tmpFile.getAbsolutePath(), CACHE_SIZE); |
227 | trace.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, false); | |
6151d86c PT |
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$ | |
6151d86c PT |
234 | } |
235 | ||
236 | tableContainer.layout(); | |
237 | container.layout(); | |
238 | } | |
239 | ||
a0a88f65 AM |
240 | /** |
241 | * Extract the output columns from the page's current contents. | |
242 | * | |
243 | * @return The list of output columns | |
244 | */ | |
6151d86c PT |
245 | public List<OutputColumn> extractOutputs() { |
246 | int numColumns = 0; | |
247 | for (int i = 0; i < outputs.size(); i++) { | |
248 | if (outputs.get(i).enabledButton.getSelection()) { | |
249 | numColumns++; | |
250 | } | |
251 | } | |
507b1336 | 252 | final List<OutputColumn> outputColumns = new ArrayList<>(numColumns); |
6151d86c PT |
253 | numColumns = 0; |
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); | |
260 | } | |
261 | } | |
262 | return outputColumns; | |
263 | } | |
264 | ||
265 | private class Output { | |
266 | String name; | |
267 | Button enabledButton; | |
268 | Text nameLabel; | |
269 | Button upButton; | |
270 | Button downButton; | |
271 | ||
272 | public Output(final Composite parent, final String name) { | |
273 | this.name = name; | |
274 | ||
275 | enabledButton = new Button(parent, SWT.CHECK); | |
276 | enabledButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_visible); | |
277 | enabledButton.setSelection(true); | |
278 | enabledButton.addSelectionListener(new SelectionAdapter() { | |
279 | @Override | |
280 | public void widgetSelected(final SelectionEvent e) { | |
281 | updatePreviewTable(); | |
282 | } | |
283 | }); | |
284 | // if (messageOutput != null) { | |
285 | // enabledButton.moveAbove(messageOutput.enabledButton); | |
286 | // } | |
287 | ||
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); | |
292 | ||
293 | upButton = new Button(parent, SWT.PUSH); | |
f6aa55e2 | 294 | upButton.setImage(UP_IMAGE); |
6151d86c PT |
295 | upButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_moveBefore); |
296 | upButton.addSelectionListener(new SelectionAdapter() { | |
297 | @Override | |
298 | public void widgetSelected(final SelectionEvent e) { | |
299 | moveBefore(Output.this); | |
300 | } | |
301 | }); | |
302 | upButton.moveBelow(nameLabel); | |
303 | ||
304 | downButton = new Button(parent, SWT.PUSH); | |
f6aa55e2 | 305 | downButton.setImage(DOWN_IMAGE); |
6151d86c PT |
306 | downButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_moveAfter); |
307 | downButton.addSelectionListener(new SelectionAdapter() { | |
308 | @Override | |
309 | public void widgetSelected(final SelectionEvent e) { | |
310 | moveAfter(Output.this); | |
311 | } | |
312 | }); | |
313 | downButton.moveBelow(upButton); | |
314 | } | |
315 | ||
316 | private void dispose() { | |
317 | enabledButton.dispose(); | |
318 | nameLabel.dispose(); | |
319 | upButton.dispose(); | |
320 | downButton.dispose(); | |
321 | } | |
322 | } | |
323 | ||
a0a88f65 AM |
324 | /** |
325 | * Get the trace definition. | |
326 | * | |
327 | * @return The trace definition | |
328 | */ | |
6151d86c PT |
329 | public CustomXmlTraceDefinition getDefinition() { |
330 | return definition; | |
331 | } | |
332 | ||
333 | } |