/**
   * Searches the PSI for both custom components and support library classes. Call this method when
   * you want to refresh the components displayed on the preview.
   */
  public void reloadComponents() {
    myDumbService.runWhenSmart(
        new Runnable() {
          @Override
          public void run() {
            Project project = myContext.getProject();
            if (!project.isOpen()) {
              return;
            }
            PsiClass viewClass =
                JavaPsiFacade.getInstance(project)
                    .findClass("android.view.View", GlobalSearchScope.allScope(project));

            if (viewClass == null) {
              LOG.error("Unable to find 'android.view.View'");
              return;
            }

            Query<PsiClass> viewClasses =
                ClassInheritorsSearch.search(
                    viewClass, ProjectScope.getProjectScope(project), true);
            final ArrayList<ComponentDefinition> customComponents =
                new ArrayList<ComponentDefinition>();
            viewClasses.forEach(
                new Processor<PsiClass>() {
                  @Override
                  public boolean process(PsiClass psiClass) {
                    String description =
                        psiClass
                            .getName(); // We use the "simple" name as description on the preview.
                    String className = psiClass.getQualifiedName();

                    if (description == null || className == null) {
                      // Currently we ignore anonymous views
                      return false;
                    }

                    customComponents.add(
                        new ComponentDefinition(
                            description, ThemePreviewBuilder.ComponentGroup.CUSTOM, className));
                    return true;
                  }
                });

            // Now search for support library components. We use a HashSet to avoid adding duplicate
            // components from source and jar files.
            myDisabledComponents.clear();
            final HashSet<ComponentDefinition> supportLibraryComponents =
                new HashSet<ComponentDefinition>();
            viewClasses =
                ClassInheritorsSearch.search(
                    viewClass, ProjectScope.getLibrariesScope(project), true);
            viewClasses.forEach(
                new Processor<PsiClass>() {
                  @Override
                  public boolean process(PsiClass psiClass) {
                    String className = psiClass.getQualifiedName();

                    ComponentDefinition component = SUPPORT_LIBRARY_COMPONENTS.get(className);
                    if (component != null) {
                      supportLibraryComponents.add(component);

                      String replaces = SUPPORT_LIBRARY_REPLACEMENTS.get(className);
                      if (replaces != null) {
                        myDisabledComponents.add(replaces);
                      }
                    }

                    return true;
                  }
                });

            myCustomComponents = Collections.unmodifiableList(customComponents);
            mySupportLibraryComponents = ImmutableList.copyOf(supportLibraryComponents);
            if (!myCustomComponents.isEmpty() || mySupportLibraryComponents.isEmpty()) {
              rebuild();
            }
          }
        });

    rebuild(false);
  }
 private void openWhenSmart(DumbService dumbService) {
   if (!waitingForSmartMode) {
     waitingForSmartMode = true;
     dumbService.runWhenSmart(this);
   }
 }