Commit | Line | Data |
---|---|---|
a0a88f65 | 1 | /******************************************************************************* |
738beb68 | 2 | * Copyright (c) 2010, 2014 Ericsson |
a0a88f65 AM |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
f6aa55e2 | 10 | * Patrick Tasse - Initial API and implementation |
a0a88f65 AM |
11 | *******************************************************************************/ |
12 | ||
be222f56 PT |
13 | package org.eclipse.linuxtools.internal.tmf.ui.parsers.wizards; |
14 | ||
15 | import java.io.BufferedReader; | |
16 | import java.io.ByteArrayInputStream; | |
17 | import java.io.IOException; | |
18 | import java.io.InputStream; | |
19 | import java.io.InputStreamReader; | |
313a5081 | 20 | import java.net.URL; |
be222f56 | 21 | import java.text.ParseException; |
be222f56 | 22 | import java.util.ArrayList; |
be222f56 PT |
23 | import java.util.List; |
24 | ||
25 | import javax.xml.parsers.DocumentBuilder; | |
26 | import javax.xml.parsers.DocumentBuilderFactory; | |
27 | import javax.xml.parsers.ParserConfigurationException; | |
28 | ||
29 | import org.eclipse.core.resources.IFile; | |
30 | import org.eclipse.core.runtime.CoreException; | |
313a5081 PT |
31 | import org.eclipse.core.runtime.FileLocator; |
32 | import org.eclipse.core.runtime.IPath; | |
33 | import org.eclipse.core.runtime.Path; | |
34 | import org.eclipse.core.runtime.Platform; | |
be222f56 PT |
35 | import org.eclipse.jface.viewers.AbstractTreeViewer; |
36 | import org.eclipse.jface.viewers.ColumnLabelProvider; | |
37 | import org.eclipse.jface.viewers.ISelection; | |
38 | import org.eclipse.jface.viewers.ISelectionChangedListener; | |
39 | import org.eclipse.jface.viewers.IStructuredSelection; | |
40 | import org.eclipse.jface.viewers.ITreeContentProvider; | |
41 | import org.eclipse.jface.viewers.SelectionChangedEvent; | |
42 | import org.eclipse.jface.viewers.StructuredSelection; | |
43 | import org.eclipse.jface.viewers.TreeViewer; | |
44 | import org.eclipse.jface.viewers.Viewer; | |
45 | import org.eclipse.jface.wizard.WizardPage; | |
46 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
47 | import org.eclipse.linuxtools.internal.tmf.ui.Messages; | |
47aafe74 AM |
48 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTraceDefinition; |
49 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTrace; | |
50 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTraceDefinition; | |
51 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTraceDefinition.InputAttribute; | |
52 | import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTraceDefinition.InputElement; | |
738beb68 | 53 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampFormat; |
be222f56 PT |
54 | import org.eclipse.swt.SWT; |
55 | import org.eclipse.swt.browser.Browser; | |
56 | import org.eclipse.swt.browser.TitleEvent; | |
57 | import org.eclipse.swt.browser.TitleListener; | |
58 | import org.eclipse.swt.custom.SashForm; | |
59 | import org.eclipse.swt.custom.ScrolledComposite; | |
60 | import org.eclipse.swt.custom.StyleRange; | |
61 | import org.eclipse.swt.custom.StyledText; | |
62 | import org.eclipse.swt.events.ModifyEvent; | |
63 | import org.eclipse.swt.events.ModifyListener; | |
64 | import org.eclipse.swt.events.SelectionAdapter; | |
65 | import org.eclipse.swt.events.SelectionEvent; | |
66 | import org.eclipse.swt.events.SelectionListener; | |
67 | import org.eclipse.swt.graphics.Color; | |
68 | import org.eclipse.swt.graphics.Font; | |
69 | import org.eclipse.swt.graphics.FontData; | |
70 | import org.eclipse.swt.graphics.Image; | |
313a5081 PT |
71 | import org.eclipse.swt.graphics.Point; |
72 | import org.eclipse.swt.graphics.Rectangle; | |
be222f56 PT |
73 | import org.eclipse.swt.layout.FillLayout; |
74 | import org.eclipse.swt.layout.GridData; | |
75 | import org.eclipse.swt.layout.GridLayout; | |
76 | import org.eclipse.swt.widgets.Button; | |
77 | import org.eclipse.swt.widgets.Combo; | |
78 | import org.eclipse.swt.widgets.Composite; | |
79 | import org.eclipse.swt.widgets.Display; | |
80 | import org.eclipse.swt.widgets.Group; | |
81 | import org.eclipse.swt.widgets.Label; | |
82 | import org.eclipse.swt.widgets.Shell; | |
83 | import org.eclipse.swt.widgets.Text; | |
313a5081 | 84 | import org.osgi.framework.Bundle; |
be222f56 PT |
85 | import org.w3c.dom.Document; |
86 | import org.w3c.dom.Element; | |
87 | import org.w3c.dom.NamedNodeMap; | |
88 | import org.w3c.dom.Node; | |
89 | import org.w3c.dom.NodeList; | |
90 | import org.xml.sax.EntityResolver; | |
91 | import org.xml.sax.ErrorHandler; | |
92 | import org.xml.sax.InputSource; | |
93 | import org.xml.sax.SAXException; | |
94 | import org.xml.sax.SAXParseException; | |
95 | ||
a0a88f65 AM |
96 | /** |
97 | * Input wizard page for custom XML trace parsers. | |
98 | * | |
f6aa55e2 | 99 | * @author Patrick Tasse |
a0a88f65 | 100 | */ |
be222f56 PT |
101 | public class CustomXmlParserInputWizardPage extends WizardPage { |
102 | ||
103 | private static final String DEFAULT_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; //$NON-NLS-1$ | |
313a5081 PT |
104 | private static final String TIMESTAMP_FORMAT_BUNDLE = "org.eclipse.linuxtools.lttng.help"; //$NON-NLS-1$ |
105 | private static final String TIMESTAMP_FORMAT_PATH = "reference/api/org/eclipse/linuxtools/tmf/core/timestamp/TmfTimestampFormat.html"; //$NON-NLS-1$ | |
f6aa55e2 PT |
106 | private static final Image ELEMENT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/element_icon.gif"); //$NON-NLS-1$ |
107 | private static final Image ADD_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/add_button.gif"); //$NON-NLS-1$ | |
108 | private static final Image ADD_NEXT_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/addnext_button.gif"); //$NON-NLS-1$ | |
109 | private static final Image ADD_CHILD_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/addchild_button.gif"); //$NON-NLS-1$ | |
110 | private static final Image ADD_MANY_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/addmany_button.gif"); //$NON-NLS-1$ | |
111 | private static final Image DELETE_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/delete_button.gif"); //$NON-NLS-1$ | |
112 | private static final Image MOVE_UP_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/moveup_button.gif"); //$NON-NLS-1$ | |
113 | private static final Image MOVE_DOWN_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/movedown_button.gif"); //$NON-NLS-1$ | |
114 | private static final Image HELP_IMAGE = Activator.getDefault().getImageFromPath("/icons/elcl16/help_button.gif"); //$NON-NLS-1$ | |
be222f56 | 115 | private static final Color COLOR_LIGHT_RED = new Color(Display.getDefault(), 255, 192, 192); |
f6aa55e2 PT |
116 | private static final Color COLOR_TEXT_BACKGROUND = Display.getDefault().getSystemColor(SWT.COLOR_WHITE); |
117 | private static final Color COLOR_WIDGET_BACKGROUND = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); | |
be222f56 PT |
118 | |
119 | private final ISelection selection; | |
120 | private CustomXmlTraceDefinition definition; | |
121 | private String editDefinitionName; | |
122 | private String defaultDescription; | |
123 | private ElementNode selectedElement; | |
124 | private Composite container; | |
125 | private Text logtypeText; | |
126 | private Text timeStampOutputFormatText; | |
127 | private Text timeStampPreviewText; | |
128 | private Button removeButton; | |
129 | private Button addChildButton; | |
130 | private Button addNextButton; | |
131 | private Button moveUpButton; | |
132 | private Button moveDownButton; | |
be222f56 PT |
133 | private ScrolledComposite elementScrolledComposite; |
134 | private TreeViewer treeViewer; | |
be222f56 PT |
135 | private Composite elementContainer; |
136 | private Text errorText; | |
137 | private StyledText inputText; | |
138 | private Font fixedFont; | |
139 | private UpdateListener updateListener; | |
140 | private Browser helpBrowser; | |
141 | private Element documentElement; | |
142 | ||
143 | // variables used recursively through element traversal | |
144 | private String timeStampValue; | |
145 | private String timeStampFormat; | |
146 | private boolean timeStampFound; | |
147 | private int logEntriesCount; | |
148 | private boolean logEntryFound; | |
149 | ||
a0a88f65 AM |
150 | /** |
151 | * Constructor | |
152 | * | |
153 | * @param selection | |
154 | * Selection object | |
155 | * @param definition | |
156 | * Trace definition | |
157 | */ | |
be222f56 PT |
158 | protected CustomXmlParserInputWizardPage(ISelection selection, CustomXmlTraceDefinition definition) { |
159 | super("CustomXmlParserWizardPage"); //$NON-NLS-1$ | |
160 | if (definition == null) { | |
161 | setTitle(Messages.CustomXmlParserInputWizardPage_titleNew); | |
162 | defaultDescription = Messages.CustomXmlParserInputWizardPage_descriptionNew; | |
163 | } else { | |
164 | setTitle(Messages.CustomXmlParserInputWizardPage_titleEdit); | |
165 | defaultDescription = Messages.CustomXmlParserInputWizardPage_descriptionEdit; | |
166 | } | |
167 | setDescription(defaultDescription); | |
168 | this.selection = selection; | |
169 | this.definition = definition; | |
170 | if (definition != null) { | |
171 | this.editDefinitionName = definition.definitionName; | |
172 | } | |
173 | } | |
174 | ||
175 | @Override | |
176 | public void createControl(Composite parent) { | |
177 | container = new Composite(parent, SWT.NULL); | |
178 | container.setLayout(new GridLayout()); | |
179 | ||
180 | updateListener = new UpdateListener(); | |
181 | ||
182 | Composite headerComposite = new Composite(container, SWT.FILL); | |
183 | GridLayout headerLayout = new GridLayout(5, false); | |
184 | headerLayout.marginHeight = 0; | |
185 | headerLayout.marginWidth = 0; | |
186 | headerComposite.setLayout(headerLayout); | |
187 | headerComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); | |
188 | ||
189 | Label logtypeLabel = new Label(headerComposite, SWT.NULL); | |
190 | logtypeLabel.setText(Messages.CustomXmlParserInputWizardPage_logType); | |
191 | ||
192 | logtypeText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE); | |
193 | logtypeText.setLayoutData(new GridData(120, SWT.DEFAULT)); | |
194 | ||
195 | Label timeStampFormatLabel = new Label(headerComposite, SWT.NULL); | |
196 | timeStampFormatLabel.setText(Messages.CustomXmlParserInputWizardPage_timestampFormat); | |
197 | ||
198 | timeStampOutputFormatText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE); | |
199 | timeStampOutputFormatText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); | |
200 | timeStampOutputFormatText.setText(DEFAULT_TIMESTAMP_FORMAT); | |
201 | ||
313a5081 PT |
202 | Button timeStampFormatHelpButton = new Button(headerComposite, SWT.PUSH); |
203 | timeStampFormatHelpButton.setImage(HELP_IMAGE); | |
204 | timeStampFormatHelpButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_timestampFormatHelp); | |
205 | timeStampFormatHelpButton.addSelectionListener(new SelectionAdapter() { | |
be222f56 PT |
206 | @Override |
207 | public void widgetSelected(SelectionEvent e) { | |
313a5081 PT |
208 | Bundle plugin = Platform.getBundle(TIMESTAMP_FORMAT_BUNDLE); |
209 | IPath path = new Path(TIMESTAMP_FORMAT_PATH); | |
210 | URL fileURL = FileLocator.find(plugin, path, null); | |
211 | try { | |
212 | URL pageURL = FileLocator.toFileURL(fileURL); | |
213 | openHelpShell(pageURL.toString()); | |
214 | } catch (IOException e1) { | |
215 | } | |
be222f56 PT |
216 | } |
217 | }); | |
218 | ||
219 | Label timeStampPreviewLabel = new Label(headerComposite, SWT.NULL); | |
220 | timeStampPreviewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 3, 1)); | |
221 | timeStampPreviewLabel.setText(Messages.CustomXmlParserInputWizardPage_preview); | |
222 | ||
223 | timeStampPreviewText = new Text(headerComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY); | |
224 | timeStampPreviewText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); | |
225 | timeStampPreviewText.setText("*no time stamp element or attribute*"); //$NON-NLS-1$ | |
226 | ||
227 | createButtonBar(); | |
228 | ||
229 | SashForm vSash = new SashForm(container, SWT.VERTICAL); | |
230 | vSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
231 | vSash.setBackground(vSash.getDisplay().getSystemColor(SWT.COLOR_GRAY)); | |
232 | ||
233 | SashForm hSash = new SashForm(vSash, SWT.HORIZONTAL); | |
234 | hSash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
235 | ||
f6aa55e2 | 236 | ScrolledComposite treeScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL | SWT.H_SCROLL); |
be222f56 | 237 | treeScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
f6aa55e2 | 238 | Composite treeContainer = new Composite(treeScrolledComposite, SWT.NONE); |
be222f56 PT |
239 | treeContainer.setLayout(new FillLayout()); |
240 | treeScrolledComposite.setContent(treeContainer); | |
241 | treeScrolledComposite.setExpandHorizontal(true); | |
242 | treeScrolledComposite.setExpandVertical(true); | |
243 | ||
244 | treeViewer = new TreeViewer(treeContainer, SWT.SINGLE | SWT.BORDER); | |
245 | treeViewer.setContentProvider(new InputElementTreeNodeContentProvider()); | |
246 | treeViewer.setLabelProvider(new InputElementTreeLabelProvider()); | |
247 | treeViewer.addSelectionChangedListener(new InputElementTreeSelectionChangedListener()); | |
248 | treeContainer.layout(); | |
249 | ||
250 | treeScrolledComposite | |
251 | .setMinSize(treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, treeContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); | |
252 | ||
253 | elementScrolledComposite = new ScrolledComposite(hSash, SWT.V_SCROLL); | |
254 | elementScrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
255 | elementContainer = new Composite(elementScrolledComposite, SWT.NONE); | |
256 | GridLayout gl = new GridLayout(); | |
257 | gl.marginHeight = 1; | |
258 | gl.marginWidth = 0; | |
259 | elementContainer.setLayout(gl); | |
260 | elementScrolledComposite.setContent(elementContainer); | |
261 | elementScrolledComposite.setExpandHorizontal(true); | |
262 | elementScrolledComposite.setExpandVertical(true); | |
263 | ||
264 | if (definition == null) { | |
265 | definition = new CustomXmlTraceDefinition(); | |
266 | } | |
267 | loadDefinition(definition); | |
268 | treeViewer.expandAll(); | |
269 | elementContainer.layout(); | |
270 | ||
271 | logtypeText.addModifyListener(updateListener); | |
272 | timeStampOutputFormatText.addModifyListener(updateListener); | |
273 | ||
274 | elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, | |
275 | elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1); | |
276 | ||
277 | hSash.setWeights(new int[] { 1, 2 }); | |
278 | ||
279 | if (definition.rootInputElement == null) { | |
280 | removeButton.setEnabled(false); | |
281 | addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addDocumentElement); | |
282 | addNextButton.setEnabled(false); | |
283 | moveUpButton.setEnabled(false); | |
284 | moveDownButton.setEnabled(false); | |
285 | } else { // root is selected | |
286 | addNextButton.setEnabled(false); | |
287 | } | |
288 | ||
289 | Composite sashBottom = new Composite(vSash, SWT.NONE); | |
290 | GridLayout sashBottomLayout = new GridLayout(2, false); | |
291 | sashBottomLayout.marginHeight = 0; | |
292 | sashBottomLayout.marginWidth = 0; | |
293 | sashBottom.setLayout(sashBottomLayout); | |
294 | ||
295 | Label previewLabel = new Label(sashBottom, SWT.NULL); | |
296 | previewLabel.setText(Messages.CustomXmlParserInputWizardPage_previewInput); | |
297 | ||
298 | errorText = new Text(sashBottom, SWT.SINGLE | SWT.READ_ONLY); | |
299 | errorText.setBackground(COLOR_WIDGET_BACKGROUND); | |
300 | errorText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); | |
301 | errorText.setVisible(false); | |
302 | ||
303 | inputText = new StyledText(sashBottom, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); | |
304 | if (fixedFont == null) { | |
305 | if (System.getProperty("os.name").contains("Windows")) { //$NON-NLS-1$ //$NON-NLS-2$ | |
306 | fixedFont = new Font(Display.getCurrent(), new FontData("Courier New", 10, SWT.NORMAL)); //$NON-NLS-1$ | |
307 | } else { | |
308 | fixedFont = new Font(Display.getCurrent(), new FontData("Monospace", 10, SWT.NORMAL)); //$NON-NLS-1$ | |
309 | } | |
310 | } | |
311 | inputText.setFont(fixedFont); | |
312 | GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1); | |
313 | gd.heightHint = inputText.computeSize(SWT.DEFAULT, inputText.getLineHeight() * 4).y; | |
314 | gd.widthHint = 800; | |
315 | inputText.setLayoutData(gd); | |
316 | inputText.setText(getSelectionText()); | |
317 | inputText.addModifyListener(new ModifyListener() { | |
318 | @Override | |
319 | public void modifyText(ModifyEvent e) { | |
320 | parseXmlInput(inputText.getText()); | |
321 | } | |
322 | }); | |
323 | inputText.addModifyListener(updateListener); | |
324 | ||
325 | vSash.setWeights(new int[] { hSash.computeSize(SWT.DEFAULT, SWT.DEFAULT).y, sashBottom.computeSize(SWT.DEFAULT, SWT.DEFAULT).y }); | |
326 | ||
327 | setControl(container); | |
328 | } | |
329 | ||
330 | private void createButtonBar() { | |
331 | Composite buttonBar = new Composite(container, SWT.NONE); | |
332 | GridLayout buttonBarLayout = new GridLayout(6, false); | |
333 | buttonBarLayout.marginHeight = 0; | |
334 | buttonBarLayout.marginWidth = 0; | |
335 | buttonBar.setLayout(buttonBarLayout); | |
336 | ||
337 | removeButton = new Button(buttonBar, SWT.PUSH); | |
f6aa55e2 | 338 | removeButton.setImage(DELETE_IMAGE); |
be222f56 PT |
339 | removeButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_removeElement); |
340 | removeButton.addSelectionListener(new SelectionAdapter() { | |
341 | @Override | |
342 | public void widgetSelected(SelectionEvent e) { | |
343 | if (treeViewer.getSelection().isEmpty() || selectedElement == null) { | |
344 | return; | |
345 | } | |
346 | removeElement(); | |
347 | InputElement inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement(); | |
348 | if (inputElement == definition.rootInputElement) { | |
349 | definition.rootInputElement = null; | |
350 | } else { | |
351 | inputElement.parentElement.childElements.remove(inputElement); | |
352 | } | |
353 | treeViewer.refresh(); | |
354 | validate(); | |
355 | updatePreviews(); | |
356 | removeButton.setEnabled(false); | |
357 | if (definition.rootInputElement == null) { | |
358 | addChildButton.setEnabled(true); | |
359 | addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addDocumentEleemnt); | |
360 | } else { | |
361 | addChildButton.setEnabled(false); | |
362 | } | |
363 | addNextButton.setEnabled(false); | |
364 | moveUpButton.setEnabled(false); | |
365 | moveDownButton.setEnabled(false); | |
366 | } | |
367 | }); | |
368 | ||
369 | addChildButton = new Button(buttonBar, SWT.PUSH); | |
f6aa55e2 | 370 | addChildButton.setImage(ADD_CHILD_IMAGE); |
be222f56 PT |
371 | addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addChildElement); |
372 | addChildButton.addSelectionListener(new SelectionAdapter() { | |
373 | @Override | |
374 | public void widgetSelected(SelectionEvent e) { | |
375 | InputElement inputElement = new InputElement("", false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null); //$NON-NLS-1$ //$NON-NLS-2$ | |
376 | if (definition.rootInputElement == null) { | |
377 | definition.rootInputElement = inputElement; | |
378 | inputElement.elementName = getChildNameSuggestion(null); | |
379 | } else if (treeViewer.getSelection().isEmpty()) { | |
380 | return; | |
381 | } else { | |
382 | InputElement parentInputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement(); | |
383 | parentInputElement.addChild(inputElement); | |
384 | inputElement.elementName = getChildNameSuggestion(parentInputElement); | |
385 | } | |
386 | treeViewer.refresh(); | |
387 | treeViewer.setSelection(new StructuredSelection(inputElement), true); | |
388 | } | |
389 | }); | |
390 | ||
391 | addNextButton = new Button(buttonBar, SWT.PUSH); | |
f6aa55e2 | 392 | addNextButton.setImage(ADD_NEXT_IMAGE); |
be222f56 PT |
393 | addNextButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addNextElement); |
394 | addNextButton.addSelectionListener(new SelectionAdapter() { | |
395 | @Override | |
396 | public void widgetSelected(SelectionEvent e) { | |
397 | InputElement inputElement = new InputElement("", false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null); //$NON-NLS-1$ //$NON-NLS-2$ | |
398 | if (definition.rootInputElement == null) { | |
399 | definition.rootInputElement = inputElement; | |
400 | inputElement.elementName = getChildNameSuggestion(null); | |
401 | } else if (treeViewer.getSelection().isEmpty()) { | |
402 | return; | |
403 | } else { | |
404 | InputElement previousInputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement(); | |
405 | if (previousInputElement == definition.rootInputElement) { | |
406 | return; | |
407 | } | |
408 | previousInputElement.addNext(inputElement); | |
409 | inputElement.elementName = getChildNameSuggestion(inputElement.parentElement); | |
410 | } | |
411 | treeViewer.refresh(); | |
412 | treeViewer.setSelection(new StructuredSelection(inputElement), true); | |
413 | } | |
414 | }); | |
415 | ||
f6aa55e2 PT |
416 | Button feelingLuckyButton = new Button(buttonBar, SWT.PUSH); |
417 | feelingLuckyButton.setImage(ADD_MANY_IMAGE); | |
be222f56 PT |
418 | feelingLuckyButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_feelingLucky); |
419 | feelingLuckyButton.addSelectionListener(new SelectionAdapter() { | |
420 | @Override | |
421 | public void widgetSelected(SelectionEvent e) { | |
422 | InputElement inputElement = null; | |
423 | if (definition.rootInputElement == null) { | |
424 | if (getChildNameSuggestion(null).length() != 0) { | |
425 | inputElement = new InputElement(getChildNameSuggestion(null), false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null); //$NON-NLS-1$ | |
426 | definition.rootInputElement = inputElement; | |
427 | feelingLucky(inputElement); | |
428 | } else { | |
429 | return; | |
430 | } | |
431 | } else if (treeViewer.getSelection().isEmpty()) { | |
432 | return; | |
433 | } else { | |
434 | inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement(); | |
435 | feelingLucky(inputElement); | |
436 | } | |
437 | treeViewer.refresh(); | |
438 | treeViewer.setSelection(new StructuredSelection(inputElement), true); | |
439 | treeViewer.expandToLevel(inputElement, AbstractTreeViewer.ALL_LEVELS); | |
440 | } | |
441 | }); | |
442 | ||
443 | moveUpButton = new Button(buttonBar, SWT.PUSH); | |
f6aa55e2 | 444 | moveUpButton.setImage(MOVE_UP_IMAGE); |
be222f56 PT |
445 | moveUpButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_moveUp); |
446 | moveUpButton.addSelectionListener(new SelectionAdapter() { | |
447 | @Override | |
448 | public void widgetSelected(SelectionEvent e) { | |
449 | if (treeViewer.getSelection().isEmpty()) { | |
450 | return; | |
451 | } | |
452 | InputElement inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement(); | |
453 | if (inputElement == definition.rootInputElement) { | |
454 | return; | |
455 | } | |
456 | inputElement.moveUp(); | |
457 | treeViewer.refresh(); | |
458 | validate(); | |
459 | updatePreviews(); | |
460 | } | |
461 | }); | |
462 | ||
463 | moveDownButton = new Button(buttonBar, SWT.PUSH); | |
f6aa55e2 | 464 | moveDownButton.setImage(MOVE_DOWN_IMAGE); |
be222f56 PT |
465 | moveDownButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_moveDown); |
466 | moveDownButton.addSelectionListener(new SelectionAdapter() { | |
467 | @Override | |
468 | public void widgetSelected(SelectionEvent e) { | |
469 | if (treeViewer.getSelection().isEmpty()) { | |
470 | return; | |
471 | } | |
472 | InputElement inputElement = (InputElement) ((IStructuredSelection) treeViewer.getSelection()).getFirstElement(); | |
473 | if (inputElement == definition.rootInputElement) { | |
474 | return; | |
475 | } | |
476 | inputElement.moveDown(); | |
477 | treeViewer.refresh(); | |
478 | validate(); | |
479 | updatePreviews(); | |
480 | } | |
481 | }); | |
482 | } | |
483 | ||
484 | private void feelingLucky(InputElement inputElement) { | |
485 | while (true) { | |
486 | String attributeName = getAttributeNameSuggestion(inputElement); | |
487 | if (attributeName.length() == 0) { | |
488 | break; | |
489 | } | |
490 | InputAttribute attribute = new InputAttribute(attributeName, attributeName, 0, ""); //$NON-NLS-1$ | |
491 | inputElement.addAttribute(attribute); | |
492 | } | |
493 | while (true) { | |
494 | String childName = getChildNameSuggestion(inputElement); | |
495 | if (childName.length() == 0) { | |
496 | break; | |
497 | } | |
498 | InputElement childElement = new InputElement(childName, false, CustomXmlTraceDefinition.TAG_IGNORE, 0, "", null); //$NON-NLS-1$ | |
499 | inputElement.addChild(childElement); | |
500 | feelingLucky(childElement); | |
501 | } | |
502 | } | |
503 | ||
504 | private static class InputElementTreeNodeContentProvider implements ITreeContentProvider { | |
505 | ||
506 | @Override | |
507 | public Object[] getElements(Object inputElement) { | |
508 | CustomXmlTraceDefinition def = (CustomXmlTraceDefinition) inputElement; | |
509 | if (def.rootInputElement != null) { | |
510 | return new Object[] { def.rootInputElement }; | |
511 | } | |
512 | return new Object[0]; | |
513 | } | |
514 | ||
515 | @Override | |
516 | public Object[] getChildren(Object parentElement) { | |
517 | InputElement inputElement = (InputElement) parentElement; | |
518 | if (inputElement.childElements == null) { | |
519 | return new InputElement[0]; | |
520 | } | |
521 | return inputElement.childElements.toArray(); | |
522 | } | |
523 | ||
524 | @Override | |
525 | public boolean hasChildren(Object element) { | |
526 | InputElement inputElement = (InputElement) element; | |
527 | return (inputElement.childElements != null && inputElement.childElements.size() > 0); | |
528 | } | |
529 | ||
530 | @Override | |
531 | public void dispose() { | |
532 | } | |
533 | ||
534 | @Override | |
535 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
536 | } | |
537 | ||
538 | @Override | |
539 | public Object getParent(Object element) { | |
540 | InputElement inputElement = (InputElement) element; | |
541 | return inputElement.parentElement; | |
542 | } | |
543 | } | |
544 | ||
545 | private static class InputElementTreeLabelProvider extends ColumnLabelProvider { | |
546 | ||
547 | @Override | |
548 | public Image getImage(Object element) { | |
f6aa55e2 | 549 | return ELEMENT_IMAGE; |
be222f56 PT |
550 | } |
551 | ||
552 | @Override | |
553 | public String getText(Object element) { | |
554 | InputElement inputElement = (InputElement) element; | |
555 | return (inputElement.elementName.trim().length() == 0) ? "?" : inputElement.elementName; //$NON-NLS-1$ | |
556 | } | |
557 | } | |
558 | ||
559 | private class InputElementTreeSelectionChangedListener implements ISelectionChangedListener { | |
560 | @Override | |
561 | public void selectionChanged(SelectionChangedEvent event) { | |
562 | if (selectedElement != null) { | |
563 | selectedElement.dispose(); | |
564 | } | |
565 | if (!(event.getSelection().isEmpty()) && event.getSelection() instanceof IStructuredSelection) { | |
3dca7aa5 AM |
566 | IStructuredSelection sel = (IStructuredSelection) event.getSelection(); |
567 | InputElement inputElement = (InputElement) sel.getFirstElement(); | |
be222f56 PT |
568 | selectedElement = new ElementNode(elementContainer, inputElement); |
569 | elementContainer.layout(); | |
570 | elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, | |
571 | elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1); | |
572 | container.layout(); | |
573 | validate(); | |
574 | updatePreviews(); | |
575 | removeButton.setEnabled(true); | |
576 | addChildButton.setEnabled(true); | |
577 | addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addChildElement); | |
578 | if (definition.rootInputElement == inputElement) { | |
579 | addNextButton.setEnabled(false); | |
580 | } else { | |
581 | addNextButton.setEnabled(true); | |
582 | } | |
583 | moveUpButton.setEnabled(true); | |
584 | moveDownButton.setEnabled(true); | |
585 | } else { | |
586 | removeButton.setEnabled(false); | |
587 | if (definition.rootInputElement == null) { | |
588 | addChildButton.setEnabled(true); | |
589 | addChildButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addDocumentElement); | |
590 | } else { | |
591 | addChildButton.setEnabled(false); | |
592 | } | |
593 | addNextButton.setEnabled(false); | |
594 | moveUpButton.setEnabled(false); | |
595 | moveDownButton.setEnabled(false); | |
596 | } | |
597 | } | |
598 | } | |
599 | ||
be222f56 PT |
600 | @Override |
601 | public void dispose() { | |
602 | if (fixedFont != null) { | |
603 | fixedFont.dispose(); | |
604 | fixedFont = null; | |
605 | } | |
606 | super.dispose(); | |
607 | } | |
608 | ||
609 | private void loadDefinition(CustomXmlTraceDefinition def) { | |
610 | logtypeText.setText(def.definitionName); | |
611 | timeStampOutputFormatText.setText(def.timeStampOutputFormat); | |
612 | treeViewer.setInput(def); | |
613 | ||
614 | if (def.rootInputElement != null) { | |
615 | treeViewer.setSelection(new StructuredSelection(def.rootInputElement)); | |
616 | } | |
617 | } | |
618 | ||
619 | private String getName(InputElement inputElement) { | |
620 | String name = (inputElement.elementName.trim().length() == 0) ? "?" : inputElement.elementName.trim(); //$NON-NLS-1$ | |
621 | if (inputElement.parentElement == null) { | |
622 | return name; | |
623 | } | |
624 | return getName(inputElement.parentElement) + " : " + name; //$NON-NLS-1$ | |
625 | } | |
626 | ||
627 | private String getName(InputAttribute inputAttribute, InputElement inputElement) { | |
628 | String name = (inputAttribute.attributeName.trim().length() == 0) ? "?" : inputAttribute.attributeName.trim(); //$NON-NLS-1$ | |
629 | return getName(inputElement) + " : " + name; //$NON-NLS-1$ | |
630 | } | |
631 | ||
be222f56 PT |
632 | @Override |
633 | public void setVisible(boolean visible) { | |
634 | if (visible) { | |
635 | validate(); | |
636 | updatePreviews(); | |
637 | } | |
638 | super.setVisible(visible); | |
639 | } | |
640 | ||
a0a88f65 AM |
641 | /** |
642 | * Get the global list of input names. | |
643 | * | |
644 | * @return The list of input names | |
645 | */ | |
be222f56 PT |
646 | public List<String> getInputNames() { |
647 | return getInputNames(definition.rootInputElement); | |
648 | } | |
649 | ||
a0a88f65 AM |
650 | /** |
651 | * Get the list of input names for a given element. | |
652 | * | |
653 | * @param inputElement | |
654 | * The element | |
655 | * @return The input names for this element | |
656 | */ | |
be222f56 | 657 | public List<String> getInputNames(InputElement inputElement) { |
507b1336 | 658 | List<String> inputs = new ArrayList<>(); |
be222f56 PT |
659 | if (inputElement.inputName != null && !inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_IGNORE)) { |
660 | String inputName = inputElement.inputName; | |
661 | if (!inputs.contains(inputName)) { | |
662 | inputs.add(inputName); | |
663 | } | |
664 | } | |
665 | if (inputElement.attributes != null) { | |
666 | for (InputAttribute attribute : inputElement.attributes) { | |
667 | String inputName = attribute.inputName; | |
668 | if (!inputs.contains(inputName)) { | |
669 | inputs.add(inputName); | |
670 | } | |
671 | } | |
672 | } | |
673 | if (inputElement.childElements != null) { | |
674 | for (InputElement childInputElement : inputElement.childElements) { | |
675 | for (String inputName : getInputNames(childInputElement)) { | |
676 | if (!inputs.contains(inputName)) { | |
677 | inputs.add(inputName); | |
678 | } | |
679 | } | |
680 | } | |
681 | } | |
682 | return inputs; | |
683 | } | |
684 | ||
685 | private void removeElement() { | |
686 | selectedElement.dispose(); | |
687 | selectedElement = null; | |
688 | elementContainer.layout(); | |
689 | elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, | |
690 | elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1); | |
691 | container.layout(); | |
692 | } | |
693 | ||
694 | private String getSelectionText() { | |
695 | InputStream inputStream = null; | |
696 | if (this.selection instanceof IStructuredSelection) { | |
3dca7aa5 AM |
697 | Object sel = ((IStructuredSelection) this.selection).getFirstElement(); |
698 | if (sel instanceof IFile) { | |
699 | IFile file = (IFile) sel; | |
be222f56 PT |
700 | try { |
701 | inputStream = file.getContents(); | |
702 | } catch (CoreException e) { | |
703 | return ""; //$NON-NLS-1$ | |
704 | } | |
705 | } | |
706 | } | |
707 | if (inputStream != null) { | |
507b1336 | 708 | try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));) { |
be222f56 PT |
709 | StringBuilder sb = new StringBuilder(); |
710 | String line = null; | |
711 | while ((line = reader.readLine()) != null) { | |
712 | sb.append(line + "\n"); //$NON-NLS-1$ | |
713 | } | |
714 | parseXmlInput(sb.toString()); | |
be222f56 PT |
715 | return sb.toString(); |
716 | } catch (IOException e) { | |
717 | return ""; //$NON-NLS-1$ | |
718 | } | |
719 | } | |
720 | return ""; //$NON-NLS-1$ | |
721 | } | |
722 | ||
723 | private void parseXmlInput(final String string) { | |
724 | try { | |
725 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
726 | DocumentBuilder db = dbf.newDocumentBuilder(); | |
727 | ||
728 | // The following allows xml parsing without access to the dtd | |
729 | EntityResolver resolver = new EntityResolver() { | |
730 | @Override | |
731 | public InputSource resolveEntity(String publicId, String systemId) { | |
732 | String empty = ""; //$NON-NLS-1$ | |
733 | ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes()); | |
734 | return new InputSource(bais); | |
735 | } | |
736 | }; | |
737 | db.setEntityResolver(resolver); | |
738 | ||
739 | // The following catches xml parsing exceptions | |
740 | db.setErrorHandler(new ErrorHandler() { | |
741 | @Override | |
742 | public void error(SAXParseException saxparseexception) throws SAXException { | |
743 | } | |
744 | ||
745 | @Override | |
746 | public void warning(SAXParseException saxparseexception) throws SAXException { | |
747 | } | |
748 | ||
749 | @Override | |
750 | public void fatalError(SAXParseException saxparseexception) throws SAXException { | |
751 | if (string.trim().length() != 0) { | |
752 | errorText.setText(saxparseexception.getMessage()); | |
753 | errorText.setBackground(COLOR_LIGHT_RED); | |
754 | errorText.setVisible(true); | |
755 | } | |
756 | throw saxparseexception; | |
757 | } | |
758 | }); | |
759 | ||
760 | errorText.setVisible(false); | |
761 | Document doc = null; | |
762 | doc = db.parse(new ByteArrayInputStream(string.getBytes())); | |
763 | documentElement = doc.getDocumentElement(); | |
764 | } catch (ParserConfigurationException e) { | |
765 | Activator.getDefault().logError("Error pasing XML input string: " + string, e); //$NON-NLS-1$ | |
766 | documentElement = null; | |
767 | } catch (SAXException e) { | |
768 | documentElement = null; | |
769 | } catch (IOException e) { | |
770 | Activator.getDefault().logError("Error pasing XML input string: " + string, e); //$NON-NLS-1$ | |
771 | documentElement = null; | |
772 | } | |
773 | } | |
774 | ||
be222f56 PT |
775 | private void initValues() { |
776 | timeStampValue = null; | |
777 | timeStampFormat = null; | |
778 | logEntriesCount = 0; | |
779 | logEntryFound = false; | |
780 | } | |
781 | ||
3dca7aa5 | 782 | private void updatePreviews() { |
be222f56 PT |
783 | if (inputText == null) { |
784 | // early update during construction | |
785 | return; | |
786 | } | |
787 | inputText.setStyleRanges(new StyleRange[] {}); | |
788 | if (selectedElement == null) { | |
789 | return; | |
790 | } | |
791 | ||
792 | initValues(); | |
793 | ||
794 | selectedElement.updatePreview(); | |
795 | ||
796 | if (timeStampValue != null && timeStampFormat != null) { | |
797 | try { | |
738beb68 PT |
798 | TmfTimestampFormat timestampFormat = new TmfTimestampFormat(timeStampFormat); |
799 | long timestamp = timestampFormat.parseValue(timeStampValue); | |
800 | timestampFormat = new TmfTimestampFormat(timeStampOutputFormatText.getText().trim()); | |
801 | timeStampPreviewText.setText(timestampFormat.format(timestamp)); | |
be222f56 PT |
802 | } catch (ParseException e) { |
803 | timeStampPreviewText.setText("*parse exception* [" + timeStampValue + "] <> [" + timeStampFormat + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
804 | } catch (IllegalArgumentException e) { | |
805 | timeStampPreviewText.setText("*parse exception* [Illegal Argument]"); //$NON-NLS-1$ | |
806 | } | |
807 | } else { | |
808 | timeStampPreviewText.setText("*no matching time stamp*"); //$NON-NLS-1$ | |
809 | } | |
810 | } | |
811 | ||
812 | private void openHelpShell(String url) { | |
813 | if (helpBrowser != null && !helpBrowser.isDisposed()) { | |
814 | helpBrowser.getShell().setActive(); | |
815 | if (!helpBrowser.getUrl().equals(url)) { | |
816 | helpBrowser.setUrl(url); | |
817 | } | |
818 | return; | |
819 | } | |
820 | final Shell helpShell = new Shell(getShell(), SWT.SHELL_TRIM); | |
821 | helpShell.setLayout(new FillLayout()); | |
822 | helpBrowser = new Browser(helpShell, SWT.NONE); | |
823 | helpBrowser.addTitleListener(new TitleListener() { | |
824 | @Override | |
825 | public void changed(TitleEvent event) { | |
826 | helpShell.setText(event.title); | |
827 | } | |
828 | }); | |
313a5081 PT |
829 | Rectangle r = container.getBounds(); |
830 | Point p = container.toDisplay(r.x, r.y); | |
831 | Rectangle trim = helpShell.computeTrim(p.x + (r.width - 750) / 2, p.y + (r.height - 400) / 2, 750, 400); | |
832 | helpShell.setBounds(trim); | |
be222f56 PT |
833 | helpShell.open(); |
834 | helpBrowser.setUrl(url); | |
835 | } | |
836 | ||
837 | private class UpdateListener implements ModifyListener, SelectionListener { | |
838 | ||
839 | @Override | |
840 | public void modifyText(ModifyEvent e) { | |
841 | validate(); | |
842 | updatePreviews(); | |
843 | } | |
844 | ||
845 | @Override | |
846 | public void widgetDefaultSelected(SelectionEvent e) { | |
847 | validate(); | |
848 | updatePreviews(); | |
849 | } | |
850 | ||
851 | @Override | |
852 | public void widgetSelected(SelectionEvent e) { | |
853 | validate(); | |
854 | updatePreviews(); | |
855 | } | |
856 | ||
857 | } | |
858 | ||
859 | private class ElementNode { | |
f6aa55e2 PT |
860 | private final InputElement inputElement; |
861 | private final Group group; | |
507b1336 AM |
862 | private List<Attribute> attributes = new ArrayList<>(); |
863 | private List<ElementNode> childElements = new ArrayList<>(); | |
f6aa55e2 PT |
864 | private Text elementNameText; |
865 | private Composite tagComposite; | |
866 | private Combo tagCombo; | |
867 | private Label tagLabel; | |
868 | private Text tagText; | |
869 | private Combo actionCombo; | |
870 | private Label previewLabel; | |
871 | private Text previewText; | |
872 | private Button logEntryButton; | |
873 | private Label fillerLabel; | |
874 | private Composite addAttributeComposite; | |
875 | private Button addAttributeButton; | |
876 | private Label addAttributeLabel; | |
be222f56 PT |
877 | |
878 | public ElementNode(Composite parent, InputElement inputElement) { | |
879 | this.inputElement = inputElement; | |
880 | ||
881 | group = new Group(parent, SWT.NONE); | |
882 | GridLayout gl = new GridLayout(2, false); | |
883 | gl.marginHeight = 0; | |
884 | group.setLayout(gl); | |
885 | group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); | |
886 | group.setText(getName(inputElement)); | |
887 | ||
888 | Label label = new Label(group, SWT.NULL); | |
889 | label.setText(Messages.CustomXmlParserInputWizardPage_elementName); | |
890 | label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); | |
891 | ||
892 | elementNameText = new Text(group, SWT.BORDER | SWT.SINGLE); | |
893 | GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
894 | gd.widthHint = 0; | |
895 | elementNameText.setLayoutData(gd); | |
896 | elementNameText.addModifyListener(new ModifyListener() { | |
897 | @Override | |
898 | public void modifyText(ModifyEvent e) { | |
899 | ElementNode.this.inputElement.elementName = elementNameText.getText().trim(); | |
900 | group.setText(getName(ElementNode.this.inputElement)); | |
901 | } | |
902 | }); | |
903 | elementNameText.setText(inputElement.elementName); | |
904 | elementNameText.addModifyListener(updateListener); | |
905 | ||
906 | if (inputElement.parentElement != null) { | |
907 | previewLabel = new Label(group, SWT.NULL); | |
908 | previewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); | |
909 | previewLabel.setText(Messages.CustomXmlParserInputWizardPage_preview); | |
910 | ||
911 | previewText = new Text(group, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY); | |
912 | gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
913 | gd.widthHint = 0; | |
914 | previewText.setLayoutData(gd); | |
915 | previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingElement); | |
916 | previewText.setBackground(COLOR_WIDGET_BACKGROUND); | |
917 | ||
918 | logEntryButton = new Button(group, SWT.CHECK); | |
919 | logEntryButton.setText(Messages.CustomXmlParserInputWizardPage_logEntry); | |
920 | logEntryButton.setSelection(inputElement.logEntry); | |
921 | logEntryButton.addSelectionListener(new SelectionListener() { | |
922 | @Override | |
923 | public void widgetDefaultSelected(SelectionEvent e) { | |
924 | } | |
925 | ||
926 | @Override | |
927 | public void widgetSelected(SelectionEvent e) { | |
3dca7aa5 AM |
928 | InputElement parentElem = ElementNode.this.inputElement.parentElement; |
929 | while (parentElem != null) { | |
930 | parentElem.logEntry = false; | |
931 | parentElem = parentElem.parentElement; | |
be222f56 PT |
932 | } |
933 | } | |
934 | }); | |
935 | logEntryButton.addSelectionListener(updateListener); | |
936 | ||
937 | tagComposite = new Composite(group, SWT.FILL); | |
938 | GridLayout tagLayout = new GridLayout(4, false); | |
939 | tagLayout.marginWidth = 0; | |
940 | tagLayout.marginHeight = 0; | |
941 | tagComposite.setLayout(tagLayout); | |
942 | tagComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); | |
943 | ||
944 | tagCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY); | |
945 | tagCombo.setItems(new String[] { CustomXmlTraceDefinition.TAG_IGNORE, CustomTraceDefinition.TAG_TIMESTAMP, | |
946 | CustomTraceDefinition.TAG_MESSAGE, CustomTraceDefinition.TAG_OTHER }); | |
947 | tagCombo.setVisibleItemCount(tagCombo.getItemCount()); | |
948 | tagCombo.addSelectionListener(new SelectionListener() { | |
949 | @Override | |
950 | public void widgetDefaultSelected(SelectionEvent e) { | |
951 | } | |
952 | ||
953 | @Override | |
954 | public void widgetSelected(SelectionEvent e) { | |
955 | tagText.removeModifyListener(updateListener); | |
956 | switch (tagCombo.getSelectionIndex()) { | |
957 | case 0: // Ignore | |
958 | tagLabel.setVisible(false); | |
959 | tagText.setVisible(false); | |
960 | actionCombo.setVisible(false); | |
961 | break; | |
962 | case 1: // Time Stamp | |
963 | tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format); | |
964 | tagLabel.setVisible(true); | |
965 | tagText.setVisible(true); | |
966 | tagText.addModifyListener(updateListener); | |
967 | actionCombo.setVisible(true); | |
968 | break; | |
969 | case 2: // Message | |
970 | tagLabel.setVisible(false); | |
971 | tagText.setVisible(false); | |
972 | actionCombo.setVisible(true); | |
973 | break; | |
974 | case 3: // Other | |
975 | tagLabel.setText(Messages.CustomXmlParserInputWizardPage_tagName); | |
976 | tagLabel.setVisible(true); | |
977 | if (tagText.getText().trim().length() == 0) { | |
978 | tagText.setText(elementNameText.getText().trim()); | |
979 | } | |
980 | tagText.setVisible(true); | |
981 | tagText.addModifyListener(updateListener); | |
982 | actionCombo.setVisible(true); | |
983 | break; | |
984 | default: | |
985 | break; | |
986 | } | |
987 | tagComposite.layout(); | |
988 | validate(); | |
989 | updatePreviews(); | |
990 | } | |
991 | }); | |
992 | ||
993 | tagLabel = new Label(tagComposite, SWT.NULL); | |
994 | tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); | |
995 | ||
996 | tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE); | |
997 | gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
998 | gd.widthHint = 0; | |
999 | tagText.setLayoutData(gd); | |
1000 | ||
1001 | actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY); | |
1002 | actionCombo.setItems(new String[] { Messages.CustomXmlParserInputWizardPage_set, Messages.CustomXmlParserInputWizardPage_append, | |
1003 | Messages.CustomXmlParserInputWizardPage_appendWith }); | |
1004 | actionCombo.select(inputElement.inputAction); | |
1005 | actionCombo.addSelectionListener(updateListener); | |
1006 | ||
1007 | if (inputElement.inputName.equals(CustomXmlTraceDefinition.TAG_IGNORE)) { | |
1008 | tagCombo.select(0); | |
1009 | tagLabel.setVisible(false); | |
1010 | tagText.setVisible(false); | |
1011 | actionCombo.setVisible(false); | |
1012 | } else if (inputElement.inputName.equals(CustomTraceDefinition.TAG_TIMESTAMP)) { | |
1013 | tagCombo.select(1); | |
1014 | tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format); | |
1015 | tagText.setText(inputElement.inputFormat); | |
1016 | tagText.addModifyListener(updateListener); | |
1017 | } else if (inputElement.inputName.equals(CustomTraceDefinition.TAG_MESSAGE)) { | |
1018 | tagCombo.select(2); | |
1019 | tagLabel.setVisible(false); | |
1020 | tagText.setVisible(false); | |
1021 | } else { | |
1022 | tagCombo.select(3); | |
1023 | tagLabel.setText(Messages.CustomXmlParserInputWizardPage_tagName); | |
1024 | tagText.setText(inputElement.inputName); | |
1025 | tagText.addModifyListener(updateListener); | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | if (inputElement.attributes != null) { | |
1030 | for (InputAttribute inputAttribute : inputElement.attributes) { | |
1031 | Attribute attribute = new Attribute(group, this, inputAttribute, attributes.size() + 1); | |
1032 | attributes.add(attribute); | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | createAddButton(); | |
1037 | } | |
1038 | ||
1039 | private void updatePreview() { | |
1040 | Element element = getPreviewElement(inputElement); | |
1041 | if (inputElement.parentElement != null) { // no preview text for | |
1042 | // document element | |
1043 | previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingElement); | |
1044 | if (element != null) { | |
1045 | previewText.setText(CustomXmlTrace.parseElement(element, new StringBuffer()).toString()); | |
1046 | if (logEntryButton.getSelection()) { | |
1047 | if (!logEntryFound) { | |
1048 | logEntryFound = true; | |
1049 | logEntriesCount++; | |
1050 | } else { | |
1051 | logEntryButton.setSelection(false); // remove nested | |
1052 | // log entry | |
1053 | } | |
1054 | } | |
1055 | if (tagCombo.getText().equals(CustomTraceDefinition.TAG_TIMESTAMP) && logEntriesCount <= 1) { | |
1056 | String value = previewText.getText().trim(); | |
1057 | if (value.length() != 0) { | |
1058 | if (actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_SET) { | |
1059 | timeStampValue = value; | |
1060 | timeStampFormat = tagText.getText().trim(); | |
1061 | } else if (actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_APPEND) { | |
1062 | if (timeStampValue != null) { | |
1063 | timeStampValue += value; | |
1064 | timeStampFormat += tagText.getText().trim(); | |
1065 | } else { | |
1066 | timeStampValue = value; | |
1067 | timeStampFormat = tagText.getText().trim(); | |
1068 | } | |
1069 | } else if (actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) { | |
1070 | if (timeStampValue != null) { | |
1071 | timeStampValue += " | " + value; //$NON-NLS-1$ | |
1072 | timeStampFormat += " | " + tagText.getText().trim(); //$NON-NLS-1$ | |
1073 | } else { | |
1074 | timeStampValue = value; | |
1075 | timeStampFormat = tagText.getText().trim(); | |
1076 | } | |
1077 | } | |
1078 | } | |
1079 | } | |
1080 | } | |
1081 | } | |
1082 | for (Attribute attribute : attributes) { | |
1083 | if (element != null) { | |
1084 | String value = element.getAttribute(attribute.attributeNameText.getText().trim()); | |
1085 | if (value.length() != 0) { | |
1086 | attribute.previewText.setText(value); | |
1087 | if (attribute.tagCombo.getText().equals(CustomTraceDefinition.TAG_TIMESTAMP) && logEntriesCount <= 1) { | |
1088 | if (attribute.actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_SET) { | |
1089 | timeStampValue = value; | |
1090 | timeStampFormat = attribute.tagText.getText().trim(); | |
1091 | } else if (attribute.actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_APPEND) { | |
1092 | if (timeStampValue != null) { | |
1093 | timeStampValue += value; | |
1094 | timeStampFormat += attribute.tagText.getText().trim(); | |
1095 | } else { | |
1096 | timeStampValue = value; | |
1097 | timeStampFormat = attribute.tagText.getText().trim(); | |
1098 | } | |
1099 | } else if (attribute.actionCombo.getSelectionIndex() == CustomTraceDefinition.ACTION_APPEND_WITH_SEPARATOR) { | |
1100 | if (timeStampValue != null) { | |
1101 | timeStampValue += " | " + value; //$NON-NLS-1$ | |
1102 | timeStampFormat += " | " + attribute.tagText.getText().trim(); //$NON-NLS-1$ | |
1103 | } else { | |
1104 | timeStampValue = value; | |
1105 | timeStampFormat = attribute.tagText.getText().trim(); | |
1106 | } | |
1107 | } | |
1108 | } | |
1109 | } else { | |
1110 | attribute.previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingAttribute); | |
1111 | } | |
1112 | } else { | |
1113 | attribute.previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatchingElement); | |
1114 | } | |
1115 | } | |
1116 | for (ElementNode child : childElements) { | |
1117 | child.updatePreview(); | |
1118 | } | |
1119 | if (logEntryButton != null && logEntryButton.getSelection()) { | |
1120 | logEntryFound = false; | |
1121 | } | |
1122 | } | |
1123 | ||
1124 | private void createAddButton() { | |
1125 | fillerLabel = new Label(group, SWT.NONE); | |
1126 | ||
1127 | addAttributeComposite = new Composite(group, SWT.NONE); | |
1128 | addAttributeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); | |
1129 | GridLayout addAttributeLayout = new GridLayout(2, false); | |
1130 | addAttributeLayout.marginHeight = 0; | |
1131 | addAttributeLayout.marginWidth = 0; | |
1132 | addAttributeComposite.setLayout(addAttributeLayout); | |
1133 | ||
1134 | addAttributeButton = new Button(addAttributeComposite, SWT.PUSH); | |
f6aa55e2 | 1135 | addAttributeButton.setImage(ADD_IMAGE); |
be222f56 PT |
1136 | addAttributeButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_addAttribute); |
1137 | addAttributeButton.addSelectionListener(new SelectionAdapter() { | |
1138 | @Override | |
1139 | public void widgetSelected(SelectionEvent e) { | |
1140 | removeAddButton(); | |
1141 | String attributeName = getAttributeNameSuggestion(inputElement); | |
1142 | InputAttribute inputAttribute = new InputAttribute(attributeName, attributeName, 0, ""); //$NON-NLS-1$ | |
1143 | attributes.add(new Attribute(group, ElementNode.this, inputAttribute, attributes.size() + 1)); | |
1144 | createAddButton(); | |
1145 | elementContainer.layout(); | |
1146 | elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, | |
1147 | elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1); | |
1148 | group.getParent().layout(); | |
1149 | validate(); | |
1150 | updatePreviews(); | |
1151 | } | |
1152 | }); | |
1153 | ||
1154 | addAttributeLabel = new Label(addAttributeComposite, SWT.NULL); | |
1155 | addAttributeLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); | |
1156 | addAttributeLabel.setText(Messages.CustomXmlParserInputWizardPage_newAttibute); | |
1157 | } | |
1158 | ||
1159 | private void removeAddButton() { | |
1160 | fillerLabel.dispose(); | |
1161 | addAttributeComposite.dispose(); | |
1162 | } | |
1163 | ||
1164 | private void removeAttribute(int attributeNumber) { | |
41b5c37f AM |
1165 | int nb = attributeNumber; |
1166 | if (--nb < attributes.size()) { | |
1167 | attributes.remove(nb).dispose(); | |
1168 | for (int i = nb; i < attributes.size(); i++) { | |
be222f56 PT |
1169 | attributes.get(i).setAttributeNumber(i + 1); |
1170 | } | |
1171 | elementContainer.layout(); | |
1172 | elementScrolledComposite.setMinSize(elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, | |
1173 | elementContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT).y - 1); | |
1174 | group.getParent().layout(); | |
1175 | } | |
1176 | } | |
1177 | ||
1178 | private void dispose() { | |
1179 | group.dispose(); | |
1180 | } | |
1181 | ||
1182 | private void extractInputs() { | |
1183 | inputElement.elementName = elementNameText.getText().trim(); | |
1184 | if (inputElement.parentElement != null) { | |
1185 | inputElement.logEntry = logEntryButton.getSelection(); | |
1186 | if (tagCombo.getText().equals(CustomTraceDefinition.TAG_OTHER)) { | |
1187 | inputElement.inputName = tagText.getText().trim(); | |
1188 | } else { | |
1189 | inputElement.inputName = tagCombo.getText(); | |
1190 | if (tagCombo.getText().equals(CustomTraceDefinition.TAG_TIMESTAMP)) { | |
1191 | inputElement.inputFormat = tagText.getText().trim(); | |
1192 | } | |
1193 | } | |
1194 | inputElement.inputAction = actionCombo.getSelectionIndex(); | |
1195 | } | |
507b1336 | 1196 | inputElement.attributes = new ArrayList<>(attributes.size()); |
be222f56 PT |
1197 | for (int i = 0; i < attributes.size(); i++) { |
1198 | Attribute attribute = attributes.get(i); | |
1199 | InputAttribute inputAttribute = new InputAttribute(); | |
1200 | inputAttribute.attributeName = attribute.attributeNameText.getText().trim(); | |
1201 | if (attribute.tagCombo.getText().equals(CustomTraceDefinition.TAG_OTHER)) { | |
1202 | inputAttribute.inputName = attribute.tagText.getText().trim(); | |
1203 | } else { | |
1204 | inputAttribute.inputName = attribute.tagCombo.getText(); | |
1205 | if (attribute.tagCombo.getText().equals(CustomTraceDefinition.TAG_TIMESTAMP)) { | |
1206 | inputAttribute.inputFormat = attribute.tagText.getText().trim(); | |
1207 | } | |
1208 | } | |
1209 | inputAttribute.inputAction = attribute.actionCombo.getSelectionIndex(); | |
1210 | inputElement.addAttribute(inputAttribute); | |
1211 | } | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | private class Attribute { | |
f6aa55e2 PT |
1216 | private ElementNode element; |
1217 | private int attributeNumber; | |
be222f56 PT |
1218 | |
1219 | // children of parent (must be disposed) | |
f6aa55e2 PT |
1220 | private Composite labelComposite; |
1221 | private Composite attributeComposite; | |
1222 | private Label filler; | |
1223 | private Composite tagComposite; | |
be222f56 PT |
1224 | |
1225 | // children of labelComposite | |
f6aa55e2 | 1226 | private Label attributeLabel; |
be222f56 PT |
1227 | |
1228 | // children of attributeComposite | |
f6aa55e2 PT |
1229 | private Text attributeNameText; |
1230 | private Text previewText; | |
be222f56 PT |
1231 | |
1232 | // children of tagComposite | |
f6aa55e2 PT |
1233 | private Combo tagCombo; |
1234 | private Label tagLabel; | |
1235 | private Text tagText; | |
1236 | private Combo actionCombo; | |
be222f56 PT |
1237 | |
1238 | public Attribute(Composite parent, ElementNode element, InputAttribute inputAttribute, int attributeNumber) { | |
1239 | this.element = element; | |
1240 | this.attributeNumber = attributeNumber; | |
1241 | ||
1242 | labelComposite = new Composite(parent, SWT.FILL); | |
1243 | GridLayout labelLayout = new GridLayout(2, false); | |
1244 | labelLayout.marginWidth = 0; | |
1245 | labelLayout.marginHeight = 0; | |
1246 | labelComposite.setLayout(labelLayout); | |
1247 | labelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); | |
1248 | ||
1249 | Button deleteButton = new Button(labelComposite, SWT.PUSH); | |
1250 | deleteButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); | |
f6aa55e2 | 1251 | deleteButton.setImage(DELETE_IMAGE); |
be222f56 PT |
1252 | deleteButton.setToolTipText(Messages.CustomXmlParserInputWizardPage_removeAttribute); |
1253 | deleteButton.addSelectionListener(new SelectionAdapter() { | |
1254 | @Override | |
1255 | public void widgetSelected(SelectionEvent e) { | |
1256 | Attribute.this.element.removeAttribute(Attribute.this.attributeNumber); | |
1257 | validate(); | |
1258 | updatePreviews(); | |
1259 | } | |
1260 | }); | |
1261 | ||
1262 | attributeLabel = new Label(labelComposite, SWT.NULL); | |
1263 | attributeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); | |
1264 | attributeLabel.setText(Messages.CustomXmlParserInputWizardPage_attibute); | |
1265 | ||
1266 | attributeComposite = new Composite(parent, SWT.FILL); | |
1267 | GridLayout attributeLayout = new GridLayout(4, false); | |
1268 | attributeLayout.marginWidth = 0; | |
1269 | attributeLayout.marginHeight = 0; | |
1270 | attributeComposite.setLayout(attributeLayout); | |
1271 | attributeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); | |
1272 | ||
1273 | Label nameLabel = new Label(attributeComposite, SWT.NONE); | |
1274 | nameLabel.setText(Messages.CustomXmlParserInputWizardPage_name); | |
1275 | ||
1276 | attributeNameText = new Text(attributeComposite, SWT.BORDER | SWT.SINGLE); | |
1277 | attributeNameText.setLayoutData(new GridData(120, SWT.DEFAULT)); | |
1278 | attributeNameText.setText(inputAttribute.attributeName); | |
1279 | attributeNameText.addModifyListener(updateListener); | |
1280 | ||
1281 | Label previewLabel = new Label(attributeComposite, SWT.NONE); | |
1282 | previewLabel.setText(Messages.CustomXmlParserInputWizardPage_preview); | |
1283 | ||
1284 | previewText = new Text(attributeComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY); | |
1285 | GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
1286 | gd.widthHint = 0; | |
1287 | previewText.setLayoutData(gd); | |
1288 | previewText.setText(Messages.CustomXmlParserInputWizardPage_noMatch); | |
1289 | previewText.setBackground(COLOR_WIDGET_BACKGROUND); | |
1290 | ||
1291 | filler = new Label(parent, SWT.NULL); | |
1292 | ||
1293 | tagComposite = new Composite(parent, SWT.FILL); | |
1294 | GridLayout tagLayout = new GridLayout(4, false); | |
1295 | tagLayout.marginWidth = 0; | |
1296 | tagLayout.marginHeight = 0; | |
1297 | tagComposite.setLayout(tagLayout); | |
1298 | tagComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); | |
1299 | ||
1300 | tagCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY); | |
1301 | tagCombo.setItems(new String[] { CustomTraceDefinition.TAG_TIMESTAMP, CustomTraceDefinition.TAG_MESSAGE, | |
1302 | CustomTraceDefinition.TAG_OTHER }); | |
1303 | tagCombo.select(2); // Other | |
1304 | tagCombo.addSelectionListener(new SelectionListener() { | |
1305 | @Override | |
1306 | public void widgetDefaultSelected(SelectionEvent e) { | |
1307 | } | |
1308 | ||
1309 | @Override | |
1310 | public void widgetSelected(SelectionEvent e) { | |
1311 | tagText.removeModifyListener(updateListener); | |
1312 | switch (tagCombo.getSelectionIndex()) { | |
1313 | case 0: // Time Stamp | |
1314 | tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format); | |
1315 | tagLabel.setVisible(true); | |
1316 | tagText.setVisible(true); | |
1317 | tagText.addModifyListener(updateListener); | |
1318 | break; | |
1319 | case 1: // Message | |
1320 | tagLabel.setVisible(false); | |
1321 | tagText.setVisible(false); | |
1322 | break; | |
1323 | case 2: // Other | |
1324 | tagLabel.setText(Messages.CustomXmlParserInputWizardPage_tagName); | |
1325 | tagLabel.setVisible(true); | |
1326 | if (tagText.getText().trim().length() == 0) { | |
1327 | tagText.setText(attributeNameText.getText().trim()); | |
1328 | } | |
1329 | tagText.setVisible(true); | |
1330 | tagText.addModifyListener(updateListener); | |
1331 | break; | |
1332 | default: | |
1333 | break; | |
1334 | } | |
1335 | tagComposite.layout(); | |
1336 | validate(); | |
1337 | updatePreviews(); | |
1338 | } | |
1339 | }); | |
1340 | ||
1341 | tagLabel = new Label(tagComposite, SWT.NULL); | |
1342 | tagLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); | |
1343 | ||
1344 | tagText = new Text(tagComposite, SWT.BORDER | SWT.SINGLE); | |
1345 | gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
1346 | gd.widthHint = 0; | |
1347 | tagText.setLayoutData(gd); | |
1348 | tagText.setText(attributeNameText.getText()); | |
1349 | ||
1350 | actionCombo = new Combo(tagComposite, SWT.DROP_DOWN | SWT.READ_ONLY); | |
1351 | actionCombo.setItems(new String[] { Messages.CustomXmlParserInputWizardPage_set, Messages.CustomXmlParserInputWizardPage_append, | |
1352 | Messages.CustomXmlParserInputWizardPage_appendWith }); | |
1353 | actionCombo.select(inputAttribute.inputAction); | |
1354 | actionCombo.addSelectionListener(updateListener); | |
1355 | ||
1356 | if (inputAttribute.inputName.equals(CustomTraceDefinition.TAG_TIMESTAMP)) { | |
1357 | tagCombo.select(0); | |
1358 | tagLabel.setText(Messages.CustomXmlParserInputWizardPage_format); | |
1359 | tagText.setText(inputAttribute.inputFormat); | |
1360 | tagText.addModifyListener(updateListener); | |
1361 | } else if (inputAttribute.inputName.equals(CustomTraceDefinition.TAG_MESSAGE)) { | |
1362 | tagCombo.select(1); | |
1363 | tagLabel.setVisible(false); | |
1364 | tagText.setVisible(false); | |
1365 | } else { | |
1366 | tagCombo.select(2); | |
1367 | tagLabel.setText(Messages.CustomXmlParserInputWizardPage_tagName); | |
1368 | tagText.setText(inputAttribute.inputName); | |
1369 | tagText.addModifyListener(updateListener); | |
1370 | } | |
1371 | } | |
1372 | ||
1373 | private void dispose() { | |
1374 | labelComposite.dispose(); | |
1375 | attributeComposite.dispose(); | |
1376 | filler.dispose(); | |
1377 | tagComposite.dispose(); | |
1378 | } | |
1379 | ||
1380 | private void setAttributeNumber(int attributeNumber) { | |
1381 | this.attributeNumber = attributeNumber; | |
1382 | labelComposite.layout(); | |
1383 | } | |
1384 | } | |
1385 | ||
1386 | private Element getPreviewElement(InputElement inputElement) { | |
41b5c37f | 1387 | InputElement currentElement = inputElement; |
be222f56 PT |
1388 | Element element = documentElement; |
1389 | if (element != null) { | |
1390 | if (!documentElement.getNodeName().equals(definition.rootInputElement.elementName)) { | |
1391 | return null; | |
1392 | } | |
507b1336 | 1393 | ArrayList<String> elementNames = new ArrayList<>(); |
41b5c37f AM |
1394 | while (currentElement != null) { |
1395 | elementNames.add(currentElement.elementName); | |
1396 | currentElement = currentElement.parentElement; | |
be222f56 PT |
1397 | } |
1398 | for (int i = elementNames.size() - 1; --i >= 0;) { | |
1399 | NodeList childList = element.getChildNodes(); | |
1400 | element = null; | |
1401 | for (int j = 0; j < childList.getLength(); j++) { | |
1402 | Node child = childList.item(j); | |
1403 | if (child instanceof Element && child.getNodeName().equals(elementNames.get(i))) { | |
1404 | element = (Element) child; | |
1405 | break; | |
1406 | } | |
1407 | } | |
1408 | if (element == null) { | |
1409 | break; | |
1410 | } | |
1411 | } | |
1412 | if (element != null) { | |
1413 | return element; | |
1414 | } | |
1415 | } | |
1416 | return null; | |
1417 | } | |
1418 | ||
1419 | private String getChildNameSuggestion(InputElement inputElement) { | |
1420 | if (inputElement == null) { | |
1421 | if (documentElement != null) { | |
1422 | return documentElement.getNodeName(); | |
1423 | } | |
1424 | } else { | |
1425 | Element element = getPreviewElement(inputElement); | |
1426 | if (element != null) { | |
1427 | NodeList childNodes = element.getChildNodes(); | |
1428 | for (int i = 0; i < childNodes.getLength(); i++) { | |
1429 | Node node = childNodes.item(i); | |
1430 | if (node instanceof Element) { | |
1431 | boolean unused = true; | |
1432 | if (inputElement.childElements != null) { | |
1433 | for (InputElement child : inputElement.childElements) { | |
1434 | if (child.elementName.equals(node.getNodeName())) { | |
1435 | unused = false; | |
1436 | break; | |
1437 | } | |
1438 | } | |
1439 | } | |
1440 | if (unused) { | |
1441 | return node.getNodeName(); | |
1442 | } | |
1443 | } | |
1444 | } | |
1445 | } | |
1446 | } | |
1447 | return ""; //$NON-NLS-1$ | |
1448 | } | |
1449 | ||
1450 | private String getAttributeNameSuggestion(InputElement inputElement) { | |
1451 | Element element = getPreviewElement(inputElement); | |
1452 | if (element != null) { | |
1453 | NamedNodeMap attributeMap = element.getAttributes(); | |
1454 | for (int i = 0; i < attributeMap.getLength(); i++) { | |
1455 | Node node = attributeMap.item(i); | |
1456 | boolean unused = true; | |
1457 | if (inputElement.attributes != null) { | |
1458 | for (InputAttribute attribute : inputElement.attributes) { | |
1459 | if (attribute.attributeName.equals(node.getNodeName())) { | |
1460 | unused = false; | |
1461 | break; | |
1462 | } | |
1463 | } | |
1464 | } | |
1465 | if (unused) { | |
1466 | return node.getNodeName(); | |
1467 | } | |
1468 | } | |
1469 | } | |
1470 | return ""; //$NON-NLS-1$ | |
1471 | } | |
1472 | ||
1473 | private void validate() { | |
1474 | definition.definitionName = logtypeText.getText().trim(); | |
1475 | definition.timeStampOutputFormat = timeStampOutputFormatText.getText().trim(); | |
1476 | ||
1477 | if (selectedElement != null) { | |
1478 | selectedElement.extractInputs(); | |
1479 | treeViewer.refresh(); | |
1480 | } | |
1481 | ||
1482 | StringBuffer errors = new StringBuffer(); | |
1483 | ||
1484 | if (definition.definitionName.length() == 0) { | |
1485 | errors.append(Messages.CustomXmlParserInputWizardPage_emptyLogTypeError); | |
1486 | logtypeText.setBackground(COLOR_LIGHT_RED); | |
1487 | } else { | |
1488 | logtypeText.setBackground(COLOR_TEXT_BACKGROUND); | |
1489 | for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) { | |
f6aa55e2 PT |
1490 | if (definition.definitionName.equals(def.definitionName) && |
1491 | (editDefinitionName == null || !editDefinitionName.equals(definition.definitionName))) { | |
1492 | errors.append(Messages.CustomXmlParserInputWizardPage_duplicatelogTypeError); | |
1493 | logtypeText.setBackground(COLOR_LIGHT_RED); | |
1494 | break; | |
be222f56 PT |
1495 | } |
1496 | } | |
1497 | } | |
1498 | ||
1499 | if (definition.rootInputElement == null) { | |
1500 | errors.append(Messages.CustomXmlParserInputWizardPage_noDocumentError); | |
1501 | } | |
1502 | ||
1503 | if (definition.rootInputElement != null) { | |
1504 | logEntryFound = false; | |
1505 | timeStampFound = false; | |
1506 | ||
1507 | errors.append(validateElement(definition.rootInputElement)); | |
1508 | ||
1509 | if ((definition.rootInputElement.attributes != null && definition.rootInputElement.attributes.size() != 0) | |
1510 | || (definition.rootInputElement.childElements != null && definition.rootInputElement.childElements.size() != 0) | |
1511 | || errors.length() == 0) { | |
1512 | if (!logEntryFound) { | |
1513 | errors.append(Messages.CustomXmlParserInputWizardPage_missingLogEntryError); | |
1514 | } | |
1515 | ||
1516 | if (timeStampFound) { | |
1517 | if (timeStampOutputFormatText.getText().trim().length() == 0) { | |
1518 | errors.append(Messages.CustomXmlParserInputWizardPage_missingTimestampFmtError); | |
1519 | timeStampOutputFormatText.setBackground(COLOR_LIGHT_RED); | |
1520 | } else { | |
1521 | try { | |
738beb68 | 1522 | new TmfTimestampFormat(timeStampOutputFormatText.getText().trim()); |
be222f56 PT |
1523 | timeStampOutputFormatText.setBackground(COLOR_TEXT_BACKGROUND); |
1524 | } catch (IllegalArgumentException e) { | |
1525 | errors.append(Messages.CustomXmlParserInputWizardPage_invalidTimestampFmtError); | |
1526 | timeStampOutputFormatText.setBackground(COLOR_LIGHT_RED); | |
1527 | } | |
1528 | } | |
1529 | } else { | |
1530 | timeStampPreviewText.setText(Messages.CustomXmlParserInputWizardPage_notimestamporAttributeError); | |
1531 | } | |
1532 | } | |
1533 | } else { | |
1534 | timeStampPreviewText.setText(Messages.CustomXmlParserInputWizardPage_notimestamporAttributeError); | |
1535 | } | |
1536 | ||
1537 | if (errors.length() == 0) { | |
1538 | setDescription(defaultDescription); | |
1539 | setPageComplete(true); | |
1540 | } else { | |
1541 | setDescription(errors.toString()); | |
1542 | setPageComplete(false); | |
1543 | } | |
1544 | } | |
1545 | ||
a0a88f65 AM |
1546 | /** |
1547 | * Clean up the specified XML element. | |
1548 | * | |
1549 | * @param inputElement | |
1550 | * The element to clean up | |
1551 | * @return The validated element | |
1552 | */ | |
be222f56 PT |
1553 | public StringBuffer validateElement(InputElement inputElement) { |
1554 | StringBuffer errors = new StringBuffer(); | |
1555 | ElementNode elementNode = null; | |
1556 | if (selectedElement != null && selectedElement.inputElement.equals(inputElement)) { | |
1557 | elementNode = selectedElement; | |
1558 | } | |
1559 | if (inputElement == definition.rootInputElement) { | |
1560 | if (inputElement.elementName.length() == 0) { | |
1561 | errors.append(Messages.CustomXmlParserInputWizardPage_missingDocumentElementError); | |
1562 | if (elementNode != null) { | |
1563 | elementNode.elementNameText.setBackground(COLOR_LIGHT_RED); | |
1564 | } | |
1565 | } else { | |
1566 | if (elementNode != null) { | |
1567 | elementNode.elementNameText.setBackground(COLOR_TEXT_BACKGROUND); | |
1568 | } | |
1569 | } | |
1570 | } | |
1571 | if (inputElement != definition.rootInputElement) { | |
1572 | if (inputElement.logEntry) { | |
1573 | logEntryFound = true; | |
1574 | } | |
1575 | if (inputElement.inputName.equals(CustomTraceDefinition.TAG_TIMESTAMP)) { | |
1576 | timeStampFound = true; | |
1577 | if (inputElement.inputFormat.length() == 0) { | |
1578 | errors.append(Messages.CustomXmlParserInputWizardPage_timestampFormatPrompt | |
1579 | + " (" + Messages.CustomXmlParserInputWizardPage_timestampElementPrompt + " " + getName(inputElement) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
1580 | if (elementNode != null) { | |
1581 | elementNode.tagText.setBackground(COLOR_LIGHT_RED); | |
1582 | } | |
1583 | } else { | |
1584 | try { | |
738beb68 | 1585 | new TmfTimestampFormat(inputElement.inputFormat); |
be222f56 PT |
1586 | if (elementNode != null) { |
1587 | elementNode.tagText.setBackground(COLOR_TEXT_BACKGROUND); | |
1588 | } | |
1589 | } catch (IllegalArgumentException e) { | |
1590 | errors.append(Messages.CustomXmlParserInputWizardPage_invalidTimestampFmtError | |
1591 | + " (" + Messages.CustomXmlParserInputWizardPage_timestampElementPrompt + " " + getName(inputElement) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
1592 | if (elementNode != null) { | |
1593 | elementNode.tagText.setBackground(COLOR_LIGHT_RED); | |
1594 | } | |
1595 | } | |
1596 | } | |
1597 | } else if (inputElement.inputName.length() == 0) { | |
1598 | errors.append(Messages.CustomXmlParserInputWizardPage_missingInputElementNameError); | |
1599 | if (elementNode != null) { | |
1600 | elementNode.tagText.setBackground(COLOR_LIGHT_RED); | |
1601 | } | |
1602 | } else { | |
1603 | if (elementNode != null) { | |
1604 | elementNode.tagText.setBackground(COLOR_TEXT_BACKGROUND); | |
1605 | } | |
1606 | } | |
1607 | } | |
1608 | if (inputElement.attributes != null) { | |
1609 | if (elementNode != null) { | |
1610 | for (Attribute attribute : elementNode.attributes) { | |
1611 | attribute.attributeNameText.setBackground(COLOR_TEXT_BACKGROUND); | |
1612 | } | |
1613 | } | |
1614 | for (int i = 0; i < inputElement.attributes.size(); i++) { | |
1615 | InputAttribute attribute = inputElement.attributes.get(i); | |
1616 | boolean duplicate = false; | |
1617 | for (int j = i + 1; j < inputElement.attributes.size(); j++) { | |
1618 | InputAttribute otherAttribute = inputElement.attributes.get(j); | |
1619 | if (otherAttribute.attributeName.equals(attribute.attributeName)) { | |
1620 | duplicate = true; | |
1621 | if (elementNode != null) { | |
1622 | elementNode.attributes.get(j).attributeNameText.setBackground(COLOR_LIGHT_RED); | |
1623 | } | |
1624 | } | |
1625 | } | |
1626 | if (attribute.attributeName.length() == 0) { | |
1627 | errors.append(Messages.CustomXmlParserInputWizardPage_missingAttribute | |
1628 | + " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(inputElement) + ": ?). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
1629 | if (elementNode != null) { | |
1630 | elementNode.attributes.get(i).attributeNameText.setBackground(COLOR_LIGHT_RED); | |
1631 | } | |
1632 | } else if (duplicate) { | |
1633 | errors.append(Messages.CustomXmlParserInputWizardPage_duplicateAttributeError | |
1634 | + " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(attribute, inputElement) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
1635 | if (elementNode != null) { | |
1636 | elementNode.attributes.get(i).attributeNameText.setBackground(COLOR_LIGHT_RED); | |
1637 | } | |
1638 | } | |
1639 | if (attribute.inputName.equals(CustomTraceDefinition.TAG_TIMESTAMP)) { | |
1640 | timeStampFound = true; | |
1641 | if (attribute.inputFormat.length() == 0) { | |
1642 | errors.append(Messages.CustomXmlParserInputWizardPage_missingTimestampInFmtError | |
1643 | + " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(attribute, inputElement) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
1644 | if (elementNode != null) { | |
1645 | elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED); | |
1646 | } | |
1647 | } else { | |
1648 | try { | |
738beb68 | 1649 | new TmfTimestampFormat(attribute.inputFormat); |
be222f56 PT |
1650 | if (elementNode != null) { |
1651 | elementNode.attributes.get(i).tagText.setBackground(COLOR_TEXT_BACKGROUND); | |
1652 | } | |
1653 | } catch (IllegalArgumentException e) { | |
1654 | errors.append(Messages.CustomXmlParserInputWizardPage_invalidTimestampInFmtError | |
1655 | + " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(attribute, inputElement) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
1656 | if (elementNode != null) { | |
1657 | elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED); | |
1658 | } | |
1659 | } | |
1660 | } | |
1661 | } else if (attribute.inputName.length() == 0) { | |
1662 | errors.append(Messages.CustomXmlParserInputWizardPage_missingDataGroupNameError | |
1663 | + " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(attribute, inputElement) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
1664 | if (elementNode != null) { | |
1665 | elementNode.attributes.get(i).tagText.setBackground(COLOR_LIGHT_RED); | |
1666 | } | |
1667 | } else { | |
1668 | if (elementNode != null) { | |
1669 | elementNode.attributes.get(i).tagText.setBackground(COLOR_TEXT_BACKGROUND); | |
1670 | } | |
1671 | } | |
1672 | } | |
1673 | } | |
1674 | if (inputElement.childElements != null) { | |
1675 | for (InputElement child : inputElement.childElements) { | |
1676 | ElementNode childElementNode = null; | |
1677 | if (selectedElement != null && selectedElement.inputElement.equals(child)) { | |
1678 | childElementNode = selectedElement; | |
1679 | } | |
1680 | if (childElementNode != null) { | |
1681 | childElementNode.elementNameText.setBackground(COLOR_TEXT_BACKGROUND); | |
1682 | } | |
1683 | } | |
1684 | for (int i = 0; i < inputElement.childElements.size(); i++) { | |
1685 | InputElement child = inputElement.childElements.get(i); | |
1686 | ElementNode childElementNode = null; | |
1687 | if (selectedElement != null && selectedElement.inputElement.equals(child)) { | |
1688 | childElementNode = selectedElement; | |
1689 | } | |
1690 | if (child.elementName.length() == 0) { | |
1691 | errors.append(Messages.CustomXmlParserInputWizardPage_missingElementNameError | |
1692 | + " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(child) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
1693 | if (childElementNode != null) { | |
1694 | childElementNode.elementNameText.setBackground(COLOR_LIGHT_RED); | |
1695 | } | |
1696 | } else { | |
1697 | boolean duplicate = false; | |
1698 | for (int j = i + 1; j < inputElement.childElements.size(); j++) { | |
1699 | InputElement otherChild = inputElement.childElements.get(j); | |
1700 | if (otherChild.elementName.equals(child.elementName)) { | |
1701 | duplicate = true; | |
1702 | ElementNode otherChildElementNode = null; | |
1703 | if (selectedElement != null && selectedElement.inputElement.equals(otherChild)) { | |
1704 | otherChildElementNode = selectedElement; | |
1705 | } | |
1706 | if (otherChildElementNode != null) { | |
1707 | otherChildElementNode.elementNameText.setBackground(COLOR_LIGHT_RED); | |
1708 | } | |
1709 | } | |
1710 | } | |
1711 | if (duplicate) { | |
1712 | errors.append(Messages.CustomXmlParserInputWizardPage_duplicateElementNameError | |
1713 | + " (" + Messages.CustomXmlParserInputWizardPage_attributePrompt + " " + getName(child) + "). "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
1714 | if (childElementNode != null) { | |
1715 | childElementNode.elementNameText.setBackground(COLOR_LIGHT_RED); | |
1716 | } | |
1717 | } | |
1718 | } | |
1719 | ||
1720 | errors.append(validateElement(child)); | |
1721 | } | |
1722 | } | |
1723 | return errors; | |
1724 | } | |
1725 | ||
a0a88f65 AM |
1726 | /** |
1727 | * Get the trace definition. | |
1728 | * | |
1729 | * @return The trace definition | |
1730 | */ | |
be222f56 PT |
1731 | public CustomXmlTraceDefinition getDefinition() { |
1732 | return definition; | |
1733 | } | |
1734 | ||
a0a88f65 AM |
1735 | /** |
1736 | * Get the raw text input. | |
1737 | * | |
1738 | * @return The raw text input. | |
1739 | */ | |
be222f56 PT |
1740 | public char[] getInputText() { |
1741 | return inputText.getText().toCharArray(); | |
1742 | } | |
1743 | } |