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