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