| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2010, 2015 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.tracecompass.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.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; |
| 48 | |
| 49 | /** |
| 50 | * Dialog for custom text parsers. |
| 51 | * |
| 52 | * @author Patrick Tassé |
| 53 | */ |
| 54 | public class ManageCustomParsersDialog extends Dialog { |
| 55 | |
| 56 | private static final String SEP = " : "; //$NON-NLS-1$ |
| 57 | private static final int SEP_LEN = SEP.length(); |
| 58 | |
| 59 | private static final Image image = Activator.getDefault().getImageFromPath("/icons/etool16/customparser_wizard.gif"); //$NON-NLS-1$ |
| 60 | |
| 61 | Button txtButton; |
| 62 | Button xmlButton; |
| 63 | List parserList; |
| 64 | Button newButton; |
| 65 | Button editButton; |
| 66 | Button deleteButton; |
| 67 | Button importButton; |
| 68 | Button exportButton; |
| 69 | |
| 70 | /** |
| 71 | * Constructor |
| 72 | * |
| 73 | * @param parent |
| 74 | * Parent shell of this dialog |
| 75 | */ |
| 76 | public ManageCustomParsersDialog(Shell parent) { |
| 77 | super(parent); |
| 78 | setShellStyle(SWT.RESIZE | SWT.MAX | getShellStyle()); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | protected Control createDialogArea(Composite parent) { |
| 83 | getShell().setText(Messages.ManageCustomParsersDialog_DialogHeader); |
| 84 | getShell().setImage(image); |
| 85 | |
| 86 | Composite composite = (Composite) super.createDialogArea(parent); |
| 87 | composite.setLayout(new GridLayout(2, false)); |
| 88 | |
| 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; |
| 93 | lcgl.marginWidth = 0; |
| 94 | listContainer.setLayout(lcgl); |
| 95 | |
| 96 | Composite radioContainer = new Composite(listContainer, SWT.NONE); |
| 97 | GridLayout rcgl = new GridLayout(2, true); |
| 98 | rcgl.marginHeight = 0; |
| 99 | rcgl.marginWidth = 0; |
| 100 | radioContainer.setLayout(rcgl); |
| 101 | |
| 102 | txtButton = new Button(radioContainer, SWT.RADIO); |
| 103 | txtButton.setText(Messages.ManageCustomParsersDialog_TextButtonLabel); |
| 104 | txtButton.setSelection(true); |
| 105 | txtButton.addSelectionListener(new SelectionListener() { |
| 106 | @Override |
| 107 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 108 | |
| 109 | @Override |
| 110 | public void widgetSelected(SelectionEvent e) { |
| 111 | fillParserList(); |
| 112 | } |
| 113 | }); |
| 114 | |
| 115 | xmlButton = new Button(radioContainer, SWT.RADIO); |
| 116 | xmlButton.setText("XML"); //$NON-NLS-1$ |
| 117 | xmlButton.addSelectionListener(new SelectionListener() { |
| 118 | @Override |
| 119 | public void widgetDefaultSelected(SelectionEvent e) { |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void widgetSelected(SelectionEvent e) { |
| 124 | fillParserList(); |
| 125 | } |
| 126 | }); |
| 127 | |
| 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() { |
| 131 | @Override |
| 132 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 133 | |
| 134 | @Override |
| 135 | public void widgetSelected(SelectionEvent e) { |
| 136 | if (parserList.getSelectionCount() == 0) { |
| 137 | editButton.setEnabled(false); |
| 138 | deleteButton.setEnabled(false); |
| 139 | exportButton.setEnabled(false); |
| 140 | } else { |
| 141 | editButton.setEnabled(true); |
| 142 | deleteButton.setEnabled(true); |
| 143 | exportButton.setEnabled(true); |
| 144 | } |
| 145 | } |
| 146 | }); |
| 147 | |
| 148 | Composite buttonContainer = new Composite(composite, SWT.NULL); |
| 149 | buttonContainer.setLayout(new GridLayout()); |
| 150 | buttonContainer.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false)); |
| 151 | |
| 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() { |
| 156 | @Override |
| 157 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 158 | |
| 159 | @Override |
| 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()); |
| 166 | } |
| 167 | if (dialog != null) { |
| 168 | dialog.open(); |
| 169 | if (dialog.getReturnCode() == Window.OK) { |
| 170 | fillParserList(); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | }); |
| 175 | |
| 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() { |
| 181 | @Override |
| 182 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 183 | |
| 184 | @Override |
| 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))); |
| 196 | } |
| 197 | if (dialog != null) { |
| 198 | dialog.open(); |
| 199 | if (dialog.getReturnCode() == Window.OK) { |
| 200 | fillParserList(); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | }); |
| 205 | |
| 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() { |
| 211 | @Override |
| 212 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 213 | |
| 214 | @Override |
| 215 | public void widgetSelected(SelectionEvent e) { |
| 216 | boolean confirm = MessageDialog.openQuestion( |
| 217 | getShell(), |
| 218 | Messages.ManageCustomParsersDialog_DeleteParserDialogHeader, |
| 219 | NLS.bind(Messages.ManageCustomParsersDialog_DeleteConfirmation, parserList.getSelection()[0])); |
| 220 | if (confirm) { |
| 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)); |
| 230 | } |
| 231 | fillParserList(); |
| 232 | } |
| 233 | } |
| 234 | }); |
| 235 | |
| 236 | new Label(buttonContainer, SWT.NONE); // filler |
| 237 | |
| 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() { |
| 242 | @Override |
| 243 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 244 | |
| 245 | @Override |
| 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(); |
| 251 | if (path != null) { |
| 252 | CustomTraceDefinition[] defs = null; |
| 253 | if (txtButton.getSelection()) { |
| 254 | defs = CustomTxtTraceDefinition.loadAll(path); |
| 255 | } else if (xmlButton.getSelection()) { |
| 256 | defs = CustomXmlTraceDefinition.loadAll(path); |
| 257 | } |
| 258 | if (defs != null && defs.length > 0) { |
| 259 | for (CustomTraceDefinition def : defs) { |
| 260 | boolean ok = checkNameConflict(def); |
| 261 | if (ok) { |
| 262 | def.save(); |
| 263 | } |
| 264 | } |
| 265 | fillParserList(); |
| 266 | } else { |
| 267 | MessageDialog.openInformation(Display.getCurrent().getActiveShell(), |
| 268 | Messages.ManageCustomParsersDialog_ImportFailureTitle, |
| 269 | Messages.ManageCustomParsersDialog_ImportFailureMessage); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | }); |
| 274 | |
| 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() { |
| 280 | @Override |
| 281 | public void widgetDefaultSelected(SelectionEvent e) {} |
| 282 | |
| 283 | @Override |
| 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(); |
| 289 | if (path != null) { |
| 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); |
| 298 | } |
| 299 | if (def != null) { |
| 300 | def.save(path); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | }); |
| 305 | |
| 306 | fillParserList(); |
| 307 | |
| 308 | getShell().setMinimumSize(300, 275); |
| 309 | return composite; |
| 310 | } |
| 311 | |
| 312 | @Override |
| 313 | protected void createButtonsForButtonBar(Composite parent) { |
| 314 | createButton(parent, IDialogConstants.OK_ID, IDialogConstants.CLOSE_LABEL, false); |
| 315 | } |
| 316 | |
| 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); |
| 322 | } |
| 323 | } else if (xmlButton.getSelection()) { |
| 324 | for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll(false)) { |
| 325 | parserList.add(def.categoryName + SEP + def.definitionName); |
| 326 | } |
| 327 | } |
| 328 | editButton.setEnabled(false); |
| 329 | deleteButton.setEnabled(false); |
| 330 | exportButton.setEnabled(false); |
| 331 | } |
| 332 | |
| 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( |
| 339 | getShell(), |
| 340 | null, |
| 341 | null, |
| 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 }, |
| 347 | 0); |
| 348 | int result = dialog.open(); |
| 349 | if (result == 0) { |
| 350 | def.definitionName = newName; |
| 351 | return true; |
| 352 | } |
| 353 | return false; |
| 354 | } |
| 355 | } |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | private static String findAvailableName(CustomTraceDefinition def) { |
| 360 | int i = 2; |
| 361 | Iterable<TraceTypeHelper> helpers = TmfTraceType.getTraceTypeHelpers(); |
| 362 | while (true) { |
| 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())) { |
| 368 | available = false; |
| 369 | break; |
| 370 | } |
| 371 | } |
| 372 | if (available) { |
| 373 | return newName; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |