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