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