コード例 #1
0
    public void beforeChildMovement(@NotNull final PsiTreeChangeEvent event) {
      final PsiElement oldParent = event.getOldParent();
      final PsiElement newParent = event.getNewParent();
      final PsiElement child = event.getChild();
      if (newParent instanceof PsiDirectory) {
        final Module module = ModuleUtil.findModuleForPsiElement(newParent);
        if (module == null) return;
        AbstractUrl childUrl = null;
        if (child instanceof PsiFile) {
          childUrl =
              new PsiFileUrl(
                  ((PsiDirectory) newParent).getVirtualFile().getUrl()
                      + "/"
                      + ((PsiFile) child).getName());
        } else if (child instanceof PsiDirectory) {
          childUrl =
              new DirectoryUrl(
                  ((PsiDirectory) newParent).getVirtualFile().getUrl()
                      + "/"
                      + ((PsiDirectory) child).getName(),
                  module.getName());
        }

        for (String listName : myName2FavoritesRoots.keySet()) {
          final List<TreeItem<Pair<AbstractUrl, String>>> roots =
              myName2FavoritesRoots.get(listName);
          final AbstractUrl finalChildUrl = childUrl;
          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 == child && finalChildUrl != null) {
                    item.setData(Pair.create(finalChildUrl, root.second));
                  } else {
                    if (element == oldParent) {
                      item.setData(
                          Pair.create(root.first.createUrlByElement(newParent), root.second));
                    }
                  }
                }
              });
        }
      }
    }
コード例 #2
0
    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);
                    }
                  }
                });
          }
        }
      }
    }