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