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