1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 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
.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
.linuxtools
.internal
.tmf
.ui
.Activator
;
21 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
22 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.wizards
.CustomTxtParserWizard
;
23 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.wizards
.CustomXmlParserWizard
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTraceDefinition
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomTxtTraceDefinition
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TmfTraceType
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.project
.model
.TraceTypeHelper
;
29 import org
.eclipse
.osgi
.util
.NLS
;
30 import org
.eclipse
.swt
.SWT
;
31 import org
.eclipse
.swt
.events
.SelectionEvent
;
32 import org
.eclipse
.swt
.events
.SelectionListener
;
33 import org
.eclipse
.swt
.graphics
.Image
;
34 import org
.eclipse
.swt
.layout
.GridData
;
35 import org
.eclipse
.swt
.layout
.GridLayout
;
36 import org
.eclipse
.swt
.widgets
.Button
;
37 import org
.eclipse
.swt
.widgets
.Composite
;
38 import org
.eclipse
.swt
.widgets
.Control
;
39 import org
.eclipse
.swt
.widgets
.Display
;
40 import org
.eclipse
.swt
.widgets
.FileDialog
;
41 import org
.eclipse
.swt
.widgets
.Label
;
42 import org
.eclipse
.swt
.widgets
.List
;
43 import org
.eclipse
.swt
.widgets
.Shell
;
46 * Dialog for custom text parsers.
48 * @author Patrick Tassé
50 public class ManageCustomParsersDialog
extends Dialog
{
52 private static final String SEP
= " : "; //$NON-NLS-1$
53 private static final int SEP_LEN
= SEP
.length();
55 private static final Image image
= Activator
.getDefault().getImageFromPath("/icons/etool16/customparser_wizard.gif"); //$NON-NLS-1$
70 * Parent shell of this dialog
72 public ManageCustomParsersDialog(Shell parent
) {
74 setShellStyle(SWT
.RESIZE
| SWT
.MAX
| getShellStyle());
78 protected Control
createDialogArea(Composite parent
) {
79 getShell().setText(Messages
.ManageCustomParsersDialog_DialogHeader
);
80 getShell().setImage(image
);
82 Composite composite
= (Composite
) super.createDialogArea(parent
);
83 composite
.setLayout(new GridLayout(2, false));
85 Composite listContainer
= new Composite(composite
, SWT
.NONE
);
86 listContainer
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
87 GridLayout lcgl
= new GridLayout();
88 lcgl
.marginHeight
= 0;
90 listContainer
.setLayout(lcgl
);
92 Composite radioContainer
= new Composite(listContainer
, SWT
.NONE
);
93 GridLayout rcgl
= new GridLayout(2, true);
94 rcgl
.marginHeight
= 0;
96 radioContainer
.setLayout(rcgl
);
98 txtButton
= new Button(radioContainer
, SWT
.RADIO
);
99 txtButton
.setText(Messages
.ManageCustomParsersDialog_TextButtonLabel
);
100 txtButton
.setSelection(true);
101 txtButton
.addSelectionListener(new SelectionListener() {
103 public void widgetDefaultSelected(SelectionEvent e
) {}
106 public void widgetSelected(SelectionEvent e
) {
111 xmlButton
= new Button(radioContainer
, SWT
.RADIO
);
112 xmlButton
.setText("XML"); //$NON-NLS-1$
113 xmlButton
.addSelectionListener(new SelectionListener() {
115 public void widgetDefaultSelected(SelectionEvent e
) {
119 public void widgetSelected(SelectionEvent e
) {
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() {
128 public void widgetDefaultSelected(SelectionEvent e
) {}
131 public void widgetSelected(SelectionEvent e
) {
132 if (parserList
.getSelectionCount() == 0) {
133 editButton
.setEnabled(false);
134 deleteButton
.setEnabled(false);
135 exportButton
.setEnabled(false);
137 editButton
.setEnabled(true);
138 deleteButton
.setEnabled(true);
139 exportButton
.setEnabled(true);
144 Composite buttonContainer
= new Composite(composite
, SWT
.NULL
);
145 buttonContainer
.setLayout(new GridLayout());
146 buttonContainer
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, false, false));
148 newButton
= new Button(buttonContainer
, SWT
.PUSH
);
149 newButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
150 newButton
.setText(Messages
.ManageCustomParsersDialog_NewButtonLabel
);
151 newButton
.addSelectionListener(new SelectionListener() {
153 public void widgetDefaultSelected(SelectionEvent e
) {}
156 public void widgetSelected(SelectionEvent e
) {
157 WizardDialog dialog
= null;
158 if (txtButton
.getSelection()) {
159 dialog
= new WizardDialog(getShell(), new CustomTxtParserWizard());
160 } else if (xmlButton
.getSelection()) {
161 dialog
= new WizardDialog(getShell(), new CustomXmlParserWizard());
163 if (dialog
!= null) {
165 if (dialog
.getReturnCode() == Window
.OK
) {
172 editButton
= new Button(buttonContainer
, SWT
.PUSH
);
173 editButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
174 editButton
.setText(Messages
.ManageCustomParsersDialog_EditButtonLabel
);
175 editButton
.setEnabled(false);
176 editButton
.addSelectionListener(new SelectionListener() {
178 public void widgetDefaultSelected(SelectionEvent e
) {}
181 public void widgetSelected(SelectionEvent e
) {
182 WizardDialog dialog
= null;
183 String selection
= parserList
.getSelection()[0];
184 String category
= selection
.substring(0, selection
.indexOf(SEP
));
185 String name
= selection
.substring(selection
.indexOf(SEP
) + SEP_LEN
);
186 if (txtButton
.getSelection()) {
187 dialog
= new WizardDialog(getShell(),
188 new CustomTxtParserWizard(CustomTxtTraceDefinition
.load(category
, name
)));
189 } else if (xmlButton
.getSelection()) {
190 dialog
= new WizardDialog(getShell(),
191 new CustomXmlParserWizard(CustomXmlTraceDefinition
.load(category
, name
)));
193 if (dialog
!= null) {
195 if (dialog
.getReturnCode() == Window
.OK
) {
202 deleteButton
= new Button(buttonContainer
, SWT
.PUSH
);
203 deleteButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
204 deleteButton
.setText(Messages
.ManageCustomParsersDialog_DeleteButtonLabel
);
205 deleteButton
.setEnabled(false);
206 deleteButton
.addSelectionListener(new SelectionListener() {
208 public void widgetDefaultSelected(SelectionEvent e
) {}
211 public void widgetSelected(SelectionEvent e
) {
212 boolean confirm
= MessageDialog
.openQuestion(
214 Messages
.ManageCustomParsersDialog_DeleteParserDialogHeader
,
215 NLS
.bind(Messages
.ManageCustomParsersDialog_DeleteConfirmation
, parserList
.getSelection()[0]));
217 String selection
= parserList
.getSelection()[0];
218 String category
= selection
.substring(0, selection
.indexOf(SEP
));
219 String name
= selection
.substring(selection
.indexOf(SEP
) + SEP_LEN
);
220 if (txtButton
.getSelection()) {
221 CustomTxtTraceDefinition
.delete(category
, name
);
222 } else if (xmlButton
.getSelection()) {
223 CustomXmlTraceDefinition
.delete(category
, name
);
230 new Label(buttonContainer
, SWT
.NONE
); // filler
232 importButton
= new Button(buttonContainer
, SWT
.PUSH
);
233 importButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
234 importButton
.setText(Messages
.ManageCustomParsersDialog_ImportButtonLabel
);
235 importButton
.addSelectionListener(new SelectionListener() {
237 public void widgetDefaultSelected(SelectionEvent e
) {}
240 public void widgetSelected(SelectionEvent e
) {
241 FileDialog dialog
= new FileDialog(Display
.getCurrent().getActiveShell(), SWT
.OPEN
);
242 dialog
.setText(Messages
.ManageCustomParsersDialog_ImportParserSelection
);
243 dialog
.setFilterExtensions(new String
[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
244 String path
= dialog
.open();
246 CustomTraceDefinition
[] defs
= null;
247 if (txtButton
.getSelection()) {
248 defs
= CustomTxtTraceDefinition
.loadAll(path
);
249 } else if (xmlButton
.getSelection()) {
250 defs
= CustomXmlTraceDefinition
.loadAll(path
);
252 if (defs
!= null && defs
.length
> 0) {
253 for (CustomTraceDefinition def
: defs
) {
254 boolean ok
= checkNameConflict(def
);
265 exportButton
= new Button(buttonContainer
, SWT
.PUSH
);
266 exportButton
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
267 exportButton
.setText(Messages
.ManageCustomParsersDialog_ExportButtonLabel
);
268 exportButton
.setEnabled(false);
269 exportButton
.addSelectionListener(new SelectionListener() {
271 public void widgetDefaultSelected(SelectionEvent e
) {}
274 public void widgetSelected(SelectionEvent e
) {
275 FileDialog dialog
= new FileDialog(Display
.getCurrent().getActiveShell(), SWT
.SAVE
);
276 dialog
.setText(NLS
.bind(Messages
.ManageCustomParsersDialog_ExportParserSelection
, parserList
.getSelection()[0]));
277 dialog
.setFilterExtensions(new String
[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
278 String path
= dialog
.open();
280 String selection
= parserList
.getSelection()[0];
281 String category
= selection
.substring(0, selection
.indexOf(SEP
));
282 String name
= selection
.substring(selection
.indexOf(SEP
) + SEP_LEN
);
283 CustomTraceDefinition def
= null;
284 if (txtButton
.getSelection()) {
285 def
= CustomTxtTraceDefinition
.load(category
, name
);
286 } else if (xmlButton
.getSelection()) {
287 def
= CustomXmlTraceDefinition
.load(category
, name
);
298 getShell().setMinimumSize(300, 275);
303 protected void createButtonsForButtonBar(Composite parent
) {
304 createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.CLOSE_LABEL
, false);
307 private void fillParserList() {
308 parserList
.removeAll();
309 if (txtButton
.getSelection()) {
310 for (CustomTxtTraceDefinition def
: CustomTxtTraceDefinition
.loadAll(false)) {
311 parserList
.add(def
.categoryName
+ SEP
+ def
.definitionName
);
313 } else if (xmlButton
.getSelection()) {
314 for (CustomXmlTraceDefinition def
: CustomXmlTraceDefinition
.loadAll(false)) {
315 parserList
.add(def
.categoryName
+ SEP
+ def
.definitionName
);
318 editButton
.setEnabled(false);
319 deleteButton
.setEnabled(false);
320 exportButton
.setEnabled(false);
323 private boolean checkNameConflict(CustomTraceDefinition def
) {
324 for (TraceTypeHelper helper
: TmfTraceType
.getTraceTypeHelpers()) {
325 if (def
.categoryName
.equals(helper
.getCategoryName()) &&
326 def
.definitionName
.equals(helper
.getName())) {
327 String newName
= findAvailableName(def
);
328 MessageDialog dialog
= new MessageDialog(
332 NLS
.bind(Messages
.ManageCustomParsersDialog_ConflictMessage
,
333 new Object
[] { def
.categoryName
, def
.definitionName
, newName
}),
334 MessageDialog
.QUESTION
,
335 new String
[] { Messages
.ManageCustomParsersDialog_ConflictRenameButtonLabel
,
336 Messages
.ManageCustomParsersDialog_ConflictSkipButtonLabel
},
338 int result
= dialog
.open();
340 def
.definitionName
= newName
;
349 private static String
findAvailableName(CustomTraceDefinition def
) {
351 Iterable
<TraceTypeHelper
> helpers
= TmfTraceType
.getTraceTypeHelpers();
353 String newName
= def
.definitionName
+ '(' + Integer
.toString(i
++) + ')';
354 boolean available
= true;
355 for (TraceTypeHelper helper
: helpers
) {
356 if (def
.categoryName
.equals(helper
.getCategoryName()) &&
357 newName
.equals(helper
.getName())) {