| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2010, 2014 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 Tasse - 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.CustomEventTableColumns; |
| 26 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
| 27 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTraceDefinition; |
| 28 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTraceDefinition.OutputColumn; |
| 29 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTrace; |
| 30 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTraceDefinition; |
| 31 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; |
| 32 | import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer; |
| 33 | import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer; |
| 34 | import org.eclipse.linuxtools.tmf.ui.viewers.events.TmfEventsTable; |
| 35 | import org.eclipse.swt.SWT; |
| 36 | import org.eclipse.swt.custom.SashForm; |
| 37 | import org.eclipse.swt.custom.ScrolledComposite; |
| 38 | import org.eclipse.swt.events.SelectionAdapter; |
| 39 | import org.eclipse.swt.events.SelectionEvent; |
| 40 | import org.eclipse.swt.graphics.Image; |
| 41 | import org.eclipse.swt.layout.GridData; |
| 42 | import org.eclipse.swt.layout.GridLayout; |
| 43 | import org.eclipse.swt.widgets.Button; |
| 44 | import org.eclipse.swt.widgets.Composite; |
| 45 | import org.eclipse.swt.widgets.Text; |
| 46 | |
| 47 | /** |
| 48 | * Output wizard page for custom XML trace parsers. |
| 49 | * |
| 50 | * @author Patrick Tasse |
| 51 | */ |
| 52 | public class CustomXmlParserOutputWizardPage extends WizardPage { |
| 53 | |
| 54 | private static final Image UP_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$ |
| 55 | private static final Image DOWN_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$ |
| 56 | private final CustomXmlParserWizard wizard; |
| 57 | private CustomXmlTraceDefinition definition; |
| 58 | private List<Output> outputs = new ArrayList<>(); |
| 59 | private Composite container; |
| 60 | private SashForm sash; |
| 61 | private ScrolledComposite outputsScrolledComposite; |
| 62 | private Composite outputsContainer; |
| 63 | private Composite tableContainer; |
| 64 | private TmfEventsTable previewTable; |
| 65 | private File tmpFile; |
| 66 | |
| 67 | /** |
| 68 | * Constructor |
| 69 | * |
| 70 | * @param wizard |
| 71 | * The wizard to which this page belongs |
| 72 | */ |
| 73 | protected CustomXmlParserOutputWizardPage(final CustomXmlParserWizard wizard) { |
| 74 | super("CustomParserOutputWizardPage"); //$NON-NLS-1$ |
| 75 | setTitle(wizard.inputPage.getTitle()); |
| 76 | setDescription(Messages.CustomXmlParserOutputWizardPage_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 TmfEventsTable(tableContainer, 0, CustomEventTableColumns.generateColumns(new CustomXmlTraceDefinition())); |
| 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 | |
| 126 | private void loadDefinition(final CustomTraceDefinition def) { |
| 127 | for (final OutputColumn outputColumn : def.outputs) { |
| 128 | final Output output = new Output(outputsContainer, outputColumn.name); |
| 129 | outputs.add(output); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void setVisible(final boolean visible) { |
| 135 | if (visible) { |
| 136 | this.definition = wizard.inputPage.getDefinition(); |
| 137 | final List<String> outputNames = wizard.inputPage.getInputNames(); |
| 138 | |
| 139 | // dispose outputs that have been removed in the input page |
| 140 | final Iterator<Output> iter = outputs.iterator(); |
| 141 | while (iter.hasNext()) { |
| 142 | final Output output = iter.next(); |
| 143 | boolean found = false; |
| 144 | for (final String name : outputNames) { |
| 145 | if (output.name.equals(name)) { |
| 146 | found = true; |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | if (!found) { |
| 151 | output.dispose(); |
| 152 | iter.remove(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // create outputs that have been added in the input page |
| 157 | for (final String name : outputNames) { |
| 158 | boolean found = false; |
| 159 | for (final Output output : outputs) { |
| 160 | if (output.name.equals(name)) { |
| 161 | found = true; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | if (!found) { |
| 166 | outputs.add(new Output(outputsContainer, name)); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | outputsContainer.layout(); |
| 171 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); |
| 172 | updatePreviewTable(); |
| 173 | if (sash.getSize().y > outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y + previewTable.getTable().getItemHeight()) { |
| 174 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sash.getSize().y - outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y}); |
| 175 | } else { |
| 176 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, previewTable.getTable().getItemHeight()}); |
| 177 | } |
| 178 | setPageComplete(true); |
| 179 | } else { |
| 180 | setPageComplete(false); |
| 181 | } |
| 182 | super.setVisible(visible); |
| 183 | } |
| 184 | |
| 185 | private void moveBefore(final Output moved) { |
| 186 | final int i = outputs.indexOf(moved); |
| 187 | if (i > 0) { |
| 188 | final Output previous = outputs.get(i-1); |
| 189 | moved.enabledButton.moveAbove(previous.enabledButton); |
| 190 | moved.nameLabel.moveBelow(moved.enabledButton); |
| 191 | moved.upButton.moveBelow(moved.nameLabel); |
| 192 | moved.downButton.moveBelow(moved.upButton); |
| 193 | outputs.add(i-1, outputs.remove(i)); |
| 194 | outputsContainer.layout(); |
| 195 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); |
| 196 | container.layout(); |
| 197 | updatePreviewTable(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | private void moveAfter(final Output moved) { |
| 202 | final int i = outputs.indexOf(moved); |
| 203 | if (i+1 < outputs.size()) { |
| 204 | final Output next = outputs.get(i+1); |
| 205 | moved.enabledButton.moveBelow(next.downButton); |
| 206 | moved.nameLabel.moveBelow(moved.enabledButton); |
| 207 | moved.upButton.moveBelow(moved.nameLabel); |
| 208 | moved.downButton.moveBelow(moved.upButton); |
| 209 | outputs.add(i+1, outputs.remove(i)); |
| 210 | outputsContainer.layout(); |
| 211 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); |
| 212 | container.layout(); |
| 213 | updatePreviewTable(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private void updatePreviewTable() { |
| 218 | final int CACHE_SIZE = 50; |
| 219 | definition.outputs = extractOutputs(); |
| 220 | tmpFile = Activator.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$ |
| 221 | |
| 222 | try (final FileWriter writer = new FileWriter(tmpFile);) { |
| 223 | writer.write(wizard.inputPage.getInputText()); |
| 224 | } catch (final IOException e) { |
| 225 | Activator.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ |
| 226 | } |
| 227 | |
| 228 | try { |
| 229 | final CustomXmlTrace trace = new CustomXmlTrace(null, definition, tmpFile.getAbsolutePath(), CACHE_SIZE) { |
| 230 | @Override |
| 231 | protected ITmfTraceIndexer createIndexer(int interval) { |
| 232 | return new TmfCheckpointIndexer(this, interval); |
| 233 | } |
| 234 | }; |
| 235 | trace.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, false); |
| 236 | previewTable.dispose(); |
| 237 | previewTable = new TmfEventsTable(tableContainer, CACHE_SIZE, CustomEventTableColumns.generateColumns(definition)); |
| 238 | previewTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 239 | previewTable.setTrace(trace, true); |
| 240 | } catch (final TmfTraceException e) { |
| 241 | Activator.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ |
| 242 | } |
| 243 | |
| 244 | tableContainer.layout(); |
| 245 | container.layout(); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Extract the output columns from the page's current contents. |
| 250 | * |
| 251 | * @return The list of output columns |
| 252 | */ |
| 253 | public List<OutputColumn> extractOutputs() { |
| 254 | int numColumns = 0; |
| 255 | for (int i = 0; i < outputs.size(); i++) { |
| 256 | if (outputs.get(i).enabledButton.getSelection()) { |
| 257 | numColumns++; |
| 258 | } |
| 259 | } |
| 260 | final List<OutputColumn> outputColumns = new ArrayList<>(numColumns); |
| 261 | numColumns = 0; |
| 262 | for (int i = 0; i < outputs.size(); i++) { |
| 263 | final Output output = outputs.get(i); |
| 264 | if (output.enabledButton.getSelection()) { |
| 265 | final OutputColumn column = new OutputColumn(); |
| 266 | column.name = output.nameLabel.getText(); |
| 267 | outputColumns.add(column); |
| 268 | } |
| 269 | } |
| 270 | return outputColumns; |
| 271 | } |
| 272 | |
| 273 | private class Output { |
| 274 | String name; |
| 275 | Button enabledButton; |
| 276 | Text nameLabel; |
| 277 | Button upButton; |
| 278 | Button downButton; |
| 279 | |
| 280 | public Output(final Composite parent, final String name) { |
| 281 | this.name = name; |
| 282 | |
| 283 | enabledButton = new Button(parent, SWT.CHECK); |
| 284 | enabledButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_visible); |
| 285 | enabledButton.setSelection(true); |
| 286 | enabledButton.addSelectionListener(new SelectionAdapter() { |
| 287 | @Override |
| 288 | public void widgetSelected(final SelectionEvent e) { |
| 289 | updatePreviewTable(); |
| 290 | } |
| 291 | }); |
| 292 | // if (messageOutput != null) { |
| 293 | // enabledButton.moveAbove(messageOutput.enabledButton); |
| 294 | // } |
| 295 | |
| 296 | nameLabel = new Text(parent, SWT.BORDER | SWT.READ_ONLY | SWT.SINGLE); |
| 297 | nameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); |
| 298 | nameLabel.setText(name); |
| 299 | nameLabel.moveBelow(enabledButton); |
| 300 | |
| 301 | upButton = new Button(parent, SWT.PUSH); |
| 302 | upButton.setImage(UP_IMAGE); |
| 303 | upButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_moveBefore); |
| 304 | upButton.addSelectionListener(new SelectionAdapter() { |
| 305 | @Override |
| 306 | public void widgetSelected(final SelectionEvent e) { |
| 307 | moveBefore(Output.this); |
| 308 | } |
| 309 | }); |
| 310 | upButton.moveBelow(nameLabel); |
| 311 | |
| 312 | downButton = new Button(parent, SWT.PUSH); |
| 313 | downButton.setImage(DOWN_IMAGE); |
| 314 | downButton.setToolTipText(Messages.CustomXmlParserOutputWizardPage_moveAfter); |
| 315 | downButton.addSelectionListener(new SelectionAdapter() { |
| 316 | @Override |
| 317 | public void widgetSelected(final SelectionEvent e) { |
| 318 | moveAfter(Output.this); |
| 319 | } |
| 320 | }); |
| 321 | downButton.moveBelow(upButton); |
| 322 | } |
| 323 | |
| 324 | private void dispose() { |
| 325 | enabledButton.dispose(); |
| 326 | nameLabel.dispose(); |
| 327 | upButton.dispose(); |
| 328 | downButton.dispose(); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Get the trace definition. |
| 334 | * |
| 335 | * @return The trace definition |
| 336 | */ |
| 337 | public CustomXmlTraceDefinition getDefinition() { |
| 338 | return definition; |
| 339 | } |
| 340 | |
| 341 | } |