Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
c8422608 | 2 | * Copyright (c) 2010, 2013 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; | |
22 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTraceDefinition; | |
23 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition; | |
24 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition; | |
25 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.wizards.CustomTxtParserWizard; | |
26 | import org.eclipse.linuxtools.internal.tmf.ui.parsers.wizards.CustomXmlParserWizard; | |
27 | import org.eclipse.swt.SWT; | |
28 | import org.eclipse.swt.events.SelectionEvent; | |
29 | import org.eclipse.swt.events.SelectionListener; | |
30 | import org.eclipse.swt.graphics.Image; | |
31 | import org.eclipse.swt.layout.GridData; | |
32 | import org.eclipse.swt.layout.GridLayout; | |
33 | import org.eclipse.swt.widgets.Button; | |
34 | import org.eclipse.swt.widgets.Composite; | |
35 | import org.eclipse.swt.widgets.Control; | |
36 | import org.eclipse.swt.widgets.Display; | |
37 | import org.eclipse.swt.widgets.FileDialog; | |
38 | import org.eclipse.swt.widgets.Label; | |
39 | import org.eclipse.swt.widgets.List; | |
40 | import org.eclipse.swt.widgets.Shell; | |
41 | ||
a0a88f65 AM |
42 | /** |
43 | * Dialog for custom text parsers. | |
44 | * | |
45 | * @author Patrick Tassé | |
46 | */ | |
be222f56 PT |
47 | public class ManageCustomParsersDialog extends Dialog { |
48 | ||
49 | private static final Image image = Activator.getDefault().getImageFromPath("/icons/etool16/customparser_wizard.gif"); //$NON-NLS-1$ | |
50 | ||
51 | Button txtButton; | |
52 | Button xmlButton; | |
53 | List parserList; | |
54 | Button newButton; | |
55 | Button editButton; | |
56 | Button deleteButton; | |
57 | Button importButton; | |
58 | Button exportButton; | |
59 | ||
a0a88f65 AM |
60 | /** |
61 | * Constructor | |
62 | * | |
63 | * @param parent | |
64 | * Parent shell of this dialog | |
65 | */ | |
be222f56 PT |
66 | public ManageCustomParsersDialog(Shell parent) { |
67 | super(parent); | |
68 | setShellStyle(SWT.RESIZE | SWT.MAX | getShellStyle()); | |
69 | } | |
70 | ||
71 | /* (non-Javadoc) | |
72 | * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) | |
73 | */ | |
74 | @Override | |
75 | protected Control createDialogArea(Composite parent) { | |
76 | getShell().setText(Messages.ManageCustomParsersDialog_DialogHeader); | |
77 | getShell().setImage(image); | |
78 | ||
79 | Composite composite = (Composite) super.createDialogArea(parent); | |
80 | composite.setLayout(new GridLayout(2, false)); | |
81 | ||
82 | Composite listContainer = new Composite(composite, SWT.NONE); | |
83 | listContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
84 | GridLayout lcgl = new GridLayout(); | |
85 | lcgl.marginHeight = 0; | |
86 | lcgl.marginWidth = 0; | |
87 | listContainer.setLayout(lcgl); | |
88 | ||
89 | Composite radioContainer = new Composite(listContainer, SWT.NONE); | |
90 | GridLayout rcgl = new GridLayout(2, true); | |
91 | rcgl.marginHeight = 0; | |
92 | rcgl.marginWidth = 0; | |
93 | radioContainer.setLayout(rcgl); | |
94 | ||
95 | txtButton = new Button(radioContainer, SWT.RADIO); | |
96 | txtButton.setText(Messages.ManageCustomParsersDialog_TextButtonLabel); | |
97 | txtButton.setSelection(true); | |
a0a88f65 | 98 | txtButton.addSelectionListener(new SelectionListener() { |
be222f56 | 99 | @Override |
a0a88f65 AM |
100 | public void widgetDefaultSelected(SelectionEvent e) {} |
101 | ||
be222f56 | 102 | @Override |
a0a88f65 | 103 | public void widgetSelected(SelectionEvent e) { |
be222f56 | 104 | fillParserList(); |
a0a88f65 AM |
105 | } |
106 | }); | |
be222f56 PT |
107 | |
108 | xmlButton = new Button(radioContainer, SWT.RADIO); | |
109 | xmlButton.setText("XML"); //$NON-NLS-1$ | |
a0a88f65 | 110 | xmlButton.addSelectionListener(new SelectionListener() { |
be222f56 | 111 | @Override |
a0a88f65 AM |
112 | public void widgetDefaultSelected(SelectionEvent e) { |
113 | } | |
114 | ||
be222f56 | 115 | @Override |
a0a88f65 | 116 | public void widgetSelected(SelectionEvent e) { |
be222f56 | 117 | fillParserList(); |
a0a88f65 AM |
118 | } |
119 | }); | |
be222f56 PT |
120 | |
121 | parserList = new List(listContainer, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); | |
122 | parserList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
a0a88f65 | 123 | parserList.addSelectionListener(new SelectionListener() { |
be222f56 | 124 | @Override |
a0a88f65 AM |
125 | public void widgetDefaultSelected(SelectionEvent e) {} |
126 | ||
be222f56 | 127 | @Override |
a0a88f65 | 128 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
129 | if (parserList.getSelectionCount() == 0) { |
130 | editButton.setEnabled(false); | |
131 | deleteButton.setEnabled(false); | |
132 | exportButton.setEnabled(false); | |
133 | } else { | |
134 | editButton.setEnabled(true); | |
135 | deleteButton.setEnabled(true); | |
136 | exportButton.setEnabled(true); | |
137 | } | |
a0a88f65 AM |
138 | } |
139 | }); | |
be222f56 PT |
140 | |
141 | Composite buttonContainer = new Composite(composite, SWT.NULL); | |
142 | buttonContainer.setLayout(new GridLayout()); | |
143 | buttonContainer.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false)); | |
144 | ||
145 | newButton = new Button(buttonContainer, SWT.PUSH); | |
146 | newButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
147 | newButton.setText(Messages.ManageCustomParsersDialog_NewButtonLabel); | |
a0a88f65 | 148 | newButton.addSelectionListener(new SelectionListener() { |
be222f56 | 149 | @Override |
a0a88f65 AM |
150 | public void widgetDefaultSelected(SelectionEvent e) {} |
151 | ||
be222f56 | 152 | @Override |
a0a88f65 | 153 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
154 | WizardDialog dialog = null; |
155 | if (txtButton.getSelection()) { | |
156 | dialog = new WizardDialog(getShell(), new CustomTxtParserWizard()); | |
157 | } else if (xmlButton.getSelection()) { | |
158 | dialog = new WizardDialog(getShell(), new CustomXmlParserWizard()); | |
159 | } | |
160 | if (dialog != null) { | |
161 | dialog.open(); | |
162 | if (dialog.getReturnCode() == Window.OK) { | |
163 | fillParserList(); | |
164 | } | |
165 | } | |
a0a88f65 AM |
166 | } |
167 | }); | |
be222f56 PT |
168 | |
169 | editButton = new Button(buttonContainer, SWT.PUSH); | |
170 | editButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
171 | editButton.setText(Messages.ManageCustomParsersDialog_EditButtonLabel); | |
172 | editButton.setEnabled(false); | |
a0a88f65 | 173 | editButton.addSelectionListener(new SelectionListener() { |
be222f56 | 174 | @Override |
a0a88f65 AM |
175 | public void widgetDefaultSelected(SelectionEvent e) {} |
176 | ||
be222f56 | 177 | @Override |
a0a88f65 | 178 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
179 | WizardDialog dialog = null; |
180 | if (txtButton.getSelection()) { | |
181 | dialog = new WizardDialog(getShell(), | |
182 | new CustomTxtParserWizard(CustomTxtTraceDefinition.load(parserList.getSelection()[0]))); | |
183 | } else if (xmlButton.getSelection()) { | |
184 | dialog = new WizardDialog(getShell(), | |
185 | new CustomXmlParserWizard(CustomXmlTraceDefinition.load(parserList.getSelection()[0]))); | |
186 | } | |
187 | if (dialog != null) { | |
188 | dialog.open(); | |
189 | if (dialog.getReturnCode() == Window.OK) { | |
190 | fillParserList(); | |
191 | } | |
192 | } | |
a0a88f65 AM |
193 | } |
194 | }); | |
be222f56 PT |
195 | |
196 | deleteButton = new Button(buttonContainer, SWT.PUSH); | |
197 | deleteButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
198 | deleteButton.setText(Messages.ManageCustomParsersDialog_DeleteButtonLabel); | |
199 | deleteButton.setEnabled(false); | |
a0a88f65 | 200 | deleteButton.addSelectionListener(new SelectionListener() { |
be222f56 | 201 | @Override |
a0a88f65 AM |
202 | public void widgetDefaultSelected(SelectionEvent e) {} |
203 | ||
be222f56 | 204 | @Override |
a0a88f65 | 205 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
206 | boolean confirm = MessageDialog.openQuestion( |
207 | getShell(), | |
208 | Messages.ManageCustomParsersDialog_DeleteParserDialogHeader, | |
209 | Messages.ManageCustomParsersDialog_DeleteConfirmation + parserList.getSelection()[0] + "?"); //$NON-NLS-1$ | |
210 | if (confirm) { | |
211 | if (txtButton.getSelection()) { | |
212 | CustomTxtTraceDefinition.delete(parserList.getSelection()[0]); | |
213 | } else if (xmlButton.getSelection()) { | |
214 | CustomXmlTraceDefinition.delete(parserList.getSelection()[0]); | |
215 | } | |
216 | fillParserList(); | |
217 | } | |
a0a88f65 AM |
218 | } |
219 | }); | |
be222f56 PT |
220 | |
221 | new Label(buttonContainer, SWT.NONE); // filler | |
222 | ||
223 | importButton = new Button(buttonContainer, SWT.PUSH); | |
224 | importButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
225 | importButton.setText(Messages.ManageCustomParsersDialog_ImportButtonLabel); | |
a0a88f65 | 226 | importButton.addSelectionListener(new SelectionListener() { |
be222f56 | 227 | @Override |
a0a88f65 AM |
228 | public void widgetDefaultSelected(SelectionEvent e) {} |
229 | ||
be222f56 | 230 | @Override |
a0a88f65 | 231 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
232 | FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN); |
233 | dialog.setText(Messages.ManageCustomParsersDialog_ImportParserSelection); | |
a0a88f65 | 234 | dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ |
be222f56 PT |
235 | String path = dialog.open(); |
236 | if (path != null) { | |
237 | CustomTraceDefinition[] defs = null; | |
238 | if (txtButton.getSelection()) { | |
239 | defs = CustomTxtTraceDefinition.loadAll(path); | |
240 | } else if (xmlButton.getSelection()) { | |
241 | defs = CustomXmlTraceDefinition.loadAll(path); | |
242 | } | |
243 | if (defs != null && defs.length > 0) { | |
244 | for (CustomTraceDefinition def : defs) { | |
245 | def.save(); | |
246 | } | |
247 | fillParserList(); | |
248 | } | |
249 | } | |
a0a88f65 AM |
250 | } |
251 | }); | |
be222f56 PT |
252 | |
253 | exportButton = new Button(buttonContainer, SWT.PUSH); | |
254 | exportButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | |
255 | exportButton.setText(Messages.ManageCustomParsersDialog_ExportButtonLabel); | |
256 | exportButton.setEnabled(false); | |
a0a88f65 | 257 | exportButton.addSelectionListener(new SelectionListener() { |
be222f56 | 258 | @Override |
a0a88f65 AM |
259 | public void widgetDefaultSelected(SelectionEvent e) {} |
260 | ||
be222f56 | 261 | @Override |
a0a88f65 | 262 | public void widgetSelected(SelectionEvent e) { |
be222f56 PT |
263 | FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE); |
264 | dialog.setText(Messages.ManageCustomParsersDialog_ExportParserSelection + parserList.getSelection()[0]); | |
a0a88f65 | 265 | dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ |
be222f56 PT |
266 | String path = dialog.open(); |
267 | if (path != null) { | |
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]); | |
273 | } | |
274 | if (def != null) { | |
275 | def.save(path); | |
276 | } | |
277 | } | |
a0a88f65 AM |
278 | } |
279 | }); | |
be222f56 PT |
280 | |
281 | fillParserList(); | |
282 | ||
283 | getShell().setMinimumSize(300, 275); | |
284 | return composite; | |
285 | } | |
286 | ||
287 | /* (non-Javadoc) | |
288 | * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) | |
289 | */ | |
290 | @Override | |
291 | protected void createButtonsForButtonBar(Composite parent) { | |
292 | createButton(parent, IDialogConstants.OK_ID, IDialogConstants.CLOSE_LABEL, false); | |
293 | } | |
294 | ||
295 | private void fillParserList() { | |
296 | parserList.removeAll(); | |
297 | if (txtButton.getSelection()) { | |
298 | for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) { | |
299 | parserList.add(def.definitionName); | |
300 | } | |
301 | } else if (xmlButton.getSelection()) { | |
302 | for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) { | |
303 | parserList.add(def.definitionName); | |
304 | } | |
305 | } | |
306 | editButton.setEnabled(false); | |
307 | deleteButton.setEnabled(false); | |
308 | exportButton.setEnabled(false); | |
309 | } | |
310 | ||
311 | } |