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