public boolean addRoots(final String name, final Collection<AbstractTreeNode> nodes) { final Collection<TreeItem<Pair<AbstractUrl, String>>> list = getFavoritesListRootUrls(name); final HashSet<AbstractUrl> set = new HashSet<AbstractUrl>(ContainerUtil.map(list, new Function<TreeItem<Pair<AbstractUrl,String>>, AbstractUrl>() { @Override public AbstractUrl fun(TreeItem<Pair<AbstractUrl, String>> item) { return item.getData().getFirst(); } })); for (AbstractTreeNode node : nodes) { final Pair<AbstractUrl, String> pair = createPairForNode(node); if (pair != null) { if (set.contains(pair.getFirst())) continue; final TreeItem<Pair<AbstractUrl, String>> treeItem = new TreeItem<Pair<AbstractUrl, String>>(pair); list.add(treeItem); set.add(pair.getFirst()); appendChildNodes(node, treeItem); } } rootsChanged(); return true; }
public boolean canAddRoots(@NotNull String name, @NotNull Collection<AbstractTreeNode> nodes) { final Collection<TreeItem<Pair<AbstractUrl, String>>> list = getFavoritesListRootUrls(name); final HashSet<AbstractUrl> set = new HashSet<AbstractUrl>(ContainerUtil.map(list, new Function<TreeItem<Pair<AbstractUrl,String>>, AbstractUrl>() { @Override public AbstractUrl fun(TreeItem<Pair<AbstractUrl, String>> item) { return item.getData().getFirst(); } })); for (AbstractTreeNode node : nodes) { final Pair<AbstractUrl, String> pair = createPairForNode(node); if (pair != null && !set.contains(pair.getFirst())) return true; } return false; }
public List<TreeItem<VcsFileRevision>> createTreeOn(List<VcsFileRevision> allRevisions) { Collections.sort(allRevisions, VcsFileRevisionComparator.INSTANCE); final List<TreeItem<VcsFileRevision>> result = new ArrayList<TreeItem<VcsFileRevision>>(); TreeItem<VcsFileRevision> prevRevision = null; for (final VcsFileRevision sortedRevision : allRevisions) { final CvsFileRevisionImpl cvsFileRevision = (CvsFileRevisionImpl)sortedRevision; final TreeItem<VcsFileRevision> treeItem = new TreeItem<VcsFileRevision>(cvsFileRevision); final TreeItem<VcsFileRevision> commonParent = getCommonParent(prevRevision, treeItem); if (commonParent != null) { commonParent.addChild(treeItem); } else { result.add(treeItem); } prevRevision = treeItem; } return result; }
public boolean addRoots(final String name, final Collection<AbstractTreeNode> nodes) { final Collection<TreeItem<Pair<AbstractUrl, String>>> list = getFavoritesListRootUrls(name); final HashSet<AbstractUrl> set = new HashSet<AbstractUrl>(ObjectsConvertor.convert(list, new Convertor<TreeItem<Pair<AbstractUrl, String>>, AbstractUrl>() { @Override public AbstractUrl convert(TreeItem<Pair<AbstractUrl, String>> o) { return o.getData().getFirst(); } })); for (AbstractTreeNode node : nodes) { final Pair<AbstractUrl, String> pair = createPairForNode(node); if (pair != null) { if (set.contains(pair.getFirst())) continue; final TreeItem<Pair<AbstractUrl, String>> treeItem = new TreeItem<Pair<AbstractUrl, String>>(pair); list.add(treeItem); set.add(pair.getFirst()); appendChildNodes(node, treeItem); } } rootsChanged(); return true; }
private boolean renameFavoritesList(@NotNull String oldName, @NotNull String newName) { List<TreeItem<Pair<AbstractUrl, String>>> list = myName2FavoritesRoots.remove(oldName); if (list != null && newName.length() > 0) { int index = myListOrder.indexOf(oldName); if (index == -1) { index = myListOrder.size(); } myListOrder.set(index, newName); myName2FavoritesRoots.put(newName, list); String description = myDescriptions.remove(oldName); if (description != null) { myDescriptions.put(newName, description); } rootsChanged(); return true; } return false; }
public boolean addRoots(final String name, final Collection<AbstractTreeNode> nodes) { final Collection<TreeItem<Pair<AbstractUrl, String>>> list = getFavoritesListRootUrls(name); final HashSet<AbstractUrl> set = new HashSet<AbstractUrl>(ContainerUtil.map(list, new Function<TreeItem<Pair<AbstractUrl, String>>, AbstractUrl>() { @Override public AbstractUrl fun(TreeItem<Pair<AbstractUrl, String>> item) { return item.getData().getFirst(); } })); for (AbstractTreeNode node : nodes) { final Pair<AbstractUrl, String> pair = createPairForNode(node); if (pair != null) { if (set.contains(pair.getFirst())) continue; final TreeItem<Pair<AbstractUrl, String>> treeItem = new TreeItem<Pair<AbstractUrl, String>>(pair); list.add(treeItem); set.add(pair.getFirst()); appendChildNodes(node, treeItem); } } rootsChanged(); return true; }
private boolean renameFavoritesList(@Nonnull String oldName, @Nonnull String newName) { List<TreeItem<Pair<AbstractUrl, String>>> list = myName2FavoritesRoots.remove(oldName); if (list != null && newName.length() > 0) { int index = myListOrder.indexOf(oldName); if (index == -1) { index = myListOrder.size(); } myListOrder.set(index, newName); myName2FavoritesRoots.put(newName, list); String description = myDescriptions.remove(oldName); if (description != null) { myDescriptions.put(newName, description); } rootsChanged(); return true; } return false; }
private static void showTreePopup(final List<TreeItem<VcsFileRevision>> roots, final VirtualFile file, final Project project, final DiffProvider diffProvider) { final TreeTableView treeTable = new TreeTableView(new ListTreeTableModelOnColumns(new TreeNodeAdapter(null, null, roots), new ColumnInfo[]{BRANCH_COLUMN, REVISION_COLUMN, DATE_COLUMN, AUTHOR_COLUMN})); Runnable runnable = new Runnable() { @Override public void run() { int index = treeTable.getSelectionModel().getMinSelectionIndex(); if (index == -1) { return; } VcsFileRevision revision = getRevisionAt(treeTable, index); if (revision != null) { DiffActionExecutor.showDiff(diffProvider, revision.getRevisionNumber(), file, project, VcsBackgroundableActions.COMPARE_WITH); } } }; treeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); new PopupChooserBuilder(treeTable). setTitle(VcsBundle.message("lookup.title.vcs.file.revisions")). setItemChoosenCallback(runnable). setSouthComponent(createCommentsPanel(treeTable)). setResizable(true). setDimensionServiceKey("Vcs.CompareWithSelectedRevision.Popup"). createPopup(). showCenteredInCurrentWindow(project); final int lastRow = treeTable.getRowCount() - 1; if (lastRow < 0) return; treeTable.getSelectionModel().addSelectionInterval(lastRow, lastRow); treeTable.scrollRectToVisible(treeTable.getCellRect(lastRow, 0, true)); }
public TreeNodeAdapter(TreeNodeAdapter parent, TreeItem<VcsFileRevision> revision, List<TreeItem<VcsFileRevision>> children) { if (parent != null) { parent.add(this); } myRevision = revision; for (TreeItem<VcsFileRevision> treeItem : children) { new TreeNodeAdapter(this, treeItem, treeItem.getChildren()); } }
private void appendChildNodes(AbstractTreeNode node, TreeItem<Pair<AbstractUrl, String>> treeItem) { final Collection<? extends AbstractTreeNode> children = node.getChildren(); for (AbstractTreeNode child : children) { final TreeItem<Pair<AbstractUrl, String>> childTreeItem = new TreeItem<Pair<AbstractUrl, String>>(createPairForNode(child)); treeItem.addChild(childTreeItem); appendChildNodes(child, childTreeItem); } }
private <T> boolean findListToRemoveFrom(@NotNull String name, @NotNull final List<T> elements, final Convertor<T, AbstractUrl> convertor) { Collection<TreeItem<Pair<AbstractUrl, String>>> list = getFavoritesListRootUrls(name); if (elements.size() > 1) { final List<T> sublist = elements.subList(0, elements.size() - 1); for (T obj : sublist) { AbstractUrl objUrl = convertor.convert(obj); final TreeItem<Pair<AbstractUrl, String>> item = findNextItem(objUrl, list); if (item == null || item.getChildren() == null) return false; list = item.getChildren(); } } TreeItem<Pair<AbstractUrl, String>> found = null; AbstractUrl url = convertor.convert(elements.get(elements.size() - 1)); if (url == null) return false; for (TreeItem<Pair<AbstractUrl, String>> pair : list) { if (url.equals(pair.getData().getFirst())) { found = pair; break; } } if (found != null) { list.remove(found); rootsChanged(); return true; } return false; }
private TreeItem<Pair<AbstractUrl, String>> findNextItem(AbstractUrl url, Collection<TreeItem<Pair<AbstractUrl, String>>> list) { for (TreeItem<Pair<AbstractUrl, String>> pair : list) { if (url.equals(pair.getData().getFirst())) { return pair; } } return null; }
private boolean renameFavoritesList(@NotNull String oldName, @NotNull String newName) { List<TreeItem<Pair<AbstractUrl, String>>> list = myName2FavoritesRoots.remove(oldName); if (list != null && newName.length() > 0) { myName2FavoritesRoots.put(newName, list); String description = myDescriptions.remove(oldName); if (description != null) { myDescriptions.put(newName, description); } rootsChanged(); return true; } return false; }
@Override public void readExternal(Element element) throws InvalidDataException { myName2FavoritesRoots.clear(); for (Object list : element.getChildren(ELEMENT_FAVORITES_LIST)) { final String name = ((Element)list).getAttributeValue(ATTRIBUTE_NAME); List<TreeItem<Pair<AbstractUrl, String>>> roots = readRoots((Element)list, myProject); myName2FavoritesRoots.put(name, roots); } DefaultJDOMExternalizer.readExternal(this, element); }
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<AbstractUrl, String>>(Pair.create(abstractUrl, className)); result.add(treeItem); readFavoritesOneLevel(favoriteElement, project, treeItem.getChildren()); } } }
private static void writeRoots(Element element, Collection<TreeItem<Pair<AbstractUrl, String>>> roots) { for (TreeItem<Pair<AbstractUrl, String>> root : roots) { final AbstractUrl url = root.getData().getFirst(); if (url == null) continue; final Element list = new Element(FAVORITES_ROOT); url.write(list); list.setAttribute(CLASS_NAME, root.getData().getSecond()); element.addContent(list); final List<TreeItem<Pair<AbstractUrl, String>>> children = root.getChildren(); if (children != null && !children.isEmpty()) { writeRoots(list, children); } } }
private static void iterateTreeItems(final Collection<TreeItem<Pair<AbstractUrl, String>>> coll, Consumer<TreeItem<Pair<AbstractUrl, String>>> consumer) { final ArrayDeque<TreeItem<Pair<AbstractUrl, String>>> queue = new ArrayDeque<TreeItem<Pair<AbstractUrl, String>>>(); queue.addAll(coll); while (!queue.isEmpty()) { final TreeItem<Pair<AbstractUrl, String>> item = queue.removeFirst(); consumer.consume(item); final List<TreeItem<Pair<AbstractUrl, String>>> children = item.getChildren(); if (children != null && !children.isEmpty()) { queue.addAll(children); } } }
@Override public void beforePropertyChange(@NotNull final PsiTreeChangeEvent event) { if (event.getPropertyName().equals(PsiTreeChangeEvent.PROP_FILE_NAME) || event.getPropertyName().equals(PsiTreeChangeEvent.PROP_DIRECTORY_NAME)) { final PsiElement psiElement = event.getChild(); if (psiElement instanceof PsiFile || psiElement instanceof PsiDirectory) { final Module module = ModuleUtil.findModuleForPsiElement(psiElement); if (module == null) return; final String url = ((PsiDirectory)psiElement.getParent()).getVirtualFile().getUrl() + "/" + event.getNewValue(); final AbstractUrl childUrl = psiElement instanceof PsiFile ? new PsiFileUrl(url) : new DirectoryUrl(url, module.getName()); for (String listName : myName2FavoritesRoots.keySet()) { final List<TreeItem<Pair<AbstractUrl, String>>> roots = myName2FavoritesRoots.get(listName); iterateTreeItems(roots, new Consumer<TreeItem<Pair<AbstractUrl, String>>>() { @Override public void consume(TreeItem<Pair<AbstractUrl, String>> item) { final Pair<AbstractUrl, String> root = item.getData(); final Object[] path = root.first.createPath(myProject); if (path == null || path.length < 1 || path[0] == null) { return; } final Object element = path[path.length - 1]; if (element == psiElement && psiElement instanceof PsiFile) { item.setData(Pair.create(childUrl, root.second)); } else { item.setData(root); } } }); } } } }
@NotNull private static Collection<AbstractTreeNode> createFavoriteRoots(Project project, @NotNull Collection<TreeItem<Pair<AbstractUrl, String>>> urls, final AbstractTreeNode me) { Collection<AbstractTreeNode> result = new ArrayList<AbstractTreeNode>(); processUrls(project, urls, result, me); return result; }
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<AbstractTreeNode>(); processUrls(project, children, childList, node); for (AbstractTreeNode treeNode : childList) { ((ProjectViewNodeWithChildrenList)node).addChild(treeNode); } } } } catch (Exception ignored) { } } }
@Nullable private static TreeItem<VcsFileRevision> getCommonParent(TreeItem<VcsFileRevision> prevRevision, TreeItem<VcsFileRevision> cvsFileRevision) { if (prevRevision == null) return null; while (!isParent(prevRevision, cvsFileRevision)) { prevRevision = prevRevision.getParent(); } return prevRevision; }
private static void showTreePopup(final List<TreeItem<VcsFileRevision>> roots, final VirtualFile file, final Project project, final DiffProvider diffProvider) { final TreeTableView treeTable = new TreeTableView(new ListTreeTableModelOnColumns(new TreeNodeAdapter(null, null, roots), new ColumnInfo[]{BRANCH_COLUMN, REVISION_COLUMN, DATE_COLUMN, AUTHOR_COLUMN})); Runnable runnable = new Runnable() { public void run() { int index = treeTable.getSelectionModel().getMinSelectionIndex(); if (index == -1) { return; } VcsFileRevision revision = getRevisionAt(treeTable, index); if (revision != null) { DiffActionExecutor.showDiff(diffProvider, revision.getRevisionNumber(), file, project, VcsBackgroundableActions.COMPARE_WITH); } } }; treeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); new PopupChooserBuilder(treeTable). setTitle(VcsBundle.message("lookup.title.vcs.file.revisions")). setItemChoosenCallback(runnable). setSouthComponent(createCommentsPanel(treeTable)). setResizable(true). setDimensionServiceKey("Vcs.CompareWithSelectedRevision.Popup"). createPopup(). showCenteredInCurrentWindow(project); }
@Override public void readExternal(Element element) throws InvalidDataException { myName2FavoritesRoots.clear(); for (Object list : element.getChildren(ELEMENT_FAVORITES_LIST)) { final String name = ((Element)list).getAttributeValue(ATTRIBUTE_NAME); List<TreeItem<Pair<AbstractUrl, String>>> roots = readRoots((Element)list, myProject); myListOrder.add(name); myName2FavoritesRoots.put(name, roots); } DefaultJDOMExternalizer.readExternal(this, element); }
private <T> boolean findListToRemoveFrom(@Nonnull String name, @Nonnull final List<T> elements, final Convertor<T, AbstractUrl> convertor) { Collection<TreeItem<Pair<AbstractUrl, String>>> list = getFavoritesListRootUrls(name); if (elements.size() > 1) { final List<T> sublist = elements.subList(0, elements.size() - 1); for (T obj : sublist) { AbstractUrl objUrl = convertor.convert(obj); final TreeItem<Pair<AbstractUrl, String>> item = findNextItem(objUrl, list); if (item == null || item.getChildren() == null) return false; list = item.getChildren(); } } TreeItem<Pair<AbstractUrl, String>> found = null; AbstractUrl url = convertor.convert(elements.get(elements.size() - 1)); if (url == null) return false; for (TreeItem<Pair<AbstractUrl, String>> pair : list) { if (url.equals(pair.getData().getFirst())) { found = pair; break; } } if (found != null) { list.remove(found); rootsChanged(); return true; } return false; }