Commit | Line | Data |
---|---|---|
a0a88f65 AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 Ericsson | |
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: | |
10 | * Patrick Tassé - Initial API and implementation | |
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.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; | |
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 | * | |
46 | * @author Patrick Tassé | |
47 | */ | |
6151d86c PT |
48 | public class CustomTxtParserOutputWizardPage extends WizardPage { |
49 | ||
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; | |
56 | Composite container; | |
57 | SashForm sash; | |
58 | // Text timestampFormatText; | |
59 | // Text timestampPreviewText; | |
60 | ScrolledComposite outputsScrolledComposite; | |
61 | Composite outputsContainer; | |
62 | // ScrolledComposite inputScrolledComposite; | |
63 | Composite tableContainer; | |
64 | CustomEventsTable previewTable; | |
65 | File tmpFile; | |
66 | ||
a0a88f65 AM |
67 | /** |
68 | * Constructor | |
69 | * | |
70 | * @param wizard | |
71 | * The wizard to which this page belongs | |
72 | */ | |
6151d86c PT |
73 | protected CustomTxtParserOutputWizardPage(final CustomTxtParserWizard wizard) { |
74 | super("CustomParserOutputWizardPage"); //$NON-NLS-1$ | |
75 | setTitle(wizard.inputPage.getTitle()); | |
76 | setDescription(Messages.CustomTxtParserOutputWizardPage_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); | |
110 | previewTable = new CustomEventsTable(new CustomTxtTraceDefinition(), tableContainer, 0); | |
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 CustomTxtTraceDefinition 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 | ||
133 | /* (non-Javadoc) | |
134 | * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean) | |
135 | */ | |
136 | @Override | |
137 | public void setVisible(final boolean visible) { | |
138 | if (visible) { | |
139 | this.definition = wizard.inputPage.getDefinition(); | |
140 | final List<String> outputNames = wizard.inputPage.getInputNames(); | |
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; | |
147 | for (final String name : outputNames) { | |
148 | if (output.name.equals(name)) { | |
149 | found = true; | |
150 | break; | |
151 | } | |
152 | } | |
153 | if (!found) { | |
154 | output.dispose(); | |
155 | iter.remove(); | |
156 | } | |
157 | } | |
158 | ||
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)) { | |
164 | found = true; | |
165 | break; | |
166 | } | |
167 | } | |
168 | if (!found) { | |
169 | outputs.add(new Output(outputsContainer, name)); | |
170 | } | |
171 | } | |
172 | ||
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}); | |
178 | } else { | |
179 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, previewTable.getTable().getItemHeight()}); | |
180 | } | |
181 | setPageComplete(true); | |
182 | } else { | |
183 | setPageComplete(false); | |
184 | } | |
185 | super.setVisible(visible); | |
186 | } | |
187 | ||
188 | private void moveBefore(final Output moved) { | |
189 | final int i = outputs.indexOf(moved); | |
190 | if (i > 0) { | |
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); | |
199 | container.layout(); | |
200 | updatePreviewTable(); | |
201 | } | |
202 | } | |
203 | ||
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); | |
215 | container.layout(); | |
216 | updatePreviewTable(); | |
217 | } | |
218 | } | |
219 | ||
220 | private void updatePreviewTable() { | |
221 | final int CACHE_SIZE = 50; | |
222 | definition.outputs = extractOutputs(); | |
223 | ||
224 | try { | |
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()); | |
228 | writer.close(); | |
229 | ||
b0422293 PT |
230 | final CustomTxtTrace trace = new CustomTxtTrace(null, definition, tmpFile.getAbsolutePath(), CACHE_SIZE); |
231 | trace.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, false); | |
6151d86c PT |
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$ | |
240 | } | |
241 | ||
242 | tableContainer.layout(); | |
243 | container.layout(); | |
244 | } | |
245 | ||
a0a88f65 AM |
246 | /** |
247 | * Extract the list of output columns from the page's contents. | |
248 | * | |
249 | * @return The output columns | |
250 | */ | |
6151d86c PT |
251 | public List<OutputColumn> extractOutputs() { |
252 | int numColumns = 0; | |
253 | for (int i = 0; i < outputs.size(); i++) { | |
254 | if (outputs.get(i).enabledButton.getSelection()) { | |
255 | numColumns++; | |
256 | } | |
257 | } | |
258 | final List<OutputColumn> outputColumns = new ArrayList<OutputColumn>(numColumns); | |
259 | numColumns = 0; | |
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); | |
266 | } | |
267 | } | |
268 | return outputColumns; | |
269 | } | |
270 | ||
271 | private class Output { | |
272 | String name; | |
273 | Button enabledButton; | |
274 | Text nameLabel; | |
275 | Button upButton; | |
276 | Button downButton; | |
277 | ||
278 | public Output(final Composite parent, final String name) { | |
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); | |
300 | upButton.setImage(upImage); | |
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); | |
311 | downButton.setImage(downImage); | |
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 | } |