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