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 void setupRootAndAnnotateExternally(
     @NotNull final OrderEntry entry,
     @NotNull final Project project,
     @NotNull final PsiModifierListOwner listOwner,
     @NotNull final String annotationFQName,
     @NotNull final PsiFile fromFile,
     @NotNull final String packageName,
     @Nullable final PsiNameValuePair[] value) {
   final FileChooserDescriptor descriptor =
       FileChooserDescriptorFactory.createSingleFolderDescriptor();
   descriptor.setTitle(
       ProjectBundle.message(
           "external.annotations.root.chooser.title", entry.getPresentableName()));
   descriptor.setDescription(
       ProjectBundle.message("external.annotations.root.chooser.description"));
   final VirtualFile newRoot = FileChooser.chooseFile(descriptor, project, null);
   if (newRoot == null) {
     notifyAfterAnnotationChanging(listOwner, annotationFQName, false);
     return;
   }
   new WriteCommandAction(project) {
     @Override
     protected void run(final Result result) throws Throwable {
       appendChosenAnnotationsRoot(entry, newRoot);
       XmlFile xmlFileInRoot =
           findXmlFileInRoot(findExternalAnnotationsXmlFiles(listOwner), newRoot);
       if (xmlFileInRoot != null) { // file already exists under appeared content root
         if (!CodeInsightUtilBase.preparePsiElementForWrite(xmlFileInRoot)) {
           notifyAfterAnnotationChanging(listOwner, annotationFQName, false);
           return;
         }
         annotateExternally(listOwner, annotationFQName, xmlFileInRoot, fromFile, value);
       } else {
         final XmlFile annotationsXml = createAnnotationsXml(newRoot, packageName);
         if (annotationsXml != null) {
           final List<PsiFile> createdFiles = new ArrayList<PsiFile>();
           createdFiles.add(annotationsXml);
           String fqn = getFQN(packageName, fromFile);
           if (fqn != null) {
             myExternalAnnotations.put(fqn, createdFiles);
           }
         }
         annotateExternally(listOwner, annotationFQName, annotationsXml, fromFile, value);
       }
     }
   }.execute();
 }