Contribute CNF based TMF project handling
[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 import java.net.URI;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IFolder;
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IWorkspace;
23 import org.eclipse.core.resources.IWorkspaceRoot;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
30 import org.eclipse.core.runtime.preferences.InstanceScope;
31 import org.eclipse.jface.dialogs.Dialog;
32 import org.eclipse.jface.dialogs.IDialogConstants;
33 import org.eclipse.jface.dialogs.MessageDialog;
34 import org.eclipse.jface.wizard.WizardDialog;
35 import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
36 import org.eclipse.linuxtools.tmf.ui.TmfUiPreferenceInitializer;
37 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
38 import org.eclipse.linuxtools.tmf.ui.internal.Messages;
39 import org.eclipse.linuxtools.tmf.ui.parsers.ParserProviderManager;
40 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTraceDefinition;
41 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtTrace;
42 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
43 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTrace;
44 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
45 import org.eclipse.linuxtools.tmf.ui.parsers.wizards.CustomTxtParserWizard;
46 import org.eclipse.linuxtools.tmf.ui.parsers.wizards.CustomXmlParserWizard;
47 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
48 import org.eclipse.swt.SWT;
49 import org.eclipse.swt.events.SelectionEvent;
50 import org.eclipse.swt.events.SelectionListener;
51 import org.eclipse.swt.graphics.Image;
52 import org.eclipse.swt.layout.GridData;
53 import org.eclipse.swt.layout.GridLayout;
54 import org.eclipse.swt.widgets.Button;
55 import org.eclipse.swt.widgets.Composite;
56 import org.eclipse.swt.widgets.Control;
57 import org.eclipse.swt.widgets.Display;
58 import org.eclipse.swt.widgets.FileDialog;
59 import org.eclipse.swt.widgets.Label;
60 import org.eclipse.swt.widgets.List;
61 import org.eclipse.swt.widgets.Shell;
62 import org.eclipse.ui.IEditorInput;
63 import org.eclipse.ui.IEditorPart;
64 import org.eclipse.ui.IReusableEditor;
65 import org.eclipse.ui.IWorkbench;
66 import org.eclipse.ui.IWorkbenchPage;
67 import org.eclipse.ui.PlatformUI;
68 import org.eclipse.ui.part.FileEditorInput;
69
70 public class ManageCustomParsersDialog extends Dialog {
71
72 private static final Image image = TmfUiPlugin.getDefault().getImageFromPath("/icons/etool16/customparser_wizard.gif"); //$NON-NLS-1$
73
74 Button txtButton;
75 Button xmlButton;
76 List parserList;
77 Button newButton;
78 Button editButton;
79 Button deleteButton;
80 Button importButton;
81 Button exportButton;
82 Button parseButton;
83
84 public ManageCustomParsersDialog(Shell parent) {
85 super(parent);
86 setShellStyle(SWT.RESIZE | SWT.MAX | getShellStyle());
87 }
88
89 /* (non-Javadoc)
90 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
91 */
92 @Override
93 protected Control createDialogArea(Composite parent) {
94 getShell().setText(Messages.ManageCustomParsersDialog_DialogHeader);
95 getShell().setImage(image);
96
97 Composite composite = (Composite) super.createDialogArea(parent);
98 composite.setLayout(new GridLayout(2, false));
99
100 Composite listContainer = new Composite(composite, SWT.NONE);
101 listContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
102 GridLayout lcgl = new GridLayout();
103 lcgl.marginHeight = 0;
104 lcgl.marginWidth = 0;
105 listContainer.setLayout(lcgl);
106
107 Composite radioContainer = new Composite(listContainer, SWT.NONE);
108 GridLayout rcgl = new GridLayout(2, true);
109 rcgl.marginHeight = 0;
110 rcgl.marginWidth = 0;
111 radioContainer.setLayout(rcgl);
112
113 txtButton = new Button(radioContainer, SWT.RADIO);
114 txtButton.setText(Messages.ManageCustomParsersDialog_TextButtonLabel);
115 txtButton.setSelection(true);
116 txtButton.addSelectionListener(new SelectionListener(){
117 @Override
118 public void widgetDefaultSelected(SelectionEvent e) {}
119 @Override
120 public void widgetSelected(SelectionEvent e) {
121 fillParserList();
122 }});
123
124 xmlButton = new Button(radioContainer, SWT.RADIO);
125 xmlButton.setText("XML"); //$NON-NLS-1$
126 xmlButton.addSelectionListener(new SelectionListener(){
127 @Override
128 public void widgetDefaultSelected(SelectionEvent e) {}
129 @Override
130 public void widgetSelected(SelectionEvent e) {
131 fillParserList();
132 }});
133
134 parserList = new List(listContainer, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
135 parserList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
136 parserList.addSelectionListener(new SelectionListener(){
137 @Override
138 public void widgetDefaultSelected(SelectionEvent e) {}
139 @Override
140 public void widgetSelected(SelectionEvent e) {
141 if (parserList.getSelectionCount() == 0) {
142 editButton.setEnabled(false);
143 deleteButton.setEnabled(false);
144 exportButton.setEnabled(false);
145 parseButton.setEnabled(false);
146 } else {
147 editButton.setEnabled(true);
148 deleteButton.setEnabled(true);
149 exportButton.setEnabled(true);
150 parseButton.setEnabled(true);
151 }
152 }});
153
154 Composite buttonContainer = new Composite(composite, SWT.NULL);
155 buttonContainer.setLayout(new GridLayout());
156 buttonContainer.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false));
157
158 newButton = new Button(buttonContainer, SWT.PUSH);
159 newButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
160 newButton.setText(Messages.ManageCustomParsersDialog_NewButtonLabel);
161 newButton.addSelectionListener(new SelectionListener(){
162 @Override
163 public void widgetDefaultSelected(SelectionEvent e) {}
164 @Override
165 public void widgetSelected(SelectionEvent e) {
166 WizardDialog dialog = null;
167 if (txtButton.getSelection()) {
168 dialog = new WizardDialog(getShell(), new CustomTxtParserWizard());
169 } else if (xmlButton.getSelection()) {
170 dialog = new WizardDialog(getShell(), new CustomXmlParserWizard());
171 }
172 dialog.open();
173 if (dialog.getReturnCode() == Dialog.OK) {
174 fillParserList();
175 }
176 }});
177
178 editButton = new Button(buttonContainer, SWT.PUSH);
179 editButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
180 editButton.setText(Messages.ManageCustomParsersDialog_EditButtonLabel);
181 editButton.setEnabled(false);
182 editButton.addSelectionListener(new SelectionListener(){
183 @Override
184 public void widgetDefaultSelected(SelectionEvent e) {}
185 @Override
186 public void widgetSelected(SelectionEvent e) {
187 WizardDialog dialog = null;
188 if (txtButton.getSelection()) {
189 dialog = new WizardDialog(getShell(),
190 new CustomTxtParserWizard(CustomTxtTraceDefinition.load(parserList.getSelection()[0])));
191 } else if (xmlButton.getSelection()) {
192 dialog = new WizardDialog(getShell(),
193 new CustomXmlParserWizard(CustomXmlTraceDefinition.load(parserList.getSelection()[0])));
194 }
195 dialog.open();
196 if (dialog.getReturnCode() == Dialog.OK) {
197 fillParserList();
198 }
199 }});
200
201 deleteButton = new Button(buttonContainer, SWT.PUSH);
202 deleteButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
203 deleteButton.setText(Messages.ManageCustomParsersDialog_DeleteButtonLabel);
204 deleteButton.setEnabled(false);
205 deleteButton.addSelectionListener(new SelectionListener(){
206 @Override
207 public void widgetDefaultSelected(SelectionEvent e) {}
208 @Override
209 public void widgetSelected(SelectionEvent e) {
210 boolean confirm = MessageDialog.openQuestion(
211 getShell(),
212 Messages.ManageCustomParsersDialog_DeleteParserDialogHeader,
213 Messages.ManageCustomParsersDialog_DeleteConfirmation + parserList.getSelection()[0] + "?"); //$NON-NLS-1$
214 if (confirm) {
215 if (txtButton.getSelection()) {
216 CustomTxtTraceDefinition.delete(parserList.getSelection()[0]);
217 } else if (xmlButton.getSelection()) {
218 CustomXmlTraceDefinition.delete(parserList.getSelection()[0]);
219 }
220 fillParserList();
221 }
222 }});
223
224 new Label(buttonContainer, SWT.NONE); // filler
225
226 importButton = new Button(buttonContainer, SWT.PUSH);
227 importButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
228 importButton.setText(Messages.ManageCustomParsersDialog_ImportButtonLabel);
229 importButton.addSelectionListener(new SelectionListener(){
230 @Override
231 public void widgetDefaultSelected(SelectionEvent e) {}
232 @Override
233 public void widgetSelected(SelectionEvent e) {
234 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);
235 dialog.setText(Messages.ManageCustomParsersDialog_ImportParserSelection);
236 dialog.setFilterExtensions(new String[] {"*.xml", "*"}); //$NON-NLS-1$ //$NON-NLS-2$
237 String path = dialog.open();
238 if (path != null) {
239 CustomTraceDefinition[] defs = null;
240 if (txtButton.getSelection()) {
241 defs = CustomTxtTraceDefinition.loadAll(path);
242 } else if (xmlButton.getSelection()) {
243 defs = CustomXmlTraceDefinition.loadAll(path);
244 }
245 if (defs != null && defs.length > 0) {
246 for (CustomTraceDefinition def : defs) {
247 def.save();
248 }
249 fillParserList();
250 }
251 }
252 }});
253
254 exportButton = new Button(buttonContainer, SWT.PUSH);
255 exportButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
256 exportButton.setText(Messages.ManageCustomParsersDialog_ExportButtonLabel);
257 exportButton.setEnabled(false);
258 exportButton.addSelectionListener(new SelectionListener(){
259 @Override
260 public void widgetDefaultSelected(SelectionEvent e) {}
261 @Override
262 public void widgetSelected(SelectionEvent e) {
263 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
264 dialog.setText(Messages.ManageCustomParsersDialog_ExportParserSelection + parserList.getSelection()[0]);
265 dialog.setFilterExtensions(new String[] {"*.xml", "*"}); //$NON-NLS-1$ //$NON-NLS-2$
266 String path = dialog.open();
267 if (path != null) {
268 CustomTraceDefinition def = null;
269 if (txtButton.getSelection()) {
270 def = CustomTxtTraceDefinition.load(parserList.getSelection()[0]);
271 } else if (xmlButton.getSelection()) {
272 def = CustomXmlTraceDefinition.load(parserList.getSelection()[0]);
273 }
274 if (def != null) {
275 def.save(path);
276 }
277 }
278 }});
279
280 parseButton = new Button(buttonContainer, SWT.PUSH);
281 parseButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
282 parseButton.setText(Messages.ManageCustomParsersDialog_ParseButtonLabel);
283 parseButton.setEnabled(false);
284 parseButton.addSelectionListener(new SelectionListener(){
285 @Override
286 public void widgetDefaultSelected(SelectionEvent e) {}
287 @Override
288 public void widgetSelected(SelectionEvent e) {
289 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);
290 dialog.setText(Messages.ManageCustomParsersDialog_TraceSelection + parserList.getSelection()[0]);
291 if (xmlButton.getSelection()) {
292 dialog.setFilterExtensions(new String[] {"*.xml", "*"}); //$NON-NLS-1$ //$NON-NLS-2$
293 }
294 String path = dialog.open();
295 String parser = null;
296 if (path != null) {
297 CustomTraceDefinition def = null;
298 if (txtButton.getSelection()) {
299 def = CustomTxtTraceDefinition.load(parserList.getSelection()[0]);
300 parser = CustomTxtTrace.class.getCanonicalName() + "." + def.definitionName; //$NON-NLS-1$
301 } else if (xmlButton.getSelection()) {
302 def = CustomXmlTraceDefinition.load(parserList.getSelection()[0]);
303 parser = CustomXmlTrace.class.getCanonicalName() + "." + def.definitionName; //$NON-NLS-1$
304 }
305 if (def != null) {
306 try {
307 IWorkspace workspace = ResourcesPlugin.getWorkspace();
308 IPath location = Path.fromOSString(path);
309 IFile file = workspace.getRoot().getFileForLocation(location);
310 if (file == null) {
311 file = createLink(new File(location.toPortableString()).toURI());
312 }
313 file.setPersistentProperty(ParserProviderManager.PARSER_PROPERTY, parser);
314 IEditorInput editorInput = new FileEditorInput(file);
315 IWorkbench wb = PlatformUI.getWorkbench();
316 IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
317
318 String editorId = TmfEventsEditor.ID;
319 IEditorPart editor = activePage.findEditor(editorInput);
320 if (editor != null && editor instanceof IReusableEditor) {
321 activePage.reuseEditor((IReusableEditor)editor, editorInput);
322 activePage.activate(editor);
323 } else {
324 editor = activePage.openEditor(editorInput, editorId);
325 }
326 } catch (CoreException e1) {
327 MessageDialog.openError(getShell(), "Parse Error", e1.getMessage()); //$NON-NLS-1$
328 }
329 }
330 }
331 }});
332
333 fillParserList();
334
335 getShell().setMinimumSize(300, 275);
336 return composite;
337 }
338
339 // /////////////////////////////////////////////////////////////////////////////
340 // FIXME: Duplicated in TmfEventsEditor
341 // From the legacy ProjectView
342 // /////////////////////////////////////////////////////////////////////////////
343
344 // ------------------------------------------------------------------------
345 // Static methods
346 // ------------------------------------------------------------------------
347
348 static public IFolder getActiveProjectTracesFolder() {
349 IEclipsePreferences node = new InstanceScope()
350 .getNode(TmfUiPlugin.PLUGIN_ID);
351 String activeProjectName = node.get(
352 TmfUiPreferenceInitializer.ACTIVE_PROJECT_PREFERENCE,
353 TmfUiPreferenceInitializer.ACTIVE_PROJECT_DEFAULT);
354 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
355 IProject[] projects = root.getProjects();
356 for (IProject project : projects) {
357 if (project.isAccessible()
358 && project.getName().equals(activeProjectName)) {
359 return project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME);
360 }
361 }
362 return null;
363 }
364
365 static public IFile createLink(URI uri) throws CoreException {
366 IFolder folder = getActiveProjectTracesFolder();
367 if (folder == null || !folder.exists()) {
368 throw new CoreException(new Status(Status.ERROR,
369 TmfUiPlugin.PLUGIN_ID, "No active project set")); //$NON-NLS-1$
370 }
371 String path = uri.getPath();
372 // TODO: support duplicate file names
373 IFile file = folder.getFile(path.substring(path
374 .lastIndexOf(Path.SEPARATOR)));
375 if (!file.exists()) {
376 file.createLink(uri, IResource.NONE, null);
377 }
378 return file;
379 }
380
381 // /////////////////////////////////////////////////////////////////////////////
382
383 /* (non-Javadoc)
384 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
385 */
386 @Override
387 protected void createButtonsForButtonBar(Composite parent) {
388 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.CLOSE_LABEL, false);
389 }
390
391 private void fillParserList() {
392 parserList.removeAll();
393 if (txtButton.getSelection()) {
394 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
395 parserList.add(def.definitionName);
396 }
397 } else if (xmlButton.getSelection()) {
398 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
399 parserList.add(def.definitionName);
400 }
401 }
402 editButton.setEnabled(false);
403 deleteButton.setEnabled(false);
404 exportButton.setEnabled(false);
405 parseButton.setEnabled(false);
406 }
407
408 }
This page took 0.03919 seconds and 5 git commands to generate.