| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2010, 2013 Ericsson |
| 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 | |
| 42 | /** |
| 43 | * Dialog for custom text parsers. |
| 44 | * |
| 45 | * @author Patrick Tassé |
| 46 | */ |
| 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 | |
| 60 | /** |
| 61 | * Constructor |
| 62 | * |
| 63 | * @param parent |
| 64 | * Parent shell of this dialog |
| 65 | */ |
| 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); |
| 98 | txtButton.addSelectionListener(new SelectionListener() { |
| 99 | @Override |
| 100 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 101 | |
| 102 | @Override |
| 103 | public void widgetSelected(SelectionEvent e) { |
| 104 | fillParserList(); |
| 105 | } |
| 106 | }); |
| 107 | |
| 108 | xmlButton = new Button(radioContainer, SWT.RADIO); |
| 109 | xmlButton.setText("XML"); //$NON-NLS-1$ |
| 110 | xmlButton.addSelectionListener(new SelectionListener() { |
| 111 | @Override |
| 112 | public void widgetDefaultSelected(SelectionEvent e) { |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void widgetSelected(SelectionEvent e) { |
| 117 | fillParserList(); |
| 118 | } |
| 119 | }); |
| 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)); |
| 123 | parserList.addSelectionListener(new SelectionListener() { |
| 124 | @Override |
| 125 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 126 | |
| 127 | @Override |
| 128 | public void widgetSelected(SelectionEvent e) { |
| 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 | } |
| 138 | } |
| 139 | }); |
| 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); |
| 148 | newButton.addSelectionListener(new SelectionListener() { |
| 149 | @Override |
| 150 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 151 | |
| 152 | @Override |
| 153 | public void widgetSelected(SelectionEvent e) { |
| 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 | } |
| 166 | } |
| 167 | }); |
| 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); |
| 173 | editButton.addSelectionListener(new SelectionListener() { |
| 174 | @Override |
| 175 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 176 | |
| 177 | @Override |
| 178 | public void widgetSelected(SelectionEvent e) { |
| 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 | } |
| 193 | } |
| 194 | }); |
| 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); |
| 200 | deleteButton.addSelectionListener(new SelectionListener() { |
| 201 | @Override |
| 202 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 203 | |
| 204 | @Override |
| 205 | public void widgetSelected(SelectionEvent e) { |
| 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 | } |
| 218 | } |
| 219 | }); |
| 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); |
| 226 | importButton.addSelectionListener(new SelectionListener() { |
| 227 | @Override |
| 228 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 229 | |
| 230 | @Override |
| 231 | public void widgetSelected(SelectionEvent e) { |
| 232 | FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN); |
| 233 | dialog.setText(Messages.ManageCustomParsersDialog_ImportParserSelection); |
| 234 | dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ |
| 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 | } |
| 250 | } |
| 251 | }); |
| 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); |
| 257 | exportButton.addSelectionListener(new SelectionListener() { |
| 258 | @Override |
| 259 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 260 | |
| 261 | @Override |
| 262 | public void widgetSelected(SelectionEvent e) { |
| 263 | FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE); |
| 264 | dialog.setText(Messages.ManageCustomParsersDialog_ExportParserSelection + parserList.getSelection()[0]); |
| 265 | dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ |
| 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 | } |
| 278 | } |
| 279 | }); |
| 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 | } |