use of com.intellij.ide.util.treeView.AbstractTreeNode in project android by JetBrains.
the class AndroidProjectTreeBuilder method findNodeByElement.
/**
* Returns the tree node corresponding to a model element.
* e.g. from a PsiDirectory -> tree node corresponding to that PsiDirectory
*
* When {@link com.intellij.ide.util.treeView.AbstractTreeUi} creates a {@link javax.swing.tree.DefaultMutableTreeNode} for a given
* {@link com.intellij.ide.util.treeView.AbstractTreeNode}, it maintains a mapping between. This mapping between the model element to
* the tree node is necessary when locating items by their model element (PsiFile or PsiDirectory).
*
* In the Android view, we have virtual nodes that don't correspond to Psi Elements, or a single virtual node corresponding to
* multiple files/directories. Since such mappings aren't saved by the tree UI, these are handled in this method.
*
* The way this works is that every time a virtual node is created, it calls back to {@link #createMapping()} to save that mapping
* between the virtual file and the node that represents it. When we need to map a virtual file to its tree node, we look at the
* saved mapping to see if that virtual file corresponds to any node. If so, we obtain the tree node corresponding to the node's parent,
* then iterate through its children to locate the tree node corresponding to the element.
*/
@Nullable
@Override
protected Object findNodeByElement(@Nullable Object element) {
if (element == null) {
return null;
}
Object node = super.findNodeByElement(element);
if (node != null) {
return node;
}
VirtualFile virtualFile = null;
if (element instanceof PsiDirectory) {
virtualFile = ((PsiDirectory) element).getVirtualFile();
} else if (element instanceof PsiFile) {
virtualFile = ((PsiFile) element).getVirtualFile();
}
if (virtualFile == null) {
return null;
}
AbstractTreeNode treeNode = getNodeForFile(virtualFile);
if (treeNode == null) {
return null;
}
// recurse and find the tree node corresponding to the parent
Object parentNode = findNodeByElement(treeNode.getParent());
if (!(parentNode instanceof DefaultMutableTreeNode)) {
return null;
}
// examine all the children of the parent tree node and return the one that maps to this virtual file.
Enumeration children = ((DefaultMutableTreeNode) parentNode).children();
while (children.hasMoreElements()) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode) children.nextElement();
if (child.getUserObject() instanceof DirectoryGroupNode) {
for (PsiDirectory folder : ((DirectoryGroupNode) child.getUserObject()).getDirectories()) {
if (folder.getVirtualFile().equals(virtualFile)) {
return child;
}
}
}
}
return null;
}
use of com.intellij.ide.util.treeView.AbstractTreeNode in project android by JetBrains.
the class AndroidManifestsGroupNode method getChildren.
@NotNull
@Override
public Collection<? extends AbstractTreeNode> getChildren() {
PsiManager psiManager = PsiManager.getInstance(myProject);
List<AbstractTreeNode> children = Lists.newArrayList();
for (VirtualFile manifest : mySources) {
if (!manifest.isValid()) {
continue;
}
PsiFile psiFile = psiManager.findFile(manifest);
if (psiFile != null) {
AndroidFacet facet = getValue();
assert facet != null : "Android Facet for module cannot be null";
children.add(new AndroidManifestFileNode(myProject, psiFile, getSettings(), facet));
}
}
return children;
}
use of com.intellij.ide.util.treeView.AbstractTreeNode in project android by JetBrains.
the class AndroidTreeStructureProvider method modify.
@Override
@NotNull
public Collection<AbstractTreeNode> modify(@NotNull AbstractTreeNode parent, @NotNull Collection<AbstractTreeNode> children, ViewSettings settings) {
Project project = parent.getProject();
if (project != null && AndroidProjectInfo.getInstance(project).requiresAndroidModel()) {
if (parent instanceof NamedLibraryElementNode) {
NamedLibraryElement value = ((NamedLibraryElementNode) parent).getValue();
LibraryOrSdkOrderEntry orderEntry = value.getOrderEntry();
if (orderEntry instanceof JdkOrderEntry) {
Sdk sdk = ((JdkOrderEntry) orderEntry).getJdk();
if (sdk.getSdkType() instanceof JavaSdk) {
List<AbstractTreeNode> newChildren = Lists.newArrayList();
for (AbstractTreeNode child : children) {
if (isRtJar(child)) {
newChildren.add(child);
}
}
if (!newChildren.isEmpty()) {
myEventDispatcher.getMulticaster().nodeChanged(parent, newChildren);
return newChildren;
}
}
}
} else if (isRtJar(parent)) {
List<AbstractTreeNode> newChildren = Lists.newArrayList();
for (AbstractTreeNode child : children) {
if (child instanceof PsiDirectoryNode) {
VirtualFile file = ((PsiDirectoryNode) child).getVirtualFile();
if (file != null && ("java".equals(file.getName()) || "javax".equals(file.getName()))) {
newChildren.add(child);
}
}
}
if (!newChildren.isEmpty()) {
myEventDispatcher.getMulticaster().nodeChanged(parent, newChildren);
return newChildren;
}
}
}
return children;
}
use of com.intellij.ide.util.treeView.AbstractTreeNode in project intellij-plugins by JetBrains.
the class SwfProjectViewStructureProvider method getChildrenForPackage.
static Collection<AbstractTreeNode> getChildrenForPackage(String aPackage, List<JSQualifiedNamedElement> elements, int from, int to, Project project, ViewSettings settings) {
List<AbstractTreeNode> packages = new ArrayList<>();
List<AbstractTreeNode> classes = new ArrayList<>();
int subpackageStart = -1;
String currentSubpackage = null;
for (int i = from; i < to; i++) {
JSQualifiedNamedElement element = elements.get(i);
String qName = element.getQualifiedName();
assert aPackage.isEmpty() || qName.startsWith(aPackage + ".") : qName + " does not start with " + aPackage;
if (StringUtil.getPackageName(qName).equals(aPackage)) {
classes.add(new SwfQualifiedNamedElementNode(project, element, settings));
} else {
final int endIndex = qName.indexOf('.', aPackage.length() + 1);
if (endIndex <= 0) {
final Attachment attachment = element.getParent() != null ? new Attachment("Parent element.txt", element.getParent().getText()) : new Attachment("Element text.txt", element.getText());
LOG.error(LogMessageEx.createEvent("package=[" + aPackage + "], qName=[" + qName + "]", DebugUtil.currentStackTrace(), attachment));
continue;
}
String subpackage = settings.isFlattenPackages() ? StringUtil.getPackageName(qName) : qName.substring(0, endIndex);
if (currentSubpackage == null) {
subpackageStart = i;
} else if (!currentSubpackage.equals(subpackage)) {
packages.add(createSubpackageNode(elements, project, settings, subpackageStart, i, currentSubpackage));
subpackageStart = i;
}
currentSubpackage = subpackage;
}
}
if (currentSubpackage != null) {
packages.add(createSubpackageNode(elements, project, settings, subpackageStart, to, currentSubpackage));
}
return ContainerUtil.concat(packages, classes);
}
use of com.intellij.ide.util.treeView.AbstractTreeNode in project android by JetBrains.
the class ResourceDirectoryNode method collectChildren.
public void collectChildren() {
for (AbstractTreeNode child : myBaseNode.getChildren()) {
Object value = child.getValue();
if (value instanceof PsiNamedElement) {
String name = ((PsiNamedElement) value).getName();
if (!myChildMap.containsKey(name)) {
myChildMap.put(name, child);
myChildren.add(child);
}
} else {
myChildren.add(child);
}
}
}
Aggregations