use of com.android.tools.idea.navigator.nodes.DirectoryGroupNode in project android by JetBrains.
the class AndroidProjectViewPane method getSelectedDirectories.
@Override
@NotNull
public PsiDirectory[] getSelectedDirectories() {
Object selectedElement = getSelectedElement();
if (selectedElement instanceof PackageElement) {
PackageElement packageElement = (PackageElement) selectedElement;
Module m = packageElement.getModule();
if (m != null) {
return packageElement.getPackage().getDirectories(GlobalSearchScope.moduleScope(m));
}
}
NodeDescriptor descriptor = getSelectedDescriptor();
if (descriptor instanceof DirectoryGroupNode) {
return ((DirectoryGroupNode) descriptor).getDirectories();
}
PsiDirectory[] selectedDirectories = super.getSelectedDirectories();
// to treat these as selectable (for target output directories etc)
if (selectedElement instanceof Module && selectedDirectories.length > 0) {
List<PsiDirectory> dirs = Lists.newArrayListWithExpectedSize(selectedDirectories.length);
for (PsiDirectory dir : selectedDirectories) {
VirtualFile file = dir.getVirtualFile();
if (!GeneratedSourcesFilter.isGeneratedSourceByAnyFilter(file, myProject)) {
if (file.getParent() != null && file.getPath().contains("/generated/")) {
/// build/generated/res/rs/debug, build/generated/res/rs/test/debug, etc.
continue;
}
dirs.add(dir);
}
}
selectedDirectories = dirs.toArray(new PsiDirectory[dirs.size()]);
}
return selectedDirectories;
}
use of com.android.tools.idea.navigator.nodes.DirectoryGroupNode 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.android.tools.idea.navigator.nodes.DirectoryGroupNode in project android by JetBrains.
the class AndroidProjectViewPane method getData.
@Override
public Object getData(String dataId) {
if (CommonDataKeys.PROJECT.is(dataId)) {
return myProject;
}
if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) {
Object o = getSelectedElement();
if (o instanceof PsiDirectory) {
VirtualFile directory = ((PsiDirectory) o).getVirtualFile();
// See https://code.google.com/p/android/issues/detail?id=212522
if (isTopModuleDirectoryOrParent(directory)) {
return new NoOpDeleteProvider();
}
}
}
if (LangDataKeys.MODULE.is(dataId)) {
Object o = getSelectedElement();
if (o instanceof PackageElement) {
PackageElement packageElement = (PackageElement) o;
return packageElement.getModule();
} else if (o instanceof AndroidFacet) {
return ((AndroidFacet) o).getModule();
}
}
if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
Object o = getSelectedElement();
if (o instanceof PackageElement) {
PackageElement packageElement = (PackageElement) o;
Module m = packageElement.getModule();
if (m != null) {
PsiDirectory[] folders = packageElement.getPackage().getDirectories(GlobalSearchScope.moduleScope(m));
if (folders.length > 0) {
return folders[0].getVirtualFile();
} else {
return null;
}
}
}
}
if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(dataId)) {
NodeDescriptor selectedDescriptor = getSelectedDescriptor();
if (selectedDescriptor instanceof FileGroupNode) {
PsiFile[] files = ((FileGroupNode) selectedDescriptor).getFiles();
if (files.length > 0) {
List<VirtualFile> virtualFiles = Lists.newArrayListWithExpectedSize(files.length);
for (PsiFile file : files) {
if (file.isValid()) {
virtualFiles.add(file.getVirtualFile());
}
}
return virtualFiles.toArray(new VirtualFile[virtualFiles.size()]);
}
}
if (selectedDescriptor instanceof DirectoryGroupNode) {
PsiDirectory[] directories = ((DirectoryGroupNode) selectedDescriptor).getDirectories();
if (directories.length > 0) {
List<VirtualFile> virtualFiles = Lists.newArrayListWithExpectedSize(directories.length);
for (PsiDirectory directory : directories) {
if (directory.isValid()) {
virtualFiles.add(directory.getVirtualFile());
}
}
return virtualFiles.toArray(new VirtualFile[virtualFiles.size()]);
}
}
}
if (CommonDataKeys.PSI_ELEMENT.is(dataId)) {
Object o = getSelectedElement();
if (o instanceof PsiElement) {
return o;
} else if (o instanceof List<?>) {
List<?> l = (List<?>) o;
if (!l.isEmpty() && l.get(0) instanceof PsiElement) {
return l.get(0);
}
}
NodeDescriptor selectedDescriptor = getSelectedDescriptor();
if (selectedDescriptor instanceof FileGroupNode) {
PsiFile[] files = ((FileGroupNode) selectedDescriptor).getFiles();
if (files.length > 0) {
return files[0];
}
}
if (selectedDescriptor instanceof DirectoryGroupNode) {
PsiDirectory[] directories = ((DirectoryGroupNode) selectedDescriptor).getDirectories();
if (directories.length > 0) {
return directories[0];
}
}
}
return super.getData(dataId);
}
Aggregations