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