@Nullable
  private static RunContentDescriptor chooseReuseContentForDescriptor(
      final ContentManager contentManager, final RunContentDescriptor descriptor) {
    Content content = null;
    if (descriptor != null) {
      if (descriptor.isContentReuseProhibited()) {
        return null;
      }
      final Content attachedContent = descriptor.getAttachedContent();
      if (attachedContent != null && attachedContent.isValid()) content = attachedContent;
    }
    if (content == null) {
      content = contentManager.getSelectedContent();
      if (content != null && content.isPinned()) content = null;
    }
    if (content == null || !isTerminated(content)) {
      return null;
    }
    final RunContentDescriptor oldDescriptor = getRunContentDescriptorByContent(content);
    if (oldDescriptor != null && !oldDescriptor.isContentReuseProhibited()) {
      return oldDescriptor;
    }

    return null;
  }
 private void removeAllContents(Project project, Content notRemove) {
   if (project.isDisposed()) {
     return;
   }
   final MessageView messageView = MessageView.SERVICE.getInstance(project);
   Content[] contents = messageView.getContentManager().getContents();
   for (Content content : contents) {
     if (content.isPinned()) {
       continue;
     }
     if (content == notRemove) {
       continue;
     }
     boolean toRemove = CONTENT_ID_KEY.get(content) == myContentId;
     if (!toRemove) {
       final Object contentSessionId = SESSION_ID_KEY.get(content);
       toRemove =
           contentSessionId != null
               && contentSessionId != mySessionId; // the content was added by previous compilation
     }
     if (toRemove) {
       messageView.getContentManager().removeContent(content, true);
     }
   }
 }
 private static void removeContents(
     @Nullable final Content notToRemove,
     @NotNull final Project myProject,
     @NotNull final String tabDisplayName) {
   MessageView messageView = ServiceManager.getService(myProject, MessageView.class);
   Content[] contents = messageView.getContentManager().getContents();
   for (Content content : contents) {
     LOG.assertTrue(content != null);
     if (content.isPinned()) continue;
     if (tabDisplayName.equals(content.getDisplayName()) && content != notToRemove) {
       ErrorTreeView listErrorView = (ErrorTreeView) content.getComponent();
       if (listErrorView != null) {
         if (messageView.getContentManager().removeContent(content, true)) {
           content.release();
         }
       }
     }
   }
 }
  public int addTab(
      String s,
      JComponent component,
      boolean selectTab,
      boolean replaceContent,
      boolean lockable,
      boolean addDefaultToolbar,
      @Nullable final ActionGroup toolbarActions,
      @NonNls String helpId) {

    int existing = getComponentNumNamed(s);
    if (existing != -1) {
      Content existingContent = getContentManager().getContent(existing);
      final JComponent existingComponent = existingContent.getComponent();
      if (existingComponent instanceof DeactivateListener) {
        ((DeactivateListener) existingComponent).deactivated();
      }
      if (!replaceContent) {
        getContentManager().setSelectedContent(existingContent);
        return existing;
      } else if (!existingContent.isPinned()) {
        getContentManager().removeContent(existingContent, true);
        existingContent.release();
      }
    }

    CvsTabbedWindowComponent newComponent =
        new CvsTabbedWindowComponent(
            component, addDefaultToolbar, toolbarActions, getContentManager(), helpId);
    Content content =
        ContentFactory.SERVICE
            .getInstance()
            .createContent(newComponent.getShownComponent(), s, lockable);
    newComponent.setContent(content);
    getContentManager().addContent(content);

    return getComponentAt(getContentManager().getContentCount() - 1, selectTab);
  }
  @Override
  public boolean isSelected(AnActionEvent e) {
    DataContext context = e.getDataContext();
    VirtualFile file = getFile(context);
    if (file != null) {
      // 1. Check editor
      EditorWindow editorWindow = getEditorWindow(context);
      if (editorWindow != null) {
        if (!editorWindow.isFileOpen(file)) {
          file = editorWindow.getSelectedFile();
          if (file == null) return false;
        }

        return editorWindow.isFilePinned(file);
      }
    }
    // 2. Check content
    final Content content = getContent(context);
    if (content != null) {
      return content.isPinned();
    } else {
      return false;
    }
  }
  /** @return can be null if user cancelled operation */
  @Nullable
  public static AntBuildMessageView openBuildMessageView(
      Project project, AntBuildFileBase buildFile, String[] targets) {
    final VirtualFile antFile = buildFile.getVirtualFile();
    if (!LOG.assertTrue(antFile != null)) {
      return null;
    }

    // check if there are running instances of the same build file

    MessageView ijMessageView = MessageView.SERVICE.getInstance(project);
    Content[] contents = ijMessageView.getContentManager().getContents();
    for (Content content : contents) {
      if (content.isPinned()) {
        continue;
      }
      AntBuildMessageView buildMessageView = content.getUserData(KEY);
      if (buildMessageView == null) {
        continue;
      }

      if (!antFile.equals(buildMessageView.getBuildFile().getVirtualFile())) {
        continue;
      }

      if (buildMessageView.isStopped()) {
        ijMessageView.getContentManager().removeContent(content, true);
        continue;
      }

      int result =
          Messages.showYesNoCancelDialog(
              AntBundle.message("ant.is.active.terminate.confirmation.text"),
              AntBundle.message("starting.ant.build.dialog.title"),
              Messages.getQuestionIcon());

      switch (result) {
        case 0: // yes
          buildMessageView.stopProcess();
          ijMessageView.getContentManager().removeContent(content, true);
          continue;
        case 1: // no
          continue;
        default: // cancel
          return null;
      }
    }

    final AntBuildMessageView messageView = new AntBuildMessageView(project, buildFile, targets);
    String contentName = buildFile.getPresentableName();
    contentName = BUILD_CONTENT_NAME + " (" + contentName + ")";

    final Content content =
        ContentFactory.SERVICE
            .getInstance()
            .createContent(messageView.getComponent(), contentName, true);
    content.putUserData(KEY, messageView);
    ijMessageView.getContentManager().addContent(content);
    ijMessageView.getContentManager().setSelectedContent(content);
    content.setDisposer(
        new Disposable() {
          @Override
          public void dispose() {
            Disposer.dispose(messageView.myAlarm);
          }
        });
    new CloseListener(content, ijMessageView.getContentManager(), project);
    // Do not inline next two variabled. Seeking for NPE.
    ToolWindow messageToolWindow =
        ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.MESSAGES_WINDOW);
    messageToolWindow.activate(null, false);
    return messageView;
  }
 private boolean mustOpenInNewTab() {
   Content selectedContent = UsageViewManager.getInstance(myProject).getSelectedContent(true);
   return selectedContent != null && selectedContent.isPinned();
 }