1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Geneviève Bastien - Initial implementation
11 *******************************************************************************/
13 package org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.handler;
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23 import org.eclipse.jface.viewers.TreeSelection;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.widgets.FileDialog;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlAnalysisModuleSource;
28 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
29 import org.eclipse.tracecompass.tmf.analysis.xml.ui.module.Messages;
30 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectModelElement;
31 import org.eclipse.tracecompass.tmf.ui.project.model.TraceUtils;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchPart;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.PlatformUI;
38 * Imports and validates an XML file
40 * @author Geneviève Bastien
42 public class ImportXmlHandler extends AbstractHandler {
45 public Object execute(ExecutionEvent event) throws ExecutionException {
47 FileDialog dlg = new FileDialog(new Shell(), SWT.OPEN);
48 dlg.setFilterNames(new String[] { Messages.ImportXmlHandler_ImportXmlFile + " (*.xml)" }); //$NON-NLS-1$
49 dlg.setFilterExtensions(new String[] { "*.xml" }); //$NON-NLS-1$
51 String fn = dlg.open();
53 File file = new File(fn);
54 IStatus status = XmlUtils.xmlValidate(file);
56 status = XmlUtils.addXmlFile(file);
58 XmlAnalysisModuleSource.notifyModuleChange();
60 * FIXME: It refreshes the list of analysis under a trace,
61 * but since modules are instantiated when the trace opens,
62 * the changes won't apply to an opened trace, it needs to
63 * be closed then reopened
67 TraceUtils.displayErrorMsg(Messages.ImportXmlHandler_ImportXmlFile, status.getMessage());
70 TraceUtils.displayErrorMsg(Messages.ImportXmlHandler_ImportXmlFile, status.getMessage());
78 * Refresh the selected project with the new XML file import
80 private static void refreshProject() {
81 // Check if we are closing down
82 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
88 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
89 IWorkbenchPart part = page.getActivePart();
93 ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
94 if (selectionProvider == null) {
97 ISelection selection = selectionProvider.getSelection();
99 if (selection instanceof TreeSelection) {
100 TreeSelection sel = (TreeSelection) selection;
101 // There should be only one item selected as per the plugin.xml
102 Object element = sel.getFirstElement();
103 if (element instanceof TmfProjectModelElement) {
104 ((TmfProjectModelElement) element).getProject().refresh();