| 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.tracecompass.internal.tmf.ui.parsers.wizards; |
| 14 | |
| 15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 16 | |
| 17 | import java.io.BufferedReader; |
| 18 | import java.io.IOException; |
| 19 | import java.io.InputStreamReader; |
| 20 | import java.net.URL; |
| 21 | import java.text.ParseException; |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Arrays; |
| 24 | import java.util.HashMap; |
| 25 | import java.util.Iterator; |
| 26 | import java.util.List; |
| 27 | import java.util.Map; |
| 28 | import java.util.Scanner; |
| 29 | import java.util.regex.Matcher; |
| 30 | import java.util.regex.Pattern; |
| 31 | import java.util.regex.PatternSyntaxException; |
| 32 | |
| 33 | import org.eclipse.core.resources.IFile; |
| 34 | import org.eclipse.core.runtime.CoreException; |
| 35 | import org.eclipse.core.runtime.FileLocator; |
| 36 | import org.eclipse.core.runtime.IPath; |
| 37 | import org.eclipse.core.runtime.Path; |
| 38 | import org.eclipse.core.runtime.Platform; |
| 39 | import org.eclipse.jface.viewers.ColumnLabelProvider; |
| 40 | import org.eclipse.jface.viewers.ISelection; |
| 41 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 42 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 43 | import org.eclipse.jface.viewers.ITreeContentProvider; |
| 44 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 45 | import org.eclipse.jface.viewers.StructuredSelection; |
| 46 | import org.eclipse.jface.viewers.TreeViewer; |
| 47 | import org.eclipse.jface.viewers.Viewer; |
| 48 | import org.eclipse.jface.wizard.WizardPage; |
| 49 | import org.eclipse.osgi.util.NLS; |
| 50 | import org.eclipse.swt.SWT; |
| 51 | import org.eclipse.swt.browser.Browser; |
| 52 | import org.eclipse.swt.browser.TitleEvent; |
| 53 | import org.eclipse.swt.browser.TitleListener; |
| 54 | import org.eclipse.swt.custom.SashForm; |
| 55 | import org.eclipse.swt.custom.ScrolledComposite; |
| 56 | import org.eclipse.swt.custom.StyleRange; |
| 57 | import org.eclipse.swt.custom.StyledText; |
| 58 | import org.eclipse.swt.events.ModifyEvent; |
| 59 | import org.eclipse.swt.events.ModifyListener; |
| 60 | import org.eclipse.swt.events.SelectionAdapter; |
| 61 | import org.eclipse.swt.events.SelectionEvent; |
| 62 | import org.eclipse.swt.events.SelectionListener; |
| 63 | import org.eclipse.swt.events.VerifyEvent; |
| 64 | import org.eclipse.swt.events.VerifyListener; |
| 65 | import org.eclipse.swt.graphics.Color; |
| 66 | import org.eclipse.swt.graphics.Font; |
| 67 | import org.eclipse.swt.graphics.FontData; |
| 68 | import org.eclipse.swt.graphics.Image; |
| 69 | import org.eclipse.swt.graphics.Point; |
| 70 | import org.eclipse.swt.graphics.Rectangle; |
| 71 | import org.eclipse.swt.layout.FillLayout; |
| 72 | import org.eclipse.swt.layout.GridData; |
| 73 | import org.eclipse.swt.layout.GridLayout; |
| 74 | import org.eclipse.swt.widgets.Button; |
| 75 | import org.eclipse.swt.widgets.Combo; |
| 76 | import org.eclipse.swt.widgets.Composite; |
| 77 | import org.eclipse.swt.widgets.Display; |
| 78 | import org.eclipse.swt.widgets.Group; |
| 79 | import org.eclipse.swt.widgets.Label; |
| 80 | import org.eclipse.swt.widgets.Shell; |
| 81 | import org.eclipse.swt.widgets.Text; |
| 82 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
| 83 | import org.eclipse.tracecompass.internal.tmf.ui.Messages; |
| 84 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition; |
| 85 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition; |
| 86 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition.Cardinality; |
| 87 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition.InputData; |
| 88 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition.InputLine; |
| 89 | import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType; |
| 90 | import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper; |
| 91 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat; |
| 92 | import org.osgi.framework.Bundle; |
| 93 | |
| 94 | /** |
| 95 | * Input wizard page for custom text parsers. |
| 96 | * |
| 97 | * @author Patrick Tasse |
| 98 | */ |
| 99 | public class CustomTxtParserInputWizardPage extends WizardPage { |
| 100 | |
| 101 | private static final String DEFAULT_REGEX = "\\s*(.*\\S)"; //$NON-NLS-1$ |
| 102 | private static final String DEFAULT_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$ |
| 103 | private static final String TIMESTAMP_FORMAT_BUNDLE = "org.eclipse.tracecompass.doc.user"; //$NON-NLS-1$ |
| 104 | private static final String TIMESTAMP_FORMAT_PATH = "reference/api/org/eclipse/tracecompass/tmf/core/timestamp/TmfTimestampFormat.html"; //$NON-NLS-1$ |
| 105 | private static final String PATTERN_URL = "http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#sum"; //$NON-NLS-1$ |
| 106 | private static final Image LINE_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/line_icon.gif"); //$NON-NLS-1$ |
| 107 | private static final Image ADD_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$ |
| 108 | private static final Image ADD_NEXT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/addnext_button.gif"); //$NON-NLS-1$ |
| 109 | private static final Image ADD_CHILD_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/addchild_button.gif"); //$NON-NLS-1$ |
| 110 | private static final Image DELETE_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$ |
| 111 | private static final Image MOVE_UP_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/moveup_button.gif"); //$NON-NLS-1$ |
| 112 | private static final Image MOVE_DOWN_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/movedown_button.gif"); //$NON-NLS-1$ |
| 113 | private static final Image HELP_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/help_button.gif"); //$NON-NLS-1$ |
| 114 | private static final Color COLOR_BLACK = Display.getDefault().getSystemColor(SWT.COLOR_BLACK); |
| 115 | private static final Color COLOR_LIGHT_GREEN = new Color(Display.getDefault(), 192, 255, 192); |
| 116 | private static final Color COLOR_GREEN = Display.getDefault().getSystemColor(SWT.COLOR_GREEN); |
| 117 | private static final Color COLOR_LIGHT_YELLOW = new Color(Display.getDefault(), 255, 255, 192); |
| 118 | private static final Color COLOR_YELLOW = Display.getDefault().getSystemColor(SWT.COLOR_YELLOW); |
| 119 | private static final Color COLOR_LIGHT_MAGENTA = new Color(Display.getDefault(), 255, 192, 255); |
| 120 | private static final Color COLOR_MAGENTA = Display.getDefault().getSystemColor(SWT.COLOR_MAGENTA); |
| 121 | private static final Color COLOR_LIGHT_RED = new Color(Display.getDefault(), 255, 192, 192); |
| 122 | private static final Color COLOR_TEXT_BACKGROUND = Display.getDefault().getSystemColor(SWT.COLOR_WHITE); |
| 123 | private static final Color COLOR_WIDGET_BACKGROUND = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); |
| 124 | |
| 125 | private final ISelection selection; |
| 126 | private CustomTxtTraceDefinition definition; |
| 127 | private String editCategoryName; |
| 128 | private String editDefinitionName; |
| 129 | private String defaultDescription; |
| 130 | private Line selectedLine; |
| 131 | private Composite container; |
| 132 | private Text categoryText; |
| 133 | private Text logtypeText; |
| 134 | private Text timestampOutputFormatText; |
| 135 | private Text timestampPreviewText; |
| 136 | private ScrolledComposite lineScrolledComposite; |
| 137 | private TreeViewer treeViewer; |
| 138 | private Composite lineContainer; |
| 139 | private StyledText inputText; |
| 140 | private Font fixedFont; |
| 141 | private UpdateListener updateListener; |
| 142 | private Browser helpBrowser; |
| 143 | |
| 144 | // variables used recursively through line traversal |
| 145 | private String timeStampFormat; |
| 146 | private boolean timestampFound; |
| 147 | |
| 148 | /** |
| 149 | * Constructor |
| 150 | * |
| 151 | * @param selection |
| 152 | * The Selection object |
| 153 | * @param definition |
| 154 | * The trace definition |
| 155 | */ |
| 156 | protected CustomTxtParserInputWizardPage(ISelection selection, |
| 157 | CustomTxtTraceDefinition definition) { |
| 158 | super("CustomParserWizardPage"); //$NON-NLS-1$ |
| 159 | if (definition == null) { |
| 160 | setTitle(Messages.CustomTxtParserInputWizardPage_titleNew); |
| 161 | defaultDescription = Messages.CustomTxtParserInputWizardPage_descriptionNew; |
| 162 | } else { |
| 163 | setTitle(Messages.CustomTxtParserInputWizardPage_titleEdit); |
| 164 | defaultDescription = Messages.CustomTxtParserInputWizardPage_desccriptionEdit; |
| 165 | } |
| 166 | setDescription(defaultDescription); |
| 167 | this.selection = selection; |
| 168 | this.definition = definition; |
| 169 | if (definition != null) { |
| 170 | this.editCategoryName = definition.categoryName; |
| 171 | this.editDefinitionName = definition.definitionName; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void createControl(Composite parent) { |
| 177 | container = new Composite(parent, SWT.NULL); |
| 178 | container.setLayout(new GridLayout()); |
| 179 | |
| 180 | updateListener = new UpdateListener(); |
| 181 | |
| 182 | Composite headerComposite = new Composite(container, SWT.FILL); |
| 183 | GridLayout headerLayout = new GridLayout(5, false); |
| 184 | headerLayout.marginHeight = 0; |
| 185 | headerLayout.marginWidth = 0; |
| 186 | headerComposite.setLayout(headerLayout); |
| 187 | headerComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 188 | |
| 189 | Label categoryLabel = new Label(headerComposite, SWT.NULL); |
| 190 | categoryLabel.setText(Messages.CustomTxtParserInputWizardPage_category); |
| 191 | |
| 192 | categoryText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE); |
| 193 | categoryText.setLayoutData(new GridData(120, SWT.DEFAULT)); |
| 194 | |
| 195 | Label timestampFormatLabel = new Label(headerComposite, SWT.NULL); |
| 196 | timestampFormatLabel.setText(Messages.CustomTxtParserInputWizardPage_timestampFormat); |
| 197 | |
| 198 | timestampOutputFormatText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE); |
| 199 | timestampOutputFormatText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 200 | timestampOutputFormatText.setText(DEFAULT_TIMESTAMP_FORMAT); |
| 201 | |
| 202 | Button timeStampFormatHelpButton = new Button(headerComposite, SWT.PUSH); |
| 203 | timeStampFormatHelpButton.setImage(HELP_IMAGE); |
| 204 | timeStampFormatHelpButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_timestampFormatHelp); |
| 205 | timeStampFormatHelpButton.addSelectionListener(new SelectionAdapter() { |
| 206 | @Override |
| 207 | public void widgetSelected(SelectionEvent e) { |
| 208 | Bundle plugin = Platform.getBundle(TIMESTAMP_FORMAT_BUNDLE); |
| 209 | IPath path = new Path(TIMESTAMP_FORMAT_PATH); |
| 210 | URL fileURL = FileLocator.find(plugin, path, null); |
| 211 | try { |
| 212 | URL pageURL = FileLocator.toFileURL(fileURL); |
| 213 | openHelpShell(pageURL.toString()); |
| 214 | } catch (IOException e1) { |
| 215 | } |
| 216 | } |
| 217 | }); |
| 218 | |
| 219 | Label logtypeLabel = new Label(headerComposite, SWT.NULL); |
| 220 | logtypeLabel.setText(Messages.CustomTxtParserInputWizardPage_logType); |
| 221 | |
| 222 | logtypeText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE); |
| 223 | logtypeText.setLayoutData(new GridData(120, SWT.DEFAULT)); |
| 224 | logtypeText.setFocus(); |
| 225 | |
| 226 | Label timestampPreviewLabel = new Label(headerComposite, SWT.NULL); |
| 227 | timestampPreviewLabel.setText(Messages.CustomTxtParserInputWizardPage_preview); |
| 228 | |
| 229 | timestampPreviewText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY); |
| 230 | timestampPreviewText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); |
| 231 | timestampPreviewText.setText(Messages.CustomTxtParserInputWizardPage_noMatchingTimestamp); |
| 232 | |
| 233 | Composite buttonBar = new Composite(container, SWT.NONE); |
| 234 | GridLayout buttonBarLayout = new GridLayout(5, false); |
| 235 | buttonBarLayout.marginHeight = 0; |
| 236 | buttonBarLayout.marginWidth = 0; |
| 237 | buttonBar.setLayout(buttonBarLayout); |
| 238 | |
| 239 | Button removeButton = new Button(buttonBar, SWT.PUSH); |
| 240 | removeButton.setImage(DELETE_IMAGE); |
| 241 | removeButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_removeLine); |
| 242 | removeButton.addSelectionListener(new SelectionAdapter() { |
| 243 | @Override |
| 244 | public void widgetSelected(SelectionEvent e) { |
| 245 | if (treeViewer.getSelection().isEmpty() || selectedLine == null) { |
| 246 | return; |
| 247 | } |
| 248 | removeLine(); |
| 249 | InputLine inputLine = (InputLine) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement(); |
| 250 | if (inputLine.parentInput == null) { |
| 251 | definition.inputs.remove(inputLine); |
| 252 | } else { |
| 253 | inputLine.parentInput.childrenInputs.remove(inputLine); |
| 254 | } |
| 255 | treeViewer.refresh(); |
| 256 | validate(); |
| 257 | updatePreviews(); |
| 258 | } |
| 259 | }); |
| 260 | Button addNextButton = new Button(buttonBar, SWT.PUSH); |
| 261 | addNextButton.setImage(ADD_NEXT_IMAGE); |
| 262 | addNextButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_addNextLine); |
| 263 | addNextButton.addSelectionListener(new SelectionAdapter() { |
| 264 | @Override |
| 265 | public void widgetSelected(SelectionEvent e) { |
| 266 | InputLine inputLine = new InputLine(Cardinality.ZERO_OR_MORE, "", null); //$NON-NLS-1$ |
| 267 | if (((List<?>) treeViewer.getInput()).size() == 0) { |
| 268 | definition.inputs.add(inputLine); |
| 269 | } else if (treeViewer.getSelection().isEmpty()) { |
| 270 | return; |
| 271 | } else { |
| 272 | InputLine previousInputLine = (InputLine) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement(); |
| 273 | if (previousInputLine.parentInput == null) { |
| 274 | for (int i = 0; i < definition.inputs.size(); i++) { |
| 275 | if (definition.inputs.get(i).equals(previousInputLine)) { |
| 276 | definition.inputs.add(i + 1, inputLine); |
| 277 | } |
| 278 | } |
| 279 | } else { |
| 280 | previousInputLine.addNext(inputLine); |
| 281 | } |
| 282 | } |
| 283 | treeViewer.refresh(); |
| 284 | treeViewer.setSelection(new StructuredSelection(inputLine), true); |
| 285 | } |
| 286 | }); |
| 287 | Button addChildButton = new Button(buttonBar, SWT.PUSH); |
| 288 | addChildButton.setImage(ADD_CHILD_IMAGE); |
| 289 | addChildButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_addChildLine); |
| 290 | addChildButton.addSelectionListener(new SelectionAdapter() { |
| 291 | @Override |
| 292 | public void widgetSelected(SelectionEvent e) { |
| 293 | InputLine inputLine = new InputLine(Cardinality.ZERO_OR_MORE, "", null); //$NON-NLS-1$ |
| 294 | if (((List<?>) treeViewer.getInput()).size() == 0) { |
| 295 | definition.inputs.add(inputLine); |
| 296 | } else if (treeViewer.getSelection().isEmpty()) { |
| 297 | return; |
| 298 | } else { |
| 299 | InputLine parentInputLine = (InputLine) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement(); |
| 300 | parentInputLine.addChild(inputLine); |
| 301 | } |
| 302 | treeViewer.refresh(); |
| 303 | treeViewer.setSelection(new StructuredSelection(inputLine), true); |
| 304 | } |
| 305 | }); |
| 306 | Button moveUpButton = new Button(buttonBar, SWT.PUSH); |
| 307 | moveUpButton.setImage(MOVE_UP_IMAGE); |
| 308 | moveUpButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_moveUp); |
| 309 | moveUpButton.addSelectionListener(new SelectionAdapter() { |
| 310 | @Override |
| 311 | public void widgetSelected(SelectionEvent e) { |
| 312 | if (treeViewer.getSelection().isEmpty()) { |
| 313 | return; |
| 314 | } |
| 315 | InputLine inputLine = (InputLine) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement(); |
| 316 | if (inputLine.parentInput == null) { |
| 317 | for (int i = 1; i < definition.inputs.size(); i++) { |
| 318 | if (definition.inputs.get(i).equals(inputLine)) { |
| 319 | definition.inputs.add(i - 1, definition.inputs.remove(i)); |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | } else { |
| 324 | inputLine.moveUp(); |
| 325 | } |
| 326 | treeViewer.refresh(); |
| 327 | validate(); |
| 328 | updatePreviews(); |
| 329 | } |
| 330 | }); |
| 331 | Button moveDownButton = new Button(buttonBar, SWT.PUSH); |
| 332 | moveDownButton.setImage(MOVE_DOWN_IMAGE); |
| 333 | moveDownButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_moveDown); |
| 334 | moveDownButton.addSelectionListener(new SelectionAdapter() { |
| 335 | @Override |
| 336 | public void widgetSelected(SelectionEvent e) { |
| 337 | if (treeViewer.getSelection().isEmpty()) { |
| 338 | return; |
| 339 | } |
| 340 | InputLine inputLine = (InputLine) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement(); |
| 341 | if (inputLine.parentInput == null) { |
| 342 | for (int i = 0; i < definition.inputs.size() - 1; i++) { |
| 343 | if (definition.inputs.get(i).equals(inputLine)) { |
| 344 | definition.inputs.add(i + 1, definition.inputs.remove(i)); |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | } else { |
| 349 | inputLine.moveDown(); |
| 350 | } |
| 351 | treeViewer.refresh(); |
| 352 | validate(); |
| 353 | updatePreviews(); |
| 354 | } |
| 355 | }); |
| 356 | |
| 357 | SashForm vSash = new SashForm(container, SWT.VERTICAL); |
| 358 | vSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 359 | vSash.setBackground(vSash.getDisplay().getSystemColor(SWT.COLOR_GRAY)); |
| 360 | |
| 361 | SashForm hSash = new SashForm(vSash, SWT.HORIZONTAL); |
| 362 | hSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 363 | |
| 364 | ScrolledComposite treeScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL | SWT.H_SCROLL); |
| 365 | GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true); |
| 366 | gd.heightHint = 200; |
| 367 | gd.widthHint = 200; |
| 368 | treeScrolledComposite.setLayoutData(gd); |
| 369 | Composite treeContainer = new Composite(treeScrolledComposite, SWT.NONE); |
| 370 | treeContainer.setLayout(new FillLayout()); |
| 371 | treeScrolledComposite.setContent(treeContainer); |
| 372 | treeScrolledComposite.setExpandHorizontal(true); |
| 373 | treeScrolledComposite.setExpandVertical(true); |
| 374 | |
| 375 | treeViewer = new TreeViewer(treeContainer, SWT.SINGLE | SWT.BORDER); |
| 376 | treeViewer.setContentProvider(new InputLineTreeNodeContentProvider()); |
| 377 | treeViewer.setLabelProvider(new InputLineTreeLabelProvider()); |
| 378 | treeViewer.addSelectionChangedListener(new InputLineTreeSelectionChangedListener()); |
| 379 | treeContainer.layout(); |
| 380 | |
| 381 | treeScrolledComposite.setMinSize(treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); |
| 382 | |
| 383 | lineScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL); |
| 384 | lineScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 385 | lineContainer = new Composite(lineScrolledComposite, SWT.NONE); |
| 386 | GridLayout linesLayout = new GridLayout(); |
| 387 | linesLayout.marginHeight = 1; |
| 388 | linesLayout.marginWidth = 0; |
| 389 | lineContainer.setLayout(linesLayout); |
| 390 | lineScrolledComposite.setContent(lineContainer); |
| 391 | lineScrolledComposite.setExpandHorizontal(true); |
| 392 | lineScrolledComposite.setExpandVertical(true); |
| 393 | |
| 394 | if (definition == null) { |
| 395 | definition = new CustomTxtTraceDefinition(); |
| 396 | definition.inputs.add(new InputLine(Cardinality.ZERO_OR_MORE, DEFAULT_REGEX, |
| 397 | Arrays.asList(new InputData(CustomTraceDefinition.TAG_MESSAGE, CustomTraceDefinition.ACTION_SET)))); |
| 398 | } |
| 399 | loadDefinition(definition); |
| 400 | treeViewer.expandAll(); |
| 401 | lineContainer.layout(); |
| 402 | |
| 403 | categoryText.addModifyListener(updateListener); |
| 404 | logtypeText.addModifyListener(updateListener); |
| 405 | timestampOutputFormatText.addModifyListener(updateListener); |
| 406 | |
| 407 | lineScrolledComposite.setMinSize(lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1); |
| 408 | |
| 409 | hSash.setWeights(new int[] { 1, 2 }); |
| 410 | |
| 411 | Composite sashBottom = new Composite(vSash, SWT.NONE); |
| 412 | GridLayout sashBottomLayout = new GridLayout(3, false); |
| 413 | sashBottomLayout.marginHeight = 0; |
| 414 | sashBottomLayout.marginWidth = 0; |
| 415 | sashBottom.setLayout(sashBottomLayout); |
| 416 | |
| 417 | Label previewLabel = new Label(sashBottom, SWT.NULL); |
| 418 | previewLabel.setText(Messages.CustomTxtParserInputWizardPage_previewInput); |
| 419 | previewLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 420 | |
| 421 | Button highlightAllButton = new Button(sashBottom, SWT.PUSH); |
| 422 | highlightAllButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 423 | highlightAllButton.setText(Messages.CustomTxtParserInputWizardPage_highlightAll); |
| 424 | highlightAllButton.addSelectionListener(new SelectionAdapter() { |
| 425 | @Override |
| 426 | public void widgetSelected(SelectionEvent e) { |
| 427 | updatePreviews(true); |
| 428 | } |
| 429 | }); |
| 430 | |
| 431 | Button legendButton = new Button(sashBottom, SWT.PUSH); |
| 432 | legendButton.setImage(HELP_IMAGE); |
| 433 | legendButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_previewLegend); |
| 434 | legendButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 435 | legendButton.addSelectionListener(new SelectionAdapter() { |
| 436 | @Override |
| 437 | public void widgetSelected(SelectionEvent e) { |
| 438 | openLegend(); |
| 439 | } |
| 440 | }); |
| 441 | |
| 442 | inputText = new StyledText(sashBottom, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); |
| 443 | if (fixedFont == null) { |
| 444 | if (System.getProperty("os.name").contains("Windows")) { //$NON-NLS-1$ //$NON-NLS-2$ |
| 445 | fixedFont = new Font(Display.getCurrent(), new FontData("Courier New", 10, SWT.NORMAL)); //$NON-NLS-1$ |
| 446 | } else { |
| 447 | fixedFont = new Font(Display.getCurrent(), new FontData("Monospace", 10, SWT.NORMAL)); //$NON-NLS-1$ |
| 448 | } |
| 449 | } |
| 450 | inputText.setFont(fixedFont); |
| 451 | gd = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1); |
| 452 | gd.heightHint = inputText.computeSize(SWT.DEFAULT, inputText.getLineHeight() * 4).y; |
| 453 | gd.widthHint = 800; |
| 454 | inputText.setLayoutData(gd); |
| 455 | inputText.setText(getSelectionText()); |
| 456 | inputText.addModifyListener(updateListener); |
| 457 | |
| 458 | vSash.setWeights(new int[] { hSash.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sashBottom.computeSize(SWT.DEFAULT, SWT.DEFAULT).y }); |
| 459 | |
| 460 | setControl(container); |
| 461 | |
| 462 | validate(); |
| 463 | updatePreviews(); |
| 464 | } |
| 465 | |
| 466 | private static class InputLineTreeNodeContentProvider implements ITreeContentProvider { |
| 467 | |
| 468 | @Override |
| 469 | public Object[] getElements(Object inputElement) { |
| 470 | return ((List<?>) inputElement).toArray(); |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public Object[] getChildren(Object parentElement) { |
| 475 | InputLine inputLine = (InputLine) parentElement; |
| 476 | if (inputLine.childrenInputs == null) { |
| 477 | return new InputLine[0]; |
| 478 | } |
| 479 | return inputLine.childrenInputs.toArray(); |
| 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public boolean hasChildren(Object element) { |
| 484 | InputLine inputLine = (InputLine) element; |
| 485 | return (inputLine.childrenInputs != null && inputLine.childrenInputs.size() > 0); |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public void dispose() { |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
| 494 | } |
| 495 | |
| 496 | @Override |
| 497 | public Object getParent(Object element) { |
| 498 | InputLine inputLine = (InputLine) element; |
| 499 | return inputLine.parentInput; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | private class InputLineTreeLabelProvider extends ColumnLabelProvider { |
| 504 | |
| 505 | @Override |
| 506 | public Image getImage(Object element) { |
| 507 | return LINE_IMAGE; |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public String getText(Object element) { |
| 512 | InputLine inputLine = (InputLine) element; |
| 513 | if (inputLine.parentInput == null) { |
| 514 | return "Root Line " + getName(inputLine) + " " + inputLine.cardinality.toString() + " : " + inputLine.getRegex(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 515 | } |
| 516 | return "Line " + getName(inputLine) + " " + inputLine.cardinality.toString() + " : " + inputLine.getRegex(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | private class InputLineTreeSelectionChangedListener implements ISelectionChangedListener { |
| 521 | @Override |
| 522 | public void selectionChanged(SelectionChangedEvent event) { |
| 523 | if (selectedLine != null) { |
| 524 | selectedLine.dispose(); |
| 525 | } |
| 526 | if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) { |
| 527 | IStructuredSelection sel = (IStructuredSelection) event.getSelection(); |
| 528 | InputLine inputLine = (InputLine) sel.getFirstElement(); |
| 529 | selectedLine = new Line(lineContainer, getName(inputLine), inputLine); |
| 530 | lineContainer.layout(); |
| 531 | lineScrolledComposite.setMinSize(lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1); |
| 532 | container.layout(); |
| 533 | validate(); |
| 534 | updatePreviews(); |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public void dispose() { |
| 541 | if (fixedFont != null) { |
| 542 | fixedFont.dispose(); |
| 543 | fixedFont = null; |
| 544 | } |
| 545 | super.dispose(); |
| 546 | } |
| 547 | |
| 548 | private void loadDefinition(CustomTxtTraceDefinition def) { |
| 549 | categoryText.setText(def.categoryName); |
| 550 | logtypeText.setText(def.definitionName); |
| 551 | timestampOutputFormatText.setText(def.timeStampOutputFormat); |
| 552 | treeViewer.setInput(def.inputs); |
| 553 | if (def.inputs.size() > 0) { |
| 554 | InputLine inputLine = def.inputs.get(0); |
| 555 | treeViewer.setSelection(new StructuredSelection(inputLine)); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | private String getName(InputLine inputLine) { |
| 560 | if (inputLine.parentInput == null) { |
| 561 | return Integer.toString(definition.inputs.indexOf(inputLine) + 1); |
| 562 | } |
| 563 | return getName(inputLine.parentInput) + "." + Integer.toString(inputLine.parentInput.childrenInputs.indexOf(inputLine) + 1); //$NON-NLS-1$ |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Get the global list of input names. |
| 568 | * |
| 569 | * @return The list of input names |
| 570 | */ |
| 571 | public List<String> getInputNames() { |
| 572 | List<String> inputs = new ArrayList<>(); |
| 573 | for (InputLine inputLine : definition.inputs) { |
| 574 | for (String inputName : getInputNames(inputLine)) { |
| 575 | if (!inputs.contains(inputName)) { |
| 576 | inputs.add(inputName); |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | return inputs; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Get the list of input names for the given input line. |
| 585 | * |
| 586 | * @param inputLine |
| 587 | * The input line |
| 588 | * @return The list of input names |
| 589 | */ |
| 590 | public List<String> getInputNames(InputLine inputLine) { |
| 591 | List<String> inputs = new ArrayList<>(); |
| 592 | if (inputLine.columns != null) { |
| 593 | for (InputData inputData : inputLine.columns) { |
| 594 | String inputName = inputData.name; |
| 595 | if (!inputs.contains(inputName)) { |
| 596 | inputs.add(inputName); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | if (inputLine.childrenInputs != null) { |
| 601 | for (InputLine childInputLine : inputLine.childrenInputs) { |
| 602 | for (String inputName : getInputNames(childInputLine)) { |
| 603 | if (!inputs.contains(inputName)) { |
| 604 | inputs.add(inputName); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | return inputs; |
| 610 | } |
| 611 | |
| 612 | private void removeLine() { |
| 613 | selectedLine.dispose(); |
| 614 | selectedLine = null; |
| 615 | lineContainer.layout(); |
| 616 | lineScrolledComposite.setMinSize(lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1); |
| 617 | container.layout(); |
| 618 | } |
| 619 | |
| 620 | private String getSelectionText() { |
| 621 | if (this.selection instanceof IStructuredSelection) { |
| 622 | Object sel = ((IStructuredSelection) this.selection).getFirstElement(); |
| 623 | if (sel instanceof IFile) { |
| 624 | IFile file = (IFile) sel; |
| 625 | BufferedReader reader = null; |
| 626 | try { |
| 627 | reader = new BufferedReader(new InputStreamReader(file.getContents())); |
| 628 | StringBuilder sb = new StringBuilder(); |
| 629 | String line = null; |
| 630 | while ((line = reader.readLine()) != null) { |
| 631 | sb.append(line); |
| 632 | sb.append('\n'); |
| 633 | } |
| 634 | return sb.toString(); |
| 635 | } catch (CoreException e) { |
| 636 | return ""; //$NON-NLS-1$ |
| 637 | } catch (IOException e) { |
| 638 | return ""; //$NON-NLS-1$ |
| 639 | } finally { |
| 640 | if (reader != null) { |
| 641 | try { |
| 642 | reader.close(); |
| 643 | } catch (IOException e) { |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | return ""; //$NON-NLS-1$ |
| 650 | } |
| 651 | |
| 652 | private void updatePreviews() { |
| 653 | updatePreviews(false); |
| 654 | } |
| 655 | |
| 656 | private void updatePreviews(boolean updateAll) { |
| 657 | if (inputText == null) { |
| 658 | // early update during construction |
| 659 | return; |
| 660 | } |
| 661 | inputText.setStyleRanges(new StyleRange[] {}); |
| 662 | |
| 663 | try (Scanner scanner = new Scanner(inputText.getText());) { |
| 664 | scanner.useDelimiter("\n"); //$NON-NLS-1$ |
| 665 | |
| 666 | int rawPos = 0; |
| 667 | // skip starting delimiters |
| 668 | String skip = scanner.findWithinHorizon("\\A\n+", 0); //$NON-NLS-1$ |
| 669 | if (skip != null) { |
| 670 | rawPos += skip.length(); |
| 671 | } |
| 672 | |
| 673 | timeStampFormat = null; |
| 674 | if (selectedLine != null) { |
| 675 | for (InputGroup input : selectedLine.inputs) { |
| 676 | input.previewText.setText(Messages.CustomTxtParserInputWizardPage_noMathcingLine); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | Map<String, String> data = new HashMap<>(); |
| 681 | int rootLineMatches = 0; |
| 682 | String firstEntryTimeStamp = null; |
| 683 | String firstEntryTimeStampInputFormat = null; |
| 684 | String line = null; |
| 685 | boolean lineIsNull = true; // needed because of JDT bug with continue at label |
| 686 | event: while (scanner.hasNext() || !lineIsNull) { |
| 687 | if (rootLineMatches > 0 && !updateAll) { |
| 688 | break; |
| 689 | } |
| 690 | if (line == null) { |
| 691 | line = scanner.next(); |
| 692 | lineIsNull = false; |
| 693 | } |
| 694 | int length = line.length(); |
| 695 | String log = line.replaceAll("\r", ""); //$NON-NLS-1$ //$NON-NLS-2$ |
| 696 | for (InputLine rootInputLine : definition.inputs) { |
| 697 | Pattern pattern; |
| 698 | try { |
| 699 | pattern = rootInputLine.getPattern(); |
| 700 | } catch (PatternSyntaxException e) { |
| 701 | continue; |
| 702 | } |
| 703 | Matcher matcher = pattern.matcher(log); |
| 704 | if (matcher.matches()) { |
| 705 | rootLineMatches++; |
| 706 | inputText.setStyleRange(new StyleRange(rawPos, length, |
| 707 | COLOR_BLACK, COLOR_YELLOW, SWT.ITALIC)); |
| 708 | data = new HashMap<>(); |
| 709 | timeStampFormat = null; |
| 710 | updatePreviewLine(rootInputLine, matcher, data, rawPos, rootLineMatches); |
| 711 | if (rootLineMatches == 1) { |
| 712 | firstEntryTimeStamp = data.get(CustomTraceDefinition.TAG_TIMESTAMP); |
| 713 | firstEntryTimeStampInputFormat = timeStampFormat; |
| 714 | } |
| 715 | HashMap<InputLine, Integer> countMap = new HashMap<>(); |
| 716 | InputLine currentInput = null; |
| 717 | if (rootInputLine.childrenInputs != null && rootInputLine.childrenInputs.size() > 0) { |
| 718 | currentInput = rootInputLine.childrenInputs.get(0); |
| 719 | countMap.put(currentInput, 0); |
| 720 | } |
| 721 | rawPos += length + 1; // +1 for \n |
| 722 | while (scanner.hasNext()) { |
| 723 | line = scanner.next(); |
| 724 | length = line.length(); |
| 725 | log = line.replaceAll("\r", ""); //$NON-NLS-1$ //$NON-NLS-2$ |
| 726 | boolean processed = false; |
| 727 | if (currentInput == null) { |
| 728 | for (InputLine input : definition.inputs) { |
| 729 | try { |
| 730 | matcher = input.getPattern().matcher(log); |
| 731 | } catch (PatternSyntaxException e) { |
| 732 | continue; |
| 733 | } |
| 734 | if (matcher.matches()) { |
| 735 | continue event; |
| 736 | } |
| 737 | } |
| 738 | } else { |
| 739 | if (checkNotNull(countMap.get(currentInput)) >= currentInput.getMinCount()) { |
| 740 | List<InputLine> nextInputs = currentInput.getNextInputs(countMap); |
| 741 | if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) { |
| 742 | for (InputLine input : definition.inputs) { |
| 743 | try { |
| 744 | matcher = input.getPattern().matcher(log); |
| 745 | } catch (PatternSyntaxException e) { |
| 746 | continue; |
| 747 | } |
| 748 | if (matcher.matches()) { |
| 749 | continue event; |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | for (InputLine input : nextInputs) { |
| 754 | try { |
| 755 | matcher = input.getPattern().matcher(log); |
| 756 | } catch (PatternSyntaxException e) { |
| 757 | continue; |
| 758 | } |
| 759 | if (matcher.matches()) { |
| 760 | inputText.setStyleRange(new StyleRange(rawPos, length, |
| 761 | COLOR_BLACK, COLOR_LIGHT_YELLOW, SWT.ITALIC)); |
| 762 | currentInput = input; |
| 763 | updatePreviewLine(currentInput, matcher, data, rawPos, rootLineMatches); |
| 764 | if (countMap.get(currentInput) == null) { |
| 765 | countMap.put(currentInput, 1); |
| 766 | } else { |
| 767 | countMap.put(currentInput, checkNotNull(countMap.get(currentInput)) + 1); |
| 768 | } |
| 769 | Iterator<InputLine> iter = countMap.keySet().iterator(); |
| 770 | while (iter.hasNext()) { |
| 771 | InputLine inputLine = iter.next(); |
| 772 | if (inputLine.level > currentInput.level) { |
| 773 | iter.remove(); |
| 774 | } |
| 775 | } |
| 776 | if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) { |
| 777 | currentInput = currentInput.childrenInputs.get(0); |
| 778 | countMap.put(currentInput, 0); |
| 779 | } else { |
| 780 | if (checkNotNull(countMap.get(currentInput)) >= currentInput.getMaxCount()) { |
| 781 | if (currentInput.getNextInputs(countMap).size() > 0) { |
| 782 | currentInput = currentInput.getNextInputs(countMap).get(0); |
| 783 | if (countMap.get(currentInput) == null) { |
| 784 | countMap.put(currentInput, 0); |
| 785 | } |
| 786 | iter = countMap.keySet().iterator(); |
| 787 | while (iter.hasNext()) { |
| 788 | InputLine inputLine = iter.next(); |
| 789 | if (inputLine.level > currentInput.level) { |
| 790 | iter.remove(); |
| 791 | } |
| 792 | } |
| 793 | } else { |
| 794 | currentInput = null; |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | processed = true; |
| 799 | break; |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | if (!processed && currentInput != null) { |
| 804 | matcher = null; |
| 805 | try { |
| 806 | matcher = currentInput.getPattern().matcher(log); |
| 807 | } catch (PatternSyntaxException e) { |
| 808 | } |
| 809 | if (matcher != null && matcher.matches()) { |
| 810 | inputText.setStyleRange(new StyleRange(rawPos, length, |
| 811 | COLOR_BLACK, COLOR_LIGHT_YELLOW, SWT.ITALIC)); |
| 812 | updatePreviewLine(currentInput, matcher, data, rawPos, rootLineMatches); |
| 813 | countMap.put(currentInput, checkNotNull(countMap.get(currentInput)) + 1); |
| 814 | if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) { |
| 815 | currentInput = currentInput.childrenInputs.get(0); |
| 816 | countMap.put(currentInput, 0); |
| 817 | } else { |
| 818 | if (checkNotNull(countMap.get(currentInput)) >= currentInput.getMaxCount()) { |
| 819 | if (currentInput.getNextInputs(countMap).size() > 0) { |
| 820 | currentInput = currentInput.getNextInputs(countMap).get(0); |
| 821 | if (countMap.get(currentInput) == null) { |
| 822 | countMap.put(currentInput, 0); |
| 823 | } |
| 824 | Iterator<InputLine> iter = countMap.keySet().iterator(); |
| 825 | while (iter.hasNext()) { |
| 826 | InputLine inputLine = iter.next(); |
| 827 | if (inputLine.level > currentInput.level) { |
| 828 | iter.remove(); |
| 829 | } |
| 830 | } |
| 831 | } else { |
| 832 | currentInput = null; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | rawPos += length + 1; // +1 for \n |
| 840 | } |
| 841 | |
| 842 | break; |
| 843 | } |
| 844 | } |
| 845 | rawPos += length + 1; // +1 for \n |
| 846 | line = null; |
| 847 | lineIsNull = true; |
| 848 | } |
| 849 | |
| 850 | if (rootLineMatches == 1) { |
| 851 | firstEntryTimeStamp = data.get(CustomTraceDefinition.TAG_TIMESTAMP); |
| 852 | firstEntryTimeStampInputFormat = timeStampFormat; |
| 853 | } |
| 854 | if (firstEntryTimeStamp == null) { |
| 855 | timestampPreviewText.setText(Messages.CustomTxtParserInputWizardPage_noTimestampGroup); |
| 856 | if (selectedLine != null) { |
| 857 | for (InputGroup group : selectedLine.inputs) { |
| 858 | if (group.tagCombo.getText().equals(CustomTraceDefinition.TAG_TIMESTAMP)) { |
| 859 | timestampPreviewText.setText(Messages.CustomTxtParserInputWizardPage_noMatchingTimestamp); |
| 860 | break; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | } else { |
| 865 | try { |
| 866 | TmfTimestampFormat timestampFormat = new TmfTimestampFormat(firstEntryTimeStampInputFormat); |
| 867 | long timestamp = timestampFormat.parseValue(firstEntryTimeStamp); |
| 868 | timestampFormat = new TmfTimestampFormat(timestampOutputFormatText.getText().trim()); |
| 869 | timestampPreviewText.setText(timestampFormat.format(timestamp)); |
| 870 | } catch (ParseException e) { |
| 871 | timestampPreviewText.setText("*parse exception* [" + firstEntryTimeStamp + "] <> [" + firstEntryTimeStampInputFormat + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 872 | } catch (IllegalArgumentException e) { |
| 873 | timestampPreviewText.setText("*parse exception* [Illegal Argument: " + e.getMessage() + "]"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 874 | } |
| 875 | |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | private void updatePreviewLine(InputLine line, Matcher matcher, Map<String, String> data, int rawPos, int rootLineMatches) { |
| 881 | for (int i = 0; i < line.columns.size(); i++) { |
| 882 | InputData input = line.columns.get(i); |
| 883 | if (i < matcher.groupCount() && matcher.group(i + 1) != null) { |
| 884 | if (line.parentInput == null) { |
| 885 | inputText.setStyleRange(new StyleRange(rawPos + matcher.start(i + 1), matcher.end(i + 1) - matcher.start(i + 1), |
| 886 | COLOR_BLACK, COLOR_GREEN, SWT.BOLD)); |
| 887 | } else { |
| 888 | inputText.setStyleRange(new StyleRange(rawPos + matcher.start(i + 1), matcher.end(i + 1) - matcher.start(i + 1), |
| 889 | COLOR_BLACK, COLOR_LIGHT_GREEN, SWT.BOLD)); |
| 890 | } |
| 891 | String value = matcher.group(i + 1).trim(); |
| 892 | if (selectedLine != null && selectedLine.inputLine.equals(line) && rootLineMatches == 1 && |
| 893 | selectedLine.inputs.get(i).previewText.getText().equals(Messages.CustomTxtParserInputWizardPage_noMatchingLine)) { |
| 894 | selectedLine.inputs.get(i).previewText.setText(value); |
| 895 | } |
| 896 | if (value.length() == 0) { |
| 897 | continue; |
| 898 | } |
| 899 | if (input.action == CustomTraceDefinition.ACTION_SET) { |
| 900 | data.put(input.name, value); |
| 901 | if (input.name.equals(CustomTraceDefinition.TAG_TIMESTAMP)) { |
| 902 | timeStampFormat = input.format; |
| 903 | } |
| 904 | } else if (input.action == CustomTraceDefinition.ACTION_APPEND) { |
| 905 | String s = data.get(input.name); |
| 906 | if (s != null) { |
| 907 | data.put(input.name, s + value); |
| 908 | } else { |
| 909 | data.put(input.name, value); |
| 910 | } |
| 911 | if (input.name.equals(CustomTraceDefinition.TAG_TIMESTAMP)) { |
| 912 | if (timeStampFormat != null) { |
| 913 | timeStampFormat += input.format; |
| 914 | } else { |
| 915 | timeStampFormat = input.format; |
| 916 | } |
| 917 | } |
| 918 | } else if (input.action == CustomTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) { |
| 919 | String s = data.get(input.name); |
| 920 | if (s != null) { |
| 921 | data.put(input.name, s + " | " + value); //$NON-NLS-1$ |
| 922 | } else { |
| 923 | data.put(input.name, value); |
| 924 | } |
| 925 | if (input.name.equals(CustomTraceDefinition.TAG_TIMESTAMP)) { |
| 926 | if (timeStampFormat != null) { |
| 927 | timeStampFormat += " | " + input.format; //$NON-NLS-1$ |
| 928 | } else { |
| 929 | timeStampFormat = input.format; |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | } else { |
| 934 | if (selectedLine != null && selectedLine.inputLine.equals(line) && rootLineMatches == 1) { |
| 935 | if (selectedLine.inputs.get(i).previewText.getText().equals(Messages.CustomTxtParserInputWizardPage_noMatchingLine)) { |
| 936 | selectedLine.inputs.get(i).previewText.setText(Messages.CustomTxtParserInputWizardPage_noMatchingGroup); |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | // highlight the matching groups that have no corresponponding input |
| 942 | for (int i = line.columns.size(); i < matcher.groupCount(); i++) { |
| 943 | if (matcher.group(i + 1) != null) { |
| 944 | if (line.parentInput == null) { |
| 945 | inputText.setStyleRange(new StyleRange(rawPos + matcher.start(i + 1), matcher.end(i + 1) - matcher.start(i + 1), |
| 946 | COLOR_BLACK, COLOR_MAGENTA)); |
| 947 | } else { |
| 948 | inputText.setStyleRange(new StyleRange(rawPos + matcher.start(i + 1), matcher.end(i + 1) - matcher.start(i + 1), |
| 949 | COLOR_BLACK, COLOR_LIGHT_MAGENTA)); |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | private void openHelpShell(String url) { |
| 956 | if (helpBrowser != null && !helpBrowser.isDisposed()) { |
| 957 | helpBrowser.getShell().setActive(); |
| 958 | if (!helpBrowser.getUrl().equals(url)) { |
| 959 | helpBrowser.setUrl(url); |
| 960 | } |
| 961 | return; |
| 962 | } |
| 963 | final Shell helpShell = new Shell(getShell(), SWT.SHELL_TRIM); |
| 964 | helpShell.setLayout(new FillLayout()); |
| 965 | helpBrowser = new Browser(helpShell, SWT.NONE); |
| 966 | helpBrowser.addTitleListener(new TitleListener() { |
| 967 | @Override |
| 968 | public void changed(TitleEvent event) { |
| 969 | helpShell.setText(event.title); |
| 970 | } |
| 971 | }); |
| 972 | Rectangle r = container.getBounds(); |
| 973 | Point p = container.toDisplay(r.x, r.y); |
| 974 | Rectangle trim = helpShell.computeTrim(p.x + (r.width - 750) / 2, p.y + (r.height - 400) / 2, 750, 400); |
| 975 | helpShell.setBounds(trim); |
| 976 | helpShell.open(); |
| 977 | helpBrowser.setUrl(url); |
| 978 | } |
| 979 | |
| 980 | private void openLegend() { |
| 981 | final String cg = Messages.CustomTxtParserInputWizardPage_capturedGroup; |
| 982 | final String ucg = Messages.CustomTxtParserInputWizardPage_unidentifiedCaptureGroup; |
| 983 | final String ut = Messages.CustomTxtParserInputWizardPage_uncapturedText; |
| 984 | int line1start = 0; |
| 985 | String line1 = Messages.CustomTxtParserInputWizardPage_nonMatchingLine; |
| 986 | int line2start = line1start + line1.length(); |
| 987 | String line2 = Messages.CustomTxtParserInputWizardPage_matchingRootLine + ' ' + cg + ' ' + ucg + ' ' + ut + " \n"; //$NON-NLS-1$ |
| 988 | int line3start = line2start + line2.length(); |
| 989 | String line3 = Messages.CustomTxtParserInputWizardPage_matchingOtherLine + ' ' + cg + ' ' + ucg + ' ' + ut + " \n"; //$NON-NLS-1$ |
| 990 | int line4start = line3start + line3.length(); |
| 991 | String line4 = Messages.CustomTxtParserInputWizardPage_matchingOtherLine + ' ' + cg + ' ' + ucg + ' ' + ut + " \n"; //$NON-NLS-1$ |
| 992 | int line5start = line4start + line4.length(); |
| 993 | String line5 = Messages.CustomTxtParserInputWizardPage_nonMatchingLine; |
| 994 | int line6start = line5start + line5.length(); |
| 995 | String line6 = Messages.CustomTxtParserInputWizardPage_matchingRootLine + cg + ' ' + ucg + ' ' + ut + " \n"; //$NON-NLS-1$ |
| 996 | |
| 997 | final Shell legendShell = new Shell(getShell(), SWT.DIALOG_TRIM); |
| 998 | legendShell.setLayout(new FillLayout()); |
| 999 | StyledText legendText = new StyledText(legendShell, SWT.MULTI); |
| 1000 | legendText.setFont(fixedFont); |
| 1001 | legendText.setText(line1 + line2 + line3 + line4 + line5 + line6); |
| 1002 | legendText.setStyleRange(new StyleRange(line2start, line2.length(), COLOR_BLACK, COLOR_YELLOW, SWT.ITALIC)); |
| 1003 | legendText.setStyleRange(new StyleRange(line3start, line3.length(), COLOR_BLACK, COLOR_LIGHT_YELLOW, SWT.ITALIC)); |
| 1004 | legendText.setStyleRange(new StyleRange(line4start, line4.length(), COLOR_BLACK, COLOR_LIGHT_YELLOW, SWT.ITALIC)); |
| 1005 | legendText.setStyleRange(new StyleRange(line6start, line6.length(), COLOR_BLACK, COLOR_YELLOW, SWT.ITALIC)); |
| 1006 | legendText.setStyleRange(new StyleRange(line2start + line2.indexOf(cg), cg.length(), COLOR_BLACK, COLOR_GREEN, SWT.BOLD)); |
| 1007 | legendText.setStyleRange(new StyleRange(line2start + line2.indexOf(ucg), ucg.length(), COLOR_BLACK, COLOR_MAGENTA)); |
| 1008 | legendText.setStyleRange(new StyleRange(line3start + line3.indexOf(cg), cg.length(), COLOR_BLACK, COLOR_LIGHT_GREEN, SWT.BOLD)); |
| 1009 | legendText.setStyleRange(new StyleRange(line3start + line3.indexOf(ucg), ucg.length(), COLOR_BLACK, COLOR_LIGHT_MAGENTA)); |
| 1010 | legendText.setStyleRange(new StyleRange(line4start + line4.indexOf(cg), cg.length(), COLOR_BLACK, COLOR_LIGHT_GREEN, SWT.BOLD)); |
| 1011 | legendText.setStyleRange(new StyleRange(line4start + line4.indexOf(ucg), ucg.length(), COLOR_BLACK, COLOR_LIGHT_MAGENTA)); |
| 1012 | legendText.setStyleRange(new StyleRange(line6start + line6.indexOf(cg), cg.length(), COLOR_BLACK, COLOR_GREEN, SWT.BOLD)); |
| 1013 | legendText.setStyleRange(new StyleRange(line6start + line6.indexOf(ucg), ucg.length(), COLOR_BLACK, COLOR_MAGENTA)); |
| 1014 | legendShell.setText(Messages.CustomTxtParserInputWizardPage_previewLegend); |
| 1015 | legendShell.pack(); |
| 1016 | legendShell.open(); |
| 1017 | } |
| 1018 | |
| 1019 | private class UpdateListener implements ModifyListener, SelectionListener { |
| 1020 | |
| 1021 | @Override |
| 1022 | public void modifyText(ModifyEvent e) { |
| 1023 | validate(); |
| 1024 | updatePreviews(); |
| 1025 | } |
| 1026 | |
| 1027 | @Override |
| 1028 | public void widgetDefaultSelected(SelectionEvent e) { |
| 1029 | validate(); |
| 1030 | updatePreviews(); |
| 1031 | } |
| 1032 | |
| 1033 | @Override |
| 1034 | public void widgetSelected(SelectionEvent e) { |
| 1035 | validate(); |
| 1036 | updatePreviews(); |
| 1037 | } |
| 1038 | |
| 1039 | } |
| 1040 | |
| 1041 | private class Line { |
| 1042 | private static final String INFINITY_STRING = "\u221E"; //$NON-NLS-1$ |
| 1043 | private InputLine inputLine; |
| 1044 | private Group group; |
| 1045 | private Composite labelComposite; |
| 1046 | private Text regexText; |
| 1047 | private Composite cardinalityContainer; |
| 1048 | private Combo cardinalityCombo; |
| 1049 | private Label cardinalityMinLabel; |
| 1050 | private Text cardinalityMinText; |
| 1051 | private Label cardinalityMaxLabel; |
| 1052 | private Text cardinalityMaxText; |
| 1053 | private Button infiniteButton; |
| 1054 | private Button eventTypeButton; |
| 1055 | private Text eventTypeText; |
| 1056 | private List<InputGroup> inputs = new ArrayList<>(); |
| 1057 | private Button addGroupButton; |
| 1058 | private Label addGroupLabel; |
| 1059 | |
| 1060 | public Line(Composite parent, String name, InputLine inputLine) { |
| 1061 | this.inputLine = inputLine; |
| 1062 | |
| 1063 | group = new Group(parent, SWT.NONE); |
| 1064 | group.setText(name); |
| 1065 | group.setLayout(new GridLayout(2, false)); |
| 1066 | group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 1067 | |
| 1068 | labelComposite = new Composite(group, SWT.FILL); |
| 1069 | GridLayout labelLayout = new GridLayout(1, false); |
| 1070 | labelLayout.marginWidth = 0; |
| 1071 | labelLayout.marginHeight = 0; |
| 1072 | labelComposite.setLayout(labelLayout); |
| 1073 | labelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 1074 | |
| 1075 | Label label = new Label(labelComposite, SWT.NULL); |
| 1076 | label.setText(Messages.CustomTxtParserInputWizardPage_regularExpression); |
| 1077 | |
| 1078 | Composite regexContainer = new Composite(group, SWT.NONE); |
| 1079 | GridLayout regexLayout = new GridLayout(2, false); |
| 1080 | regexLayout.marginHeight = 0; |
| 1081 | regexLayout.marginWidth = 0; |
| 1082 | regexContainer.setLayout(regexLayout); |
| 1083 | regexContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 1084 | |
| 1085 | regexText = new Text(regexContainer, SWT.BORDER | SWT.SINGLE); |
| 1086 | GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false); |
| 1087 | gd.widthHint = 0; |
| 1088 | regexText.setLayoutData(gd); |
| 1089 | regexText.setText(inputLine.getRegex()); |
| 1090 | regexText.addModifyListener(updateListener); |
| 1091 | |
| 1092 | Button regexHelpButton = new Button(regexContainer, SWT.PUSH); |
| 1093 | regexHelpButton.setImage(HELP_IMAGE); |
| 1094 | regexHelpButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_regularExpressionHelp); |
| 1095 | regexHelpButton.addSelectionListener(new SelectionAdapter() { |
| 1096 | @Override |
| 1097 | public void widgetSelected(SelectionEvent e) { |
| 1098 | openHelpShell(PATTERN_URL); |
| 1099 | } |
| 1100 | }); |
| 1101 | |
| 1102 | label = new Label(group, SWT.NONE); |
| 1103 | label.setText(Messages.CustomTxtParserInputWizardPage_cardinality); |
| 1104 | label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 1105 | |
| 1106 | cardinalityContainer = new Composite(group, SWT.NONE); |
| 1107 | GridLayout cardinalityLayout = new GridLayout(6, false); |
| 1108 | cardinalityLayout.marginHeight = 0; |
| 1109 | cardinalityLayout.marginWidth = 0; |
| 1110 | cardinalityContainer.setLayout(cardinalityLayout); |
| 1111 | cardinalityContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 1112 | |
| 1113 | cardinalityCombo = new Combo(cardinalityContainer, SWT.DROP_DOWN | SWT.READ_ONLY); |
| 1114 | cardinalityCombo.setItems(new String[] { |
| 1115 | Cardinality.ZERO_OR_MORE.toString(), |
| 1116 | Cardinality.ONE_OR_MORE.toString(), |
| 1117 | Cardinality.ZERO_OR_ONE.toString(), |
| 1118 | Cardinality.ONE.toString(), "(?,?)" }); //$NON-NLS-1$ |
| 1119 | cardinalityCombo.addSelectionListener(new SelectionListener() { |
| 1120 | @Override |
| 1121 | public void widgetDefaultSelected(SelectionEvent e) { |
| 1122 | } |
| 1123 | |
| 1124 | @Override |
| 1125 | public void widgetSelected(SelectionEvent e) { |
| 1126 | switch (cardinalityCombo.getSelectionIndex()) { |
| 1127 | case 4: // (?,?) |
| 1128 | cardinalityMinLabel.setVisible(true); |
| 1129 | cardinalityMinText.setVisible(true); |
| 1130 | cardinalityMaxLabel.setVisible(true); |
| 1131 | cardinalityMaxText.setVisible(true); |
| 1132 | infiniteButton.setVisible(true); |
| 1133 | break; |
| 1134 | default: |
| 1135 | cardinalityMinLabel.setVisible(false); |
| 1136 | cardinalityMinText.setVisible(false); |
| 1137 | cardinalityMaxLabel.setVisible(false); |
| 1138 | cardinalityMaxText.setVisible(false); |
| 1139 | infiniteButton.setVisible(false); |
| 1140 | break; |
| 1141 | } |
| 1142 | cardinalityContainer.layout(); |
| 1143 | validate(); |
| 1144 | updatePreviews(); |
| 1145 | } |
| 1146 | }); |
| 1147 | |
| 1148 | cardinalityMinLabel = new Label(cardinalityContainer, SWT.NONE); |
| 1149 | cardinalityMinLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 1150 | cardinalityMinLabel.setText(Messages.CustomTxtParserInputWizardPage_min); |
| 1151 | cardinalityMinLabel.setVisible(false); |
| 1152 | |
| 1153 | cardinalityMinText = new Text(cardinalityContainer, SWT.BORDER | SWT.SINGLE); |
| 1154 | gd = new GridData(SWT.CENTER, SWT.CENTER, false, false); |
| 1155 | gd.widthHint = 20; |
| 1156 | cardinalityMinText.setLayoutData(gd); |
| 1157 | cardinalityMinText.setVisible(false); |
| 1158 | |
| 1159 | cardinalityMaxLabel = new Label(cardinalityContainer, SWT.NONE); |
| 1160 | cardinalityMaxLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 1161 | cardinalityMaxLabel.setText(Messages.CustomTxtParserInputWizardPage_max); |
| 1162 | cardinalityMaxLabel.setVisible(false); |
| 1163 | |
| 1164 | cardinalityMaxText = new Text(cardinalityContainer, SWT.BORDER | SWT.SINGLE); |
| 1165 | gd = new GridData(SWT.CENTER, SWT.CENTER, false, false); |
| 1166 | gd.widthHint = 20; |
| 1167 | cardinalityMaxText.setLayoutData(gd); |
| 1168 | cardinalityMaxText.setVisible(false); |
| 1169 | |
| 1170 | infiniteButton = new Button(cardinalityContainer, SWT.PUSH); |
| 1171 | infiniteButton.setText(INFINITY_STRING); |
| 1172 | infiniteButton.setVisible(false); |
| 1173 | infiniteButton.addSelectionListener(new SelectionAdapter() { |
| 1174 | @Override |
| 1175 | public void widgetSelected(SelectionEvent e) { |
| 1176 | cardinalityMaxText.setText(INFINITY_STRING); |
| 1177 | } |
| 1178 | }); |
| 1179 | |
| 1180 | if (inputLine.cardinality.equals(Cardinality.ZERO_OR_MORE)) { |
| 1181 | cardinalityCombo.select(0); |
| 1182 | } else if (inputLine.cardinality.equals(Cardinality.ONE_OR_MORE)) { |
| 1183 | cardinalityCombo.select(1); |
| 1184 | } else if (inputLine.cardinality.equals(Cardinality.ZERO_OR_ONE)) { |
| 1185 | cardinalityCombo.select(2); |
| 1186 | } else if (inputLine.cardinality.equals(Cardinality.ONE)) { |
| 1187 | cardinalityCombo.select(3); |
| 1188 | } else { |
| 1189 | cardinalityCombo.select(4); |
| 1190 | cardinalityMinLabel.setVisible(true); |
| 1191 | cardinalityMinText.setVisible(true); |
| 1192 | if (inputLine.getMinCount() >= 0) { |
| 1193 | cardinalityMinText.setText(Integer.toString(inputLine.getMinCount())); |
| 1194 | } |
| 1195 | cardinalityMaxLabel.setVisible(true); |
| 1196 | cardinalityMaxText.setVisible(true); |
| 1197 | if (inputLine.getMaxCount() == Cardinality.INF) { |
| 1198 | cardinalityMaxText.setText(INFINITY_STRING); |
| 1199 | } else if (inputLine.getMaxCount() >= 0) { |
| 1200 | cardinalityMaxText.setText(Integer.toString(inputLine.getMaxCount())); |
| 1201 | } |
| 1202 | infiniteButton.setVisible(true); |
| 1203 | } |
| 1204 | |
| 1205 | VerifyListener digitsListener = new VerifyListener() { |
| 1206 | @Override |
| 1207 | public void verifyText(VerifyEvent e) { |
| 1208 | if (e.text.equals(INFINITY_STRING)) { |
| 1209 | e.doit = e.widget == cardinalityMaxText && e.start == 0 && e.end == ((Text) e.widget).getText().length(); |
| 1210 | } else { |
| 1211 | if (((Text) e.widget).getText().equals(INFINITY_STRING)) { |
| 1212 | e.doit = e.start == 0 && e.end == ((Text) e.widget).getText().length(); |
| 1213 | } |
| 1214 | for (int i = 0; i < e.text.length(); i++) { |
| 1215 | if (!Character.isDigit(e.text.charAt(i))) { |
| 1216 | e.doit = false; |
| 1217 | break; |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | }; |
| 1223 | |
| 1224 | cardinalityMinText.addModifyListener(updateListener); |
| 1225 | cardinalityMaxText.addModifyListener(updateListener); |
| 1226 | cardinalityMinText.addVerifyListener(digitsListener); |
| 1227 | cardinalityMaxText.addVerifyListener(digitsListener); |
| 1228 | |
| 1229 | eventTypeButton = new Button(group, SWT.CHECK); |
| 1230 | eventTypeButton.setText(Messages.CustomTxtParserInputWizardPage_eventType); |
| 1231 | eventTypeButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 1232 | eventTypeButton.addSelectionListener(new SelectionAdapter() { |
| 1233 | @Override |
| 1234 | public void widgetSelected(SelectionEvent e) { |
| 1235 | if (eventTypeButton.getSelection()) { |
| 1236 | eventTypeText.setEnabled(true); |
| 1237 | } else { |
| 1238 | eventTypeText.setEnabled(false); |
| 1239 | } |
| 1240 | } |
| 1241 | }); |
| 1242 | eventTypeButton.addSelectionListener(updateListener); |
| 1243 | |
| 1244 | eventTypeText = new Text(group, SWT.BORDER | SWT.SINGLE); |
| 1245 | gd = new GridData(SWT.FILL, SWT.CENTER, true, false); |
| 1246 | gd.widthHint = 0; |
| 1247 | eventTypeText.setLayoutData(gd); |
| 1248 | if (inputLine.eventType != null) { |
| 1249 | eventTypeText.setText(inputLine.eventType); |
| 1250 | eventTypeButton.setSelection(true); |
| 1251 | } else { |
| 1252 | eventTypeText.setEnabled(false); |
| 1253 | eventTypeButton.setSelection(false); |
| 1254 | } |
| 1255 | eventTypeText.addModifyListener(updateListener); |
| 1256 | |
| 1257 | |
| 1258 | if (inputLine.columns != null) { |
| 1259 | for (InputData inputData : inputLine.columns) { |
| 1260 | InputGroup inputGroup = new InputGroup(group, this, inputs.size() + 1); |
| 1261 | if (inputData.name.equals(CustomTraceDefinition.TAG_TIMESTAMP)) { |
| 1262 | inputGroup.tagCombo.select(0); |
| 1263 | inputGroup.tagText.setText(inputData.format); |
| 1264 | inputGroup.tagLabel.setText(Messages.CustomTxtParserInputWizardPage_format); |
| 1265 | inputGroup.tagLabel.setVisible(true); |
| 1266 | inputGroup.tagText.setVisible(true); |
| 1267 | inputGroup.tagText.addModifyListener(updateListener); |
| 1268 | } else if (inputData.name.equals(CustomTraceDefinition.TAG_EVENT_TYPE)) { |
| 1269 | inputGroup.tagCombo.select(1); |
| 1270 | } else if (inputData.name.equals(CustomTraceDefinition.TAG_MESSAGE)) { |
| 1271 | inputGroup.tagCombo.select(2); |
| 1272 | } else { |
| 1273 | inputGroup.tagCombo.select(3); |
| 1274 | inputGroup.tagText.setText(inputData.name); |
| 1275 | inputGroup.tagLabel.setText(Messages.CustomTxtParserInputWizardPage_name); |
| 1276 | inputGroup.tagLabel.setVisible(true); |
| 1277 | inputGroup.tagText.setVisible(true); |
| 1278 | inputGroup.tagText.addModifyListener(updateListener); |
| 1279 | } |
| 1280 | inputGroup.actionCombo.select(inputData.action); |
| 1281 | inputs.add(inputGroup); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | createAddGroupButton(); |
| 1286 | } |
| 1287 | |
| 1288 | private void createAddGroupButton() { |
| 1289 | addGroupButton = new Button(group, SWT.PUSH); |
| 1290 | addGroupButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 1291 | addGroupButton.setImage(ADD_IMAGE); |
| 1292 | addGroupButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_addGroup); |
| 1293 | addGroupButton.addSelectionListener(new SelectionAdapter() { |
| 1294 | @Override |
| 1295 | public void widgetSelected(SelectionEvent e) { |
| 1296 | removeAddGroupButton(); |
| 1297 | inputs.add(new InputGroup(group, Line.this, inputs.size() + 1)); |
| 1298 | createAddGroupButton(); |
| 1299 | lineContainer.layout(); |
| 1300 | lineScrolledComposite.setMinSize(lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1); |
| 1301 | group.getParent().layout(); |
| 1302 | validate(); |
| 1303 | updatePreviews(); |
| 1304 | } |
| 1305 | }); |
| 1306 | |
| 1307 | addGroupLabel = new Label(group, SWT.NULL); |
| 1308 | addGroupLabel.setText(Messages.CustomTxtParserInputWizardPage_newGroup); |
| 1309 | } |
| 1310 | |
| 1311 | private void removeAddGroupButton() { |
| 1312 | addGroupButton.dispose(); |
| 1313 | addGroupLabel.dispose(); |
| 1314 | } |
| 1315 | |
| 1316 | private void removeInput(int inputNumber) { |
| 1317 | int nb = inputNumber; |
| 1318 | if (--nb < inputs.size()) { |
| 1319 | inputs.remove(nb).dispose(); |
| 1320 | for (int i = nb; i < inputs.size(); i++) { |
| 1321 | inputs.get(i).setInputNumber(i + 1); |
| 1322 | } |
| 1323 | lineContainer.layout(); |
| 1324 | lineScrolledComposite.setMinSize(lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1); |
| 1325 | group.getParent().layout(); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | private void dispose() { |
| 1330 | group.dispose(); |
| 1331 | } |
| 1332 | |
| 1333 | private void extractInputs() { |
| 1334 | inputLine.setRegex(selectedLine.regexText.getText()); |
| 1335 | inputLine.eventType = selectedLine.eventTypeButton.getSelection() ? selectedLine.eventTypeText.getText().trim() : null; |
| 1336 | switch (cardinalityCombo.getSelectionIndex()) { |
| 1337 | case 0: |
| 1338 | inputLine.cardinality = Cardinality.ZERO_OR_MORE; |
| 1339 | break; |
| 1340 | case 1: |
| 1341 | inputLine.cardinality = Cardinality.ONE_OR_MORE; |
| 1342 | break; |
| 1343 | case 2: |
| 1344 | inputLine.cardinality = Cardinality.ZERO_OR_ONE; |
| 1345 | break; |
| 1346 | case 3: |
| 1347 | inputLine.cardinality = Cardinality.ONE; |
| 1348 | break; |
| 1349 | case 4: // (?,?) |
| 1350 | int min, |
| 1351 | max; |
| 1352 | try { |
| 1353 | min = Integer.parseInt(cardinalityMinText.getText()); |
| 1354 | } catch (NumberFormatException e) { |
| 1355 | min = -1; |
| 1356 | } |
| 1357 | try { |
| 1358 | if (cardinalityMaxText.getText().equals(INFINITY_STRING)) { |
| 1359 | max = Cardinality.INF; |
| 1360 | } else { |
| 1361 | max = Integer.parseInt(cardinalityMaxText.getText()); |
| 1362 | } |
| 1363 | } catch (NumberFormatException e) { |
| 1364 | max = -1; |
| 1365 | } |
| 1366 | inputLine.cardinality = new Cardinality(min, max); |
| 1367 | break; |
| 1368 | default: |
| 1369 | inputLine.cardinality = Cardinality.ZERO_OR_MORE; |
| 1370 | break; |
| 1371 | } |
| 1372 | inputLine.columns = new ArrayList<>(inputs.size()); |
| 1373 | for (int i = 0; i < inputs.size(); i++) { |
| 1374 | InputGroup grp = inputs.get(i); |
| 1375 | InputData inputData = new InputData(); |
| 1376 | if (grp.tagCombo.getText().equals(CustomTraceDefinition.TAG_OTHER)) { |
| 1377 | inputData.name = grp.tagText.getText().trim(); |
| 1378 | } else { |
| 1379 | inputData.name = grp.tagCombo.getText(); |
| 1380 | if (grp.tagCombo.getText().equals(CustomTraceDefinition.TAG_TIMESTAMP)) { |
| 1381 | inputData.format = grp.tagText.getText().trim(); |
| 1382 | } |
| 1383 | } |
| 1384 | inputData.action = grp.actionCombo.getSelectionIndex(); |
| 1385 | inputLine.columns.add(inputData); |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | private class InputGroup { |
| 1391 | private Line line; |
| 1392 | private int inputNumber; |
| 1393 | |
| 1394 | // children of parent (must be disposed) |
| 1395 | private Composite labelComposite; |
| 1396 | private Composite tagComposite; |
| 1397 | private Label previewLabel; |
| 1398 | private Text previewText; |
| 1399 | |
| 1400 | // children of labelComposite |
| 1401 | private Label inputLabel; |
| 1402 | |
| 1403 | // children of tagComposite |
| 1404 | private Combo tagCombo; |
| 1405 | private Label tagLabel; |
| 1406 | private Text tagText; |
| 1407 | private Combo actionCombo; |
| 1408 | |
| 1409 | public InputGroup(Composite parent, Line line, int inputNumber) { |
| 1410 | this.line = line; |
| 1411 | this.inputNumber = inputNumber; |
| 1412 | |
| 1413 | labelComposite = new Composite(parent, SWT.FILL); |
| 1414 | GridLayout labelLayout = new GridLayout(2, false); |
| 1415 | labelLayout.marginWidth = 0; |
| 1416 | labelLayout.marginHeight = 0; |
| 1417 | labelComposite.setLayout(labelLayout); |
| 1418 | labelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 1419 | |
| 1420 | Button deleteButton = new Button(labelComposite, SWT.PUSH); |
| 1421 | deleteButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 1422 | deleteButton.setImage(DELETE_IMAGE); |
| 1423 | deleteButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_removeGroup); |
| 1424 | deleteButton.addSelectionListener(new SelectionAdapter() { |
| 1425 | @Override |
| 1426 | public void widgetSelected(SelectionEvent e) { |
| 1427 | InputGroup.this.line.removeInput(InputGroup.this.inputNumber); |
| 1428 | validate(); |
| 1429 | updatePreviews(); |
| 1430 | } |
| 1431 | }); |
| 1432 | |
| 1433 | inputLabel = new Label(labelComposite, SWT.NULL); |
| 1434 | inputLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 1435 | inputLabel.setText(NLS.bind(Messages.CustomTxtParserInputWizardPage_group, inputNumber)); |
| 1436 | |
| 1437 | tagComposite = new Composite(parent, SWT.FILL); |
| 1438 | GridLayout tagLayout = new GridLayout(4, false); |
| 1439 | tagLayout.marginWidth = 0; |
| 1440 | tagLayout.marginHeight = 0; |
| 1441 | tagComposite.setLayout(tagLayout); |
| 1442 | tagComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 1443 | |
| 1444 | tagCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY); |
| 1445 | tagCombo.setItems(new String[] { CustomTraceDefinition.TAG_TIMESTAMP, |
| 1446 | CustomTraceDefinition.TAG_EVENT_TYPE, |
| 1447 | CustomTraceDefinition.TAG_MESSAGE, |
| 1448 | CustomTraceDefinition.TAG_OTHER}); |
| 1449 | tagCombo.select(2); |
| 1450 | tagCombo.addSelectionListener(new SelectionListener() { |
| 1451 | @Override |
| 1452 | public void widgetDefaultSelected(SelectionEvent e) { |
| 1453 | } |
| 1454 | |
| 1455 | @Override |
| 1456 | public void widgetSelected(SelectionEvent e) { |
| 1457 | tagText.removeModifyListener(updateListener); |
| 1458 | switch (tagCombo.getSelectionIndex()) { |
| 1459 | case 0: // Time Stamp |
| 1460 | tagLabel.setText(Messages.CustomTxtParserInputWizardPage_format); |
| 1461 | tagLabel.setVisible(true); |
| 1462 | tagText.setVisible(true); |
| 1463 | tagText.addModifyListener(updateListener); |
| 1464 | break; |
| 1465 | case 1: // Event type |
| 1466 | tagLabel.setVisible(false); |
| 1467 | tagText.setVisible(false); |
| 1468 | break; |
| 1469 | case 2: // Message |
| 1470 | tagLabel.setVisible(false); |
| 1471 | tagText.setVisible(false); |
| 1472 | break; |
| 1473 | case 3: // Other |
| 1474 | tagLabel.setText(Messages.CustomTxtParserInputWizardPage_name); |
| 1475 | tagLabel.setVisible(true); |
| 1476 | tagText.setVisible(true); |
| 1477 | tagText.addModifyListener(updateListener); |
| 1478 | break; |
| 1479 | default: |
| 1480 | break; |
| 1481 | } |
| 1482 | tagComposite.layout(); |
| 1483 | validate(); |
| 1484 | updatePreviews(); |
| 1485 | } |
| 1486 | }); |
| 1487 | |
| 1488 | tagLabel = new Label(tagComposite, SWT.NULL); |
| 1489 | tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 1490 | tagLabel.setVisible(false); |
| 1491 | |
| 1492 | tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE); |
| 1493 | GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false); |
| 1494 | gd.widthHint = 0; |
| 1495 | tagText.setLayoutData(gd); |
| 1496 | tagText.setVisible(false); |
| 1497 | |
| 1498 | actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY); |
| 1499 | actionCombo.setItems(new String[] { Messages.CustomTxtParserInputWizardPage_set, Messages.CustomTxtParserInputWizardPage_append, Messages.CustomTxtParserInputWizardPage_appendWith }); |
| 1500 | actionCombo.select(0); |
| 1501 | actionCombo.addSelectionListener(updateListener); |
| 1502 | |
| 1503 | previewLabel = new Label(parent, SWT.NULL); |
| 1504 | previewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); |
| 1505 | previewLabel.setText(Messages.CustomTxtParserInputWizardPage_preview); |
| 1506 | |
| 1507 | previewText = new Text(parent, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY); |
| 1508 | gd = new GridData(SWT.FILL, SWT.CENTER, true, false); |
| 1509 | gd.widthHint = 0; |
| 1510 | previewText.setLayoutData(gd); |
| 1511 | previewText.setText(Messages.CustomTxtParserInputWizardPage_noMatch); |
| 1512 | previewText.setBackground(COLOR_WIDGET_BACKGROUND); |
| 1513 | } |
| 1514 | |
| 1515 | private void dispose() { |
| 1516 | labelComposite.dispose(); |
| 1517 | tagComposite.dispose(); |
| 1518 | previewLabel.dispose(); |
| 1519 | previewText.dispose(); |
| 1520 | } |
| 1521 | |
| 1522 | private void setInputNumber(int inputNumber) { |
| 1523 | this.inputNumber = inputNumber; |
| 1524 | inputLabel.setText(NLS.bind(Messages.CustomTxtParserInputWizardPage_group, inputNumber)); |
| 1525 | labelComposite.layout(); |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | private void validate() { |
| 1530 | |
| 1531 | definition.categoryName = categoryText.getText().trim(); |
| 1532 | definition.definitionName = logtypeText.getText().trim(); |
| 1533 | definition.timeStampOutputFormat = timestampOutputFormatText.getText().trim(); |
| 1534 | |
| 1535 | if (selectedLine != null) { |
| 1536 | selectedLine.extractInputs(); |
| 1537 | treeViewer.refresh(); |
| 1538 | } |
| 1539 | |
| 1540 | StringBuffer errors = new StringBuffer(); |
| 1541 | |
| 1542 | if (definition.categoryName.length() == 0) { |
| 1543 | errors.append("Enter a category for the new trace type. "); //$NON-NLS-1$ |
| 1544 | categoryText.setBackground(COLOR_LIGHT_RED); |
| 1545 | } else if (definition.definitionName.length() == 0) { |
| 1546 | errors.append("Enter a name for the new trace type. "); //$NON-NLS-1$ |
| 1547 | logtypeText.setBackground(COLOR_LIGHT_RED); |
| 1548 | } else { |
| 1549 | categoryText.setBackground(COLOR_TEXT_BACKGROUND); |
| 1550 | logtypeText.setBackground(COLOR_TEXT_BACKGROUND); |
| 1551 | if (definition.categoryName.indexOf(':') != -1) { |
| 1552 | errors.append("Invalid character ':' in category. "); //$NON-NLS-1$ |
| 1553 | categoryText.setBackground(COLOR_LIGHT_RED); |
| 1554 | } |
| 1555 | if (definition.definitionName.indexOf(':') != -1) { |
| 1556 | errors.append("Invalid character ':' in trace type. "); //$NON-NLS-1$ |
| 1557 | logtypeText.setBackground(COLOR_LIGHT_RED); |
| 1558 | } |
| 1559 | for (TraceTypeHelper helper : TmfTraceType.getTraceTypeHelpers()) { |
| 1560 | if (definition.categoryName.equals(helper.getCategoryName()) && |
| 1561 | definition.definitionName.equals(helper.getName()) && |
| 1562 | (editDefinitionName == null || !editDefinitionName.equals(definition.definitionName)) && |
| 1563 | (editCategoryName == null || !editCategoryName.equals(definition.categoryName))) { |
| 1564 | errors.append("The trace type name already exists. "); //$NON-NLS-1$ |
| 1565 | logtypeText.setBackground(COLOR_LIGHT_RED); |
| 1566 | break; |
| 1567 | } |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | timestampFound = false; |
| 1572 | for (int i = 0; i < definition.inputs.size(); i++) { |
| 1573 | |
| 1574 | InputLine inputLine = definition.inputs.get(i); |
| 1575 | String name = Integer.toString(i + 1); |
| 1576 | errors.append(validateLine(inputLine, name)); |
| 1577 | } |
| 1578 | if (timestampFound) { |
| 1579 | if (definition.timeStampOutputFormat.length() == 0) { |
| 1580 | errors.append("Enter the output format for the Time Stamp field. "); //$NON-NLS-1$ |
| 1581 | timestampOutputFormatText.setBackground(COLOR_LIGHT_RED); |
| 1582 | } else { |
| 1583 | try { |
| 1584 | new TmfTimestampFormat(definition.timeStampOutputFormat); |
| 1585 | timestampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND); |
| 1586 | } catch (IllegalArgumentException e) { |
| 1587 | errors.append("Enter a valid output format for the Time Stamp field [" + e.getMessage() + "]."); //$NON-NLS-1$ //$NON-NLS-2$ |
| 1588 | timestampOutputFormatText.setBackground(COLOR_LIGHT_RED); |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | } else { |
| 1593 | timestampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND); |
| 1594 | } |
| 1595 | |
| 1596 | if (errors.length() == 0) { |
| 1597 | setDescription(defaultDescription); |
| 1598 | setPageComplete(true); |
| 1599 | } else { |
| 1600 | setDescription(errors.toString()); |
| 1601 | setPageComplete(false); |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | /** |
| 1606 | * Validate an input line. |
| 1607 | * |
| 1608 | * @param inputLine |
| 1609 | * The line to clean up |
| 1610 | * @param name |
| 1611 | * The name of the line |
| 1612 | * @return The cleaned up line |
| 1613 | */ |
| 1614 | public StringBuffer validateLine(InputLine inputLine, String name) { |
| 1615 | StringBuffer errors = new StringBuffer(); |
| 1616 | Line line = null; |
| 1617 | if (selectedLine != null && selectedLine.inputLine.equals(inputLine)) { |
| 1618 | line = selectedLine; |
| 1619 | } |
| 1620 | try { |
| 1621 | Pattern.compile(inputLine.getRegex()); |
| 1622 | if (line != null) { |
| 1623 | line.regexText.setBackground(COLOR_TEXT_BACKGROUND); |
| 1624 | } |
| 1625 | } catch (PatternSyntaxException e) { |
| 1626 | errors.append("Enter a valid regular expression (Line " + name + "). "); //$NON-NLS-1$ //$NON-NLS-2$ |
| 1627 | if (line != null) { |
| 1628 | line.regexText.setBackground(COLOR_LIGHT_RED); |
| 1629 | } |
| 1630 | } |
| 1631 | if (inputLine.getMinCount() == -1) { |
| 1632 | errors.append("Enter a minimum value for cardinality (Line " + name + "). "); //$NON-NLS-1$ //$NON-NLS-2$ |
| 1633 | if (line != null) { |
| 1634 | line.cardinalityMinText.setBackground(COLOR_LIGHT_RED); |
| 1635 | } |
| 1636 | } else { |
| 1637 | if (line != null) { |
| 1638 | line.cardinalityMinText.setBackground(COLOR_TEXT_BACKGROUND); |
| 1639 | } |
| 1640 | } |
| 1641 | if (inputLine.getMaxCount() == -1) { |
| 1642 | errors.append("Enter a maximum value for cardinality (Line " + name + "). "); //$NON-NLS-1$ //$NON-NLS-2$ |
| 1643 | if (line != null) { |
| 1644 | line.cardinalityMaxText.setBackground(COLOR_LIGHT_RED); |
| 1645 | } |
| 1646 | } else if (inputLine.getMinCount() > inputLine.getMaxCount()) { |
| 1647 | errors.append("Enter correct (min <= max) values for cardinality (Line " + name + "). "); //$NON-NLS-1$ //$NON-NLS-2$ |
| 1648 | if (line != null) { |
| 1649 | line.cardinalityMinText.setBackground(COLOR_LIGHT_RED); |
| 1650 | } |
| 1651 | if (line != null) { |
| 1652 | line.cardinalityMaxText.setBackground(COLOR_LIGHT_RED); |
| 1653 | } |
| 1654 | } else { |
| 1655 | if (line != null) { |
| 1656 | line.cardinalityMaxText.setBackground(COLOR_TEXT_BACKGROUND); |
| 1657 | } |
| 1658 | } |
| 1659 | if (inputLine.eventType != null && inputLine.eventType.trim().isEmpty()) { |
| 1660 | errors.append("Enter the event type (Line " + name + "). "); //$NON-NLS-1$ //$NON-NLS-2$ |
| 1661 | if (line != null) { |
| 1662 | line.eventTypeText.setBackground(COLOR_LIGHT_RED); |
| 1663 | } |
| 1664 | } else { |
| 1665 | if (line != null) { |
| 1666 | line.eventTypeText.setBackground(COLOR_TEXT_BACKGROUND); |
| 1667 | } |
| 1668 | } |
| 1669 | for (int i = 0; inputLine.columns != null && i < inputLine.columns.size(); i++) { |
| 1670 | InputData inputData = inputLine.columns.get(i); |
| 1671 | InputGroup group = null; |
| 1672 | if (line != null) { |
| 1673 | group = line.inputs.get(i); |
| 1674 | } |
| 1675 | if (inputData.name.equals(CustomTraceDefinition.TAG_TIMESTAMP)) { |
| 1676 | timestampFound = true; |
| 1677 | if (inputData.format.length() == 0) { |
| 1678 | errors.append("Enter the input format for the Time Stamp (Line " + name + " Group " + (i + 1) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 1679 | if (group != null) { |
| 1680 | group.tagText.setBackground(COLOR_LIGHT_RED); |
| 1681 | } |
| 1682 | } else { |
| 1683 | try { |
| 1684 | new TmfTimestampFormat(inputData.format); |
| 1685 | if (group != null) { |
| 1686 | group.tagText.setBackground(COLOR_TEXT_BACKGROUND); |
| 1687 | } |
| 1688 | } catch (IllegalArgumentException e) { |
| 1689 | errors.append("Enter a valid input format for the Time Stamp (Line " + name + " Group " + (i + 1) + ") [" + e.getMessage() + "]. "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
| 1690 | if (group != null) { |
| 1691 | group.tagText.setBackground(COLOR_LIGHT_RED); |
| 1692 | } |
| 1693 | } |
| 1694 | } |
| 1695 | } else if (inputData.name.length() == 0) { |
| 1696 | errors.append("Enter a name for the data group (Line " + name + " Group " + (i + 1) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 1697 | if (group != null) { |
| 1698 | group.tagText.setBackground(COLOR_LIGHT_RED); |
| 1699 | } |
| 1700 | } else { |
| 1701 | if (group != null) { |
| 1702 | group.tagText.setBackground(COLOR_TEXT_BACKGROUND); |
| 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | for (int i = 0; inputLine.childrenInputs != null && i < inputLine.childrenInputs.size(); i++) { |
| 1707 | errors.append(validateLine(inputLine.childrenInputs.get(i), name + "." + (i + 1))); //$NON-NLS-1$ |
| 1708 | } |
| 1709 | return errors; |
| 1710 | } |
| 1711 | |
| 1712 | /** |
| 1713 | * Get the trace definition. |
| 1714 | * |
| 1715 | * @return The trace definition |
| 1716 | */ |
| 1717 | public CustomTxtTraceDefinition getDefinition() { |
| 1718 | return definition; |
| 1719 | } |
| 1720 | |
| 1721 | /** |
| 1722 | * Get the raw text of the input. |
| 1723 | * |
| 1724 | * @return The raw input text |
| 1725 | */ |
| 1726 | public char[] getInputText() { |
| 1727 | return inputText.getText().toCharArray(); |
| 1728 | } |
| 1729 | } |