1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 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
.tracecompass
.internal
.tmf
.ui
.dialogs
;
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
.window
.Window
;
19 import org
.eclipse
.jface
.wizard
.WizardDialog
;
20 import org
.eclipse
.osgi
.util
.NLS
;
21 import org
.eclipse
.swt
.SWT
;
22 import org
.eclipse
.swt
.events
.SelectionEvent
;
23 import org
.eclipse
.swt
.events
.SelectionListener
;
24 import org
.eclipse
.swt
.graphics
.Image
;
25 import org
.eclipse
.swt
.layout
.GridData
;
26 import org
.eclipse
.swt
.layout
.GridLayout
;
27 import org
.eclipse
.swt
.widgets
.Button
;
28 import org
.eclipse
.swt
.widgets
.Composite
;
29 import org
.eclipse
.swt
.widgets
.Control
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.swt
.widgets
.FileDialog
;
32 import org
.eclipse
.swt
.widgets
.Label
;
33 import org
.eclipse
.swt
.widgets
.List
;
34 import org
.eclipse
.swt
.widgets
.Shell
;
35 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Messages
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.parsers
.CustomParserUtils
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.parsers
.wizards
.CustomTxtParserWizard
;
39 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.parsers
.wizards
.CustomXmlParserWizard
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTxtTrace
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTxtTraceDefinition
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTrace
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceType
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TraceTypeHelper
;
49 * Dialog for custom text parsers.
51 * @author Patrick Tassé
53 public class ManageCustomParsersDialog
extends Dialog
{
55 private static final String SEP
= " : "; //$NON-NLS-1$
56 private static final int SEP_LEN
= SEP
.length();
58 private static final Image image
= Activator
.getDefault().getImageFromPath("/icons/etool16/customparser_wizard.gif"); //$NON-NLS-1$
73 * Parent shell of this dialog
75 public ManageCustomParsersDialog(Shell parent
) {
77 setShellStyle(SWT
.RESIZE
| SWT
.MAX
| getShellStyle());
81 protected Control
createDialogArea(Composite parent
) {
82 getShell().setText(Messages
.ManageCustomParsersDialog_DialogHeader
);
83 getShell().setImage(image
);
85 Composite composite
= (Composite
) super.createDialogArea(parent
);
86 composite
.setLayout(new GridLayout(2, false));
88 Composite listContainer
= new Composite(composite
, SWT
.NONE
);
89 listContainer
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
90 GridLayout lcgl
= new GridLayout();
91 lcgl
.marginHeight
= 0;
93 listContainer
.setLayout(lcgl
);
95 Composite radioContainer
= new Composite(listContainer
, SWT
.NONE
);
96 GridLayout rcgl
= new GridLayout(2, true);
97 rcgl
.marginHeight
= 0;
99 radioContainer
.setLayout(rcgl
);
101 txtButton
= new Button(radioContainer
, SWT
.RADIO
);
102 txtButton
.setText(Messages
.ManageCustomParsersDialog_TextButtonLabel
);
103 txtButton
.setSelection(true);
104 txtButton
.addSelectionListener(new SelectionListener() {
106 public void widgetDefaultSelected(SelectionEvent e
) {}
109 public void widgetSelected(SelectionEvent e
) {
114 xmlButton
= new Button(radioContainer
, SWT
.RADIO
);
115 xmlButton
.setText("XML"); //$NON-NLS-1$
116 xmlButton
.addSelectionListener(new SelectionListener() {
118 public void widgetDefaultSelected(SelectionEvent e
) {
122 public void widgetSelected(SelectionEvent e
) {
127 parserList
= new List(listContainer
, SWT
.SINGLE
| SWT
.BORDER
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
128 parserList
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
129 parserList
.addSelectionListener(new SelectionListener() {
131 public void widgetDefaultSelected(SelectionEvent e
) {}
134 public void widgetSelected(SelectionEvent e
) {
135 if (parserList
.getSelectionCount() == 0) {
136 editButton
.setEnabled(false);
137 deleteButton
.setEnabled(false);
138 exportButton
.setEnabled(false);
140 editButton
.setEnabled(true);
141 deleteButton
.setEnabled(true);
142 exportButton
.setEnabled(true);
147 Composite buttonContainer
= new Composite(composite
, SWT
.NULL
);
148 buttonContainer
.setLayout(new GridLayout());
149 buttonContainer
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, false, false));
151 newButton
= new Button(buttonContainer
, SWT
.PUSH
);
152 newButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
153 newButton
.setText(Messages
.ManageCustomParsersDialog_NewButtonLabel
);
154 newButton
.addSelectionListener(new SelectionListener() {
156 public void widgetDefaultSelected(SelectionEvent e
) {}
159 public void widgetSelected(SelectionEvent e
) {
160 WizardDialog dialog
= null;
161 if (txtButton
.getSelection()) {
162 dialog
= new WizardDialog(getShell(), new CustomTxtParserWizard());
163 } else if (xmlButton
.getSelection()) {
164 dialog
= new WizardDialog(getShell(), new CustomXmlParserWizard());
166 if (dialog
!= null) {
168 if (dialog
.getReturnCode() == Window
.OK
) {
175 editButton
= new Button(buttonContainer
, SWT
.PUSH
);
176 editButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
177 editButton
.setText(Messages
.ManageCustomParsersDialog_EditButtonLabel
);
178 editButton
.setEnabled(false);
179 editButton
.addSelectionListener(new SelectionListener() {
181 public void widgetDefaultSelected(SelectionEvent e
) {}
184 public void widgetSelected(SelectionEvent e
) {
185 WizardDialog dialog
= null;
186 String selection
= parserList
.getSelection()[0];
187 String category
= selection
.substring(0, selection
.indexOf(SEP
));
188 String name
= selection
.substring(selection
.indexOf(SEP
) + SEP_LEN
);
189 if (txtButton
.getSelection()) {
190 dialog
= new WizardDialog(getShell(),
191 new CustomTxtParserWizard(CustomTxtTraceDefinition
.load(category
, name
)));
192 } else if (xmlButton
.getSelection()) {
193 dialog
= new WizardDialog(getShell(),
194 new CustomXmlParserWizard(CustomXmlTraceDefinition
.load(category
, name
)));
196 if (dialog
!= null) {
198 if (dialog
.getReturnCode() == Window
.OK
) {
205 deleteButton
= new Button(buttonContainer
, SWT
.PUSH
);
206 deleteButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
207 deleteButton
.setText(Messages
.ManageCustomParsersDialog_DeleteButtonLabel
);
208 deleteButton
.setEnabled(false);
209 deleteButton
.addSelectionListener(new SelectionListener() {
211 public void widgetDefaultSelected(SelectionEvent e
) {}
214 public void widgetSelected(SelectionEvent e
) {
215 boolean confirm
= MessageDialog
.openQuestion(
217 Messages
.ManageCustomParsersDialog_DeleteParserDialogHeader
,
218 NLS
.bind(Messages
.ManageCustomParsersDialog_DeleteConfirmation
, parserList
.getSelection()[0]));
220 String selection
= parserList
.getSelection()[0];
221 String category
= selection
.substring(0, selection
.indexOf(SEP
));
222 String name
= selection
.substring(selection
.indexOf(SEP
) + SEP_LEN
);
223 if (txtButton
.getSelection()) {
224 CustomTxtTraceDefinition
.delete(category
, name
);
225 CustomParserUtils
.cleanup(CustomTxtTrace
.buildTraceTypeId(category
, name
));
226 } else if (xmlButton
.getSelection()) {
227 CustomXmlTraceDefinition
.delete(category
, name
);
228 CustomParserUtils
.cleanup(CustomXmlTrace
.buildTraceTypeId(category
, name
));
235 new Label(buttonContainer
, SWT
.NONE
); // filler
237 importButton
= new Button(buttonContainer
, SWT
.PUSH
);
238 importButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
239 importButton
.setText(Messages
.ManageCustomParsersDialog_ImportButtonLabel
);
240 importButton
.addSelectionListener(new SelectionListener() {
242 public void widgetDefaultSelected(SelectionEvent e
) {}
245 public void widgetSelected(SelectionEvent e
) {
246 FileDialog dialog
= new FileDialog(Display
.getCurrent().getActiveShell(), SWT
.OPEN
);
247 dialog
.setText(Messages
.ManageCustomParsersDialog_ImportParserSelection
);
248 dialog
.setFilterExtensions(new String
[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
249 String path
= dialog
.open();
251 CustomTraceDefinition
[] defs
= null;
252 if (txtButton
.getSelection()) {
253 defs
= CustomTxtTraceDefinition
.loadAll(path
);
254 } else if (xmlButton
.getSelection()) {
255 defs
= CustomXmlTraceDefinition
.loadAll(path
);
257 if (defs
!= null && defs
.length
> 0) {
258 for (CustomTraceDefinition def
: defs
) {
259 boolean ok
= checkNameConflict(def
);
266 MessageDialog
.openInformation(Display
.getCurrent().getActiveShell(),
267 Messages
.ManageCustomParsersDialog_ImportFailureTitle
,
268 Messages
.ManageCustomParsersDialog_ImportFailureMessage
);
274 exportButton
= new Button(buttonContainer
, SWT
.PUSH
);
275 exportButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
276 exportButton
.setText(Messages
.ManageCustomParsersDialog_ExportButtonLabel
);
277 exportButton
.setEnabled(false);
278 exportButton
.addSelectionListener(new SelectionListener() {
280 public void widgetDefaultSelected(SelectionEvent e
) {}
283 public void widgetSelected(SelectionEvent e
) {
284 FileDialog dialog
= new FileDialog(Display
.getCurrent().getActiveShell(), SWT
.SAVE
);
285 dialog
.setText(NLS
.bind(Messages
.ManageCustomParsersDialog_ExportParserSelection
, parserList
.getSelection()[0]));
286 dialog
.setFilterExtensions(new String
[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
287 String path
= dialog
.open();
289 String selection
= parserList
.getSelection()[0];
290 String category
= selection
.substring(0, selection
.indexOf(SEP
));
291 String name
= selection
.substring(selection
.indexOf(SEP
) + SEP_LEN
);
292 CustomTraceDefinition def
= null;
293 if (txtButton
.getSelection()) {
294 def
= CustomTxtTraceDefinition
.load(category
, name
);
295 } else if (xmlButton
.getSelection()) {
296 def
= CustomXmlTraceDefinition
.load(category
, name
);
307 getShell().setMinimumSize(300, 275);
312 protected void createButtonsForButtonBar(Composite parent
) {
313 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.CLOSE_LABEL
, false);
316 private void fillParserList() {
317 parserList
.removeAll();
318 if (txtButton
.getSelection()) {
319 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll(false)) {
320 parserList
.add(def
.categoryName
+ SEP
+ def
.definitionName
);
322 } else if (xmlButton
.getSelection()) {
323 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll(false)) {
324 parserList
.add(def
.categoryName
+ SEP
+ def
.definitionName
);
327 editButton
.setEnabled(false);
328 deleteButton
.setEnabled(false);
329 exportButton
.setEnabled(false);
332 private boolean checkNameConflict(CustomTraceDefinition def
) {
333 for (TraceTypeHelper helper
: TmfTraceType
.getTraceTypeHelpers()) {
334 if (def
.categoryName
.equals(helper
.getCategoryName()) &&
335 def
.definitionName
.equals(helper
.getName())) {
336 String newName
= findAvailableName(def
);
337 MessageDialog dialog
= new MessageDialog(
341 NLS
.bind(Messages
.ManageCustomParsersDialog_ConflictMessage
,
342 new Object
[] { def
.categoryName
, def
.definitionName
, newName
}),
343 MessageDialog
.QUESTION
,
344 new String
[] { Messages
.ManageCustomParsersDialog_ConflictRenameButtonLabel
,
345 Messages
.ManageCustomParsersDialog_ConflictSkipButtonLabel
},
347 int result
= dialog
.open();
349 def
.definitionName
= newName
;
358 private static String
findAvailableName(CustomTraceDefinition def
) {
360 Iterable
<TraceTypeHelper
> helpers
= TmfTraceType
.getTraceTypeHelpers();
362 String newName
= def
.definitionName
+ '(' + Integer
.toString(i
++) + ')';
363 boolean available
= true;
364 for (TraceTypeHelper helper
: helpers
) {
365 if (def
.categoryName
.equals(helper
.getCategoryName()) &&
366 newName
.equals(helper
.getName())) {