Commit | Line | Data |
---|---|---|
d04ec5a7 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 Ericsson |
d04ec5a7 MK |
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 | * Matthew Khouzam - Initial API and implementation | |
4196e938 | 11 | * Marc-Andre Laperle - Log some exceptions |
89730b51 | 12 | * Patrick Tasse - Add support for source location |
d04ec5a7 MK |
13 | *******************************************************************************/ |
14 | ||
1de10308 | 15 | package org.eclipse.linuxtools.internal.tmf.ui.project.wizards.importtrace; |
d04ec5a7 MK |
16 | |
17 | import java.io.File; | |
a2d29ca1 | 18 | import java.io.FileInputStream; |
d04ec5a7 MK |
19 | import java.lang.reflect.InvocationTargetException; |
20 | import java.util.ArrayList; | |
a2d29ca1 MK |
21 | import java.util.Collections; |
22 | import java.util.Comparator; | |
d04ec5a7 | 23 | import java.util.HashMap; |
11c9462b | 24 | import java.util.HashSet; |
9b48d94c | 25 | import java.util.Iterator; |
d04ec5a7 MK |
26 | import java.util.List; |
27 | import java.util.Map; | |
28 | import java.util.Set; | |
29 | import java.util.TreeSet; | |
30 | import java.util.concurrent.BlockingQueue; | |
31 | ||
32 | import org.eclipse.core.resources.IFile; | |
33 | import org.eclipse.core.resources.IFolder; | |
34 | import org.eclipse.core.resources.IResource; | |
d04ec5a7 MK |
35 | import org.eclipse.core.resources.ResourcesPlugin; |
36 | import org.eclipse.core.runtime.CoreException; | |
d04ec5a7 MK |
37 | import org.eclipse.core.runtime.IPath; |
38 | import org.eclipse.core.runtime.IProgressMonitor; | |
39 | import org.eclipse.core.runtime.IStatus; | |
9b48d94c | 40 | import org.eclipse.core.runtime.NullProgressMonitor; |
d04ec5a7 MK |
41 | import org.eclipse.core.runtime.Path; |
42 | import org.eclipse.core.runtime.Status; | |
43 | import org.eclipse.core.runtime.SubMonitor; | |
89730b51 | 44 | import org.eclipse.core.runtime.URIUtil; |
d04ec5a7 MK |
45 | import org.eclipse.jface.dialogs.ErrorDialog; |
46 | import org.eclipse.jface.dialogs.IDialogSettings; | |
47 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
48 | import org.eclipse.jface.viewers.IStructuredSelection; | |
49 | import org.eclipse.jface.wizard.IWizardPage; | |
50 | import org.eclipse.jface.wizard.WizardDialog; | |
51 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; | |
76fccfb0 | 52 | import org.eclipse.linuxtools.internal.tmf.ui.project.model.TmfImportHelper; |
89730b51 | 53 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
47aafe74 AM |
54 | import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType; |
55 | import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper; | |
56 | import org.eclipse.linuxtools.tmf.core.project.model.TraceValidationHelper; | |
9b48d94c | 57 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
47aafe74 | 58 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceTypeUIUtils; |
d04ec5a7 MK |
59 | import org.eclipse.ui.IWorkbench; |
60 | import org.eclipse.ui.dialogs.IOverwriteQuery; | |
61 | import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider; | |
62 | import org.eclipse.ui.wizards.datatransfer.ImportOperation; | |
63 | ||
64 | /** | |
65 | * Batch Import trace wizard. | |
66 | * | |
67 | * @author Matthew Khouzam | |
68 | * @since 2.0 | |
69 | */ | |
002f9f07 | 70 | public class BatchImportTraceWizard extends ImportTraceWizard { |
d04ec5a7 | 71 | |
11c9462b MK |
72 | private static final int WIN_HEIGHT = 400; |
73 | private static final int WIN_WIDTH = 800; | |
74 | private static final Status CANCEL_STATUS = new Status(IStatus.CANCEL, Activator.PLUGIN_ID, ""); //$NON-NLS-1$ | |
d04ec5a7 MK |
75 | private static final int TOTALWORK = 65536; |
76 | // ----------------- | |
77 | // Constants | |
78 | // ----------------- | |
79 | ||
80 | private static final int MAX_FILES = TOTALWORK - 1; | |
d04ec5a7 MK |
81 | private static final String BATCH_IMPORT_WIZARD = "BatchImportTraceWizard"; //$NON-NLS-1$ |
82 | ||
83 | // ------------------ | |
84 | // Fields | |
85 | // ------------------ | |
86 | ||
87 | private IWizardPage fSelectDirectoriesPage; | |
ba486486 | 88 | private ImportTraceWizardScanPage fScanPage; |
d04ec5a7 | 89 | private IWizardPage fSelectTypePage; |
a2d29ca1 | 90 | private IWizardPage fOptions; |
d04ec5a7 | 91 | |
507b1336 AM |
92 | private final List<String> fTraceTypesToScan = new ArrayList<>(); |
93 | private final Set<String> fParentFilesToScan = new HashSet<>(); | |
d04ec5a7 | 94 | |
ba486486 | 95 | private ImportTraceContentProvider fScannedTraces = new ImportTraceContentProvider(fTraceTypesToScan, fParentFilesToScan); |
d04ec5a7 | 96 | |
507b1336 | 97 | private final Map<TraceValidationHelper, Boolean> fResults = new HashMap<>(); |
d04ec5a7 MK |
98 | private boolean fOverwrite = true; |
99 | private boolean fLinked = true; | |
100 | ||
101 | private BlockingQueue<TraceValidationHelper> fTracesToScan; | |
507b1336 | 102 | private final Set<FileAndName> fTraces = new TreeSet<>(); |
11c9462b | 103 | |
507b1336 | 104 | private Map<String, Set<String>> fParentFiles = new HashMap<>(); |
11c9462b | 105 | |
d80cac0e | 106 | // Target import directory (trace folder) |
d04ec5a7 MK |
107 | private IFolder fTargetFolder; |
108 | ||
109 | /** | |
110 | * Returns the ScannedTraces model | |
111 | * | |
112 | * @return the ScannedTraces model | |
113 | */ | |
114 | public ImportTraceContentProvider getScannedTraces() { | |
115 | return fScannedTraces; | |
116 | } | |
117 | ||
118 | /** | |
119 | * Constructor | |
120 | */ | |
121 | public BatchImportTraceWizard() { | |
122 | IDialogSettings workbenchSettings = Activator.getDefault().getDialogSettings(); | |
123 | IDialogSettings section = workbenchSettings.getSection(BATCH_IMPORT_WIZARD); | |
124 | if (section == null) { | |
125 | section = workbenchSettings.addNewSection(BATCH_IMPORT_WIZARD); | |
126 | } | |
127 | setDialogSettings(section); | |
128 | setNeedsProgressMonitor(true); | |
129 | } | |
130 | ||
131 | @Override | |
132 | public void init(IWorkbench workbench, IStructuredSelection selection) { | |
133 | ||
134 | fSelectDirectoriesPage = new ImportTraceWizardSelectDirectoriesPage(workbench, selection); | |
135 | fScanPage = new ImportTraceWizardScanPage(workbench, selection); | |
136 | fSelectTypePage = new ImportTraceWizardSelectTraceTypePage(workbench, selection); | |
a2d29ca1 | 137 | fOptions = new ImportTraceWizardPageOptions(workbench, selection); |
d04ec5a7 | 138 | // keep in case it's called later |
9b48d94c MK |
139 | Iterator<?> iter = selection.iterator(); |
140 | while (iter.hasNext()) { | |
141 | Object selected = iter.next(); | |
142 | if (selected instanceof TmfTraceFolder) { | |
143 | fTargetFolder = ((TmfTraceFolder) selected).getResource(); | |
144 | break; | |
145 | } | |
146 | } | |
d04ec5a7 MK |
147 | fResults.clear(); |
148 | } | |
149 | ||
150 | @Override | |
151 | public void addPages() { | |
152 | addPage(fSelectTypePage); | |
153 | addPage(fSelectDirectoriesPage); | |
154 | addPage(fScanPage); | |
a2d29ca1 | 155 | addPage(fOptions); |
11c9462b MK |
156 | final WizardDialog container = (WizardDialog) getContainer(); |
157 | if (container != null) { | |
158 | container.setPageSize(WIN_WIDTH, WIN_HEIGHT); | |
d04ec5a7 | 159 | } |
d04ec5a7 MK |
160 | } |
161 | ||
162 | /** | |
163 | * Add a file to scan | |
164 | * | |
165 | * @param fileName | |
166 | * the file to scan | |
167 | */ | |
168 | public void addFileToScan(final String fileName) { | |
91e941e8 MAL |
169 | String absolutePath = new File(fileName).getAbsolutePath(); |
170 | if (!fParentFiles.containsKey(absolutePath)) { | |
171 | fParentFiles.put(absolutePath, new HashSet<String>()); | |
172 | startUpdateTask(Messages.BatchImportTraceWizardAdd + ' ' + absolutePath, absolutePath); | |
11c9462b MK |
173 | |
174 | } | |
175 | ||
d04ec5a7 MK |
176 | } |
177 | ||
178 | /** | |
179 | * Remove files from selection | |
180 | * | |
181 | * @param fileName | |
182 | * the name of the file to remove | |
183 | */ | |
184 | public void removeFile(final String fileName) { | |
11c9462b | 185 | fParentFiles.remove(fileName); |
d04ec5a7 | 186 | fParentFilesToScan.remove(fileName); |
97c6e624 | 187 | startUpdateTask(Messages.BatchImportTraceWizardRemove + ' ' + fileName, null); |
d04ec5a7 MK |
188 | } |
189 | ||
91e941e8 | 190 | private void startUpdateTask(final String taskName, final String fileAbsolutePath) { |
d04ec5a7 | 191 | try { |
11c9462b | 192 | this.getContainer().run(true, true, new IRunnableWithProgress() { |
d04ec5a7 MK |
193 | |
194 | @Override | |
195 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
196 | synchronized (BatchImportTraceWizard.this) { // this should | |
197 | // only run one | |
198 | // at a time | |
199 | SubMonitor sm; | |
200 | sm = SubMonitor.convert(monitor); | |
201 | sm.setTaskName(taskName); | |
202 | sm.setWorkRemaining(TOTALWORK); | |
91e941e8 | 203 | updateFiles(sm, fileAbsolutePath); |
d04ec5a7 MK |
204 | sm.done(); |
205 | } | |
206 | } | |
207 | }); | |
208 | } catch (InvocationTargetException e) { | |
4196e938 | 209 | Activator.getDefault().logError(Messages.ImportTraceWizardImportProblem, e); |
d04ec5a7 | 210 | } catch (InterruptedException e) { |
d04ec5a7 MK |
211 | } |
212 | } | |
213 | ||
214 | /** | |
215 | * The set of names of the selected files | |
216 | * | |
217 | * @return the set of names of the selected files | |
218 | */ | |
219 | public Set<String> getFileNames() { | |
220 | return fParentFilesToScan; | |
221 | } | |
222 | ||
223 | /** | |
224 | * Reset the trace list to import | |
225 | */ | |
226 | public void clearTraces() { | |
227 | fTraces.clear(); | |
228 | } | |
229 | ||
230 | @Override | |
231 | public boolean performFinish() { | |
232 | if (fTraces.isEmpty()) { | |
233 | return false; | |
234 | } | |
235 | // if this turns out to be too slow, put in a progress monitor. Does not | |
236 | // appear to be slow for the moment. | |
237 | boolean success = importTraces(); | |
238 | return success; | |
239 | } | |
240 | ||
241 | private boolean importTraces() { | |
242 | boolean success = false; | |
243 | IOverwriteQuery overwriteQuery = new IOverwriteQuery() { | |
244 | @Override | |
245 | public String queryOverwrite(String file) { | |
246 | return fOverwrite ? IOverwriteQuery.ALL : IOverwriteQuery.NO_ALL; | |
247 | } | |
248 | }; | |
249 | FileSystemStructureProvider fileSystemStructureProvider = FileSystemStructureProvider.INSTANCE; | |
250 | ||
251 | for (FileAndName traceToImport : fTraces) { | |
252 | try { | |
253 | if (fLinked) { | |
82cefbfe | 254 | if (TmfImportHelper.createLink(fTargetFolder, Path.fromOSString(traceToImport.getFile().getAbsolutePath()), traceToImport.getName()) == null) { |
76fccfb0 MK |
255 | success = false; |
256 | } | |
257 | else { | |
89730b51 | 258 | success = setTraceTypeAndSourceLocation(traceToImport).isOK(); |
76fccfb0 | 259 | } |
d04ec5a7 MK |
260 | } |
261 | else { | |
507b1336 | 262 | List<File> subList = new ArrayList<>(); |
a2d29ca1 MK |
263 | IPath path = fTargetFolder.getFullPath(); |
264 | File parentFile = traceToImport.getFile(); | |
265 | final boolean isFile = parentFile.isFile(); | |
266 | if (isFile) { | |
267 | IFile resource = ResourcesPlugin.getWorkspace().getRoot().getFile(path.append(traceToImport.getName())); | |
268 | if (fOverwrite || !resource.exists()) { | |
269 | subList.add(parentFile); | |
270 | parentFile = parentFile.getParentFile(); | |
507b1336 AM |
271 | try (final FileInputStream source = new FileInputStream(traceToImport.getFile());) { |
272 | if (resource.exists()) { | |
273 | resource.delete(IResource.FORCE, new NullProgressMonitor()); | |
274 | } | |
275 | resource.create(source, true, new NullProgressMonitor()); | |
a2d29ca1 | 276 | } |
89730b51 | 277 | setTraceTypeAndSourceLocation(traceToImport); |
a2d29ca1 MK |
278 | success = true; |
279 | } | |
280 | } else { | |
b8acd769 | 281 | path = path.addTrailingSeparator().append(traceToImport.getName()); |
2e622f00 | 282 | // Add all files in trace directory |
a2d29ca1 MK |
283 | File[] fileList = traceToImport.getFile().listFiles(); |
284 | for (File child : fileList) { | |
285 | subList.add(child); | |
286 | } | |
287 | ||
288 | Collections.sort(subList, new Comparator<File>() { | |
289 | @Override | |
290 | public int compare(File o1, File o2) { | |
291 | return o1.getAbsolutePath().compareTo(o2.getAbsolutePath()); | |
292 | } | |
293 | }); | |
a2d29ca1 MK |
294 | ImportOperation operation = new ImportOperation( |
295 | path, | |
b8acd769 | 296 | parentFile, |
a2d29ca1 MK |
297 | fileSystemStructureProvider, |
298 | overwriteQuery, | |
299 | subList); | |
300 | operation.setContext(getShell()); | |
301 | operation.setCreateContainerStructure(false); | |
302 | if (executeImportOperation(operation)) { | |
89730b51 | 303 | setTraceTypeAndSourceLocation(traceToImport); |
a2d29ca1 MK |
304 | success = true; |
305 | } | |
d04ec5a7 | 306 | } |
a2d29ca1 | 307 | |
d04ec5a7 MK |
308 | } |
309 | } catch (Exception e) { | |
310 | } | |
311 | } | |
312 | return success; | |
313 | } | |
314 | ||
89730b51 PT |
315 | private IStatus setTraceTypeAndSourceLocation(FileAndName traceToImport) { |
316 | IStatus status = Status.OK_STATUS; | |
a6e37e4c | 317 | IResource resource = fTargetFolder.findMember(traceToImport.getName()); |
d04ec5a7 MK |
318 | if (resource != null) { |
319 | try { | |
26e33e67 MAL |
320 | // Set the trace type for this resource |
321 | String traceTypeId = traceToImport.getTraceTypeId(); | |
a4a116c3 | 322 | TraceTypeHelper traceType = TmfTraceType.getTraceType(traceTypeId); |
26e33e67 | 323 | if (traceType != null) { |
89730b51 | 324 | status = TmfTraceTypeUIUtils.setTraceType(resource, traceType); |
d04ec5a7 | 325 | } |
26e33e67 | 326 | |
89730b51 PT |
327 | // Set the source location for this resource |
328 | File file = traceToImport.getFile(); | |
329 | String sourceLocation = null; | |
330 | IResource sourceResource; | |
331 | if (file.isDirectory()) { | |
332 | sourceResource = ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation(Path.fromOSString(file.getAbsolutePath())); | |
333 | } else { | |
334 | sourceResource = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(Path.fromOSString(file.getAbsolutePath())); | |
335 | } | |
336 | if (sourceResource != null && sourceResource.exists()) { | |
337 | sourceLocation = sourceResource.getPersistentProperty(TmfCommonConstants.SOURCE_LOCATION); | |
338 | } | |
339 | if (sourceLocation == null) { | |
340 | sourceLocation = URIUtil.toUnencodedString(file.toURI()); | |
d04ec5a7 | 341 | } |
89730b51 | 342 | resource.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation); |
d04ec5a7 | 343 | } catch (CoreException e) { |
97c6e624 | 344 | Activator.getDefault().logError(Messages.BatchImportTraceWizardErrorImportingTraceResource |
32a0863e | 345 | + ' ' + resource.getName(), e); |
d04ec5a7 MK |
346 | } |
347 | } | |
89730b51 | 348 | return status; |
d04ec5a7 MK |
349 | } |
350 | ||
351 | @Override | |
352 | public boolean canFinish() { | |
a2d29ca1 | 353 | return super.canFinish() && hasTracesToImport() && !hasConflicts() && (fTargetFolder != null); |
d04ec5a7 MK |
354 | } |
355 | ||
356 | /** | |
357 | * Returns if a trace to import is selected | |
11c9462b | 358 | * |
d04ec5a7 MK |
359 | * @return if there are traces to import |
360 | */ | |
361 | public boolean hasTracesToImport() { | |
362 | return fTraces.size() > 0; | |
363 | } | |
364 | ||
365 | /** | |
366 | * Reset the files to scan | |
367 | */ | |
368 | public void clearFilesToScan() { | |
369 | fTracesToScan.clear(); | |
370 | } | |
371 | ||
372 | /** | |
373 | * Set the trace types to scan | |
374 | * | |
375 | * @param tracesToScan | |
376 | * a list of trace types to scan for | |
377 | */ | |
378 | public void setTraceTypesToScan(List<String> tracesToScan) { | |
379 | // intersection to know if there's a diff. | |
380 | // if there's a diff, we need to re-enque everything | |
507b1336 | 381 | List<String> added = new ArrayList<>(); |
9b48d94c MK |
382 | for (String traceLoc : tracesToScan) { |
383 | if (!fTraceTypesToScan.contains(traceLoc)) { | |
384 | added.add(traceLoc); | |
385 | } | |
386 | } | |
d04ec5a7 MK |
387 | fTraceTypesToScan.clear(); |
388 | fTraceTypesToScan.addAll(tracesToScan); | |
9b48d94c | 389 | updateTracesToScan(added); |
d04ec5a7 MK |
390 | } |
391 | ||
392 | /** | |
393 | * Get the trace types to scan | |
394 | * | |
395 | * @return a list of traces to Scan for | |
396 | */ | |
397 | public List<String> getTraceTypesToScan() { | |
398 | return fTraceTypesToScan; | |
399 | } | |
400 | ||
401 | /** | |
402 | * Add files to Import | |
403 | * | |
404 | * @param element | |
405 | * add the file and tracetype to import | |
406 | */ | |
407 | public void addFileToImport(FileAndName element) { | |
408 | fTraces.add(element); | |
409 | updateConflicts(); | |
410 | } | |
411 | ||
412 | /** | |
413 | * Remove the file to scan | |
414 | * | |
415 | * @param element | |
416 | * the element to remove | |
417 | */ | |
418 | public void removeFileToImport(FileAndName element) { | |
419 | fTraces.remove(element); | |
420 | element.setConflictingName(false); | |
421 | updateConflicts(); | |
422 | } | |
423 | ||
424 | /** | |
425 | * Updates the trace to see if there are conflicts. | |
426 | */ | |
427 | public void updateConflicts() { | |
428 | final FileAndName[] fChildren = fTraces.toArray(new FileAndName[0]); | |
429 | for (int i = 0; i < fChildren.length; i++) { | |
430 | fChildren[i].setConflictingName(false); | |
431 | } | |
432 | for (int i = 1; i < fChildren.length; i++) { | |
433 | for (int j = 0; j < i; j++) { | |
434 | if (fChildren[i].getName().equals(fChildren[j].getName())) { | |
435 | fChildren[i].setConflictingName(true); | |
436 | fChildren[j].setConflictingName(true); | |
437 | } | |
438 | } | |
439 | } | |
440 | getContainer().updateButtons(); | |
441 | } | |
442 | ||
443 | /** | |
444 | * Is there a name conflict | |
445 | */ | |
446 | boolean hasConflicts() { | |
447 | boolean conflict = false; | |
448 | for (FileAndName child : fTraces) { | |
449 | conflict |= child.isConflictingName(); | |
450 | } | |
451 | return conflict; | |
452 | } | |
453 | ||
454 | private boolean executeImportOperation(ImportOperation op) { | |
455 | initializeOperation(op); | |
456 | ||
457 | try { | |
458 | getContainer().run(true, true, op); | |
459 | } catch (InterruptedException e) { | |
460 | return false; | |
461 | } catch (InvocationTargetException e) { | |
4196e938 | 462 | Activator.getDefault().logError(Messages.ImportTraceWizardImportProblem, e); |
d04ec5a7 MK |
463 | return false; |
464 | } | |
465 | ||
466 | IStatus status = op.getStatus(); | |
467 | if (!status.isOK()) { | |
97c6e624 | 468 | ErrorDialog.openError(getContainer().getShell(), Messages.ImportTraceWizardImportProblem, null, status); |
d04ec5a7 MK |
469 | return false; |
470 | } | |
471 | ||
472 | return true; | |
473 | } | |
474 | ||
475 | private static void initializeOperation(ImportOperation op) { | |
476 | op.setCreateContainerStructure(false); | |
477 | op.setOverwriteResources(false); | |
d04ec5a7 MK |
478 | op.setVirtualFolders(false); |
479 | } | |
480 | ||
481 | /** | |
482 | * Override existing resources | |
483 | * | |
484 | * @param selection | |
485 | * true or false | |
486 | */ | |
487 | public void setOverwrite(boolean selection) { | |
488 | fOverwrite = selection; | |
489 | } | |
490 | ||
491 | /** | |
492 | * Is the trace linked? | |
493 | * | |
494 | * @param isLink | |
495 | * true or false | |
496 | */ | |
497 | public void setLinked(boolean isLink) { | |
498 | fLinked = isLink; | |
499 | } | |
500 | ||
501 | /** | |
502 | * @param tracesToScan | |
503 | * sets the common traces to scan | |
504 | */ | |
505 | public void setTracesToScan(BlockingQueue<TraceValidationHelper> tracesToScan) { | |
506 | fTracesToScan = tracesToScan; | |
507 | } | |
508 | ||
509 | /** | |
510 | * @param traceToScan | |
511 | * The trace to scan | |
512 | * @return if the trace has been scanned yet or not | |
47aafe74 | 513 | * @since 3.0 |
d04ec5a7 MK |
514 | */ |
515 | public boolean hasScanned(TraceValidationHelper traceToScan) { | |
516 | return fResults.containsKey(traceToScan); | |
517 | } | |
518 | ||
519 | /** | |
520 | * Add a result to a cache | |
521 | * | |
522 | * @param traceToScan | |
523 | * The trace that has been scanned | |
524 | * @param validate | |
525 | * if the trace is valid | |
47aafe74 | 526 | * @since 3.0 |
d04ec5a7 MK |
527 | */ |
528 | public void addResult(TraceValidationHelper traceToScan, boolean validate) { | |
529 | fResults.put(traceToScan, validate); | |
530 | } | |
531 | ||
532 | /** | |
533 | * Gets if the trace has been scanned or not | |
534 | * | |
535 | * @param traceToScan | |
536 | * the scanned trace | |
537 | * @return whether it passes or not | |
47aafe74 | 538 | * @since 3.0 |
d04ec5a7 | 539 | */ |
0126a8ca | 540 | public boolean getResult(TraceValidationHelper traceToScan) { |
d04ec5a7 MK |
541 | return fResults.get(traceToScan); |
542 | } | |
543 | ||
544 | /** | |
545 | * Returns the amount of files scanned | |
546 | * | |
547 | * @return the amount of files scanned | |
548 | */ | |
549 | public int getNumberOfResults() { | |
550 | return fResults.size(); | |
551 | } | |
552 | ||
9b48d94c MK |
553 | private void updateTracesToScan(final List<String> added) { |
554 | // Treeset is used instead of a hashset since the traces should be read | |
555 | // in the order they were added. | |
507b1336 | 556 | final Set<String> filesToScan = new TreeSet<>(); |
9b48d94c MK |
557 | for (String name : fParentFiles.keySet()) { |
558 | filesToScan.addAll(fParentFiles.get(name)); | |
559 | } | |
560 | IProgressMonitor pm = new NullProgressMonitor(); | |
561 | try { | |
562 | updateScanQueue(pm, filesToScan, added); | |
563 | } catch (InterruptedException e) { | |
564 | } | |
ba486486 | 565 | |
9b48d94c MK |
566 | } |
567 | ||
d04ec5a7 MK |
568 | /* |
569 | * I am a job. Make me work | |
570 | */ | |
91e941e8 | 571 | private synchronized IStatus updateFiles(IProgressMonitor monitor, String traceToScanAbsPath) { |
507b1336 | 572 | final Set<String> filesToScan = new TreeSet<>(); |
9b48d94c | 573 | |
d04ec5a7 | 574 | int workToDo = 1; |
11c9462b | 575 | for (String name : fParentFiles.keySet()) { |
d04ec5a7 MK |
576 | |
577 | final File file = new File(name); | |
578 | final File[] listFiles = file.listFiles(); | |
579 | if (listFiles != null) { | |
580 | workToDo += listFiles.length; | |
581 | } | |
582 | } | |
583 | int step = TOTALWORK / workToDo; | |
584 | try { | |
11c9462b | 585 | for (String name : fParentFiles.keySet()) { |
d04ec5a7 | 586 | final File fileToAdd = new File(name); |
11c9462b MK |
587 | final Set<String> parentFilesToScan = fParentFiles.get(fileToAdd.getAbsolutePath()); |
588 | recurse(parentFilesToScan, fileToAdd, monitor, step); | |
589 | if (monitor.isCanceled()) { | |
91e941e8 MAL |
590 | fParentFilesToScan.remove(traceToScanAbsPath); |
591 | fParentFiles.remove(traceToScanAbsPath); | |
11c9462b MK |
592 | return CANCEL_STATUS; |
593 | } | |
594 | } | |
595 | filesToScan.clear(); | |
596 | for (String name : fParentFiles.keySet()) { | |
597 | filesToScan.addAll(fParentFiles.get(name)); | |
598 | fParentFilesToScan.add(name); | |
d04ec5a7 | 599 | } |
9b48d94c MK |
600 | IStatus cancelled = updateScanQueue(monitor, filesToScan, fTraceTypesToScan); |
601 | if (cancelled.matches(IStatus.CANCEL)) { | |
91e941e8 MAL |
602 | fParentFilesToScan.remove(traceToScanAbsPath); |
603 | fParentFiles.remove(traceToScanAbsPath); | |
d04ec5a7 MK |
604 | } |
605 | } catch (InterruptedException e) { | |
606 | monitor.done(); | |
607 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e); | |
608 | } | |
609 | ||
610 | monitor.done(); | |
611 | return Status.OK_STATUS; | |
612 | } | |
613 | ||
9b48d94c MK |
614 | private IStatus updateScanQueue(IProgressMonitor monitor, final Set<String> filesToScan, final List<String> traceTypes) throws InterruptedException { |
615 | for (String fileToScan : filesToScan) { | |
616 | for (String traceCat : traceTypes) { | |
617 | TraceValidationHelper tv = new TraceValidationHelper(fileToScan, traceCat); | |
618 | // for thread safety, keep checks in this order. | |
619 | if (!fResults.containsKey(tv)) { | |
620 | if (!fTracesToScan.contains(tv)) { | |
621 | fTracesToScan.put(tv); | |
622 | monitor.subTask(tv.getTraceToScan()); | |
623 | if (monitor.isCanceled()) { | |
ba486486 | 624 | fScanPage.refresh(); |
9b48d94c MK |
625 | return CANCEL_STATUS; |
626 | } | |
627 | } | |
628 | } | |
629 | } | |
630 | } | |
ba486486 | 631 | fScanPage.refresh(); |
9b48d94c MK |
632 | return Status.OK_STATUS; |
633 | } | |
634 | ||
11c9462b | 635 | private IStatus recurse(Set<String> filesToScan, File fileToAdd, IProgressMonitor monitor, int step) { |
d04ec5a7 MK |
636 | final String absolutePath = fileToAdd.getAbsolutePath(); |
637 | if (!filesToScan.contains(absolutePath) && (filesToScan.size() < MAX_FILES)) { | |
638 | filesToScan.add(absolutePath); | |
639 | final File[] listFiles = fileToAdd.listFiles(); | |
640 | if (null != listFiles) { | |
641 | for (File child : listFiles) { | |
642 | monitor.subTask(child.getName()); | |
11c9462b MK |
643 | if (monitor.isCanceled()) { |
644 | return CANCEL_STATUS; | |
645 | } | |
646 | IStatus retVal = recurse(filesToScan, child, monitor); | |
9b48d94c | 647 | if (retVal.matches(IStatus.CANCEL)) { |
11c9462b MK |
648 | return retVal; |
649 | } | |
d04ec5a7 MK |
650 | monitor.worked(step); |
651 | } | |
652 | } | |
653 | } | |
11c9462b | 654 | return Status.OK_STATUS; |
d04ec5a7 MK |
655 | } |
656 | ||
11c9462b | 657 | private IStatus recurse(Set<String> filesToScan, File fileToAdd, IProgressMonitor monitor) { |
d04ec5a7 MK |
658 | final String absolutePath = fileToAdd.getAbsolutePath(); |
659 | if (!filesToScan.contains(absolutePath) && (filesToScan.size() < MAX_FILES)) { | |
660 | filesToScan.add(absolutePath); | |
661 | final File[] listFiles = fileToAdd.listFiles(); | |
662 | if (null != listFiles) { | |
663 | for (File child : listFiles) { | |
11c9462b MK |
664 | if (monitor.isCanceled()) { |
665 | return CANCEL_STATUS; | |
666 | } | |
667 | IStatus retVal = recurse(filesToScan, child, monitor); | |
9b48d94c | 668 | if (retVal.matches(IStatus.CANCEL)) { |
11c9462b MK |
669 | return retVal; |
670 | } | |
d04ec5a7 MK |
671 | } |
672 | } | |
673 | } | |
11c9462b | 674 | return Status.OK_STATUS; |
d04ec5a7 MK |
675 | } |
676 | ||
677 | /** | |
678 | * Gets the folder in the resource (project) | |
679 | * | |
680 | * @param targetFolder | |
681 | * the folder to import to | |
682 | */ | |
683 | public void setTraceFolder(IFolder targetFolder) { | |
684 | fTargetFolder = targetFolder; | |
a2d29ca1 MK |
685 | if (this.getContainer() != null && this.getContainer().getCurrentPage() != null) { |
686 | this.getContainer().updateButtons(); | |
687 | } | |
688 | } | |
689 | ||
690 | /** | |
691 | * Gets the target folder | |
692 | * | |
693 | * @return the target folder | |
694 | */ | |
695 | public IFolder getTargetFolder() { | |
696 | return fTargetFolder; | |
d04ec5a7 MK |
697 | } |
698 | ||
699 | } |