blob: 9a2787ac4e9bb171e3c924a58ca7bbcda0fc8b81 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/*******************************************************************************
* Copyright (c) 2000, 2018 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.debug.internal.ui.sourcelookup.browsers;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.custom.BusyIndicator;
/**
* ArchiveFilter
*/
public class ArchiveFilter extends ViewerFilter {
/**
* Collection of archives and containers to display
*/
private Set<IResource> fArchives;
/**
* Collection of already existing archives
*/
private List<ISourceContainer> fExisting;
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
return fArchives.contains(element) && !fExisting.contains(element);
}
/**
* Constructs a new filter to display archives and their containers,
* excluding the resources in the given list.
*
* @param objects resources to exclude
*/
public ArchiveFilter(List<ISourceContainer> objects) {
fExisting = objects;
init();
}
/**
* Search for all archives in the workspace.
*/
private void init() {
BusyIndicator.showWhile(DebugUIPlugin.getStandardDisplay(), () -> {
fArchives = new HashSet<>();
traverse(ResourcesPlugin.getWorkspace().getRoot(), fArchives);
});
}
/**
* Traverse the given container, adding archives to the given set.
* Returns whether any files were added
* @param container the container to check
* @param set the set to add any found archives to
* @return <code>true</code> if any archives have been added false otherwise
*/
private boolean traverse(IContainer container, Set<IResource> set) {
boolean added = false;
try {
IResource[] resources = container.members();
for (IResource resource : resources) {
if (resource instanceof IFile) {
IFile file = (IFile)resource;
String ext = file.getFileExtension();
if (ext != null && (ext.equalsIgnoreCase("jar") || ext.equalsIgnoreCase("zip"))) { //$NON-NLS-1$ //$NON-NLS-2$
set.add(file);
added = true;
}
} else if (resource instanceof IContainer) {
if (traverse((IContainer)resource, set)) {
set.add(resource);
added = true;
}
}
}
} catch (CoreException e) {
}
return added;
}
}
|