Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
ca2ff0c7 | 2 | * Copyright (c) 2010, 2015 Ericsson |
be222f56 PT |
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: | |
10 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.internal.tmf.ui.dialogs; |
be222f56 PT |
14 | |
15 | import org.eclipse.jface.dialogs.Dialog; | |
16 | import org.eclipse.jface.dialogs.IDialogConstants; | |
17 | import org.eclipse.jface.dialogs.MessageDialog; | |
18 | import org.eclipse.jface.window.Window; | |
19 | import org.eclipse.jface.wizard.WizardDialog; | |
9753257b | 20 | import org.eclipse.osgi.util.NLS; |
be222f56 PT |
21 | import org.eclipse.swt.SWT; |
22 | import org.eclipse.swt.events.SelectionEvent; | |
23 | import org.eclipse.swt.events.SelectionListener; | |
24 | import org.eclipse.swt.graphics.Image; | |
25 | import org.eclipse.swt.layout.GridData; | |
26 | import org.eclipse.swt.layout.GridLayout; | |
27 | import org.eclipse.swt.widgets.Button; | |
28 | import org.eclipse.swt.widgets.Composite; | |
29 | import org.eclipse.swt.widgets.Control; | |
30 | import org.eclipse.swt.widgets.Display; | |
31 | import org.eclipse.swt.widgets.FileDialog; | |
32 | import org.eclipse.swt.widgets.Label; | |
33 | import org.eclipse.swt.widgets.List; | |
34 | import org.eclipse.swt.widgets.Shell; | |
2bdf0193 AM |
35 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
36 | import org.eclipse.tracecompass.internal.tmf.ui.Messages; | |
ca2ff0c7 | 37 | import org.eclipse.tracecompass.internal.tmf.ui.parsers.CustomParserUtils; |
2bdf0193 AM |
38 | import org.eclipse.tracecompass.internal.tmf.ui.parsers.wizards.CustomTxtParserWizard; |
39 | import org.eclipse.tracecompass.internal.tmf.ui.parsers.wizards.CustomXmlParserWizard; | |
40 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition; | |
ca2ff0c7 | 41 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace; |
2bdf0193 | 42 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition; |
ca2ff0c7 | 43 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace; |
2bdf0193 AM |
44 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition; |
45 | import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType; | |
46 | import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper; | |
be222f56 | 47 | |
a0a88f65 AM |
48 | /** |
49 | * Dialog for custom text parsers. | |
50 | * | |
51 | * @author Patrick Tassé | |
52 | */ | |
be222f56 PT |
53 | public class ManageCustomParsersDialog extends Dialog { |
54 | ||
c22ca172 PT |
55 | private static final String SEP = " : "; //$NON-NLS-1$ |
56 | private static final int SEP_LEN = SEP.length(); | |
57 | ||
be222f56 PT |
58 | private static final Image image = Activator.getDefault().getImageFromPath("/icons/etool16/customparser_wizard.gif"); //$NON-NLS-1$ |
59 | ||
60 | Button txtButton; | |
61 | Button xmlButton; | |
62 | List parserList; | |
63 | Button newButton; | |
64 | Button editButton; | |
65 | Button deleteButton; | |
66 | Button importButton; | |
67 | Button exportButton; | |
68 | ||
a0a88f65 AM |
69 | /** |
70 | * Constructor | |
71 | * | |
72 | * @param parent | |
73 | * Parent shell of this dialog | |
74 | */ | |
be222f56 PT |
75 | public ManageCustomParsersDialog(Shell parent) { |
76 | super(parent); | |
77 | setShellStyle(SWT.RESIZE | SWT.MAX | getShellStyle()); | |
78 | } | |
79 | ||
be222f56 PT |
80 | @Override |
81 | protected Control createDialogArea(Composite parent) { | |
82 | getShell().setText(Messages.ManageCustomParsersDialog_DialogHeader); | |
83 | getShell().setImage(image); | |
84 | ||
85 | Composite composite = (Composite) super.createDialogArea(parent); | |
86 | composite.setLayout(new GridLayout(2, false)); | |
87 | ||
88 | Composite listContainer = new Composite(composite, SWT.NONE); | |
89 | listContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
90 | GridLayout lcgl = new GridLayout(); | |
91 | lcgl.marginHeight = 0; | |
92 | lcgl.marginWidth = 0; | |
93 | listContainer.setLayout(lcgl); | |
94 | ||
95 | Composite radioContainer = new Composite(listContainer, SWT.NONE); | |
96 | GridLayout rcgl = new GridLayout(2, true); | |
97 | rcgl.marginHeight = 0; | |
98 | rcgl.marginWidth = 0; | |
99 | radioContainer.setLayout(rcgl); | |
100 | ||
101 | txtButton = new Button(radioContainer, SWT.RADIO); | |
102 | txtButton.setText(Messages.ManageCustomParsersDialog_TextButtonLabel); | |
103 | txtButton.setSelection(true); | |
a0a88f65 | 104 | txtButton.addSelectionListener(new SelectionListener() { |
be222f56 | 105 | @Override |
a0a88f65 AM |
106 | public void widgetDefaultSelected(SelectionEvent e) {} |
107 | ||
be222f56 | 108 | @Override |
a0a88f65 | 109 | public void widgetSelected(SelectionEvent e) { |
be222f56 | 110 | fillParserList(); |
a0a88f65 AM |
111 | } |
112 | }); | |
be222f56 PT |
113 | |
114 | xmlButton = new Button(radioContainer, SWT.RADIO); | |
115 | xmlButton.setText("XML"); //$NON-NLS-1$ | |
a0a88f65 | 116 | xmlButton.addSelectionListener(new SelectionListener() { |
be222f56 | 117 | @Override |
a0a88f65 AM |
118 | public void widgetDefaultSelected(SelectionEvent e) { |
119 | } | |
120 | ||
be222f56 | 121 | @Override |
a0a88f65 | 122 | public void widgetSelected(SelectionEvent e) { |
be222f56 | 123 | fillParserList(); |
a0a88f65 AM |
124 | } |
125 | }); | |
be222f56 PT |
126 | |
127 | parserList = new List(listContainer, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); | |
128 | parserList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
a0a88f65 | 129 | parserList.addSelectionListener(new SelectionListener() { |
be222f56 | 130 | @Override |
a0a88f65 AM |
131 | public void widgetDefaultSelected(SelectionEvent e) {} |
132 | ||
be222f56 | 133 | @Override |
a0a88f65 | 134 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
135 | if (parserList.getSelectionCount() == 0) { |
136 | editButton.setEnabled(false); | |
137 | deleteButton.setEnabled(false); | |
138 | exportButton.setEnabled(false); | |
139 | } else { | |
140 | editButton.setEnabled(true); | |
141 | deleteButton.setEnabled(true); | |
142 | exportButton.setEnabled(true); | |
143 | } | |
a0a88f65 AM |
144 | } |
145 | }); | |
be222f56 PT |
146 | |
147 | Composite buttonContainer = new Composite(composite, SWT.NULL); | |
148 | buttonContainer.setLayout(new GridLayout()); | |
149 | buttonContainer.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false)); | |
150 | ||
151 | newButton = new Button(buttonContainer, SWT.PUSH); | |
152 | newButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
153 | newButton.setText(Messages.ManageCustomParsersDialog_NewButtonLabel); | |
a0a88f65 | 154 | newButton.addSelectionListener(new SelectionListener() { |
be222f56 | 155 | @Override |
a0a88f65 AM |
156 | public void widgetDefaultSelected(SelectionEvent e) {} |
157 | ||
be222f56 | 158 | @Override |
a0a88f65 | 159 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
160 | WizardDialog dialog = null; |
161 | if (txtButton.getSelection()) { | |
162 | dialog = new WizardDialog(getShell(), new CustomTxtParserWizard()); | |
163 | } else if (xmlButton.getSelection()) { | |
164 | dialog = new WizardDialog(getShell(), new CustomXmlParserWizard()); | |
165 | } | |
166 | if (dialog != null) { | |
167 | dialog.open(); | |
168 | if (dialog.getReturnCode() == Window.OK) { | |
169 | fillParserList(); | |
170 | } | |
171 | } | |
a0a88f65 AM |
172 | } |
173 | }); | |
be222f56 PT |
174 | |
175 | editButton = new Button(buttonContainer, SWT.PUSH); | |
176 | editButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
177 | editButton.setText(Messages.ManageCustomParsersDialog_EditButtonLabel); | |
178 | editButton.setEnabled(false); | |
a0a88f65 | 179 | editButton.addSelectionListener(new SelectionListener() { |
be222f56 | 180 | @Override |
a0a88f65 AM |
181 | public void widgetDefaultSelected(SelectionEvent e) {} |
182 | ||
be222f56 | 183 | @Override |
a0a88f65 | 184 | public void widgetSelected(SelectionEvent e) { |
be222f56 | 185 | WizardDialog dialog = null; |
c22ca172 PT |
186 | String selection = parserList.getSelection()[0]; |
187 | String category = selection.substring(0, selection.indexOf(SEP)); | |
188 | String name = selection.substring(selection.indexOf(SEP) + SEP_LEN); | |
be222f56 PT |
189 | if (txtButton.getSelection()) { |
190 | dialog = new WizardDialog(getShell(), | |
c22ca172 | 191 | new CustomTxtParserWizard(CustomTxtTraceDefinition.load(category, name))); |
be222f56 PT |
192 | } else if (xmlButton.getSelection()) { |
193 | dialog = new WizardDialog(getShell(), | |
c22ca172 | 194 | new CustomXmlParserWizard(CustomXmlTraceDefinition.load(category, name))); |
be222f56 PT |
195 | } |
196 | if (dialog != null) { | |
197 | dialog.open(); | |
198 | if (dialog.getReturnCode() == Window.OK) { | |
199 | fillParserList(); | |
200 | } | |
201 | } | |
a0a88f65 AM |
202 | } |
203 | }); | |
be222f56 PT |
204 | |
205 | deleteButton = new Button(buttonContainer, SWT.PUSH); | |
206 | deleteButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
207 | deleteButton.setText(Messages.ManageCustomParsersDialog_DeleteButtonLabel); | |
208 | deleteButton.setEnabled(false); | |
a0a88f65 | 209 | deleteButton.addSelectionListener(new SelectionListener() { |
be222f56 | 210 | @Override |
a0a88f65 AM |
211 | public void widgetDefaultSelected(SelectionEvent e) {} |
212 | ||
be222f56 | 213 | @Override |
a0a88f65 | 214 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
215 | boolean confirm = MessageDialog.openQuestion( |
216 | getShell(), | |
217 | Messages.ManageCustomParsersDialog_DeleteParserDialogHeader, | |
89151d6a | 218 | NLS.bind(Messages.ManageCustomParsersDialog_DeleteConfirmation, parserList.getSelection()[0])); |
be222f56 | 219 | if (confirm) { |
c22ca172 PT |
220 | String selection = parserList.getSelection()[0]; |
221 | String category = selection.substring(0, selection.indexOf(SEP)); | |
222 | String name = selection.substring(selection.indexOf(SEP) + SEP_LEN); | |
be222f56 | 223 | if (txtButton.getSelection()) { |
c22ca172 | 224 | CustomTxtTraceDefinition.delete(category, name); |
ca2ff0c7 | 225 | CustomParserUtils.cleanup(CustomTxtTrace.buildTraceTypeId(category, name)); |
be222f56 | 226 | } else if (xmlButton.getSelection()) { |
c22ca172 | 227 | CustomXmlTraceDefinition.delete(category, name); |
ca2ff0c7 | 228 | CustomParserUtils.cleanup(CustomXmlTrace.buildTraceTypeId(category, name)); |
be222f56 PT |
229 | } |
230 | fillParserList(); | |
231 | } | |
a0a88f65 AM |
232 | } |
233 | }); | |
be222f56 PT |
234 | |
235 | new Label(buttonContainer, SWT.NONE); // filler | |
236 | ||
237 | importButton = new Button(buttonContainer, SWT.PUSH); | |
238 | importButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
239 | importButton.setText(Messages.ManageCustomParsersDialog_ImportButtonLabel); | |
a0a88f65 | 240 | importButton.addSelectionListener(new SelectionListener() { |
be222f56 | 241 | @Override |
a0a88f65 AM |
242 | public void widgetDefaultSelected(SelectionEvent e) {} |
243 | ||
be222f56 | 244 | @Override |
a0a88f65 | 245 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
246 | FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN); |
247 | dialog.setText(Messages.ManageCustomParsersDialog_ImportParserSelection); | |
a0a88f65 | 248 | dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ |
be222f56 PT |
249 | String path = dialog.open(); |
250 | if (path != null) { | |
251 | CustomTraceDefinition[] defs = null; | |
252 | if (txtButton.getSelection()) { | |
253 | defs = CustomTxtTraceDefinition.loadAll(path); | |
254 | } else if (xmlButton.getSelection()) { | |
255 | defs = CustomXmlTraceDefinition.loadAll(path); | |
256 | } | |
257 | if (defs != null && defs.length > 0) { | |
258 | for (CustomTraceDefinition def : defs) { | |
9753257b PT |
259 | boolean ok = checkNameConflict(def); |
260 | if (ok) { | |
261 | def.save(); | |
262 | } | |
be222f56 PT |
263 | } |
264 | fillParserList(); | |
d1b0903f PT |
265 | } else { |
266 | MessageDialog.openInformation(Display.getCurrent().getActiveShell(), | |
267 | Messages.ManageCustomParsersDialog_ImportFailureTitle, | |
268 | Messages.ManageCustomParsersDialog_ImportFailureMessage); | |
be222f56 PT |
269 | } |
270 | } | |
a0a88f65 AM |
271 | } |
272 | }); | |
be222f56 PT |
273 | |
274 | exportButton = new Button(buttonContainer, SWT.PUSH); | |
275 | exportButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
276 | exportButton.setText(Messages.ManageCustomParsersDialog_ExportButtonLabel); | |
277 | exportButton.setEnabled(false); | |
a0a88f65 | 278 | exportButton.addSelectionListener(new SelectionListener() { |
be222f56 | 279 | @Override |
a0a88f65 AM |
280 | public void widgetDefaultSelected(SelectionEvent e) {} |
281 | ||
be222f56 | 282 | @Override |
a0a88f65 | 283 | public void widgetSelected(SelectionEvent e) { |
be222f56 | 284 | FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE); |
89151d6a | 285 | dialog.setText(NLS.bind(Messages.ManageCustomParsersDialog_ExportParserSelection, parserList.getSelection()[0])); |
a0a88f65 | 286 | dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ |
be222f56 PT |
287 | String path = dialog.open(); |
288 | if (path != null) { | |
c22ca172 PT |
289 | String selection = parserList.getSelection()[0]; |
290 | String category = selection.substring(0, selection.indexOf(SEP)); | |
291 | String name = selection.substring(selection.indexOf(SEP) + SEP_LEN); | |
be222f56 PT |
292 | CustomTraceDefinition def = null; |
293 | if (txtButton.getSelection()) { | |
c22ca172 | 294 | def = CustomTxtTraceDefinition.load(category, name); |
be222f56 | 295 | } else if (xmlButton.getSelection()) { |
c22ca172 | 296 | def = CustomXmlTraceDefinition.load(category, name); |
be222f56 PT |
297 | } |
298 | if (def != null) { | |
299 | def.save(path); | |
300 | } | |
301 | } | |
a0a88f65 AM |
302 | } |
303 | }); | |
be222f56 PT |
304 | |
305 | fillParserList(); | |
306 | ||
307 | getShell().setMinimumSize(300, 275); | |
308 | return composite; | |
309 | } | |
310 | ||
be222f56 PT |
311 | @Override |
312 | protected void createButtonsForButtonBar(Composite parent) { | |
313 | createButton(parent, IDialogConstants.OK_ID, IDialogConstants.CLOSE_LABEL, false); | |
314 | } | |
315 | ||
316 | private void fillParserList() { | |
317 | parserList.removeAll(); | |
318 | if (txtButton.getSelection()) { | |
54af96ab | 319 | for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll(false)) { |
c22ca172 | 320 | parserList.add(def.categoryName + SEP + def.definitionName); |
be222f56 PT |
321 | } |
322 | } else if (xmlButton.getSelection()) { | |
54af96ab | 323 | for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll(false)) { |
c22ca172 | 324 | parserList.add(def.categoryName + SEP + def.definitionName); |
be222f56 PT |
325 | } |
326 | } | |
327 | editButton.setEnabled(false); | |
328 | deleteButton.setEnabled(false); | |
329 | exportButton.setEnabled(false); | |
330 | } | |
331 | ||
9753257b PT |
332 | private boolean checkNameConflict(CustomTraceDefinition def) { |
333 | for (TraceTypeHelper helper : TmfTraceType.getTraceTypeHelpers()) { | |
334 | if (def.categoryName.equals(helper.getCategoryName()) && | |
335 | def.definitionName.equals(helper.getName())) { | |
336 | String newName = findAvailableName(def); | |
337 | MessageDialog dialog = new MessageDialog( | |
338 | getShell(), | |
339 | null, | |
340 | null, | |
341 | NLS.bind(Messages.ManageCustomParsersDialog_ConflictMessage, | |
342 | new Object[] { def.categoryName, def.definitionName, newName}), | |
343 | MessageDialog.QUESTION, | |
344 | new String[] { Messages.ManageCustomParsersDialog_ConflictRenameButtonLabel, | |
345 | Messages.ManageCustomParsersDialog_ConflictSkipButtonLabel }, | |
346 | 0); | |
347 | int result = dialog.open(); | |
348 | if (result == 0) { | |
349 | def.definitionName = newName; | |
350 | return true; | |
351 | } | |
352 | return false; | |
353 | } | |
354 | } | |
355 | return true; | |
356 | } | |
357 | ||
358 | private static String findAvailableName(CustomTraceDefinition def) { | |
359 | int i = 2; | |
360 | Iterable<TraceTypeHelper> helpers = TmfTraceType.getTraceTypeHelpers(); | |
361 | while (true) { | |
362 | String newName = def.definitionName + '(' + Integer.toString(i++) + ')'; | |
363 | boolean available = true; | |
364 | for (TraceTypeHelper helper : helpers) { | |
365 | if (def.categoryName.equals(helper.getCategoryName()) && | |
366 | newName.equals(helper.getName())) { | |
367 | available = false; | |
368 | break; | |
369 | } | |
370 | } | |
371 | if (available) { | |
372 | return newName; | |
373 | } | |
374 | } | |
375 | } | |
be222f56 | 376 | } |