Internalize some classes and fix a pile of warnings
[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.Messages;
33 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
34 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
35 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition.Cardinality;
36 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition.InputData;
37 import org.eclipse.linuxtools.internal.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 = Activator.getDefault().getImageFromPath("/icons/elcl16/line_icon.gif"); //$NON-NLS-1$
76 private static final Image addImage = Activator.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$
77 private static final Image addNextImage = Activator.getDefault().getImageFromPath("/icons/elcl16/addnext_button.gif"); //$NON-NLS-1$
78 private static final Image addChildImage = Activator.getDefault().getImageFromPath("/icons/elcl16/addchild_button.gif"); //$NON-NLS-1$
79 private static final Image deleteImage = Activator.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$
80 private static final Image moveUpImage = Activator.getDefault().getImageFromPath("/icons/elcl16/moveup_button.gif"); //$NON-NLS-1$
81 private static final Image moveDownImage = Activator.getDefault().getImageFromPath("/icons/elcl16/movedown_button.gif"); //$NON-NLS-1$
82 private static final Image helpImage = Activator.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 static 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 BufferedReader reader = null;
554 try {
555 reader = new BufferedReader(new InputStreamReader(file.getContents()));
556 StringBuilder sb = new StringBuilder();
557 String line = null;
558 while ((line = reader.readLine()) != null) {
559 sb.append(line + "\n"); //$NON-NLS-1$
560 }
561 return sb.toString();
562 } catch (CoreException e) {
563 return ""; //$NON-NLS-1$
564 } catch (IOException e) {
565 return ""; //$NON-NLS-1$
566 } finally {
567 if (reader != null) {
568 try {
569 reader.close();
570 } catch (IOException e) {
571 }
572 }
573 }
574 }
575 }
576 return ""; //$NON-NLS-1$
577 }
578
579 private void updatePreviews() {
580 updatePreviews(false);
581 }
582
583 private void updatePreviews(boolean updateAll) {
584 if (inputText == null) {
585 // early update during construction
586 return;
587 }
588 inputText.setStyleRanges(new StyleRange[] {});
589
590 Scanner scanner = new Scanner(inputText.getText());
591 scanner.useDelimiter("\n"); //$NON-NLS-1$
592 int rawPos = 0;
593 String skip; // skip starting delimiters
594 if ((skip = scanner.findWithinHorizon("\\A\n+", 0)) != null) { //$NON-NLS-1$
595 rawPos += skip.length();
596 }
597
598 timeStampFormat = null;
599 if (selectedLine != null) {
600 for (InputGroup input : selectedLine.inputs) {
601 input.previewText.setText(Messages.CustomTxtParserInputWizardPage_noMathcingLine);
602 }
603 }
604
605 Map<String, String> data = new HashMap<String, String>();
606 int rootLineMatches = 0;
607 String firstEntryTimeStamp = null;
608 String firstEntryTimeStampInputFormat = null;
609 String log = null;
610 event:
611 while (log != null || scanner.hasNext()) {
612 if (rootLineMatches > 0 && !updateAll) {
613 break;
614 }
615 if (log == null) {
616 log = scanner.next();
617 }
618 int length = log.length();
619 for (InputLine rootInputLine : definition.inputs) {
620 Pattern pattern;
621 try {
622 pattern = rootInputLine.getPattern();
623 } catch (PatternSyntaxException e) {
624 continue;
625 }
626 Matcher matcher = pattern.matcher(log);
627 if (matcher.find()) {
628 rootLineMatches++;
629 inputText.setStyleRange(new StyleRange(rawPos, length,
630 COLOR_BLACK, COLOR_YELLOW, SWT.ITALIC));
631 data = new HashMap<String, String>();
632 timeStampFormat = null;
633 updatePreviewLine(rootInputLine, matcher, data, rawPos, rootLineMatches);
634 if (rootLineMatches == 1) {
635 firstEntryTimeStamp = data.get(CustomTxtTraceDefinition.TAG_TIMESTAMP);
636 firstEntryTimeStampInputFormat = timeStampFormat;
637 }
638 HashMap<InputLine, Integer> countMap = new HashMap<InputLine, Integer>();
639 InputLine currentInput = null;
640 if (rootInputLine.childrenInputs != null && rootInputLine.childrenInputs.size() > 0) {
641 currentInput = rootInputLine.childrenInputs.get(0);
642 countMap.put(currentInput, 0);
643 }
644 rawPos += length + 1; // +1 for \n
645 while (scanner.hasNext()) {
646 log = scanner.next();
647 length = log.length();
648 boolean processed = false;
649 if (currentInput == null) {
650 for (InputLine input : definition.inputs) {
651 matcher = input.getPattern().matcher(log);
652 if (matcher.find()) {
653 continue event;
654 }
655 }
656 } else {
657 if (countMap.get(currentInput) >= currentInput.getMinCount()) {
658 List<InputLine> nextInputs = currentInput.getNextInputs(countMap);
659 if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {
660 for (InputLine input : definition.inputs) {
661 matcher = input.getPattern().matcher(log);
662 if (matcher.find()) {
663 continue event;
664 }
665 }
666 }
667 for (InputLine input : nextInputs) {
668 matcher = input.getPattern().matcher(log);
669 if (matcher.find()) {
670 inputText.setStyleRange(new StyleRange(rawPos, length,
671 COLOR_BLACK, COLOR_LIGHT_YELLOW, SWT.ITALIC));
672 currentInput = input;
673 updatePreviewLine(currentInput, matcher, data, rawPos, rootLineMatches);
674 if (countMap.get(currentInput) == null) {
675 countMap.put(currentInput, 1);
676 } else {
677 countMap.put(currentInput, countMap.get(currentInput) + 1);
678 }
679 Iterator<InputLine> iter = countMap.keySet().iterator();
680 while (iter.hasNext()) {
681 InputLine inputLine = iter.next();
682 if (inputLine.level > currentInput.level) {
683 iter.remove();
684 }
685 }
686 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
687 currentInput = currentInput.childrenInputs.get(0);
688 countMap.put(currentInput, 0);
689 } else {
690 if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
691 if (currentInput.getNextInputs(countMap).size() > 0) {
692 currentInput = currentInput.getNextInputs(countMap).get(0);
693 if (countMap.get(currentInput) == null) {
694 countMap.put(currentInput, 0);
695 }
696 iter = countMap.keySet().iterator();
697 while (iter.hasNext()) {
698 InputLine inputLine = iter.next();
699 if (inputLine.level > currentInput.level) {
700 iter.remove();
701 }
702 }
703 } else {
704 currentInput = null;
705 }
706 }
707 }
708 processed = true;
709 break;
710 }
711 }
712 }
713 if (! processed) {
714 matcher = currentInput.getPattern().matcher(log);
715 if (matcher.find()) {
716 inputText.setStyleRange(new StyleRange(rawPos, length,
717 COLOR_BLACK, COLOR_LIGHT_YELLOW, SWT.ITALIC));
718 updatePreviewLine(currentInput, matcher, data, rawPos, rootLineMatches);
719 countMap.put(currentInput, countMap.get(currentInput) + 1);
720 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
721 currentInput = currentInput.childrenInputs.get(0);
722 countMap.put(currentInput, 0);
723 } else {
724 if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
725 if (currentInput.getNextInputs(countMap).size() > 0) {
726 currentInput = currentInput.getNextInputs(countMap).get(0);
727 if (countMap.get(currentInput) == null) {
728 countMap.put(currentInput, 0);
729 }
730 Iterator<InputLine> iter = countMap.keySet().iterator();
731 while (iter.hasNext()) {
732 InputLine inputLine = iter.next();
733 if (inputLine.level > currentInput.level) {
734 iter.remove();
735 }
736 }
737 } else {
738 currentInput = null;
739 }
740 }
741 }
742 }
743 }
744 }
745 rawPos += length + 1; // +1 for \n
746 }
747
748 break;
749 }
750 }
751 rawPos += length + 1; // +1 for \n
752 log = null;
753 }
754 scanner.close();
755 if (rootLineMatches == 1) {
756 firstEntryTimeStamp = data.get(CustomTxtTraceDefinition.TAG_TIMESTAMP);
757 firstEntryTimeStampInputFormat = timeStampFormat;
758 }
759 if (firstEntryTimeStamp == null) {
760 timestampPreviewText.setText(Messages.CustomTxtParserInputWizardPage_noTimestampGroup);
761 if (selectedLine != null) {
762 for (InputGroup group : selectedLine.inputs) {
763 if (group.tagCombo.getText().equals(CustomTxtTraceDefinition.TAG_TIMESTAMP)) {
764 timestampPreviewText.setText(Messages.CustomTxtParserInputWizardPage_noMatchingTimestamp);
765 break;
766 }
767 }
768 }
769 } else {
770 try {
771 SimpleDateFormat dateFormat = new SimpleDateFormat(firstEntryTimeStampInputFormat);
772 Date date = dateFormat.parse(firstEntryTimeStamp);
773 dateFormat = new SimpleDateFormat(timestampOutputFormatText.getText().trim());
774 timestampPreviewText.setText(dateFormat.format(date));
775 } catch (ParseException e) {
776 timestampPreviewText.setText("*parse exception* [" + firstEntryTimeStamp + "] <> [" + firstEntryTimeStampInputFormat + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
777 } catch (IllegalArgumentException e) {
778 timestampPreviewText.setText("*parse exception* [Illegal Argument]"); //$NON-NLS-1$
779 }
780
781 }
782 }
783
784 private void updatePreviewLine(InputLine line, Matcher matcher, Map<String, String> data, int rawPos, int rootLineMatches) {
785 for (int i = 0; i < line.columns.size(); i++) {
786 InputData input = line.columns.get(i);
787 if (i < matcher.groupCount() && matcher.group(i+1) != null) {
788 if (line.parentInput == null) {
789 inputText.setStyleRange(new StyleRange(rawPos + matcher.start(i+1), matcher.end(i+1) - matcher.start(i+1),
790 COLOR_BLACK, COLOR_GREEN, SWT.BOLD));
791 } else {
792 inputText.setStyleRange(new StyleRange(rawPos + matcher.start(i+1), matcher.end(i+1) - matcher.start(i+1),
793 COLOR_BLACK, COLOR_LIGHT_GREEN, SWT.BOLD));
794 }
795 String value = matcher.group(i+1).trim();
796 if (selectedLine != null && selectedLine.inputLine.equals(line) && rootLineMatches == 1) {
797 if (selectedLine.inputs.get(i).previewText.getText().equals(Messages.CustomTxtParserInputWizardPage_noMatchingLine)) {
798 selectedLine.inputs.get(i).previewText.setText(value);
799 }
800 }
801 if (value.length() == 0) {
802 continue;
803 }
804 if (input.action == CustomTxtTraceDefinition.ACTION_SET) {
805 data.put(input.name, value);
806 if (input.name.equals(CustomTxtTraceDefinition.TAG_TIMESTAMP)) {
807 timeStampFormat = input.format;
808 }
809 } else if (input.action == CustomTxtTraceDefinition.ACTION_APPEND) {
810 String s = data.get(input.name);
811 if (s != null) {
812 data.put(input.name, s + value);
813 } else {
814 data.put(input.name, value);
815 }
816 if (input.name.equals(CustomTxtTraceDefinition.TAG_TIMESTAMP)) {
817 if (timeStampFormat != null) {
818 timeStampFormat += input.format;
819 } else {
820 timeStampFormat = input.format;
821 }
822 }
823 } else if (input.action == CustomTxtTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) {
824 String s = data.get(input.name);
825 if (s != null) {
826 data.put(input.name, s + " | " + value); //$NON-NLS-1$
827 } else {
828 data.put(input.name, value);
829 }
830 if (input.name.equals(CustomTxtTraceDefinition.TAG_TIMESTAMP)) {
831 if (timeStampFormat != null) {
832 timeStampFormat += " | " + input.format; //$NON-NLS-1$
833 } else {
834 timeStampFormat = input.format;
835 }
836 }
837 }
838 } else {
839 if (selectedLine != null && selectedLine.inputLine.equals(line) && rootLineMatches == 1) {
840 if (selectedLine.inputs.get(i).previewText.getText().equals(Messages.CustomTxtParserInputWizardPage_noMatchingLine)) {
841 selectedLine.inputs.get(i).previewText.setText(Messages.CustomTxtParserInputWizardPage_noMatchingGroup);
842 }
843 }
844 }
845 }
846 // highlight the matching groups that have no corresponponding input
847 for (int i = line.columns.size(); i < matcher.groupCount(); i++) {
848 if (matcher.group(i+1) != null) {
849 if (line.parentInput == null) {
850 inputText.setStyleRange(new StyleRange(rawPos + matcher.start(i+1), matcher.end(i+1) - matcher.start(i+1),
851 COLOR_BLACK, COLOR_MAGENTA));
852 } else {
853 inputText.setStyleRange(new StyleRange(rawPos + matcher.start(i+1), matcher.end(i+1) - matcher.start(i+1),
854 COLOR_BLACK, COLOR_LIGHT_MAGENTA));
855 }
856 }
857 }
858 }
859
860 private void openHelpShell(String url) {
861 if (helpBrowser != null && !helpBrowser.isDisposed()) {
862 helpBrowser.getShell().setActive();
863 if (!helpBrowser.getUrl().equals(url)) {
864 helpBrowser.setUrl(url);
865 }
866 return;
867 }
868 final Shell helpShell = new Shell(getShell(), SWT.SHELL_TRIM);
869 helpShell.setLayout(new FillLayout());
870 helpBrowser = new Browser(helpShell, SWT.NONE);
871 helpBrowser.addTitleListener(new TitleListener() {
872 @Override
873 public void changed(TitleEvent event) {
874 helpShell.setText(event.title);
875 }
876 });
877 helpBrowser.setBounds(0,0,600,400);
878 helpShell.pack();
879 helpShell.open();
880 helpBrowser.setUrl(url);
881 }
882
883 private void openLegend() {
884 final String CG = Messages.CustomTxtParserInputWizardPage_capturedGroup;
885 final String UCG = Messages.CustomTxtParserInputWizardPage_unidentifiedCaptureGroup;
886 final String UT = Messages.CustomTxtParserInputWizardPage_uncapturedText;
887 int line1start = 0;
888 String line1 = Messages.CustomTxtParserInputWizardPage_nonMatchingLine;
889 int line2start = line1start + line1.length();
890 String line2 = Messages.CustomTxtParserInputWizardPage_matchingLineRoot + CG + " " + UCG + " " + UT + " \n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
891 int line3start = line2start + line2.length();
892 String line3 = Messages.CustomTxtParserInputWizardPage_matchingOtherLine + CG + " " + UCG + " " + UT + " \n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
893 int line4start = line3start + line3.length();
894 String line4 = Messages.CustomTxtParserInputWizardPage_matchingOtherLine + CG + " " + UCG + " " + UT + " \n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
895 int line5start = line4start + line4.length();
896 String line5 = Messages.CustomTxtParserInputWizardPage_nonMatchingLine;
897 int line6start = line5start + line5.length();
898 String line6 = Messages.CustomTxtParserInputWizardPage_matchingRootLine + CG + " " + UCG + " " + UT + " \n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
899
900 final Shell legendShell = new Shell(getShell(), SWT.DIALOG_TRIM);
901 legendShell.setLayout(new FillLayout());
902 StyledText legendText = new StyledText(legendShell, SWT.MULTI);
903 legendText.setFont(fixedFont);
904 legendText.setText(line1 + line2 + line3 + line4 + line5 + line6);
905 legendText.setStyleRange(new StyleRange(line2start, line2.length(), COLOR_BLACK, COLOR_YELLOW, SWT.ITALIC));
906 legendText.setStyleRange(new StyleRange(line3start, line3.length(), COLOR_BLACK, COLOR_LIGHT_YELLOW, SWT.ITALIC));
907 legendText.setStyleRange(new StyleRange(line4start, line4.length(), COLOR_BLACK, COLOR_LIGHT_YELLOW, SWT.ITALIC));
908 legendText.setStyleRange(new StyleRange(line6start, line6.length(), COLOR_BLACK, COLOR_YELLOW, SWT.ITALIC));
909 legendText.setStyleRange(new StyleRange(line2start + line2.indexOf(CG), CG.length(), COLOR_BLACK, COLOR_GREEN, SWT.BOLD));
910 legendText.setStyleRange(new StyleRange(line2start + line2.indexOf(UCG), UCG.length(), COLOR_BLACK, COLOR_MAGENTA));
911 legendText.setStyleRange(new StyleRange(line3start + line3.indexOf(CG), CG.length(), COLOR_BLACK, COLOR_LIGHT_GREEN, SWT.BOLD));
912 legendText.setStyleRange(new StyleRange(line3start + line3.indexOf(UCG), UCG.length(), COLOR_BLACK, COLOR_LIGHT_MAGENTA));
913 legendText.setStyleRange(new StyleRange(line4start + line4.indexOf(CG), CG.length(), COLOR_BLACK, COLOR_LIGHT_GREEN, SWT.BOLD));
914 legendText.setStyleRange(new StyleRange(line4start + line4.indexOf(UCG), UCG.length(), COLOR_BLACK, COLOR_LIGHT_MAGENTA));
915 legendText.setStyleRange(new StyleRange(line6start + line6.indexOf(CG), CG.length(), COLOR_BLACK, COLOR_GREEN, SWT.BOLD));
916 legendText.setStyleRange(new StyleRange(line6start + line6.indexOf(UCG), UCG.length(), COLOR_BLACK, COLOR_MAGENTA));
917 legendShell.setText(Messages.CustomTxtParserInputWizardPage_previewLegend);
918 legendShell.pack();
919 legendShell.open();
920 }
921
922 private class UpdateListener implements ModifyListener, SelectionListener {
923
924 @Override
925 public void modifyText(ModifyEvent e) {
926 validate();
927 updatePreviews();
928 }
929
930 @Override
931 public void widgetDefaultSelected(SelectionEvent e) {
932 validate();
933 updatePreviews();
934 }
935
936 @Override
937 public void widgetSelected(SelectionEvent e) {
938 validate();
939 updatePreviews();
940 }
941
942 }
943
944 private class Line {
945 private static final String INFINITY_STRING = "\u221E"; //$NON-NLS-1$
946 InputLine inputLine;
947 Group group;
948 Composite labelComposite;
949 Text regexText;
950 Composite cardinalityContainer;
951 Combo cardinalityCombo;
952 Label cardinalityMinLabel;
953 Text cardinalityMinText;
954 Label cardinalityMaxLabel;
955 Text cardinalityMaxText;
956 Button infiniteButton;
957 List<InputGroup> inputs = new ArrayList<InputGroup>();
958 Button addGroupButton;
959 Label addGroupLabel;
960
961 public Line(Composite parent, String name, InputLine inputLine) {
962 this.inputLine = inputLine;
963
964 group = new Group(parent, SWT.NONE);
965 group.setText(name);
966 group.setLayout(new GridLayout(2, false));
967 group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
968
969 labelComposite = new Composite(group, SWT.FILL);
970 GridLayout labelLayout = new GridLayout(1, false);
971 labelLayout.marginWidth = 0;
972 labelLayout.marginHeight = 0;
973 labelComposite.setLayout(labelLayout);
974 labelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
975
976 Label label = new Label(labelComposite, SWT.NULL);
977 label.setText(Messages.CustomTxtParserInputWizardPage_regularExpression);
978
979 Composite regexContainer = new Composite(group, SWT.NONE);
980 GridLayout regexLayout = new GridLayout(2, false);
981 regexLayout.marginHeight = 0;
982 regexLayout.marginWidth = 0;
983 regexContainer.setLayout(regexLayout);
984 regexContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
985
986 regexText = new Text(regexContainer, SWT.BORDER | SWT.SINGLE);
987 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
988 gd.widthHint = 0;
989 regexText.setLayoutData(gd);
990 regexText.setText(inputLine.getRegex());
991 regexText.addModifyListener(updateListener);
992
993 Button regexHelpButton = new Button(regexContainer, SWT.PUSH);
994 regexHelpButton.setImage(helpImage);
995 regexHelpButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_regularExpressionHelp);
996 regexHelpButton.addSelectionListener(new SelectionAdapter() {
997 @Override
998 public void widgetSelected(SelectionEvent e) {
999 openHelpShell(PATTERN_URL);
1000 }
1001 });
1002
1003 label = new Label(group, SWT.NONE);
1004 label.setText(Messages.CustomTxtParserInputWizardPage_cardinality);
1005 label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1006
1007 cardinalityContainer = new Composite(group, SWT.NONE);
1008 GridLayout cardinalityLayout = new GridLayout(6, false);
1009 cardinalityLayout.marginHeight = 0;
1010 cardinalityLayout.marginWidth = 0;
1011 cardinalityContainer.setLayout(cardinalityLayout);
1012 cardinalityContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1013
1014 cardinalityCombo = new Combo(cardinalityContainer, SWT.DROP_DOWN | SWT.READ_ONLY);
1015 cardinalityCombo.setItems(new String[] {
1016 Cardinality.ZERO_OR_MORE.toString(),
1017 Cardinality.ONE_OR_MORE.toString(),
1018 Cardinality.ZERO_OR_ONE.toString(),
1019 Cardinality.ONE.toString(),
1020 "(?,?)"}); //$NON-NLS-1$
1021 cardinalityCombo.addSelectionListener(new SelectionListener(){
1022 @Override
1023 public void widgetDefaultSelected(SelectionEvent e) {}
1024 @Override
1025 public void widgetSelected(SelectionEvent e) {
1026 switch (cardinalityCombo.getSelectionIndex()) {
1027 case 4: //(?,?)
1028 cardinalityMinLabel.setVisible(true);
1029 cardinalityMinText.setVisible(true);
1030 cardinalityMaxLabel.setVisible(true);
1031 cardinalityMaxText.setVisible(true);
1032 infiniteButton.setVisible(true);
1033 break;
1034 default:
1035 cardinalityMinLabel.setVisible(false);
1036 cardinalityMinText.setVisible(false);
1037 cardinalityMaxLabel.setVisible(false);
1038 cardinalityMaxText.setVisible(false);
1039 infiniteButton.setVisible(false);
1040 break;
1041 }
1042 cardinalityContainer.layout();
1043 validate();
1044 updatePreviews();
1045 }});
1046
1047 cardinalityMinLabel = new Label(cardinalityContainer, SWT.NONE);
1048 cardinalityMinLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1049 cardinalityMinLabel.setText(Messages.CustomTxtParserInputWizardPage_min);
1050 cardinalityMinLabel.setVisible(false);
1051
1052 cardinalityMinText = new Text(cardinalityContainer, SWT.BORDER | SWT.SINGLE);
1053 gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
1054 gd.widthHint = 20;
1055 cardinalityMinText.setLayoutData(gd);
1056 cardinalityMinText.setVisible(false);
1057
1058 cardinalityMaxLabel = new Label(cardinalityContainer, SWT.NONE);
1059 cardinalityMaxLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1060 cardinalityMaxLabel.setText(Messages.CustomTxtParserInputWizardPage_max);
1061 cardinalityMaxLabel.setVisible(false);
1062
1063 cardinalityMaxText = new Text(cardinalityContainer, SWT.BORDER | SWT.SINGLE);
1064 gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
1065 gd.widthHint = 20;
1066 cardinalityMaxText.setLayoutData(gd);
1067 cardinalityMaxText.setVisible(false);
1068
1069 infiniteButton = new Button(cardinalityContainer, SWT.PUSH);
1070 infiniteButton.setText(INFINITY_STRING);
1071 infiniteButton.setVisible(false);
1072 infiniteButton.addSelectionListener(new SelectionAdapter(){
1073 @Override
1074 public void widgetSelected(SelectionEvent e) {
1075 cardinalityMaxText.setText(INFINITY_STRING);
1076 }});
1077
1078 if (inputLine.cardinality.equals(Cardinality.ZERO_OR_MORE)) {
1079 cardinalityCombo.select(0);
1080 } else if (inputLine.cardinality.equals(Cardinality.ONE_OR_MORE)) {
1081 cardinalityCombo.select(1);
1082 } else if (inputLine.cardinality.equals(Cardinality.ZERO_OR_ONE)) {
1083 cardinalityCombo.select(2);
1084 } else if (inputLine.cardinality.equals(Cardinality.ONE)) {
1085 cardinalityCombo.select(3);
1086 } else {
1087 cardinalityCombo.select(4);
1088 cardinalityMinLabel.setVisible(true);
1089 cardinalityMinText.setVisible(true);
1090 if (inputLine.getMinCount() >= 0) {
1091 cardinalityMinText.setText(Integer.toString(inputLine.getMinCount()));
1092 }
1093 cardinalityMaxLabel.setVisible(true);
1094 cardinalityMaxText.setVisible(true);
1095 if (inputLine.getMaxCount() == Cardinality.INF) {
1096 cardinalityMaxText.setText(INFINITY_STRING);
1097 } else if (inputLine.getMaxCount() >= 0) {
1098 cardinalityMaxText.setText(Integer.toString(inputLine.getMaxCount()));
1099 }
1100 infiniteButton.setVisible(true);
1101 }
1102
1103 VerifyListener digitsListener = new VerifyListener() {
1104 @Override
1105 public void verifyText(VerifyEvent e) {
1106 if (e.text.equals(INFINITY_STRING)) {
1107 e.doit = e.widget == cardinalityMaxText && e.start == 0 && e.end == ((Text) e.widget).getText().length();
1108 } else {
1109 if (((Text) e.widget).getText().equals(INFINITY_STRING)) {
1110 e.doit = e.start == 0 && e.end == ((Text) e.widget).getText().length();
1111 }
1112 for (int i = 0; i < e.text.length(); i++) {
1113 if (!Character.isDigit(e.text.charAt(i))) {
1114 e.doit = false;
1115 break;
1116 }
1117 }
1118 }
1119 }};
1120
1121 cardinalityMinText.addModifyListener(updateListener);
1122 cardinalityMaxText.addModifyListener(updateListener);
1123 cardinalityMinText.addVerifyListener(digitsListener);
1124 cardinalityMaxText.addVerifyListener(digitsListener);
1125
1126 if (inputLine.columns != null) {
1127 for (InputData inputData : inputLine.columns) {
1128 InputGroup inputGroup = new InputGroup(group, this, inputs.size()+1);
1129 if (inputData.name.equals(CustomTxtTraceDefinition.TAG_TIMESTAMP)) {
1130 inputGroup.tagCombo.select(0);
1131 inputGroup.tagText.setText(inputData.format);
1132 inputGroup.tagLabel.setText(Messages.CustomTxtParserInputWizardPage_format);
1133 inputGroup.tagLabel.setVisible(true);
1134 inputGroup.tagText.setVisible(true);
1135 inputGroup.tagText.addModifyListener(updateListener);
1136 } else if (inputData.name.equals(CustomTxtTraceDefinition.TAG_MESSAGE)) {
1137 inputGroup.tagCombo.select(1);
1138 } else {
1139 inputGroup.tagCombo.select(2);
1140 inputGroup.tagText.setText(inputData.name);
1141 inputGroup.tagLabel.setText(Messages.CustomTxtParserInputWizardPage_name);
1142 inputGroup.tagLabel.setVisible(true);
1143 inputGroup.tagText.setVisible(true);
1144 inputGroup.tagText.addModifyListener(updateListener);
1145 }
1146 inputGroup.actionCombo.select(inputData.action);
1147 inputs.add(inputGroup);
1148 }
1149 }
1150
1151 createAddGroupButton();
1152 }
1153
1154 private void createAddGroupButton() {
1155 addGroupButton = new Button(group, SWT.PUSH);
1156 addGroupButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1157 addGroupButton.setImage(addImage);
1158 addGroupButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_addGroup);
1159 addGroupButton.addSelectionListener(new SelectionAdapter() {
1160 @Override
1161 public void widgetSelected(SelectionEvent e) {
1162 removeAddGroupButton();
1163 inputs.add(new InputGroup(group, Line.this, inputs.size()+1));
1164 createAddGroupButton();
1165 lineContainer.layout();
1166 lineScrolledComposite.setMinSize(lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);
1167 group.getParent().layout();
1168 validate();
1169 updatePreviews();
1170 }
1171 });
1172
1173 addGroupLabel = new Label(group, SWT.NULL);
1174 addGroupLabel.setText(Messages.CustomTxtParserInputWizardPage_newGroup);
1175 }
1176
1177 private void removeAddGroupButton() {
1178 addGroupButton.dispose();
1179 addGroupLabel.dispose();
1180 }
1181
1182 private void removeInput(int inputNumber) {
1183 if (--inputNumber < inputs.size()) {
1184 inputs.remove(inputNumber).dispose();
1185 for (int i = inputNumber; i < inputs.size(); i++) {
1186 inputs.get(i).setInputNumber(i+1);
1187 }
1188 lineContainer.layout();
1189 lineScrolledComposite.setMinSize(lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, lineContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y-1);
1190 group.getParent().layout();
1191 }
1192 }
1193
1194 // private void setName(String name) {
1195 // this.name = name;
1196 // group.setText("Line " + name);
1197 // }
1198
1199 private void dispose() {
1200 group.dispose();
1201 }
1202
1203 private void extractInputs() {
1204 inputLine.setRegex(selectedLine.regexText.getText());
1205 switch (cardinalityCombo.getSelectionIndex()) {
1206 case 0:
1207 inputLine.cardinality = Cardinality.ZERO_OR_MORE;
1208 break;
1209 case 1:
1210 inputLine.cardinality = Cardinality.ONE_OR_MORE;
1211 break;
1212 case 2:
1213 inputLine.cardinality = Cardinality.ZERO_OR_ONE;
1214 break;
1215 case 3:
1216 inputLine.cardinality = Cardinality.ONE;
1217 break;
1218 case 4: //(?,?)
1219 int min, max;
1220 try {
1221 min = Integer.parseInt(cardinalityMinText.getText());
1222 } catch (NumberFormatException e) {
1223 min = -1;
1224 }
1225 try {
1226 if (cardinalityMaxText.getText().equals(INFINITY_STRING)) {
1227 max = Cardinality.INF;
1228 } else {
1229 max = Integer.parseInt(cardinalityMaxText.getText());
1230 }
1231 } catch (NumberFormatException e) {
1232 max = -1;
1233 }
1234 inputLine.cardinality = new Cardinality(min, max);
1235 break;
1236 default:
1237 inputLine.cardinality = Cardinality.ZERO_OR_MORE;
1238 break;
1239 }
1240 inputLine.columns = new ArrayList<InputData>(inputs.size());
1241 for (int i = 0; i < inputs.size(); i++) {
1242 InputGroup group = inputs.get(i);
1243 InputData inputData = new InputData();
1244 if (group.tagCombo.getText().equals(CustomTxtTraceDefinition.TAG_OTHER)) {
1245 inputData.name = group.tagText.getText().trim();
1246 } else {
1247 inputData.name = group.tagCombo.getText();
1248 if (group.tagCombo.getText().equals(CustomTxtTraceDefinition.TAG_TIMESTAMP)) {
1249 inputData.format = group.tagText.getText().trim();
1250 }
1251 }
1252 inputData.action = group.actionCombo.getSelectionIndex();
1253 inputLine.columns.add(inputData);
1254 }
1255 }
1256 }
1257
1258 private class InputGroup {
1259 Line line;
1260 int inputNumber;
1261
1262 // children of parent (must be disposed)
1263 Composite labelComposite;
1264 Composite tagComposite;
1265 Label previewLabel;
1266 Text previewText;
1267
1268 // children of labelComposite
1269 Label inputLabel;
1270
1271 // children of tagComposite
1272 Combo tagCombo;
1273 Label tagLabel;
1274 Text tagText;
1275 Combo actionCombo;
1276
1277 public InputGroup(Composite parent, Line line, int inputNumber) {
1278 this.line = line;
1279 this.inputNumber = inputNumber;
1280
1281 labelComposite = new Composite(parent, SWT.FILL);
1282 GridLayout labelLayout = new GridLayout(2, false);
1283 labelLayout.marginWidth = 0;
1284 labelLayout.marginHeight = 0;
1285 labelComposite.setLayout(labelLayout);
1286 labelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1287
1288 Button deleteButton = new Button(labelComposite, SWT.PUSH);
1289 deleteButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1290 deleteButton.setImage(deleteImage);
1291 deleteButton.setToolTipText(Messages.CustomTxtParserInputWizardPage_removeGroup);
1292 deleteButton.addSelectionListener(new SelectionAdapter() {
1293 @Override
1294 public void widgetSelected(SelectionEvent e) {
1295 InputGroup.this.line.removeInput(InputGroup.this.inputNumber);
1296 validate();
1297 updatePreviews();
1298 }
1299 });
1300
1301 inputLabel = new Label(labelComposite, SWT.NULL);
1302 inputLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1303 inputLabel.setText(Messages.CustomTxtParserInputWizardPage_group + inputNumber + ":"); //$NON-NLS-1$
1304
1305 tagComposite = new Composite(parent, SWT.FILL);
1306 GridLayout tagLayout = new GridLayout(4, false);
1307 tagLayout.marginWidth = 0;
1308 tagLayout.marginHeight = 0;
1309 tagComposite.setLayout(tagLayout);
1310 tagComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
1311
1312 tagCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
1313 tagCombo.setItems(new String[] {CustomTxtTraceDefinition.TAG_TIMESTAMP,
1314 CustomTxtTraceDefinition.TAG_MESSAGE,
1315 CustomTxtTraceDefinition.TAG_OTHER});
1316 tagCombo.select(1);
1317 tagCombo.addSelectionListener(new SelectionListener(){
1318 @Override
1319 public void widgetDefaultSelected(SelectionEvent e) {}
1320 @Override
1321 public void widgetSelected(SelectionEvent e) {
1322 tagText.removeModifyListener(updateListener);
1323 switch (tagCombo.getSelectionIndex()) {
1324 case 0: //Time Stamp
1325 tagLabel.setText(Messages.CustomTxtParserInputWizardPage_format);
1326 tagLabel.setVisible(true);
1327 tagText.setVisible(true);
1328 tagText.addModifyListener(updateListener);
1329 break;
1330 case 1: //Message
1331 tagLabel.setVisible(false);
1332 tagText.setVisible(false);
1333 break;
1334 case 2: //Other
1335 tagLabel.setText(Messages.CustomTxtParserInputWizardPage_name);
1336 tagLabel.setVisible(true);
1337 tagText.setVisible(true);
1338 tagText.addModifyListener(updateListener);
1339 break;
1340 case 3: //Continue
1341 tagLabel.setVisible(false);
1342 tagText.setVisible(false);
1343 break;
1344 }
1345 tagComposite.layout();
1346 validate();
1347 updatePreviews();
1348 }});
1349
1350 tagLabel = new Label(tagComposite, SWT.NULL);
1351 tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1352 tagLabel.setVisible(false);
1353
1354 tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE);
1355 GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1356 gd.widthHint = 0;
1357 tagText.setLayoutData(gd);
1358 tagText.setVisible(false);
1359
1360 actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
1361 actionCombo.setItems(new String[] {Messages.CustomTxtParserInputWizardPage_set, Messages.CustomTxtParserInputWizardPage_append, Messages.CustomTxtParserInputWizardPage_appendWith});
1362 actionCombo.select(0);
1363 actionCombo.addSelectionListener(updateListener);
1364
1365 previewLabel = new Label(parent, SWT.NULL);
1366 previewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
1367 previewLabel.setText(Messages.CustomTxtParserInputWizardPage_preview);
1368
1369 previewText = new Text(parent, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
1370 gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
1371 gd.widthHint = 0;
1372 previewText.setLayoutData(gd);
1373 previewText.setText(Messages.CustomTxtParserInputWizardPage_noMatch);
1374 previewText.setBackground(COLOR_WIDGET_BACKGROUND);
1375 }
1376
1377 private void dispose() {
1378 labelComposite.dispose();
1379 tagComposite.dispose();
1380 previewLabel.dispose();
1381 previewText.dispose();
1382 }
1383
1384 private void setInputNumber(int inputNumber) {
1385 this.inputNumber = inputNumber;
1386 inputLabel.setText(Messages.CustomTxtParserInputWizardPage_group + inputNumber + ":"); //$NON-NLS-1$
1387 labelComposite.layout();
1388 }
1389 }
1390
1391 private void validate() {
1392
1393 definition.definitionName = logtypeText.getText().trim();
1394 definition.timeStampOutputFormat = timestampOutputFormatText.getText().trim();
1395
1396 if (selectedLine != null) {
1397 selectedLine.extractInputs();
1398 treeViewer.refresh();
1399 }
1400
1401 StringBuffer errors = new StringBuffer();
1402
1403 if (definition.definitionName.length() == 0) {
1404 errors.append("Enter a name for the new log type. "); //$NON-NLS-1$
1405 logtypeText.setBackground(COLOR_LIGHT_RED);
1406 } else {
1407 logtypeText.setBackground(COLOR_TEXT_BACKGROUND);
1408 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
1409 if (definition.definitionName.equals(def.definitionName)) {
1410 if (editDefinitionName == null || ! editDefinitionName.equals(definition.definitionName)) {
1411 errors.append("The log type name already exists. "); //$NON-NLS-1$
1412 logtypeText.setBackground(COLOR_LIGHT_RED);
1413 break;
1414 }
1415 }
1416 }
1417 }
1418
1419 timestampFound = false;
1420 for (int i = 0; i < definition.inputs.size(); i++) {
1421
1422 InputLine inputLine = definition.inputs.get(i);
1423 String name = Integer.toString(i+1);
1424 errors.append(validateLine(inputLine, name));
1425 }
1426 if (timestampFound) {
1427 if (definition.timeStampOutputFormat.length() == 0) {
1428 errors.append("Enter the output format for the Time Stamp field. "); //$NON-NLS-1$
1429 timestampOutputFormatText.setBackground(COLOR_LIGHT_RED);
1430 } else {
1431 try {
1432 new SimpleDateFormat(definition.timeStampOutputFormat);
1433 timestampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND);
1434 } catch (IllegalArgumentException e) {
1435 errors.append("Enter a valid output format for the Time Stamp field. "); //$NON-NLS-1$
1436 timestampOutputFormatText.setBackground(COLOR_LIGHT_RED);
1437 }
1438 }
1439
1440 } else {
1441 timestampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND);
1442 // timestampPreviewText.setBackground(COLOR_WIDGET_BACKGROUND);
1443 // errors.append("Identify a Time Stamp group (Line "+name+"). ");
1444 // timestampPreviewText.setText("*no timestamp group*");
1445 // timestampPreviewText.setBackground(COLOR_LIGHT_RED);
1446 }
1447
1448 if (errors.length() == 0) {
1449 setDescription(defaultDescription);
1450 setPageComplete(true);
1451 } else {
1452 setDescription(errors.toString());
1453 setPageComplete(false);
1454 }
1455 }
1456
1457 public StringBuffer validateLine(InputLine inputLine, String name) {
1458 StringBuffer errors = new StringBuffer();
1459 Line line = null;
1460 if (selectedLine != null && selectedLine.inputLine.equals(inputLine)) line = selectedLine;
1461 try {
1462 Pattern.compile(inputLine.getRegex());
1463 if (line != null) line.regexText.setBackground(COLOR_TEXT_BACKGROUND);
1464 } catch (PatternSyntaxException e) {
1465 errors.append("Enter a valid regular expression (Line "+name+"). "); //$NON-NLS-1$ //$NON-NLS-2$
1466 if (line != null) line.regexText.setBackground(COLOR_LIGHT_RED);
1467 }
1468 if (inputLine.getMinCount() == -1) {
1469 errors.append("Enter a minimum value for cardinality (Line "+name+"). "); //$NON-NLS-1$ //$NON-NLS-2$
1470 if (line != null) line.cardinalityMinText.setBackground(COLOR_LIGHT_RED);
1471 } else {
1472 if (line != null) line.cardinalityMinText.setBackground(COLOR_TEXT_BACKGROUND);
1473 }
1474 if (inputLine.getMaxCount() == -1) {
1475 errors.append("Enter a maximum value for cardinality (Line "+name+"). "); //$NON-NLS-1$ //$NON-NLS-2$
1476 if (line != null) line.cardinalityMaxText.setBackground(COLOR_LIGHT_RED);
1477 } else if (inputLine.getMinCount() > inputLine.getMaxCount()) {
1478 errors.append("Enter correct (min <= max) values for cardinality (Line "+name+"). "); //$NON-NLS-1$ //$NON-NLS-2$
1479 if (line != null) line.cardinalityMinText.setBackground(COLOR_LIGHT_RED);
1480 if (line != null) line.cardinalityMaxText.setBackground(COLOR_LIGHT_RED);
1481 } else {
1482 if (line != null) line.cardinalityMaxText.setBackground(COLOR_TEXT_BACKGROUND);
1483 }
1484 for (int i = 0; inputLine.columns != null && i < inputLine.columns.size(); i++) {
1485 InputData inputData = inputLine.columns.get(i);
1486 InputGroup group = null;
1487 if (line != null) group = line.inputs.get(i);
1488 if (inputData.name.equals(CustomTxtTraceDefinition.TAG_TIMESTAMP)) {
1489 timestampFound = true;
1490 if (inputData.format.length() == 0) {
1491 errors.append("Enter the input format for the Time Stamp (Line "+name+" Group "+(i+1)+"). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1492 if (group != null) group.tagText.setBackground(COLOR_LIGHT_RED);
1493 } else {
1494 try {
1495 new SimpleDateFormat(inputData.format);
1496 if (group != null) group.tagText.setBackground(COLOR_TEXT_BACKGROUND);
1497 } catch (IllegalArgumentException e) {
1498 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$
1499 if (group != null) group.tagText.setBackground(COLOR_LIGHT_RED);
1500 }
1501 }
1502 } else if (inputData.name.length() == 0) {
1503 errors.append("Enter a name for the data group (Line "+name+" Group "+(i+1)+"). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1504 if (group != null) group.tagText.setBackground(COLOR_LIGHT_RED);
1505 } else {
1506 if (group != null) group.tagText.setBackground(COLOR_TEXT_BACKGROUND);
1507 }
1508 }
1509 for (int i = 0; inputLine.childrenInputs != null && i < inputLine.childrenInputs.size(); i++) {
1510 errors.append(validateLine(inputLine.childrenInputs.get(i), name+"."+(i+1))); //$NON-NLS-1$
1511 }
1512 return errors;
1513 }
1514
1515 public CustomTxtTraceDefinition getDefinition() {
1516 return definition;
1517 }
1518
1519 public char[] getInputText() {
1520 return inputText.getText().toCharArray();
1521 }
1522 }
This page took 0.069471 seconds and 6 git commands to generate.