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