2010-10-26 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug309042
[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 java.io.File;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IWorkspace;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jface.dialogs.Dialog;
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.wizard.WizardDialog;
27 import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
28 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
29 import org.eclipse.linuxtools.tmf.ui.parsers.ParserProviderManager;
30 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTraceDefinition;
31 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtTrace;
32 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
33 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTrace;
34 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
35 import org.eclipse.linuxtools.tmf.ui.views.project.ProjectView;
36 import org.eclipse.linuxtools.tmf.ui.wizards.CustomTxtParserWizard;
37 import org.eclipse.linuxtools.tmf.ui.wizards.CustomXmlParserWizard;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.events.SelectionEvent;
40 import org.eclipse.swt.events.SelectionListener;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Button;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Control;
47 import org.eclipse.swt.widgets.Display;
48 import org.eclipse.swt.widgets.FileDialog;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.swt.widgets.List;
51 import org.eclipse.swt.widgets.Shell;
52 import org.eclipse.ui.IEditorInput;
53 import org.eclipse.ui.IEditorPart;
54 import org.eclipse.ui.IReusableEditor;
55 import org.eclipse.ui.IWorkbench;
56 import org.eclipse.ui.IWorkbenchPage;
57 import org.eclipse.ui.PlatformUI;
58 import org.eclipse.ui.part.FileEditorInput;
59
60 public class ManageCustomParsersDialog extends Dialog {
61
62 private static final Image image = TmfUiPlugin.getDefault().getImageFromPath("/icons/customparser_wizard.gif");
63
64 Button txtButton;
65 Button xmlButton;
66 List parserList;
67 Button newButton;
68 Button editButton;
69 Button deleteButton;
70 Button importButton;
71 Button exportButton;
72 Button parseButton;
73
74 public ManageCustomParsersDialog(Shell parent) {
75 super(parent);
76 setShellStyle(SWT.RESIZE | SWT.MAX | getShellStyle());
77 }
78
79 /* (non-Javadoc)
80 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
81 */
82 @Override
83 protected Control createDialogArea(Composite parent) {
84 getShell().setText("Manage Custom Parsers");
85 getShell().setImage(image);
86
87 Composite composite = (Composite) super.createDialogArea(parent);
88 composite.setLayout(new GridLayout(2, false));
89
90 Composite listContainer = new Composite(composite, SWT.NONE);
91 listContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
92 GridLayout lcgl = new GridLayout();
93 lcgl.marginHeight = 0;
94 lcgl.marginWidth = 0;
95 listContainer.setLayout(lcgl);
96
97 Composite radioContainer = new Composite(listContainer, SWT.NONE);
98 GridLayout rcgl = new GridLayout(2, true);
99 rcgl.marginHeight = 0;
100 rcgl.marginWidth = 0;
101 radioContainer.setLayout(rcgl);
102
103 txtButton = new Button(radioContainer, SWT.RADIO);
104 txtButton.setText("Text");
105 txtButton.setSelection(true);
106 txtButton.addSelectionListener(new SelectionListener(){
107 @Override
108 public void widgetDefaultSelected(SelectionEvent e) {}
109 @Override
110 public void widgetSelected(SelectionEvent e) {
111 fillParserList();
112 }});
113
114 xmlButton = new Button(radioContainer, SWT.RADIO);
115 xmlButton.setText("XML");
116 xmlButton.addSelectionListener(new SelectionListener(){
117 @Override
118 public void widgetDefaultSelected(SelectionEvent e) {}
119 @Override
120 public void widgetSelected(SelectionEvent e) {
121 fillParserList();
122 }});
123
124 parserList = new List(listContainer, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
125 parserList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
126 parserList.addSelectionListener(new SelectionListener(){
127 @Override
128 public void widgetDefaultSelected(SelectionEvent e) {}
129 @Override
130 public void widgetSelected(SelectionEvent e) {
131 if (parserList.getSelectionCount() == 0) {
132 editButton.setEnabled(false);
133 deleteButton.setEnabled(false);
134 exportButton.setEnabled(false);
135 parseButton.setEnabled(false);
136 } else {
137 editButton.setEnabled(true);
138 deleteButton.setEnabled(true);
139 exportButton.setEnabled(true);
140 parseButton.setEnabled(true);
141 }
142 }});
143
144 Composite buttonContainer = new Composite(composite, SWT.NULL);
145 buttonContainer.setLayout(new GridLayout());
146 buttonContainer.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false));
147
148 newButton = new Button(buttonContainer, SWT.PUSH);
149 newButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
150 newButton.setText("New...");
151 newButton.addSelectionListener(new SelectionListener(){
152 @Override
153 public void widgetDefaultSelected(SelectionEvent e) {}
154 @Override
155 public void widgetSelected(SelectionEvent e) {
156 WizardDialog dialog = null;
157 if (txtButton.getSelection()) {
158 dialog = new WizardDialog(getShell(), new CustomTxtParserWizard());
159 } else if (xmlButton.getSelection()) {
160 dialog = new WizardDialog(getShell(), new CustomXmlParserWizard());
161 }
162 dialog.open();
163 if (dialog.getReturnCode() == Dialog.OK) {
164 fillParserList();
165 }
166 }});
167
168 editButton = new Button(buttonContainer, SWT.PUSH);
169 editButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
170 editButton.setText("Edit...");
171 editButton.setEnabled(false);
172 editButton.addSelectionListener(new SelectionListener(){
173 @Override
174 public void widgetDefaultSelected(SelectionEvent e) {}
175 @Override
176 public void widgetSelected(SelectionEvent e) {
177 WizardDialog dialog = null;
178 if (txtButton.getSelection()) {
179 dialog = new WizardDialog(getShell(),
180 new CustomTxtParserWizard(CustomTxtTraceDefinition.load(parserList.getSelection()[0])));
181 } else if (xmlButton.getSelection()) {
182 dialog = new WizardDialog(getShell(),
183 new CustomXmlParserWizard(CustomXmlTraceDefinition.load(parserList.getSelection()[0])));
184 }
185 dialog.open();
186 if (dialog.getReturnCode() == Dialog.OK) {
187 fillParserList();
188 }
189 }});
190
191 deleteButton = new Button(buttonContainer, SWT.PUSH);
192 deleteButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
193 deleteButton.setText("Delete");
194 deleteButton.setEnabled(false);
195 deleteButton.addSelectionListener(new SelectionListener(){
196 @Override
197 public void widgetDefaultSelected(SelectionEvent e) {}
198 @Override
199 public void widgetSelected(SelectionEvent e) {
200 boolean confirm = MessageDialog.openQuestion(
201 getShell(),
202 "Delete Custom Parser",
203 "Are you sure you want to delete the " + parserList.getSelection()[0] + " custom parser?");
204 if (confirm) {
205 if (txtButton.getSelection()) {
206 CustomTxtTraceDefinition.delete(parserList.getSelection()[0]);
207 } else if (xmlButton.getSelection()) {
208 CustomXmlTraceDefinition.delete(parserList.getSelection()[0]);
209 }
210 fillParserList();
211 }
212 }});
213
214 new Label(buttonContainer, SWT.NONE); // filler
215
216 importButton = new Button(buttonContainer, SWT.PUSH);
217 importButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
218 importButton.setText("Import...");
219 importButton.addSelectionListener(new SelectionListener(){
220 @Override
221 public void widgetDefaultSelected(SelectionEvent e) {}
222 @Override
223 public void widgetSelected(SelectionEvent e) {
224 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);
225 dialog.setText("Select custom parser file to import");
226 dialog.setFilterExtensions(new String[] {"*.xml", "*"});
227 String path = dialog.open();
228 if (path != null) {
229 CustomTraceDefinition[] defs = null;
230 if (txtButton.getSelection()) {
231 defs = CustomTxtTraceDefinition.loadAll(path);
232 } else if (xmlButton.getSelection()) {
233 defs = CustomXmlTraceDefinition.loadAll(path);
234 }
235 if (defs != null && defs.length > 0) {
236 for (CustomTraceDefinition def : defs) {
237 def.save();
238 }
239 fillParserList();
240 }
241 }
242 }});
243
244 exportButton = new Button(buttonContainer, SWT.PUSH);
245 exportButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
246 exportButton.setText("Export...");
247 exportButton.setEnabled(false);
248 exportButton.addSelectionListener(new SelectionListener(){
249 @Override
250 public void widgetDefaultSelected(SelectionEvent e) {}
251 @Override
252 public void widgetSelected(SelectionEvent e) {
253 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
254 dialog.setText("Select file to export the " + parserList.getSelection()[0] + " custom parser");
255 dialog.setFilterExtensions(new String[] {"*.xml", "*"});
256 String path = dialog.open();
257 if (path != null) {
258 CustomTraceDefinition def = null;
259 if (txtButton.getSelection()) {
260 def = CustomTxtTraceDefinition.load(parserList.getSelection()[0]);
261 } else if (xmlButton.getSelection()) {
262 def = CustomXmlTraceDefinition.load(parserList.getSelection()[0]);
263 }
264 if (def != null) {
265 def.save(path);
266 }
267 }
268 }});
269
270 parseButton = new Button(buttonContainer, SWT.PUSH);
271 parseButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
272 parseButton.setText("Parse...");
273 parseButton.setEnabled(false);
274 parseButton.addSelectionListener(new SelectionListener(){
275 @Override
276 public void widgetDefaultSelected(SelectionEvent e) {}
277 @Override
278 public void widgetSelected(SelectionEvent e) {
279 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);
280 dialog.setText("Select log file to parse using the " + parserList.getSelection()[0] + " custom parser");
281 if (xmlButton.getSelection()) {
282 dialog.setFilterExtensions(new String[] {"*.xml", "*"});
283 }
284 String path = dialog.open();
285 String parser = null;
286 if (path != null) {
287 CustomTraceDefinition def = null;
288 if (txtButton.getSelection()) {
289 def = CustomTxtTraceDefinition.load(parserList.getSelection()[0]);
290 parser = CustomTxtTrace.class.getCanonicalName() + "." + def.definitionName;
291 } else if (xmlButton.getSelection()) {
292 def = CustomXmlTraceDefinition.load(parserList.getSelection()[0]);
293 parser = CustomXmlTrace.class.getCanonicalName() + "." + def.definitionName;
294 }
295 if (def != null) {
296 try {
297 IWorkspace workspace = ResourcesPlugin.getWorkspace();
298 IPath location = Path.fromOSString(path);
299 IFile file = workspace.getRoot().getFileForLocation(location);
300 if (file == null) {
301 file = ProjectView.createLink(new File(location.toPortableString()).toURI());
302 }
303 file.setPersistentProperty(ParserProviderManager.PARSER_PROPERTY, parser);
304 IEditorInput editorInput = new FileEditorInput(file);
305 IWorkbench wb = PlatformUI.getWorkbench();
306 IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
307
308 String editorId = TmfEventsEditor.ID;
309 IEditorPart editor = activePage.findEditor(editorInput);
310 if (editor != null && editor instanceof IReusableEditor) {
311 activePage.reuseEditor((IReusableEditor)editor, editorInput);
312 activePage.activate(editor);
313 } else {
314 editor = activePage.openEditor(editorInput, editorId);
315 }
316 } catch (CoreException e1) {
317 MessageDialog.openError(getShell(), "Parse Error", e1.getMessage());
318 }
319 }
320 }
321 }});
322
323 fillParserList();
324
325 getShell().setMinimumSize(300, 275);
326 return composite;
327 }
328
329 /* (non-Javadoc)
330 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
331 */
332 @Override
333 protected void createButtonsForButtonBar(Composite parent) {
334 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.CLOSE_LABEL, false);
335 }
336
337 private void fillParserList() {
338 parserList.removeAll();
339 if (txtButton.getSelection()) {
340 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
341 parserList.add(def.definitionName);
342 }
343 } else if (xmlButton.getSelection()) {
344 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
345 parserList.add(def.definitionName);
346 }
347 }
348 editButton.setEnabled(false);
349 deleteButton.setEnabled(false);
350 exportButton.setEnabled(false);
351 parseButton.setEnabled(false);
352 }
353
354 }
This page took 0.040629 seconds and 5 git commands to generate.