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
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.dialog
.TmfFileDialogFactory
;
50 * Dialog for custom text parsers.
52 * @author Patrick Tassé
54 public class ManageCustomParsersDialog
extends Dialog
{
56 private static final String SEP
= " : "; //$NON-NLS-1$
57 private static final int SEP_LEN
= SEP
.length();
59 private static final Image image
= Activator
.getDefault().getImageFromPath("/icons/etool16/customparser_wizard.gif"); //$NON-NLS-1$
74 * Parent shell of this dialog
76 public ManageCustomParsersDialog(Shell parent
) {
78 setShellStyle(SWT
.RESIZE
| SWT
.MAX
| getShellStyle());
82 protected Control
createDialogArea(Composite parent
) {
83 getShell().setText(Messages
.ManageCustomParsersDialog_DialogHeader
);
84 getShell().setImage(image
);
86 Composite composite
= (Composite
) super.createDialogArea(parent
);
87 composite
.setLayout(new GridLayout(2, false));
89 Composite listContainer
= new Composite(composite
, SWT
.NONE
);
90 listContainer
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
91 GridLayout lcgl
= new GridLayout();
92 lcgl
.marginHeight
= 0;
94 listContainer
.setLayout(lcgl
);
96 Composite radioContainer
= new Composite(listContainer
, SWT
.NONE
);
97 GridLayout rcgl
= new GridLayout(2, true);
98 rcgl
.marginHeight
= 0;
100 radioContainer
.setLayout(rcgl
);
102 txtButton
= new Button(radioContainer
, SWT
.RADIO
);
103 txtButton
.setText(Messages
.ManageCustomParsersDialog_TextButtonLabel
);
104 txtButton
.setSelection(true);
105 txtButton
.addSelectionListener(new SelectionListener() {
107 public void widgetDefaultSelected(SelectionEvent e
) {}
110 public void widgetSelected(SelectionEvent e
) {
115 xmlButton
= new Button(radioContainer
, SWT
.RADIO
);
116 xmlButton
.setText("XML"); //$NON-NLS-1$
117 xmlButton
.addSelectionListener(new SelectionListener() {
119 public void widgetDefaultSelected(SelectionEvent e
) {
123 public void widgetSelected(SelectionEvent e
) {
128 parserList
= new List(listContainer
, SWT
.SINGLE
| SWT
.BORDER
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
129 parserList
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
130 parserList
.addSelectionListener(new SelectionListener() {
132 public void widgetDefaultSelected(SelectionEvent e
) {}
135 public void widgetSelected(SelectionEvent e
) {
136 if (parserList
.getSelectionCount() == 0) {
137 editButton
.setEnabled(false);
138 deleteButton
.setEnabled(false);
139 exportButton
.setEnabled(false);
141 editButton
.setEnabled(true);
142 deleteButton
.setEnabled(true);
143 exportButton
.setEnabled(true);
148 Composite buttonContainer
= new Composite(composite
, SWT
.NULL
);
149 buttonContainer
.setLayout(new GridLayout());
150 buttonContainer
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, false, false));
152 newButton
= new Button(buttonContainer
, SWT
.PUSH
);
153 newButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
154 newButton
.setText(Messages
.ManageCustomParsersDialog_NewButtonLabel
);
155 newButton
.addSelectionListener(new SelectionListener() {
157 public void widgetDefaultSelected(SelectionEvent e
) {}
160 public void widgetSelected(SelectionEvent e
) {
161 WizardDialog dialog
= null;
162 if (txtButton
.getSelection()) {
163 dialog
= new WizardDialog(getShell(), new CustomTxtParserWizard());
164 } else if (xmlButton
.getSelection()) {
165 dialog
= new WizardDialog(getShell(), new CustomXmlParserWizard());
167 if (dialog
!= null) {
169 if (dialog
.getReturnCode() == Window
.OK
) {
176 editButton
= new Button(buttonContainer
, SWT
.PUSH
);
177 editButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
178 editButton
.setText(Messages
.ManageCustomParsersDialog_EditButtonLabel
);
179 editButton
.setEnabled(false);
180 editButton
.addSelectionListener(new SelectionListener() {
182 public void widgetDefaultSelected(SelectionEvent e
) {}
185 public void widgetSelected(SelectionEvent e
) {
186 WizardDialog dialog
= null;
187 String selection
= parserList
.getSelection()[0];
188 String category
= selection
.substring(0, selection
.indexOf(SEP
));
189 String name
= selection
.substring(selection
.indexOf(SEP
) + SEP_LEN
);
190 if (txtButton
.getSelection()) {
191 dialog
= new WizardDialog(getShell(),
192 new CustomTxtParserWizard(CustomTxtTraceDefinition
.load(category
, name
)));
193 } else if (xmlButton
.getSelection()) {
194 dialog
= new WizardDialog(getShell(),
195 new CustomXmlParserWizard(CustomXmlTraceDefinition
.load(category
, name
)));
197 if (dialog
!= null) {
199 if (dialog
.getReturnCode() == Window
.OK
) {
206 deleteButton
= new Button(buttonContainer
, SWT
.PUSH
);
207 deleteButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
208 deleteButton
.setText(Messages
.ManageCustomParsersDialog_DeleteButtonLabel
);
209 deleteButton
.setEnabled(false);
210 deleteButton
.addSelectionListener(new SelectionListener() {
212 public void widgetDefaultSelected(SelectionEvent e
) {}
215 public void widgetSelected(SelectionEvent e
) {
216 boolean confirm
= MessageDialog
.openQuestion(
218 Messages
.ManageCustomParsersDialog_DeleteParserDialogHeader
,
219 NLS
.bind(Messages
.ManageCustomParsersDialog_DeleteConfirmation
, parserList
.getSelection()[0]));
221 String selection
= parserList
.getSelection()[0];
222 String category
= selection
.substring(0, selection
.indexOf(SEP
));
223 String name
= selection
.substring(selection
.indexOf(SEP
) + SEP_LEN
);
224 if (txtButton
.getSelection()) {
225 CustomTxtTraceDefinition
.delete(category
, name
);
226 CustomParserUtils
.cleanup(CustomTxtTrace
.buildTraceTypeId(category
, name
));
227 } else if (xmlButton
.getSelection()) {
228 CustomXmlTraceDefinition
.delete(category
, name
);
229 CustomParserUtils
.cleanup(CustomXmlTrace
.buildTraceTypeId(category
, name
));
236 new Label(buttonContainer
, SWT
.NONE
); // filler
238 importButton
= new Button(buttonContainer
, SWT
.PUSH
);
239 importButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
240 importButton
.setText(Messages
.ManageCustomParsersDialog_ImportButtonLabel
);
241 importButton
.addSelectionListener(new SelectionListener() {
243 public void widgetDefaultSelected(SelectionEvent e
) {}
246 public void widgetSelected(SelectionEvent e
) {
247 FileDialog dialog
= TmfFileDialogFactory
.create(Display
.getCurrent().getActiveShell(), SWT
.OPEN
);
248 dialog
.setText(Messages
.ManageCustomParsersDialog_ImportParserSelection
);
249 dialog
.setFilterExtensions(new String
[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
250 String path
= dialog
.open();
252 CustomTraceDefinition
[] defs
= null;
253 if (txtButton
.getSelection()) {
254 defs
= CustomTxtTraceDefinition
.loadAll(path
);
255 } else if (xmlButton
.getSelection()) {
256 defs
= CustomXmlTraceDefinition
.loadAll(path
);
258 if (defs
!= null && defs
.length
> 0) {
259 for (CustomTraceDefinition def
: defs
) {
260 boolean ok
= checkNameConflict(def
);
267 MessageDialog
.openInformation(Display
.getCurrent().getActiveShell(),
268 Messages
.ManageCustomParsersDialog_ImportFailureTitle
,
269 Messages
.ManageCustomParsersDialog_ImportFailureMessage
);
275 exportButton
= new Button(buttonContainer
, SWT
.PUSH
);
276 exportButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
277 exportButton
.setText(Messages
.ManageCustomParsersDialog_ExportButtonLabel
);
278 exportButton
.setEnabled(false);
279 exportButton
.addSelectionListener(new SelectionListener() {
281 public void widgetDefaultSelected(SelectionEvent e
) {}
284 public void widgetSelected(SelectionEvent e
) {
285 FileDialog dialog
= TmfFileDialogFactory
.create(Display
.getCurrent().getActiveShell(), SWT
.SAVE
);
286 dialog
.setText(NLS
.bind(Messages
.ManageCustomParsersDialog_ExportParserSelection
, parserList
.getSelection()[0]));
287 dialog
.setFilterExtensions(new String
[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
288 String path
= dialog
.open();
290 String selection
= parserList
.getSelection()[0];
291 String category
= selection
.substring(0, selection
.indexOf(SEP
));
292 String name
= selection
.substring(selection
.indexOf(SEP
) + SEP_LEN
);
293 CustomTraceDefinition def
= null;
294 if (txtButton
.getSelection()) {
295 def
= CustomTxtTraceDefinition
.load(category
, name
);
296 } else if (xmlButton
.getSelection()) {
297 def
= CustomXmlTraceDefinition
.load(category
, name
);
308 getShell().setMinimumSize(300, 275);
313 protected void createButtonsForButtonBar(Composite parent
) {
314 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.CLOSE_LABEL
, false);
317 private void fillParserList() {
318 parserList
.removeAll();
319 if (txtButton
.getSelection()) {
320 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll(false)) {
321 parserList
.add(def
.categoryName
+ SEP
+ def
.definitionName
);
323 } else if (xmlButton
.getSelection()) {
324 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll(false)) {
325 parserList
.add(def
.categoryName
+ SEP
+ def
.definitionName
);
328 editButton
.setEnabled(false);
329 deleteButton
.setEnabled(false);
330 exportButton
.setEnabled(false);
333 private boolean checkNameConflict(CustomTraceDefinition def
) {
334 for (TraceTypeHelper helper
: TmfTraceType
.getTraceTypeHelpers()) {
335 if (def
.categoryName
.equals(helper
.getCategoryName()) &&
336 def
.definitionName
.equals(helper
.getName())) {
337 String newName
= findAvailableName(def
);
338 MessageDialog dialog
= new MessageDialog(
342 NLS
.bind(Messages
.ManageCustomParsersDialog_ConflictMessage
,
343 new Object
[] { def
.categoryName
, def
.definitionName
, newName
}),
344 MessageDialog
.QUESTION
,
345 new String
[] { Messages
.ManageCustomParsersDialog_ConflictRenameButtonLabel
,
346 Messages
.ManageCustomParsersDialog_ConflictSkipButtonLabel
},
348 int result
= dialog
.open();
350 def
.definitionName
= newName
;
359 private static String
findAvailableName(CustomTraceDefinition def
) {
361 Iterable
<TraceTypeHelper
> helpers
= TmfTraceType
.getTraceTypeHelpers();
363 String newName
= def
.definitionName
+ '(' + Integer
.toString(i
++) + ')';
364 boolean available
= true;
365 for (TraceTypeHelper helper
: helpers
) {
366 if (def
.categoryName
.equals(helper
.getCategoryName()) &&
367 newName
.equals(helper
.getName())) {