1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.dialogs
;
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
;
70 public class ManageCustomParsersDialog
extends Dialog
{
72 private static final Image image
= TmfUiPlugin
.getDefault().getImageFromPath("/icons/etool16/customparser_wizard.gif"); //$NON-NLS-1$
84 public ManageCustomParsersDialog(Shell parent
) {
86 setShellStyle(SWT
.RESIZE
| SWT
.MAX
| getShellStyle());
90 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
93 protected Control
createDialogArea(Composite parent
) {
94 getShell().setText(Messages
.ManageCustomParsersDialog_DialogHeader
);
95 getShell().setImage(image
);
97 Composite composite
= (Composite
) super.createDialogArea(parent
);
98 composite
.setLayout(new GridLayout(2, false));
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
);
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
);
113 txtButton
= new Button(radioContainer
, SWT
.RADIO
);
114 txtButton
.setText(Messages
.ManageCustomParsersDialog_TextButtonLabel
);
115 txtButton
.setSelection(true);
116 txtButton
.addSelectionListener(new SelectionListener(){
118 public void widgetDefaultSelected(SelectionEvent e
) {}
120 public void widgetSelected(SelectionEvent e
) {
124 xmlButton
= new Button(radioContainer
, SWT
.RADIO
);
125 xmlButton
.setText("XML"); //$NON-NLS-1$
126 xmlButton
.addSelectionListener(new SelectionListener(){
128 public void widgetDefaultSelected(SelectionEvent e
) {}
130 public void widgetSelected(SelectionEvent e
) {
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(){
138 public void widgetDefaultSelected(SelectionEvent e
) {}
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);
147 editButton
.setEnabled(true);
148 deleteButton
.setEnabled(true);
149 exportButton
.setEnabled(true);
150 parseButton
.setEnabled(true);
154 Composite buttonContainer
= new Composite(composite
, SWT
.NULL
);
155 buttonContainer
.setLayout(new GridLayout());
156 buttonContainer
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, false, false));
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(){
163 public void widgetDefaultSelected(SelectionEvent e
) {}
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());
173 if (dialog
.getReturnCode() == Dialog
.OK
) {
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(){
184 public void widgetDefaultSelected(SelectionEvent e
) {}
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])));
196 if (dialog
.getReturnCode() == Dialog
.OK
) {
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(){
207 public void widgetDefaultSelected(SelectionEvent e
) {}
209 public void widgetSelected(SelectionEvent e
) {
210 boolean confirm
= MessageDialog
.openQuestion(
212 Messages
.ManageCustomParsersDialog_DeleteParserDialogHeader
,
213 Messages
.ManageCustomParsersDialog_DeleteConfirmation
+ parserList
.getSelection()[0] + "?"); //$NON-NLS-1$
215 if (txtButton
.getSelection()) {
216 CustomTxtTraceDefinition
.delete(parserList
.getSelection()[0]);
217 } else if (xmlButton
.getSelection()) {
218 CustomXmlTraceDefinition
.delete(parserList
.getSelection()[0]);
224 new Label(buttonContainer
, SWT
.NONE
); // filler
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(){
231 public void widgetDefaultSelected(SelectionEvent e
) {}
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();
239 CustomTraceDefinition
[] defs
= null;
240 if (txtButton
.getSelection()) {
241 defs
= CustomTxtTraceDefinition
.loadAll(path
);
242 } else if (xmlButton
.getSelection()) {
243 defs
= CustomXmlTraceDefinition
.loadAll(path
);
245 if (defs
!= null && defs
.length
> 0) {
246 for (CustomTraceDefinition def
: defs
) {
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(){
260 public void widgetDefaultSelected(SelectionEvent e
) {}
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();
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]);
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(){
286 public void widgetDefaultSelected(SelectionEvent e
) {}
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$
294 String path
= dialog
.open();
295 String parser
= 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$
307 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
308 IPath location
= Path
.fromOSString(path
);
309 IFile file
= workspace
.getRoot().getFileForLocation(location
);
311 file
= createLink(new File(location
.toPortableString()).toURI());
313 file
.setPersistentProperty(ParserProviderManager
.PARSER_PROPERTY
, parser
);
314 IEditorInput editorInput
= new FileEditorInput(file
);
315 IWorkbench wb
= PlatformUI
.getWorkbench();
316 IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
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
);
324 editor
= activePage
.openEditor(editorInput
, editorId
);
326 } catch (CoreException e1
) {
327 MessageDialog
.openError(getShell(), "Parse Error", e1
.getMessage()); //$NON-NLS-1$
335 getShell().setMinimumSize(300, 275);
339 // /////////////////////////////////////////////////////////////////////////////
340 // FIXME: Duplicated in TmfEventsEditor
341 // From the legacy ProjectView
342 // /////////////////////////////////////////////////////////////////////////////
344 // ------------------------------------------------------------------------
346 // ------------------------------------------------------------------------
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
);
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$
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);
381 // /////////////////////////////////////////////////////////////////////////////
384 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
387 protected void createButtonsForButtonBar(Composite parent
) {
388 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.CLOSE_LABEL
, false);
391 private void fillParserList() {
392 parserList
.removeAll();
393 if (txtButton
.getSelection()) {
394 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll()) {
395 parserList
.add(def
.definitionName
);
397 } else if (xmlButton
.getSelection()) {
398 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll()) {
399 parserList
.add(def
.definitionName
);
402 editButton
.setEnabled(false);
403 deleteButton
.setEnabled(false);
404 exportButton
.setEnabled(false);
405 parseButton
.setEnabled(false);