Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2010, 2014 Ericsson |
be222f56 PT |
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.internal.tmf.ui.dialogs; | |
14 | ||
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; | |
be222f56 PT |
22 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.wizards.CustomTxtParserWizard; |
23 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.wizards.CustomXmlParserWizard; | |
47aafe74 AM |
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; | |
9753257b PT |
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; | |
be222f56 PT |
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; | |
44 | ||
a0a88f65 AM |
45 | /** |
46 | * Dialog for custom text parsers. | |
47 | * | |
48 | * @author Patrick Tassé | |
49 | */ | |
be222f56 PT |
50 | public class ManageCustomParsersDialog extends Dialog { |
51 | ||
c22ca172 PT |
52 | private static final String SEP = " : "; //$NON-NLS-1$ |
53 | private static final int SEP_LEN = SEP.length(); | |
54 | ||
be222f56 PT |
55 | private static final Image image = Activator.getDefault().getImageFromPath("/icons/etool16/customparser_wizard.gif"); //$NON-NLS-1$ |
56 | ||
57 | Button txtButton; | |
58 | Button xmlButton; | |
59 | List parserList; | |
60 | Button newButton; | |
61 | Button editButton; | |
62 | Button deleteButton; | |
63 | Button importButton; | |
64 | Button exportButton; | |
65 | ||
a0a88f65 AM |
66 | /** |
67 | * Constructor | |
68 | * | |
69 | * @param parent | |
70 | * Parent shell of this dialog | |
71 | */ | |
be222f56 PT |
72 | public ManageCustomParsersDialog(Shell parent) { |
73 | super(parent); | |
74 | setShellStyle(SWT.RESIZE | SWT.MAX | getShellStyle()); | |
75 | } | |
76 | ||
be222f56 PT |
77 | @Override |
78 | protected Control createDialogArea(Composite parent) { | |
79 | getShell().setText(Messages.ManageCustomParsersDialog_DialogHeader); | |
80 | getShell().setImage(image); | |
81 | ||
82 | Composite composite = (Composite) super.createDialogArea(parent); | |
83 | composite.setLayout(new GridLayout(2, false)); | |
84 | ||
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; | |
89 | lcgl.marginWidth = 0; | |
90 | listContainer.setLayout(lcgl); | |
91 | ||
92 | Composite radioContainer = new Composite(listContainer, SWT.NONE); | |
93 | GridLayout rcgl = new GridLayout(2, true); | |
94 | rcgl.marginHeight = 0; | |
95 | rcgl.marginWidth = 0; | |
96 | radioContainer.setLayout(rcgl); | |
97 | ||
98 | txtButton = new Button(radioContainer, SWT.RADIO); | |
99 | txtButton.setText(Messages.ManageCustomParsersDialog_TextButtonLabel); | |
100 | txtButton.setSelection(true); | |
a0a88f65 | 101 | txtButton.addSelectionListener(new SelectionListener() { |
be222f56 | 102 | @Override |
a0a88f65 AM |
103 | public void widgetDefaultSelected(SelectionEvent e) {} |
104 | ||
be222f56 | 105 | @Override |
a0a88f65 | 106 | public void widgetSelected(SelectionEvent e) { |
be222f56 | 107 | fillParserList(); |
a0a88f65 AM |
108 | } |
109 | }); | |
be222f56 PT |
110 | |
111 | xmlButton = new Button(radioContainer, SWT.RADIO); | |
112 | xmlButton.setText("XML"); //$NON-NLS-1$ | |
a0a88f65 | 113 | xmlButton.addSelectionListener(new SelectionListener() { |
be222f56 | 114 | @Override |
a0a88f65 AM |
115 | public void widgetDefaultSelected(SelectionEvent e) { |
116 | } | |
117 | ||
be222f56 | 118 | @Override |
a0a88f65 | 119 | public void widgetSelected(SelectionEvent e) { |
be222f56 | 120 | fillParserList(); |
a0a88f65 AM |
121 | } |
122 | }); | |
be222f56 PT |
123 | |
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)); | |
a0a88f65 | 126 | parserList.addSelectionListener(new SelectionListener() { |
be222f56 | 127 | @Override |
a0a88f65 AM |
128 | public void widgetDefaultSelected(SelectionEvent e) {} |
129 | ||
be222f56 | 130 | @Override |
a0a88f65 | 131 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
132 | if (parserList.getSelectionCount() == 0) { |
133 | editButton.setEnabled(false); | |
134 | deleteButton.setEnabled(false); | |
135 | exportButton.setEnabled(false); | |
136 | } else { | |
137 | editButton.setEnabled(true); | |
138 | deleteButton.setEnabled(true); | |
139 | exportButton.setEnabled(true); | |
140 | } | |
a0a88f65 AM |
141 | } |
142 | }); | |
be222f56 PT |
143 | |
144 | Composite buttonContainer = new Composite(composite, SWT.NULL); | |
145 | buttonContainer.setLayout(new GridLayout()); | |
146 | buttonContainer.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false)); | |
147 | ||
148 | newButton = new Button(buttonContainer, SWT.PUSH); | |
149 | newButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
150 | newButton.setText(Messages.ManageCustomParsersDialog_NewButtonLabel); | |
a0a88f65 | 151 | newButton.addSelectionListener(new SelectionListener() { |
be222f56 | 152 | @Override |
a0a88f65 AM |
153 | public void widgetDefaultSelected(SelectionEvent e) {} |
154 | ||
be222f56 | 155 | @Override |
a0a88f65 | 156 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
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()); | |
162 | } | |
163 | if (dialog != null) { | |
164 | dialog.open(); | |
165 | if (dialog.getReturnCode() == Window.OK) { | |
166 | fillParserList(); | |
167 | } | |
168 | } | |
a0a88f65 AM |
169 | } |
170 | }); | |
be222f56 PT |
171 | |
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); | |
a0a88f65 | 176 | editButton.addSelectionListener(new SelectionListener() { |
be222f56 | 177 | @Override |
a0a88f65 AM |
178 | public void widgetDefaultSelected(SelectionEvent e) {} |
179 | ||
be222f56 | 180 | @Override |
a0a88f65 | 181 | public void widgetSelected(SelectionEvent e) { |
be222f56 | 182 | WizardDialog dialog = null; |
c22ca172 PT |
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); | |
be222f56 PT |
186 | if (txtButton.getSelection()) { |
187 | dialog = new WizardDialog(getShell(), | |
c22ca172 | 188 | new CustomTxtParserWizard(CustomTxtTraceDefinition.load(category, name))); |
be222f56 PT |
189 | } else if (xmlButton.getSelection()) { |
190 | dialog = new WizardDialog(getShell(), | |
c22ca172 | 191 | new CustomXmlParserWizard(CustomXmlTraceDefinition.load(category, name))); |
be222f56 PT |
192 | } |
193 | if (dialog != null) { | |
194 | dialog.open(); | |
195 | if (dialog.getReturnCode() == Window.OK) { | |
196 | fillParserList(); | |
197 | } | |
198 | } | |
a0a88f65 AM |
199 | } |
200 | }); | |
be222f56 PT |
201 | |
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); | |
a0a88f65 | 206 | deleteButton.addSelectionListener(new SelectionListener() { |
be222f56 | 207 | @Override |
a0a88f65 AM |
208 | public void widgetDefaultSelected(SelectionEvent e) {} |
209 | ||
be222f56 | 210 | @Override |
a0a88f65 | 211 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
212 | boolean confirm = MessageDialog.openQuestion( |
213 | getShell(), | |
214 | Messages.ManageCustomParsersDialog_DeleteParserDialogHeader, | |
89151d6a | 215 | NLS.bind(Messages.ManageCustomParsersDialog_DeleteConfirmation, parserList.getSelection()[0])); |
be222f56 | 216 | if (confirm) { |
c22ca172 PT |
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); | |
be222f56 | 220 | if (txtButton.getSelection()) { |
c22ca172 | 221 | CustomTxtTraceDefinition.delete(category, name); |
be222f56 | 222 | } else if (xmlButton.getSelection()) { |
c22ca172 | 223 | CustomXmlTraceDefinition.delete(category, name); |
be222f56 PT |
224 | } |
225 | fillParserList(); | |
226 | } | |
a0a88f65 AM |
227 | } |
228 | }); | |
be222f56 PT |
229 | |
230 | new Label(buttonContainer, SWT.NONE); // filler | |
231 | ||
232 | importButton = new Button(buttonContainer, SWT.PUSH); | |
233 | importButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
234 | importButton.setText(Messages.ManageCustomParsersDialog_ImportButtonLabel); | |
a0a88f65 | 235 | importButton.addSelectionListener(new SelectionListener() { |
be222f56 | 236 | @Override |
a0a88f65 AM |
237 | public void widgetDefaultSelected(SelectionEvent e) {} |
238 | ||
be222f56 | 239 | @Override |
a0a88f65 | 240 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
241 | FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN); |
242 | dialog.setText(Messages.ManageCustomParsersDialog_ImportParserSelection); | |
a0a88f65 | 243 | dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ |
be222f56 PT |
244 | String path = dialog.open(); |
245 | if (path != null) { | |
246 | CustomTraceDefinition[] defs = null; | |
247 | if (txtButton.getSelection()) { | |
248 | defs = CustomTxtTraceDefinition.loadAll(path); | |
249 | } else if (xmlButton.getSelection()) { | |
250 | defs = CustomXmlTraceDefinition.loadAll(path); | |
251 | } | |
252 | if (defs != null && defs.length > 0) { | |
253 | for (CustomTraceDefinition def : defs) { | |
9753257b PT |
254 | boolean ok = checkNameConflict(def); |
255 | if (ok) { | |
256 | def.save(); | |
257 | } | |
be222f56 PT |
258 | } |
259 | fillParserList(); | |
260 | } | |
261 | } | |
a0a88f65 AM |
262 | } |
263 | }); | |
be222f56 PT |
264 | |
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); | |
a0a88f65 | 269 | exportButton.addSelectionListener(new SelectionListener() { |
be222f56 | 270 | @Override |
a0a88f65 AM |
271 | public void widgetDefaultSelected(SelectionEvent e) {} |
272 | ||
be222f56 | 273 | @Override |
a0a88f65 | 274 | public void widgetSelected(SelectionEvent e) { |
be222f56 | 275 | FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE); |
89151d6a | 276 | dialog.setText(NLS.bind(Messages.ManageCustomParsersDialog_ExportParserSelection, parserList.getSelection()[0])); |
a0a88f65 | 277 | dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ |
be222f56 PT |
278 | String path = dialog.open(); |
279 | if (path != null) { | |
c22ca172 PT |
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); | |
be222f56 PT |
283 | CustomTraceDefinition def = null; |
284 | if (txtButton.getSelection()) { | |
c22ca172 | 285 | def = CustomTxtTraceDefinition.load(category, name); |
be222f56 | 286 | } else if (xmlButton.getSelection()) { |
c22ca172 | 287 | def = CustomXmlTraceDefinition.load(category, name); |
be222f56 PT |
288 | } |
289 | if (def != null) { | |
290 | def.save(path); | |
291 | } | |
292 | } | |
a0a88f65 AM |
293 | } |
294 | }); | |
be222f56 PT |
295 | |
296 | fillParserList(); | |
297 | ||
298 | getShell().setMinimumSize(300, 275); | |
299 | return composite; | |
300 | } | |
301 | ||
be222f56 PT |
302 | @Override |
303 | protected void createButtonsForButtonBar(Composite parent) { | |
304 | createButton(parent, IDialogConstants.OK_ID, IDialogConstants.CLOSE_LABEL, false); | |
305 | } | |
306 | ||
307 | private void fillParserList() { | |
308 | parserList.removeAll(); | |
309 | if (txtButton.getSelection()) { | |
54af96ab | 310 | for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll(false)) { |
c22ca172 | 311 | parserList.add(def.categoryName + SEP + def.definitionName); |
be222f56 PT |
312 | } |
313 | } else if (xmlButton.getSelection()) { | |
54af96ab | 314 | for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll(false)) { |
c22ca172 | 315 | parserList.add(def.categoryName + SEP + def.definitionName); |
be222f56 PT |
316 | } |
317 | } | |
318 | editButton.setEnabled(false); | |
319 | deleteButton.setEnabled(false); | |
320 | exportButton.setEnabled(false); | |
321 | } | |
322 | ||
9753257b PT |
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( | |
329 | getShell(), | |
330 | null, | |
331 | null, | |
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 }, | |
337 | 0); | |
338 | int result = dialog.open(); | |
339 | if (result == 0) { | |
340 | def.definitionName = newName; | |
341 | return true; | |
342 | } | |
343 | return false; | |
344 | } | |
345 | } | |
346 | return true; | |
347 | } | |
348 | ||
349 | private static String findAvailableName(CustomTraceDefinition def) { | |
350 | int i = 2; | |
351 | Iterable<TraceTypeHelper> helpers = TmfTraceType.getTraceTypeHelpers(); | |
352 | while (true) { | |
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())) { | |
358 | available = false; | |
359 | break; | |
360 | } | |
361 | } | |
362 | if (available) { | |
363 | return newName; | |
364 | } | |
365 | } | |
366 | } | |
be222f56 | 367 | } |