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