Java Code Examples for com.intellij.ui.content.ContentManager#getContents()

The following examples show how to use com.intellij.ui.content.ContentManager#getContents() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnalyzeSizeToolWindowFactory.java    From size-analyzer with Apache License 2.0 6 votes vote down vote up
@Override
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
  ContentManager contentManager = toolWindow.getContentManager();
  AnalyzeSizeToolWindow analyzeSizeToolWindow =
      new AnalyzeSizeToolWindow(
          toolWindow,
          project,
          categorizedSuggestions,
          categoryDisplayOrder
          );
  Content content =
      ContentFactory.SERVICE
          .getInstance()
          .createContent(analyzeSizeToolWindow.getContent(), CONTENT_TITLE, false);
  // We want only one content tab to exist at a time, but clearing all of them will cause the
  // tool window to disappear, so we add the new tab and remove the others after.
  contentManager.addContent(content);
  contentManager.setSelectedContent(content);
  for (Content otherContent : contentManager.getContents()) {
    if (otherContent != content) {
      contentManager.removeContent(otherContent, true);
    }
  }
  toolWindow.show(null);
}
 
Example 2
Source File: ContentsUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void addOrReplaceContent(ContentManager manager, Content content, boolean select) {
  final String contentName = content.getDisplayName();

  Content[] contents = manager.getContents();
  Content oldContentFound = null;
  for(Content oldContent: contents) {
    if (!oldContent.isPinned() && oldContent.getDisplayName().equals(contentName)) {
      oldContentFound = oldContent;
      break;
    }
  }

  manager.addContent(content);
  if (oldContentFound != null) {
    manager.removeContent(oldContentFound, true);
  }
  if (select) {
    manager.setSelectedContent(content);
  }
}
 
Example 3
Source File: ContentUtilEx.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Searches through all {@link Content simple} and {@link TabbedContent tabbed} contents of the given ContentManager,
 * and selects the one which holds the specified {@code contentComponent}.
 *
 * @return true if the necessary content was found (and thus selected) among content components of the given ContentManager.
 */
public static boolean selectContent(@Nonnull ContentManager manager, @Nonnull final JComponent contentComponent, boolean requestFocus) {
  for (Content content : manager.getContents()) {
    if (content instanceof TabbedContentImpl) {
      boolean found = ((TabbedContentImpl)content).findAndSelectContent(contentComponent);
      if (found) {
        manager.setSelectedContent(content, requestFocus);
        return true;
      }
    }
    else if (Comparing.equal(content.getComponent(), contentComponent)) {
      manager.setSelectedContent(content, requestFocus);
      return true;
    }
  }
  return false;
}
 
Example 4
Source File: ContentUtilEx.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Searches through all {@link Content simple} and {@link TabbedContent tabbed} contents of the given ContentManager,
 * trying to find the first one which matches the given condition.
 */
@Nullable
public static JComponent findContentComponent(@Nonnull ContentManager manager, @Nonnull Condition<JComponent> condition) {
  for (Content content : manager.getContents()) {
    if (content instanceof TabbedContentImpl) {
      List<Pair<String, JComponent>> tabs = ((TabbedContentImpl)content).getTabs();
      for (Pair<String, JComponent> tab : tabs) {
        if (condition.value(tab.second)) {
          return tab.second;
        }
      }
    }
    else if (condition.value(content.getComponent())) {
      return content.getComponent();
    }
  }
  return null;
}
 
Example 5
Source File: ExternalSystemUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
public static <T> T getToolWindowElement(@Nonnull Class<T> clazz,
                                         @Nonnull Project project,
                                         @Nonnull Key<T> key,
                                         @Nonnull ProjectSystemId externalSystemId) {
  if (project.isDisposed() || !project.isOpen()) {
    return null;
  }
  final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
  if (toolWindowManager == null) {
    return null;
  }
  final ToolWindow toolWindow = ensureToolWindowContentInitialized(project, externalSystemId);
  if (toolWindow == null) {
    return null;
  }

  final ContentManager contentManager = toolWindow.getContentManager();
  if (contentManager == null) {
    return null;
  }

  for (Content content : contentManager.getContents()) {
    final JComponent component = content.getComponent();
    if (component instanceof DataProvider) {
      final Object data = ((DataProvider)component).getData(key);
      if (data != null && clazz.isInstance(data)) {
        return (T)data;
      }
    }
  }
  return null;
}
 
Example 6
Source File: ContentUtilEx.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static TabbedContent findTabbedContent(@Nonnull ContentManager manager, @Nonnull String groupPrefix) {
  TabbedContent tabbedContent = null;
  for (Content content : manager.getContents()) {
    if (content instanceof TabbedContent && content.getTabName().startsWith(getFullPrefix(groupPrefix))) {
      tabbedContent = (TabbedContent)content;
      break;
    }
  }
  return tabbedContent;
}
 
Example 7
Source File: CloseOtherViewsAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void actionPerformed(final AnActionEvent e, final ViewContext context, final Content[] content) {
  final ContentManager manager = context.getContentManager();
  for (Content c : manager.getContents()) {
    if (c != content[0] && c.isCloseable()) {
      manager.removeContent(c, context.isToDisposeRemovedContent());
    }
  }
}
 
Example 8
Source File: CloseAllViewsAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void actionPerformed(final AnActionEvent e, final ViewContext context, final Content[] content) {
  final ContentManager manager = context.getContentManager();
  for (Content c : manager.getContents()) {
    if (c.isCloseable()) {
      manager.removeContent(c, context.isToDisposeRemovedContent());
    }
  }
}
 
Example 9
Source File: ContentUtilEx.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static void addTabbedContent(@Nonnull ContentManager manager,
                                    @Nonnull JComponent contentComponent,
                                    @Nonnull String groupPrefix,
                                    @Nonnull String tabName,
                                    boolean select,
                                    @Nullable Disposable childDisposable) {
  if (PropertiesComponent.getInstance().getBoolean(TabbedContent.SPLIT_PROPERTY_PREFIX + groupPrefix)) {
    final Content content = ContentFactory.getInstance().createContent(contentComponent, getFullName(groupPrefix, tabName), true);
    content.putUserData(Content.TABBED_CONTENT_KEY, Boolean.TRUE);
    content.putUserData(Content.TAB_GROUP_NAME_KEY, groupPrefix);

    for (Content c : manager.getContents()) {
      if (c.getComponent() == contentComponent) {
        if (select) {
          manager.setSelectedContent(c);
        }
        return;
      }
    }
    addContent(manager, content, select);

    registerDisposable(content, childDisposable, contentComponent);

    return;
  }

  TabbedContent tabbedContent = findTabbedContent(manager, groupPrefix);

  if (tabbedContent == null) {
    final Disposable disposable = Disposable.newDisposable();
    tabbedContent = new TabbedContentImpl(contentComponent, tabName, true, groupPrefix);
    ContentsUtil.addOrReplaceContent(manager, tabbedContent, select);
    Disposer.register(tabbedContent, disposable);
  }
  else {
    for (Pair<String, JComponent> tab : new ArrayList<>(tabbedContent.getTabs())) {
      if (Comparing.equal(tab.second, contentComponent)) {
        tabbedContent.removeContent(tab.second);
      }
    }
    if (select) {
      manager.setSelectedContent(tabbedContent, true, true);
    }
    tabbedContent.addContent(contentComponent, tabName, true);
  }

  registerDisposable(tabbedContent, childDisposable, contentComponent);
}