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