tmf/lttng: Remove unneeded (non-Javadoc) comments
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / parsers / wizards / CustomXmlParserOutputWizardPage.java
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
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;
31 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
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
44 /**
45 * Output wizard page for custom XML trace parsers.
46 *
47 * @author Patrick Tassé
48 */
49 public 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
68 /**
69 * Constructor
70 *
71 * @param wizard
72 * The wizard to which this page belongs
73 */
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 def) {
128 for (final OutputColumn outputColumn : def.outputs) {
129 final Output output = new Output(outputsContainer, outputColumn.name);
130 outputs.add(output);
131 }
132 }
133
134 @Override
135 public void setVisible(final boolean visible) {
136 if (visible) {
137 this.definition = wizard.inputPage.getDefinition();
138 final List<String> outputNames = wizard.inputPage.getInputNames();
139
140 // dispose outputs that have been removed in the input page
141 final Iterator<Output> iter = outputs.iterator();
142 while (iter.hasNext()) {
143 final Output output = iter.next();
144 boolean found = false;
145 for (final String name : outputNames) {
146 if (output.name.equals(name)) {
147 found = true;
148 break;
149 }
150 }
151 if (!found) {
152 output.dispose();
153 iter.remove();
154 }
155 }
156
157 // create outputs that have been added in the input page
158 for (final String name : outputNames) {
159 boolean found = false;
160 for (final Output output : outputs) {
161 if (output.name.equals(name)) {
162 found = true;
163 break;
164 }
165 }
166 if (!found) {
167 outputs.add(new Output(outputsContainer, name));
168 }
169 }
170
171 outputsContainer.layout();
172 outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5);
173 updatePreviewTable();
174 if (sash.getSize().y > outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y + previewTable.getTable().getItemHeight()) {
175 sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sash.getSize().y - outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y});
176 } else {
177 sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, previewTable.getTable().getItemHeight()});
178 }
179 setPageComplete(true);
180 } else {
181 setPageComplete(false);
182 }
183 super.setVisible(visible);
184 }
185
186 private void moveBefore(final Output moved) {
187 final int i = outputs.indexOf(moved);
188 if (i > 0) {
189 final Output previous = outputs.get(i-1);
190 moved.enabledButton.moveAbove(previous.enabledButton);
191 moved.nameLabel.moveBelow(moved.enabledButton);
192 moved.upButton.moveBelow(moved.nameLabel);
193 moved.downButton.moveBelow(moved.upButton);
194 outputs.add(i-1, outputs.remove(i));
195 outputsContainer.layout();
196 outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5);
197 container.layout();
198 updatePreviewTable();
199 }
200 }
201
202 private void moveAfter(final Output moved) {
203 final int i = outputs.indexOf(moved);
204 if (i+1 < outputs.size()) {
205 final Output next = outputs.get(i+1);
206 moved.enabledButton.moveBelow(next.downButton);
207 moved.nameLabel.moveBelow(moved.enabledButton);
208 moved.upButton.moveBelow(moved.nameLabel);
209 moved.downButton.moveBelow(moved.upButton);
210 outputs.add(i+1, outputs.remove(i));
211 outputsContainer.layout();
212 outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5);
213 container.layout();
214 updatePreviewTable();
215 }
216 }
217
218 private void updatePreviewTable() {
219 final int CACHE_SIZE = 50;
220 definition.outputs = extractOutputs();
221
222 try {
223 tmpFile = Activator.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$
224 final FileWriter writer = new FileWriter(tmpFile);
225 writer.write(wizard.inputPage.getInputText());
226 writer.close();
227
228 final CustomXmlTrace trace = new CustomXmlTrace(null, definition, tmpFile.getAbsolutePath(), CACHE_SIZE);
229 trace.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, false);
230 previewTable.dispose();
231 previewTable = new CustomEventsTable(definition, tableContainer, CACHE_SIZE);
232 previewTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
233 previewTable.setTrace(trace, true);
234 } catch (final TmfTraceException e) {
235 Activator.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$
236 } catch (final IOException e) {
237 Activator.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$
238 }
239
240 tableContainer.layout();
241 container.layout();
242 }
243
244 /**
245 * Extract the output columns from the page's current contents.
246 *
247 * @return The list of output columns
248 */
249 public List<OutputColumn> extractOutputs() {
250 int numColumns = 0;
251 for (int i = 0; i < outputs.size(); i++) {
252 if (outputs.get(i).enabledButton.getSelection()) {
253 numColumns++;
254 }
255 }
256 final List<OutputColumn> outputColumns = new ArrayList<OutputColumn>(numColumns);
257 numColumns = 0;
258 for (int i = 0; i < outputs.size(); i++) {
259 final Output output = outputs.get(i);
260 if (output.enabledButton.getSelection()) {
261 final OutputColumn column = new OutputColumn();
262 column.name = output.nameLabel.getText();
263 outputColumns.add(column);
264 }
265 }
266 return outputColumns;
267 }
268
269 private class Output {
270 String name;
271 Button enabledButton;
272 Text nameLabel;
273 Button upButton;
274 Button downButton;
275
276 public Output(final Composite parent, final String name) {
277 this.name = name;
278
279 enabledButton = new Button(parent, SWT.CHECK);
280 enabledButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_visible);
281 enabledButton.setSelection(true);
282 enabledButton.addSelectionListener(new SelectionAdapter() {
283 @Override
284 public void widgetSelected(final SelectionEvent e) {
285 updatePreviewTable();
286 }
287 });
288 // if (messageOutput != null) {
289 // enabledButton.moveAbove(messageOutput.enabledButton);
290 // }
291
292 nameLabel = new Text(parent, SWT.BORDER | SWT.READ_ONLY | SWT.SINGLE);
293 nameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
294 nameLabel.setText(name);
295 nameLabel.moveBelow(enabledButton);
296
297 upButton = new Button(parent, SWT.PUSH);
298 upButton.setImage(upImage);
299 upButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_moveBefore);
300 upButton.addSelectionListener(new SelectionAdapter() {
301 @Override
302 public void widgetSelected(final SelectionEvent e) {
303 moveBefore(Output.this);
304 }
305 });
306 upButton.moveBelow(nameLabel);
307
308 downButton = new Button(parent, SWT.PUSH);
309 downButton.setImage(downImage);
310 downButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_moveAfter);
311 downButton.addSelectionListener(new SelectionAdapter() {
312 @Override
313 public void widgetSelected(final SelectionEvent e) {
314 moveAfter(Output.this);
315 }
316 });
317 downButton.moveBelow(upButton);
318 }
319
320 private void dispose() {
321 enabledButton.dispose();
322 nameLabel.dispose();
323 upButton.dispose();
324 downButton.dispose();
325 }
326 }
327
328 /**
329 * Get the trace definition.
330 *
331 * @return The trace definition
332 */
333 public CustomXmlTraceDefinition getDefinition() {
334 return definition;
335 }
336
337 }
This page took 0.03865 seconds and 5 git commands to generate.