| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2010, 2016 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.tracecompass.internal.tmf.ui.parsers.wizards; |
| 14 | |
| 15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 16 | |
| 17 | import java.io.File; |
| 18 | import java.io.FileWriter; |
| 19 | import java.io.IOException; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
| 23 | import java.util.Map.Entry; |
| 24 | |
| 25 | import org.eclipse.jface.wizard.WizardPage; |
| 26 | import org.eclipse.swt.SWT; |
| 27 | import org.eclipse.swt.custom.SashForm; |
| 28 | import org.eclipse.swt.custom.ScrolledComposite; |
| 29 | import org.eclipse.swt.events.SelectionAdapter; |
| 30 | import org.eclipse.swt.events.SelectionEvent; |
| 31 | import org.eclipse.swt.graphics.Image; |
| 32 | import org.eclipse.swt.layout.GridData; |
| 33 | import org.eclipse.swt.layout.GridLayout; |
| 34 | import org.eclipse.swt.widgets.Button; |
| 35 | import org.eclipse.swt.widgets.Composite; |
| 36 | import org.eclipse.swt.widgets.Text; |
| 37 | import org.eclipse.tracecompass.internal.tmf.core.parsers.custom.CustomEventAspects; |
| 38 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
| 39 | import org.eclipse.tracecompass.internal.tmf.ui.Messages; |
| 40 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; |
| 41 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition; |
| 42 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.OutputColumn; |
| 43 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.Tag; |
| 44 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace; |
| 45 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition; |
| 46 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; |
| 47 | import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer; |
| 48 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer; |
| 49 | import org.eclipse.tracecompass.tmf.ui.viewers.events.TmfEventsTable; |
| 50 | |
| 51 | /** |
| 52 | * Output wizard page for custom XML trace parsers. |
| 53 | * |
| 54 | * @author Patrick Tasse |
| 55 | */ |
| 56 | public class CustomXmlParserOutputWizardPage extends WizardPage { |
| 57 | |
| 58 | private static final Image UP_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/up_button.gif"); //$NON-NLS-1$ |
| 59 | private static final Image DOWN_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/down_button.gif"); //$NON-NLS-1$ |
| 60 | private final CustomXmlParserWizard wizard; |
| 61 | private CustomXmlTraceDefinition definition; |
| 62 | private List<Output> outputs = new ArrayList<>(); |
| 63 | private Composite container; |
| 64 | private SashForm sash; |
| 65 | private ScrolledComposite outputsScrolledComposite; |
| 66 | private Composite outputsContainer; |
| 67 | private Composite tableContainer; |
| 68 | private TmfEventsTable previewTable; |
| 69 | private File tmpFile; |
| 70 | |
| 71 | /** |
| 72 | * Constructor |
| 73 | * |
| 74 | * @param wizard |
| 75 | * The wizard to which this page belongs |
| 76 | */ |
| 77 | protected CustomXmlParserOutputWizardPage(final CustomXmlParserWizard wizard) { |
| 78 | super("CustomParserOutputWizardPage"); //$NON-NLS-1$ |
| 79 | setTitle(wizard.inputPage.getTitle()); |
| 80 | setDescription(Messages.CustomXmlParserOutputWizardPage_description); |
| 81 | this.wizard = wizard; |
| 82 | setPageComplete(false); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void createControl(final Composite parent) { |
| 87 | container = new Composite(parent, SWT.NULL); |
| 88 | container.setLayout(new GridLayout()); |
| 89 | |
| 90 | sash = new SashForm(container, SWT.VERTICAL); |
| 91 | sash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 92 | sash.setBackground(sash.getDisplay().getSystemColor(SWT.COLOR_GRAY)); |
| 93 | |
| 94 | outputsScrolledComposite = new ScrolledComposite(sash, SWT.V_SCROLL); |
| 95 | outputsScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 96 | outputsContainer = new Composite(outputsScrolledComposite, SWT.NONE); |
| 97 | final GridLayout outputsLayout = new GridLayout(4, false); |
| 98 | outputsLayout.marginHeight = 10; |
| 99 | outputsLayout.marginWidth = 0; |
| 100 | outputsContainer.setLayout(outputsLayout); |
| 101 | outputsScrolledComposite.setContent(outputsContainer); |
| 102 | outputsScrolledComposite.setExpandHorizontal(true); |
| 103 | outputsScrolledComposite.setExpandVertical(true); |
| 104 | |
| 105 | outputsContainer.layout(); |
| 106 | |
| 107 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); |
| 108 | |
| 109 | tableContainer = new Composite(sash, SWT.NONE); |
| 110 | final GridLayout tableLayout = new GridLayout(); |
| 111 | tableLayout.marginHeight = 0; |
| 112 | tableLayout.marginWidth = 0; |
| 113 | tableContainer.setLayout(tableLayout); |
| 114 | previewTable = new TmfEventsTable(tableContainer, 0, CustomEventAspects.generateAspects(new CustomXmlTraceDefinition())); |
| 115 | previewTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 116 | |
| 117 | if (wizard.definition != null) { |
| 118 | loadDefinition(wizard.definition); |
| 119 | } |
| 120 | setControl(container); |
| 121 | |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void dispose() { |
| 126 | previewTable.dispose(); |
| 127 | super.dispose(); |
| 128 | } |
| 129 | |
| 130 | private void loadDefinition(final CustomTraceDefinition def) { |
| 131 | for (final OutputColumn outputColumn : def.outputs) { |
| 132 | final Output output = new Output(outputsContainer, outputColumn.tag, outputColumn.name); |
| 133 | outputs.add(output); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void setVisible(final boolean visible) { |
| 139 | if (visible) { |
| 140 | this.definition = wizard.inputPage.getDefinition(); |
| 141 | final List<Entry<Tag, String>> inputs = wizard.inputPage.getInputs(); |
| 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 Entry<Tag, String> input : inputs) { |
| 149 | if (output.tag.equals(input.getKey()) && |
| 150 | output.name.equals(input.getValue())) { |
| 151 | found = true; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | if (!found) { |
| 156 | output.dispose(); |
| 157 | iter.remove(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // create outputs that have been added in the input page |
| 162 | for (final Entry<Tag, String> input : inputs) { |
| 163 | boolean found = false; |
| 164 | for (final Output output : outputs) { |
| 165 | if (output.tag.equals(input.getKey()) && |
| 166 | output.name.equals(input.getValue())) { |
| 167 | found = true; |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | if (!found) { |
| 172 | outputs.add(new Output(outputsContainer, input.getKey(), input.getValue())); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | outputsContainer.layout(); |
| 177 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); |
| 178 | updatePreviewTable(); |
| 179 | if (sash.getSize().y > outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y + previewTable.getTable().getItemHeight()) { |
| 180 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sash.getSize().y - outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y}); |
| 181 | } else { |
| 182 | sash.setWeights(new int[] {outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, previewTable.getTable().getItemHeight()}); |
| 183 | } |
| 184 | setPageComplete(true); |
| 185 | } else { |
| 186 | setPageComplete(false); |
| 187 | } |
| 188 | super.setVisible(visible); |
| 189 | } |
| 190 | |
| 191 | private void moveBefore(final Output moved) { |
| 192 | final int i = outputs.indexOf(moved); |
| 193 | if (i > 0) { |
| 194 | final Output previous = outputs.get(i-1); |
| 195 | moved.enabledButton.moveAbove(previous.enabledButton); |
| 196 | moved.nameLabel.moveBelow(moved.enabledButton); |
| 197 | moved.upButton.moveBelow(moved.nameLabel); |
| 198 | moved.downButton.moveBelow(moved.upButton); |
| 199 | outputs.add(i-1, outputs.remove(i)); |
| 200 | outputsContainer.layout(); |
| 201 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); |
| 202 | container.layout(); |
| 203 | updatePreviewTable(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | private void moveAfter(final Output moved) { |
| 208 | final int i = outputs.indexOf(moved); |
| 209 | if (i+1 < outputs.size()) { |
| 210 | final Output next = outputs.get(i+1); |
| 211 | moved.enabledButton.moveBelow(next.downButton); |
| 212 | moved.nameLabel.moveBelow(moved.enabledButton); |
| 213 | moved.upButton.moveBelow(moved.nameLabel); |
| 214 | moved.downButton.moveBelow(moved.upButton); |
| 215 | outputs.add(i+1, outputs.remove(i)); |
| 216 | outputsContainer.layout(); |
| 217 | outputsScrolledComposite.setMinSize(outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, outputsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-5); |
| 218 | container.layout(); |
| 219 | updatePreviewTable(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | private void updatePreviewTable() { |
| 224 | final int CACHE_SIZE = 50; |
| 225 | definition.outputs = extractOutputs(); |
| 226 | tmpFile = Activator.getDefault().getStateLocation().addTrailingSeparator().append("customwizard.tmp").toFile(); //$NON-NLS-1$ |
| 227 | |
| 228 | try (final FileWriter writer = new FileWriter(tmpFile);) { |
| 229 | writer.write(wizard.inputPage.getInputText()); |
| 230 | } catch (final IOException e) { |
| 231 | Activator.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ |
| 232 | } |
| 233 | |
| 234 | try { |
| 235 | final CustomXmlTrace trace = new CustomXmlTrace(null, definition, tmpFile.getAbsolutePath(), CACHE_SIZE) { |
| 236 | @Override |
| 237 | protected ITmfTraceIndexer createIndexer(int interval) { |
| 238 | return new TmfCheckpointIndexer(this, interval); |
| 239 | } |
| 240 | }; |
| 241 | trace.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, false); |
| 242 | previewTable.dispose(); |
| 243 | previewTable = new TmfEventsTable(tableContainer, CACHE_SIZE, CustomEventAspects.generateAspects(definition)); |
| 244 | previewTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 245 | previewTable.setTrace(trace, true); |
| 246 | } catch (final TmfTraceException e) { |
| 247 | Activator.getDefault().logError("Error creating CustomXmlTrace. File:" + tmpFile.getAbsolutePath(), e); //$NON-NLS-1$ |
| 248 | } |
| 249 | |
| 250 | tableContainer.layout(); |
| 251 | container.layout(); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Extract the output columns from the page's current contents. |
| 256 | * |
| 257 | * @return The list of output columns |
| 258 | */ |
| 259 | public List<OutputColumn> extractOutputs() { |
| 260 | final List<OutputColumn> outputColumns = new ArrayList<>(); |
| 261 | for (Output output : outputs) { |
| 262 | if (output.enabledButton.getSelection()) { |
| 263 | final OutputColumn column = new OutputColumn(checkNotNull(output.tag), checkNotNull(output.name)); |
| 264 | outputColumns.add(column); |
| 265 | } |
| 266 | } |
| 267 | return outputColumns; |
| 268 | } |
| 269 | |
| 270 | private class Output { |
| 271 | Tag tag; |
| 272 | String name; |
| 273 | Button enabledButton; |
| 274 | Text nameLabel; |
| 275 | Button upButton; |
| 276 | Button downButton; |
| 277 | |
| 278 | public Output(final Composite parent, final Tag tag, final String name) { |
| 279 | this.tag = tag; |
| 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(UP_IMAGE); |
| 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(DOWN_IMAGE); |
| 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 | |
| 331 | /** |
| 332 | * Get the trace definition. |
| 333 | * |
| 334 | * @return The trace definition |
| 335 | */ |
| 336 | public CustomXmlTraceDefinition getDefinition() { |
| 337 | return definition; |
| 338 | } |
| 339 | |
| 340 | } |