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