a69b77ff82e578b1b69216094044f45dd248c93e
[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 }
266 }
267 }
268 });
269
270 exportButton = new Button(buttonContainer, SWT.PUSH);
271 exportButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
272 exportButton.setText(Messages.ManageCustomParsersDialog_ExportButtonLabel);
273 exportButton.setEnabled(false);
274 exportButton.addSelectionListener(new SelectionListener() {
275 @Override
276 public void widgetDefaultSelected(SelectionEvent e) {}
277
278 @Override
279 public void widgetSelected(SelectionEvent e) {
280 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
281 dialog.setText(NLS.bind(Messages.ManageCustomParsersDialog_ExportParserSelection, parserList.getSelection()[0]));
282 dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
283 String path = dialog.open();
284 if (path != null) {
285 String selection = parserList.getSelection()[0];
286 String category = selection.substring(0, selection.indexOf(SEP));
287 String name = selection.substring(selection.indexOf(SEP) + SEP_LEN);
288 CustomTraceDefinition def = null;
289 if (txtButton.getSelection()) {
290 def = CustomTxtTraceDefinition.load(category, name);
291 } else if (xmlButton.getSelection()) {
292 def = CustomXmlTraceDefinition.load(category, name);
293 }
294 if (def != null) {
295 def.save(path);
296 }
297 }
298 }
299 });
300
301 fillParserList();
302
303 getShell().setMinimumSize(300, 275);
304 return composite;
305 }
306
307 @Override
308 protected void createButtonsForButtonBar(Composite parent) {
309 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.CLOSE_LABEL, false);
310 }
311
312 private void fillParserList() {
313 parserList.removeAll();
314 if (txtButton.getSelection()) {
315 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll(false)) {
316 parserList.add(def.categoryName + SEP + def.definitionName);
317 }
318 } else if (xmlButton.getSelection()) {
319 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll(false)) {
320 parserList.add(def.categoryName + SEP + def.definitionName);
321 }
322 }
323 editButton.setEnabled(false);
324 deleteButton.setEnabled(false);
325 exportButton.setEnabled(false);
326 }
327
328 private boolean checkNameConflict(CustomTraceDefinition def) {
329 for (TraceTypeHelper helper : TmfTraceType.getTraceTypeHelpers()) {
330 if (def.categoryName.equals(helper.getCategoryName()) &&
331 def.definitionName.equals(helper.getName())) {
332 String newName = findAvailableName(def);
333 MessageDialog dialog = new MessageDialog(
334 getShell(),
335 null,
336 null,
337 NLS.bind(Messages.ManageCustomParsersDialog_ConflictMessage,
338 new Object[] { def.categoryName, def.definitionName, newName}),
339 MessageDialog.QUESTION,
340 new String[] { Messages.ManageCustomParsersDialog_ConflictRenameButtonLabel,
341 Messages.ManageCustomParsersDialog_ConflictSkipButtonLabel },
342 0);
343 int result = dialog.open();
344 if (result == 0) {
345 def.definitionName = newName;
346 return true;
347 }
348 return false;
349 }
350 }
351 return true;
352 }
353
354 private static String findAvailableName(CustomTraceDefinition def) {
355 int i = 2;
356 Iterable<TraceTypeHelper> helpers = TmfTraceType.getTraceTypeHelpers();
357 while (true) {
358 String newName = def.definitionName + '(' + Integer.toString(i++) + ')';
359 boolean available = true;
360 for (TraceTypeHelper helper : helpers) {
361 if (def.categoryName.equals(helper.getCategoryName()) &&
362 newName.equals(helper.getName())) {
363 available = false;
364 break;
365 }
366 }
367 if (available) {
368 return newName;
369 }
370 }
371 }
372 }
This page took 0.042363 seconds and 4 git commands to generate.