Commit | Line | Data |
---|---|---|
97ed0cf0 FW |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 École Polytechnique de Montréal | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Geneviève Bastien - Initial implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.handler; |
97ed0cf0 FW |
14 | |
15 | import java.io.File; | |
16 | ||
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; | |
97ed0cf0 FW |
24 | import org.eclipse.swt.SWT; |
25 | import org.eclipse.swt.widgets.FileDialog; | |
26 | import org.eclipse.swt.widgets.Shell; | |
0dcf01f1 | 27 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlAnalysisModuleSource; |
2bdf0193 AM |
28 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; |
29 | import org.eclipse.tracecompass.tmf.analysis.xml.ui.module.Messages; | |
2bdf0193 AM |
30 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectModelElement; |
31 | import org.eclipse.tracecompass.tmf.ui.project.model.TraceUtils; | |
97ed0cf0 FW |
32 | import org.eclipse.ui.IWorkbenchPage; |
33 | import org.eclipse.ui.IWorkbenchPart; | |
34 | import org.eclipse.ui.IWorkbenchWindow; | |
35 | import org.eclipse.ui.PlatformUI; | |
36 | ||
37 | /** | |
38 | * Imports and validates an XML file | |
39 | * | |
40 | * @author Geneviève Bastien | |
41 | */ | |
42 | public class ImportXmlHandler extends AbstractHandler { | |
43 | ||
44 | @Override | |
45 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
46 | ||
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$ | |
50 | ||
51 | String fn = dlg.open(); | |
52 | if (fn != null) { | |
53 | File file = new File(fn); | |
54 | IStatus status = XmlUtils.xmlValidate(file); | |
55 | if (status.isOK()) { | |
56 | status = XmlUtils.addXmlFile(file); | |
57 | if (status.isOK()) { | |
58 | XmlAnalysisModuleSource.notifyModuleChange(); | |
59 | /* | |
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 | |
64 | */ | |
65 | refreshProject(); | |
66 | } else { | |
67 | TraceUtils.displayErrorMsg(Messages.ImportXmlHandler_ImportXmlFile, status.getMessage()); | |
68 | } | |
69 | } else { | |
70 | TraceUtils.displayErrorMsg(Messages.ImportXmlHandler_ImportXmlFile, status.getMessage()); | |
71 | } | |
72 | } | |
73 | ||
74 | return null; | |
75 | } | |
76 | ||
77 | /** | |
78 | * Refresh the selected project with the new XML file import | |
79 | */ | |
80 | private static void refreshProject() { | |
81 | // Check if we are closing down | |
82 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
83 | if (window == null) { | |
84 | return; | |
85 | } | |
86 | ||
87 | // Get the selection | |
88 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
89 | IWorkbenchPart part = page.getActivePart(); | |
90 | if (part == null) { | |
91 | return; | |
92 | } | |
93 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); | |
94 | if (selectionProvider == null) { | |
95 | return; | |
96 | } | |
97 | ISelection selection = selectionProvider.getSelection(); | |
98 | ||
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(); | |
105 | } | |
106 | } | |
107 | ||
108 | } | |
109 | ||
110 | } |