d7c38d44f6c1198a727328f5e2b8518cb2f42920
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / dialogs / ManageCustomParsersDialog.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 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.tracecompass.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.osgi.util.NLS;
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;
35 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
36 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
37 import org.eclipse.tracecompass.internal.tmf.ui.parsers.CustomParserUtils;
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;
41 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace;
42 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition;
43 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace;
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;
47
48 /**
49 * Dialog for custom text parsers.
50 *
51 * @author Patrick Tassé
52 */
53 public class ManageCustomParsersDialog extends Dialog {
54
55 private static final String SEP = " : "; //$NON-NLS-1$
56 private static final int SEP_LEN = SEP.length();
57
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
69 /**
70 * Constructor
71 *
72 * @param parent
73 * Parent shell of this dialog
74 */
75 public ManageCustomParsersDialog(Shell parent) {
76 super(parent);
77 setShellStyle(SWT.RESIZE | SWT.MAX | getShellStyle());
78 }
79
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);
104 txtButton.addSelectionListener(new SelectionListener() {
105 @Override
106 public void widgetDefaultSelected(SelectionEvent e) {}
107
108 @Override
109 public void widgetSelected(SelectionEvent e) {
110 fillParserList();
111 }
112 });
113
114 xmlButton = new Button(radioContainer, SWT.RADIO);
115 xmlButton.setText("XML"); //$NON-NLS-1$
116 xmlButton.addSelectionListener(new SelectionListener() {
117 @Override
118 public void widgetDefaultSelected(SelectionEvent e) {
119 }
120
121 @Override
122 public void widgetSelected(SelectionEvent e) {
123 fillParserList();
124 }
125 });
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));
129 parserList.addSelectionListener(new SelectionListener() {
130 @Override
131 public void widgetDefaultSelected(SelectionEvent e) {}
132
133 @Override
134 public void widgetSelected(SelectionEvent e) {
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 }
144 }
145 });
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);
154 newButton.addSelectionListener(new SelectionListener() {
155 @Override
156 public void widgetDefaultSelected(SelectionEvent e) {}
157
158 @Override
159 public void widgetSelected(SelectionEvent e) {
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 }
172 }
173 });
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);
179 editButton.addSelectionListener(new SelectionListener() {
180 @Override
181 public void widgetDefaultSelected(SelectionEvent e) {}
182
183 @Override
184 public void widgetSelected(SelectionEvent e) {
185 WizardDialog dialog = null;
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);
189 if (txtButton.getSelection()) {
190 dialog = new WizardDialog(getShell(),
191 new CustomTxtParserWizard(CustomTxtTraceDefinition.load(category, name)));
192 } else if (xmlButton.getSelection()) {
193 dialog = new WizardDialog(getShell(),
194 new CustomXmlParserWizard(CustomXmlTraceDefinition.load(category, name)));
195 }
196 if (dialog != null) {
197 dialog.open();
198 if (dialog.getReturnCode() == Window.OK) {
199 fillParserList();
200 }
201 }
202 }
203 });
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);
209 deleteButton.addSelectionListener(new SelectionListener() {
210 @Override
211 public void widgetDefaultSelected(SelectionEvent e) {}
212
213 @Override
214 public void widgetSelected(SelectionEvent e) {
215 boolean confirm = MessageDialog.openQuestion(
216 getShell(),
217 Messages.ManageCustomParsersDialog_DeleteParserDialogHeader,
218 NLS.bind(Messages.ManageCustomParsersDialog_DeleteConfirmation, parserList.getSelection()[0]));
219 if (confirm) {
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);
223 if (txtButton.getSelection()) {
224 CustomTxtTraceDefinition.delete(category, name);
225 CustomParserUtils.cleanup(CustomTxtTrace.buildTraceTypeId(category, name));
226 } else if (xmlButton.getSelection()) {
227 CustomXmlTraceDefinition.delete(category, name);
228 CustomParserUtils.cleanup(CustomXmlTrace.buildTraceTypeId(category, name));
229 }
230 fillParserList();
231 }
232 }
233 });
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);
240 importButton.addSelectionListener(new SelectionListener() {
241 @Override
242 public void widgetDefaultSelected(SelectionEvent e) {}
243
244 @Override
245 public void widgetSelected(SelectionEvent e) {
246 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);
247 dialog.setText(Messages.ManageCustomParsersDialog_ImportParserSelection);
248 dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
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) {
259 boolean ok = checkNameConflict(def);
260 if (ok) {
261 def.save();
262 }
263 }
264 fillParserList();
265 } else {
266 MessageDialog.openInformation(Display.getCurrent().getActiveShell(),
267 Messages.ManageCustomParsersDialog_ImportFailureTitle,
268 Messages.ManageCustomParsersDialog_ImportFailureMessage);
269 }
270 }
271 }
272 });
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);
278 exportButton.addSelectionListener(new SelectionListener() {
279 @Override
280 public void widgetDefaultSelected(SelectionEvent e) {}
281
282 @Override
283 public void widgetSelected(SelectionEvent e) {
284 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
285 dialog.setText(NLS.bind(Messages.ManageCustomParsersDialog_ExportParserSelection, parserList.getSelection()[0]));
286 dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
287 String path = dialog.open();
288 if (path != null) {
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);
292 CustomTraceDefinition def = null;
293 if (txtButton.getSelection()) {
294 def = CustomTxtTraceDefinition.load(category, name);
295 } else if (xmlButton.getSelection()) {
296 def = CustomXmlTraceDefinition.load(category, name);
297 }
298 if (def != null) {
299 def.save(path);
300 }
301 }
302 }
303 });
304
305 fillParserList();
306
307 getShell().setMinimumSize(300, 275);
308 return composite;
309 }
310
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()) {
319 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll(false)) {
320 parserList.add(def.categoryName + SEP + def.definitionName);
321 }
322 } else if (xmlButton.getSelection()) {
323 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll(false)) {
324 parserList.add(def.categoryName + SEP + def.definitionName);
325 }
326 }
327 editButton.setEnabled(false);
328 deleteButton.setEnabled(false);
329 exportButton.setEnabled(false);
330 }
331
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 }
376 }
This page took 0.050998 seconds and 4 git commands to generate.