use of com.intellij.util.TreeItem in project intellij-community by JetBrains.
the class FavoritesManager method addRoot.
public synchronized boolean addRoot(@NotNull String name, @NotNull List<AbstractTreeNode> parentElements, final AbstractTreeNode newElement, @Nullable AbstractTreeNode sibling) {
final List<TreeItem<Pair<AbstractUrl, String>>> items = myName2FavoritesRoots.get(name);
if (items == null)
return false;
AbstractUrl url = createUrlByElement(newElement.getValue(), myProject);
if (url == null)
return false;
final TreeItem<Pair<AbstractUrl, String>> newItem = new TreeItem<>(Pair.create(url, newElement.getClass().getName()));
if (parentElements.isEmpty()) {
// directly to list
if (sibling != null) {
TreeItem<Pair<AbstractUrl, String>> after = null;
AbstractUrl siblingUrl = createUrlByElement(sibling.getValue(), myProject);
int idx = -1;
for (int i = 0; i < items.size(); i++) {
TreeItem<Pair<AbstractUrl, String>> item = items.get(i);
if (item.getData().getFirst().equals(siblingUrl)) {
idx = i;
break;
}
}
if (idx != -1) {
items.add(idx, newItem);
} else {
items.add(newItem);
}
} else {
items.add(newItem);
}
rootsChanged();
return true;
}
Collection<TreeItem<Pair<AbstractUrl, String>>> list = items;
TreeItem<Pair<AbstractUrl, String>> item = null;
for (AbstractTreeNode obj : parentElements) {
AbstractUrl objUrl = createUrlByElement(obj.getValue(), myProject);
item = findNextItem(objUrl, list);
if (item == null)
return false;
list = item.getChildren();
}
if (sibling != null) {
TreeItem<Pair<AbstractUrl, String>> after = null;
AbstractUrl siblingUrl = createUrlByElement(sibling.getValue(), myProject);
for (TreeItem<Pair<AbstractUrl, String>> treeItem : list) {
if (treeItem.getData().getFirst().equals(siblingUrl)) {
after = treeItem;
break;
}
}
if (after == null) {
item.addChild(newItem);
} else {
item.addChildAfter(newItem, after);
}
} else {
item.addChild(newItem);
}
rootsChanged();
return true;
}
use of com.intellij.util.TreeItem in project intellij-community by JetBrains.
the class FavoritesManager method contains.
// currently only one level here..
public boolean contains(@NotNull String name, @NotNull final VirtualFile vFile) {
final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
final Set<Boolean> find = new HashSet<>();
final ContentIterator contentIterator = new ContentIterator() {
@Override
public boolean processFile(VirtualFile fileOrDir) {
if (fileOrDir != null && fileOrDir.getPath().equals(vFile.getPath())) {
find.add(Boolean.TRUE);
}
return true;
}
};
Collection<TreeItem<Pair<AbstractUrl, String>>> urls = getFavoritesListRootUrls(name);
for (TreeItem<Pair<AbstractUrl, String>> pair : urls) {
AbstractUrl abstractUrl = pair.getData().getFirst();
if (abstractUrl == null) {
continue;
}
final Object[] path = abstractUrl.createPath(myProject);
if (path == null || path.length < 1 || path[0] == null) {
continue;
}
Object element = path[path.length - 1];
if (element instanceof SmartPsiElementPointer) {
final VirtualFile virtualFile = PsiUtilBase.getVirtualFile(((SmartPsiElementPointer) element).getElement());
if (virtualFile == null)
continue;
if (vFile.getPath().equals(virtualFile.getPath())) {
return true;
}
if (!virtualFile.isDirectory()) {
continue;
}
projectFileIndex.iterateContentUnderDirectory(virtualFile, contentIterator);
}
if (element instanceof PsiElement) {
final VirtualFile virtualFile = PsiUtilBase.getVirtualFile((PsiElement) element);
if (virtualFile == null)
continue;
if (vFile.getPath().equals(virtualFile.getPath())) {
return true;
}
if (!virtualFile.isDirectory()) {
continue;
}
projectFileIndex.iterateContentUnderDirectory(virtualFile, contentIterator);
}
if (element instanceof Module) {
ModuleRootManager.getInstance((Module) element).getFileIndex().iterateContent(contentIterator);
}
if (element instanceof LibraryGroupElement) {
final boolean inLibrary = ModuleRootManager.getInstance(((LibraryGroupElement) element).getModule()).getFileIndex().isInContent(vFile) && projectFileIndex.isInLibraryClasses(vFile);
if (inLibrary) {
return true;
}
}
if (element instanceof NamedLibraryElement) {
NamedLibraryElement namedLibraryElement = (NamedLibraryElement) element;
final VirtualFile[] files = namedLibraryElement.getOrderEntry().getRootFiles(OrderRootType.CLASSES);
if (files != null && ArrayUtil.find(files, vFile) > -1) {
return true;
}
}
if (element instanceof ModuleGroup) {
ModuleGroup group = (ModuleGroup) element;
final Collection<Module> modules = group.modulesInGroup(myProject, true);
for (Module module : modules) {
ModuleRootManager.getInstance(module).getFileIndex().iterateContent(contentIterator);
}
}
for (FavoriteNodeProvider provider : Extensions.getExtensions(FavoriteNodeProvider.EP_NAME, myProject)) {
if (provider.elementContainsFile(element, vFile)) {
return true;
}
}
if (!find.isEmpty()) {
return true;
}
}
return false;
}
use of com.intellij.util.TreeItem in project intellij-community by JetBrains.
the class FavoritesManager method readFavoritesOneLevel.
private static void readFavoritesOneLevel(Element list, Project project, Collection<TreeItem<Pair<AbstractUrl, String>>> result) {
final List listChildren = list.getChildren(FAVORITES_ROOT);
if (listChildren == null || listChildren.isEmpty())
return;
for (Object favorite : listChildren) {
final Element favoriteElement = (Element) favorite;
final String className = favoriteElement.getAttributeValue(CLASS_NAME);
final AbstractUrl abstractUrl = readUrlFromElement(favoriteElement, project);
if (abstractUrl != null) {
final TreeItem<Pair<AbstractUrl, String>> treeItem = new TreeItem<>(Pair.create(abstractUrl, className));
result.add(treeItem);
readFavoritesOneLevel(favoriteElement, project, treeItem.getChildren());
}
}
}
use of com.intellij.util.TreeItem in project intellij-community by JetBrains.
the class FavoritesListNode method processUrls.
private static void processUrls(Project project, Collection<TreeItem<Pair<AbstractUrl, String>>> urls, Collection<AbstractTreeNode> result, final AbstractTreeNode me) {
for (TreeItem<Pair<AbstractUrl, String>> pair : urls) {
AbstractUrl abstractUrl = pair.getData().getFirst();
final Object[] path = abstractUrl.createPath(project);
if (path == null || path.length < 1 || path[0] == null) {
continue;
}
try {
final String className = pair.getData().getSecond();
@SuppressWarnings("unchecked") final Class<? extends AbstractTreeNode> nodeClass = (Class<? extends AbstractTreeNode>) Class.forName(className);
final AbstractTreeNode node = ProjectViewNode.createTreeNode(nodeClass, project, path[path.length - 1], FavoritesManager.getInstance(project).getViewSettings());
node.setParent(me);
node.setIndex(result.size());
result.add(node);
if (node instanceof ProjectViewNodeWithChildrenList) {
final List<TreeItem<Pair<AbstractUrl, String>>> children = pair.getChildren();
if (children != null && !children.isEmpty()) {
Collection<AbstractTreeNode> childList = new ArrayList<>();
processUrls(project, children, childList, node);
for (AbstractTreeNode treeNode : childList) {
((ProjectViewNodeWithChildrenList) node).addChild(treeNode);
}
}
}
} catch (Exception e) {
LOGGER.error(e);
}
}
}
Aggregations