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 Project project,
     @NotNull final PsiModifierListOwner listOwner,
     @NotNull final String annotationFQName,
     @NotNull final PsiFile fromFile,
     @NotNull final String packageName,
     @NotNull final VirtualFile virtualFile,
     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 file = FileChooser.chooseFile(descriptor, project, null);
   if (file == null) {
     return;
   }
   new WriteCommandAction(project) {
     @Override
     protected void run(final Result result) throws Throwable {
       appendChosenAnnotationsRoot(entry, file);
       final List<XmlFile> xmlFiles = findExternalAnnotationsXmlFiles(listOwner);
       if (xmlFiles != null) { // file already exists under appeared content root
         if (!CodeInsightUtilBase.preparePsiElementForWrite(xmlFiles.get(0))) return;
         annotateExternally(listOwner, annotationFQName, xmlFiles.get(0), fromFile, value);
       } else {
         final XmlFile annotationsXml = createAnnotationsXml(file, packageName);
         if (annotationsXml != null) {
           final List<PsiFile> createdFiles = new ArrayList<PsiFile>();
           createdFiles.add(annotationsXml);
           String fqn = getFQN(packageName, virtualFile);
           if (fqn != null) {
             myExternalAnnotations.put(fqn, createdFiles);
           }
         }
         annotateExternally(listOwner, annotationFQName, annotationsXml, fromFile, value);
       }
     }
   }.execute();
 }