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