private void appendChosenAnnotationsRoot(
     @NotNull final OrderEntry entry, @NotNull final VirtualFile vFile) {
   if (entry instanceof LibraryOrderEntry) {
     Library library = ((LibraryOrderEntry) entry).getLibrary();
     LOG.assertTrue(library != null);
     final ModifiableRootModel rootModel =
         ModuleRootManager.getInstance(entry.getOwnerModule()).getModifiableModel();
     final Library.ModifiableModel model = library.getModifiableModel();
     model.addRoot(vFile, AnnotationOrderRootType.getInstance());
     model.commit();
     rootModel.commit();
   } else if (entry instanceof ModuleSourceOrderEntry) {
     final ModifiableRootModel model =
         ModuleRootManager.getInstance(entry.getOwnerModule()).getModifiableModel();
     final JavaModuleExternalPaths extension =
         model.getModuleExtension(JavaModuleExternalPaths.class);
     extension.setExternalAnnotationUrls(
         ArrayUtil.mergeArrays(extension.getExternalAnnotationsUrls(), vFile.getUrl()));
     model.commit();
   } else if (entry instanceof JdkOrderEntry) {
     final SdkModificator sdkModificator = ((JdkOrderEntry) entry).getJdk().getSdkModificator();
     sdkModificator.addRoot(vFile, AnnotationOrderRootType.getInstance());
     sdkModificator.commitChanges();
   }
   myExternalAnnotations.clear();
 }
  private static void attachJdkAnnotations(Sdk jdk) {
    LocalFileSystem lfs = LocalFileSystem.getInstance();
    VirtualFile root = null;
    if (root == null) { // community idea under idea
      root =
          lfs.findFileByPath(
              FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/java/jdkAnnotations");
    }
    if (root == null) { // idea under idea
      root =
          lfs.findFileByPath(
              FileUtil.toSystemIndependentName(PathManager.getHomePath())
                  + "/community/java/jdkAnnotations");
    }
    if (root == null) { // build
      root =
          VirtualFileManager.getInstance()
              .findFileByUrl(
                  "jar://"
                      + FileUtil.toSystemIndependentName(PathManager.getHomePath())
                      + "/lib/jdkAnnotations.jar!/");
    }
    if (root == null) {
      LOG.error(
          "jdk annotations not found in: "
              + FileUtil.toSystemIndependentName(PathManager.getHomePath())
              + "/lib/jdkAnnotations.jar!/");
      return;
    }

    SdkModificator modificator = jdk.getSdkModificator();
    modificator.removeRoot(root, AnnotationOrderRootType.getInstance());
    modificator.addRoot(root, AnnotationOrderRootType.getInstance());
    modificator.commitChanges();
  }
 @Override
 public boolean isRootTypeApplicable(@NotNull OrderRootType type) {
   return type == OrderRootType.CLASSES
       || type == OrderRootType.SOURCES
       || type == JavadocOrderRootType.getInstance()
       || type == AnnotationOrderRootType.getInstance();
 }
 protected DefaultTableModel createModel() {
   final MyTableModel tableModel = new MyTableModel();
   final String[] urls = getModel().getRootUrls(AnnotationOrderRootType.getInstance());
   for (String javadocUrl : urls) {
     tableModel.addTableItem(new TableItem(javadocUrl));
   }
   return tableModel;
 }
  @Override
  public void annotateExternally(
      @NotNull final PsiModifierListOwner listOwner,
      @NotNull final String annotationFQName,
      @NotNull final PsiFile fromFile,
      final PsiNameValuePair[] value) {
    final Project project = myPsiManager.getProject();
    final PsiFile containingFile = listOwner.getContainingFile();
    if (!(containingFile instanceof PsiJavaFile)) {
      return;
    }
    final String packageName = ((PsiJavaFile) containingFile).getPackageName();
    final VirtualFile virtualFile = containingFile.getVirtualFile();
    LOG.assertTrue(virtualFile != null);
    final List<OrderEntry> entries =
        ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
    if (entries.isEmpty()) {
      return;
    }
    for (final OrderEntry entry : entries) {
      if (entry instanceof ModuleOrderEntry) continue;
      VirtualFile[] virtualFiles = AnnotationOrderRootType.getFiles(entry);
      virtualFiles = filterByReadOnliness(virtualFiles);

      if (virtualFiles.length > 0) {
        chooseRootAndAnnotateExternally(
            listOwner,
            annotationFQName,
            fromFile,
            project,
            packageName,
            virtualFile,
            virtualFiles,
            value);
      } else {
        if (ApplicationManager.getApplication().isUnitTestMode()
            || ApplicationManager.getApplication().isHeadlessEnvironment()) {
          return;
        }
        SwingUtilities.invokeLater(
            new Runnable() {
              @Override
              public void run() {
                setupRootAndAnnotateExternally(
                    entry,
                    project,
                    listOwner,
                    annotationFQName,
                    fromFile,
                    packageName,
                    virtualFile,
                    value);
              }
            });
      }
      break;
    }
  }
 public void saveData() {
   TableUtil.stopEditing(myTable);
   final int count = myTable.getRowCount();
   String[] urls = ArrayUtil.newStringArray(count);
   for (int row = 0; row < count; row++) {
     final TableItem item = ((MyTableModel) myTable.getModel()).getTableItemAt(row);
     urls[row] = item.getUrl();
   }
   getModel().setRootUrls(AnnotationOrderRootType.getInstance(), urls);
 }
 public static Sdk addJdkAnnotations(Sdk sdk) {
   String path =
       FileUtil.toSystemIndependentName(PlatformTestUtil.getCommunityPath())
           + "/java/jdkAnnotations";
   VirtualFile root = LocalFileSystem.getInstance().findFileByPath(path);
   if (root != null) {
     SdkModificator sdkModificator = sdk.getSdkModificator();
     sdkModificator.addRoot(root, AnnotationOrderRootType.getInstance());
     sdkModificator.commitChanges();
   }
   return sdk;
 }
  public static void attachJdkAnnotations(@NotNull SdkModificator modificator) {
    LocalFileSystem lfs = LocalFileSystem.getInstance();
    List<String> pathsChecked = new ArrayList<>();
    // community idea under idea
    String path =
        FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/java/jdkAnnotations";
    VirtualFile root = lfs.findFileByPath(path);
    pathsChecked.add(path);

    if (root == null) { // idea under idea
      path =
          FileUtil.toSystemIndependentName(PathManager.getHomePath())
              + "/community/java/jdkAnnotations";
      root = lfs.findFileByPath(path);
      pathsChecked.add(path);
    }
    if (root == null) { // build
      String url =
          "jar://"
              + FileUtil.toSystemIndependentName(PathManager.getHomePath())
              + "/lib/jdkAnnotations.jar!/";
      root = VirtualFileManager.getInstance().findFileByUrl(url);
      pathsChecked.add(
          FileUtil.toSystemIndependentName(PathManager.getHomePath()) + "/lib/jdkAnnotations.jar");
    }
    if (root == null) {
      String msg = "Paths checked:\n";
      for (String p : pathsChecked) {
        File file = new File(p);
        msg +=
            "Path: '"
                + p
                + "' "
                + (file.exists() ? "Found" : "Not found")
                + "; directory children: "
                + Arrays.toString(file.getParentFile().listFiles())
                + "\n";
      }
      LOG.error("JDK annotations not found", msg);
      return;
    }

    OrderRootType annoType = AnnotationOrderRootType.getInstance();
    modificator.removeRoot(root, annoType);
    modificator.addRoot(root, annoType);
  }
 @Override
 protected boolean hasAnyAnnotationsRoots() {
   if (myHasAnyAnnotationsRoots == ThreeState.UNSURE) {
     final Module[] modules = ModuleManager.getInstance(myPsiManager.getProject()).getModules();
     for (Module module : modules) {
       for (OrderEntry entry : ModuleRootManager.getInstance(module).getOrderEntries()) {
         final String[] urls = AnnotationOrderRootType.getUrls(entry);
         if (urls.length > 0) {
           myHasAnyAnnotationsRoots = ThreeState.YES;
           return true;
         }
       }
     }
     myHasAnyAnnotationsRoots = ThreeState.NO;
   }
   return myHasAnyAnnotationsRoots == ThreeState.YES;
 }
 @Override
 @NotNull
 protected List<VirtualFile> getExternalAnnotationsRoots(@NotNull VirtualFile libraryFile) {
   final List<OrderEntry> entries =
       ProjectRootManager.getInstance(myPsiManager.getProject())
           .getFileIndex()
           .getOrderEntriesForFile(libraryFile);
   List<VirtualFile> result = new ArrayList<VirtualFile>();
   for (OrderEntry entry : entries) {
     if (entry instanceof ModuleOrderEntry) {
       continue;
     }
     final String[] externalUrls = AnnotationOrderRootType.getUrls(entry);
     for (String url : externalUrls) {
       VirtualFile root = VirtualFileManager.getInstance().findFileByUrl(url);
       if (root != null) {
         result.add(root);
       }
     }
   }
   return result;
 }
  @Override
  @NotNull
  public AnnotationPlace chooseAnnotationsPlace(@NotNull final PsiElement element) {
    if (!element.isPhysical()) return AnnotationPlace.IN_CODE; // element just created
    if (!element.getManager().isInProject(element)) return AnnotationPlace.EXTERNAL;
    final Project project = myPsiManager.getProject();
    final PsiFile containingFile = element.getContainingFile();
    final VirtualFile virtualFile = containingFile.getVirtualFile();
    LOG.assertTrue(virtualFile != null);
    final List<OrderEntry> entries =
        ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
    if (!entries.isEmpty()) {
      for (OrderEntry entry : entries) {
        if (!(entry instanceof ModuleOrderEntry)) {
          if (AnnotationOrderRootType.getUrls(entry).length > 0) {
            return AnnotationPlace.EXTERNAL;
          }
          break;
        }
      }
    }
    final MyExternalPromptDialog dialog =
        ApplicationManager.getApplication().isUnitTestMode()
                || ApplicationManager.getApplication().isHeadlessEnvironment()
            ? null
            : new MyExternalPromptDialog(project);
    if (dialog != null && dialog.isToBeShown()) {
      final PsiElement highlightElement =
          element instanceof PsiNameIdentifierOwner
              ? ((PsiNameIdentifierOwner) element).getNameIdentifier()
              : element.getNavigationElement();
      LOG.assertTrue(highlightElement != null);
      final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
      final List<RangeHighlighter> highlighters = new ArrayList<RangeHighlighter>();
      final boolean highlight =
          editor != null
              && editor.getDocument()
                  == PsiDocumentManager.getInstance(project).getDocument(containingFile);
      try {
        if (highlight) { // do not highlight for batch inspections
          final EditorColorsManager colorsManager = EditorColorsManager.getInstance();
          final TextAttributes attributes =
              colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
          final TextRange textRange = highlightElement.getTextRange();
          HighlightManager.getInstance(project)
              .addRangeHighlight(
                  editor,
                  textRange.getStartOffset(),
                  textRange.getEndOffset(),
                  attributes,
                  true,
                  highlighters);
          final LogicalPosition logicalPosition =
              editor.offsetToLogicalPosition(textRange.getStartOffset());
          editor.getScrollingModel().scrollTo(logicalPosition, ScrollType.CENTER);
        }

        dialog.show();
        if (dialog.getExitCode() == 2) {
          return AnnotationPlace.EXTERNAL;
        } else if (dialog.getExitCode() == 1) {
          return AnnotationPlace.NOWHERE;
        }

      } finally {
        if (highlight) {
          HighlightManager.getInstance(project)
              .removeSegmentHighlighter(editor, highlighters.get(0));
        }
      }
    } else if (dialog != null) {
      dialog.close(DialogWrapper.OK_EXIT_CODE);
    }
    return AnnotationPlace.IN_CODE;
  }
 public SdkPathEditor createPathEditor(Sdk sdk) {
   return new SdkPathEditor(
       ProjectBundle.message("sdk.configure.annotations.tab"),
       AnnotationOrderRootType.getInstance(),
       new FileChooserDescriptor(false, true, true, false, true, false));
 }