tmf.xml: Delete existing supplementary files when importing xml analysis
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.ui / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / ui / handler / ManageXMLAnalysisDialog.java
CommitLineData
f98e9dbe
JCK
1/*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 *******************************************************************************/
9package org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.handler;
10
11import java.io.File;
12import java.util.ArrayList;
13import java.util.Map;
14
15import org.eclipse.core.resources.IProject;
16import org.eclipse.core.resources.IResource;
17import org.eclipse.core.resources.ResourcesPlugin;
18import org.eclipse.core.runtime.CoreException;
537572cd 19import org.eclipse.core.runtime.IPath;
f98e9dbe 20import org.eclipse.core.runtime.IStatus;
537572cd 21import org.eclipse.core.runtime.Path;
f98e9dbe
JCK
22import org.eclipse.jface.dialogs.Dialog;
23import org.eclipse.jface.dialogs.IDialogConstants;
24import org.eclipse.jface.dialogs.MessageDialog;
25import org.eclipse.jface.viewers.ISelection;
26import org.eclipse.jface.viewers.ISelectionProvider;
27import org.eclipse.jface.viewers.TreeSelection;
28import org.eclipse.osgi.util.NLS;
29import org.eclipse.swt.SWT;
30import org.eclipse.swt.events.SelectionEvent;
31import org.eclipse.swt.events.SelectionListener;
32import org.eclipse.swt.layout.GridData;
33import org.eclipse.swt.layout.GridLayout;
34import org.eclipse.swt.widgets.Button;
35import org.eclipse.swt.widgets.Composite;
36import org.eclipse.swt.widgets.Control;
37import org.eclipse.swt.widgets.Display;
38import org.eclipse.swt.widgets.FileDialog;
39import org.eclipse.swt.widgets.Label;
40import org.eclipse.swt.widgets.List;
41import org.eclipse.swt.widgets.Shell;
6eca054d
GB
42import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlAnalysisModuleSource;
43import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlUtils;
f98e9dbe 44import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.Activator;
f98e9dbe
JCK
45import org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement;
46import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
47import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectModelElement;
48import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
49import org.eclipse.tracecompass.tmf.ui.project.model.TraceUtils;
50import org.eclipse.ui.IWorkbenchPage;
51import org.eclipse.ui.IWorkbenchPart;
52import org.eclipse.ui.IWorkbenchWindow;
53import org.eclipse.ui.PlatformUI;
54
55/**
56 * Dialog for XML analysis files
57 *
58 * @author Jean-Christian Kouame
59 */
60public class ManageXMLAnalysisDialog extends Dialog {
61
62 private final String XML_FILTER_EXTENSION = "*.xml"; //$NON-NLS-1$
63 private List fAnalysesList;
64 private Button fDeleteButton;
65 private Button fImportButton;
66 private Button fExportButton;
67 private Label fInvalidFileLabel;
68
69 /**
70 * Constructor
71 *
72 * @param parent
73 * Parent shell of this dialog
74 */
75 public ManageXMLAnalysisDialog(Shell parent) {
76 super(parent);
77 setShellStyle(SWT.RESIZE | SWT.MAX | getShellStyle());
78 }
79
80 @Override
81 protected Control createDialogArea(Composite parent) {
82 getShell().setText(Messages.ManageXMLAnalysisDialog_ManageXmlAnalysesFiles);
83
84 Composite composite = (Composite) super.createDialogArea(parent);
85 composite.setLayout(new GridLayout(2, false));
86
87 Composite listContainer = new Composite(composite, SWT.NONE);
88 listContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
89 GridLayout lcgl = new GridLayout();
90 lcgl.marginHeight = 0;
91 lcgl.marginWidth = 0;
92 listContainer.setLayout(lcgl);
93
94 fAnalysesList = new List(listContainer, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
95 fAnalysesList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
96 fAnalysesList.addSelectionListener(new SelectionListener() {
97 @Override
98 public void widgetDefaultSelected(SelectionEvent e) {
99 }
100
101 @Override
102 public void widgetSelected(SelectionEvent e) {
103 if (fAnalysesList.getSelectionCount() == 0) {
104 fDeleteButton.setEnabled(false);
105 fExportButton.setEnabled(false);
106 } else {
107 fDeleteButton.setEnabled(true);
108 fExportButton.setEnabled(true);
109 handleSelection(fAnalysesList.getSelection());
110 }
111 }
112 });
113
114 fInvalidFileLabel = new Label(listContainer, SWT.ICON_ERROR);
115 fInvalidFileLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
116 fInvalidFileLabel.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_RED));
117 fInvalidFileLabel.setText(Messages.ManageXMLAnalysisDialog_FileValidationError);
118 fInvalidFileLabel.setVisible(false);
119
120 Composite buttonContainer = new Composite(composite, SWT.NULL);
121 buttonContainer.setLayout(new GridLayout());
122 buttonContainer.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false));
123
124 fImportButton = new Button(buttonContainer, SWT.PUSH);
125 fImportButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
126 fImportButton.setText(Messages.ManageXMLAnalysisDialog_Import);
127 fImportButton.addSelectionListener(new SelectionListener() {
128 @Override
129 public void widgetDefaultSelected(SelectionEvent e) {
130 }
131
132 @Override
133 public void widgetSelected(SelectionEvent e) {
134 importAnalysis();
135 }
136 });
137
138 fExportButton = new Button(buttonContainer, SWT.PUSH);
139 fExportButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
140 fExportButton.setText(Messages.ManageXMLAnalysisDialog_Export);
141 fExportButton.setEnabled(false);
142 fExportButton.addSelectionListener(new SelectionListener() {
143 @Override
144 public void widgetDefaultSelected(SelectionEvent e) {
145 }
146
147 @Override
148 public void widgetSelected(SelectionEvent e) {
149 exportAnalysis();
150 }
151 });
152
153 fDeleteButton = new Button(buttonContainer, SWT.PUSH);
154 fDeleteButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
155 fDeleteButton.setText(Messages.ManageXMLAnalysisDialog_Delete);
156 fDeleteButton.setEnabled(false);
157 fDeleteButton.addSelectionListener(new SelectionListener() {
158 @Override
159 public void widgetDefaultSelected(SelectionEvent e) {
160 }
161
162 @Override
163 public void widgetSelected(SelectionEvent e) {
164 deleteAnalysis();
165 }
166
167 });
168
169 fillAnalysesList();
170
171 getShell().setMinimumSize(300, 275);
172 return composite;
173 }
174
175 private void handleSelection(String[] selection) {
537572cd
BH
176 Map<String, File> files = XmlUtils.listFiles();
177 File file = files.get(createXmlFileString(selection[0]));
f98e9dbe
JCK
178 if (file != null && XmlUtils.xmlValidate(file).isOK()) {
179 fInvalidFileLabel.setVisible(false);
180 } else {
181 fInvalidFileLabel.setVisible(true);
182 }
183 }
184
537572cd 185 private static void deleteSupplementaryFile(String xmlFile) {
f98e9dbe
JCK
186 // 1. Look for all traces that have this analysis
187 // 2. Close them if they are opened.
188 // 3. Delete the related supplementary files
189 java.util.List<IResource> resourceToDelete = new ArrayList<>();
537572cd 190 java.util.List<String> ids = XmlUtils.getAnalysisIdsFromFile(xmlFile);
f98e9dbe
JCK
191 IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(0);
192 for (IProject project : projects) {
193 TmfProjectElement pElement = TmfProjectRegistry.getProject(project);
194 if (pElement != null) {
195 java.util.List<TmfCommonProjectElement> tElements = new ArrayList<>();
196 tElements.addAll(pElement.getTracesFolder().getTraces());
197 tElements.addAll(pElement.getExperimentsFolder().getExperiments());
198 for (TmfCommonProjectElement tElement : tElements) {
199 boolean closeEditor = false;
200 for (IResource resource : tElement.getSupplementaryResources()) {
201 for (String id : ids) {
202 if (resource.getName().startsWith(id)) {
203 resourceToDelete.add(resource);
204 closeEditor = true;
205 }
206 }
207 }
208 if (closeEditor) {
209 tElement.closeEditors();
210 }
211 }
212 }
213 }
214 for (IResource resource : resourceToDelete) {
215 try {
216 resource.delete(false, null);
217 } catch (CoreException e) {
218 Activator.logError(NLS.bind(Messages.ManageXMLAnalysisDialog_DeleteFileError, resource.getName()));
219 }
220 }
221 }
222
223 @Override
224 protected void createButtonsForButtonBar(Composite parent) {
225 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.CLOSE_LABEL, false);
226 }
227
228 private void fillAnalysesList() {
229 fAnalysesList.removeAll();
537572cd 230 Map<String, File> files = XmlUtils.listFiles();
f98e9dbe
JCK
231 for (String file : files.keySet()) {
232 // Remove the extension from the file path. The extension is at the
233 // end of the file path
537572cd
BH
234 IPath path = new Path(file);
235 fAnalysesList.add(path.removeFileExtension().toString());
f98e9dbe
JCK
236 }
237 fDeleteButton.setEnabled(false);
238 fExportButton.setEnabled(false);
239 }
240
241 private void importAnalysis() {
242 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);
243 dialog.setText(Messages.ManageXMLAnalysisDialog_SelectFileImport);
244 dialog.setFilterNames(new String[] { Messages.ManageXMLAnalysisDialog_ImportXmlFile + " (*.xml)" }); //$NON-NLS-1$
245 dialog.setFilterExtensions(new String[] { XML_FILTER_EXTENSION });
246 String path = dialog.open();
247 if (path != null) {
248 File file = new File(path);
249 IStatus status = XmlUtils.xmlValidate(file);
250 if (status.isOK()) {
cfe92fc5 251 deleteSupplementaryFile(file.getName());
f98e9dbe
JCK
252 status = XmlUtils.addXmlFile(file);
253 if (status.isOK()) {
254 fillAnalysesList();
255 XmlAnalysisModuleSource.notifyModuleChange();
256 /*
257 * FIXME: It refreshes the list of analysis under a trace,
258 * but since modules are instantiated when the trace opens,
259 * the changes won't apply to an opened trace, it needs to
260 * be closed then reopened
261 */
262 refreshProject();
263 } else {
264 Activator.logError(Messages.ManageXMLAnalysisDialog_ImportFileFailed);
265 TraceUtils.displayErrorMsg(Messages.ManageXMLAnalysisDialog_ImportFileFailed, status.getMessage());
266 }
267 } else {
268 Activator.logError(Messages.ManageXMLAnalysisDialog_ImportFileFailed);
269 TraceUtils.displayErrorMsg(Messages.ManageXMLAnalysisDialog_ImportFileFailed, status.getMessage());
270 }
271 }
272 }
273
274 private void exportAnalysis() {
275 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
276 dialog.setText(NLS.bind(Messages.ManageXMLAnalysisDialog_SelectFileExport, fAnalysesList.getSelection()[0]));
277 dialog.setFilterExtensions(new String[] { XML_FILTER_EXTENSION, "*" }); //$NON-NLS-1$
537572cd
BH
278 String selection = createXmlFileString(fAnalysesList.getSelection()[0]);
279 dialog.setFileName(selection);
f98e9dbe
JCK
280 String path = dialog.open();
281 if (path != null) {
537572cd
BH
282 if (!XmlUtils.exportXmlFile(selection, path).isOK()) {
283 Activator.logError(NLS.bind(Messages.ManageXMLAnalysisDialog_FailedToExport, selection));
f98e9dbe
JCK
284 }
285 }
286 }
287
288 private void deleteAnalysis() {
289 boolean confirm = MessageDialog.openQuestion(
290 getShell(),
291 Messages.ManageXMLAnalysisDialog_DeleteFile,
292 NLS.bind(Messages.ManageXMLAnalysisDialog_DeleteConfirmation, fAnalysesList.getSelection()[0]));
293 if (confirm) {
537572cd 294 String selection = createXmlFileString(fAnalysesList.getSelection()[0]);
f98e9dbe 295 deleteSupplementaryFile(selection);
537572cd 296 XmlUtils.deleteFile(selection);
f98e9dbe
JCK
297 fillAnalysesList();
298 fInvalidFileLabel.setVisible(false);
299 XmlAnalysisModuleSource.notifyModuleChange();
300 /*
301 * FIXME: It refreshes the list of analysis under a trace, but since
302 * modules are instantiated when the trace opens, the changes won't
303 * apply to an opened trace, it needs to be closed then reopened
304 */
305 refreshProject();
306 }
307 }
308
309 /**
310 * Refresh the selected project with the new XML file import
311 */
312 private static void refreshProject() {
313 // Check if we are closing down
314 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
315 if (window == null) {
316 return;
317 }
318
319 // Get the selection
320 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
321 IWorkbenchPart part = page.getActivePart();
322 if (part == null) {
323 return;
324 }
325 ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
326 if (selectionProvider == null) {
327 return;
328 }
329 ISelection selection = selectionProvider.getSelection();
330
331 if (selection instanceof TreeSelection) {
332 TreeSelection sel = (TreeSelection) selection;
333 // There should be only one item selected as per the plugin.xml
334 Object element = sel.getFirstElement();
335 if (element instanceof TmfProjectModelElement) {
336 ((TmfProjectModelElement) element).getProject().refresh();
337 }
338 }
339 }
537572cd
BH
340
341 private static String createXmlFileString(String baseName) {
342 IPath path = new Path(baseName).addFileExtension(XmlUtils.XML_EXTENSION);
343 return path.toString();
344 }
f98e9dbe 345}
This page took 0.043136 seconds and 5 git commands to generate.