2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.util;
18 import com.intellij.ide.util.PropertiesComponent;
19 import com.intellij.openapi.Disposable;
20 import com.intellij.openapi.util.*;
21 import com.intellij.ui.content.Content;
22 import com.intellij.ui.content.ContentFactory;
23 import com.intellij.ui.content.ContentManager;
24 import com.intellij.ui.content.TabbedContent;
25 import com.intellij.ui.content.impl.TabbedContentImpl;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
30 import java.util.ArrayList;
31 import java.util.List;
34 * @author Konstantin Bulenkov
36 public class ContentUtilEx extends ContentsUtil {
37 public static final String DISPOSABLE_KEY = "TabContentDisposable";
39 public static void addTabbedContent(ContentManager manager, JComponent contentComponent, String groupPrefix, String tabName, boolean select) {
40 addTabbedContent(manager, contentComponent, groupPrefix, tabName, select, null);
43 public static void addTabbedContent(ContentManager manager, JComponent contentComponent, String groupPrefix, String tabName, boolean select, @Nullable Disposable childDisposable) {
44 if (PropertiesComponent.getInstance().getBoolean(TabbedContent.SPLIT_PROPERTY_PREFIX + groupPrefix)) {
45 final Content content = ContentFactory.SERVICE.getInstance().createContent(contentComponent, getFullName(groupPrefix, tabName), true);
46 content.putUserData(Content.TABBED_CONTENT_KEY, Boolean.TRUE);
47 content.putUserData(Content.TAB_GROUP_NAME_KEY, groupPrefix);
49 for (Content c : manager.getContents()) {
50 if (c.getComponent() == contentComponent) {
52 manager.setSelectedContent(c);
57 addContent(manager, content, select);
59 registerDisposable(content, childDisposable, contentComponent);
64 TabbedContent tabbedContent = findTabbedContent(manager, groupPrefix);
66 if (tabbedContent == null) {
67 final Disposable disposable = Disposer.newDisposable();
68 tabbedContent = new TabbedContentImpl(contentComponent, tabName, true, groupPrefix);
69 ContentsUtil.addOrReplaceContent(manager, tabbedContent, select);
70 Disposer.register(tabbedContent, disposable);
73 for (Pair<String, JComponent> tab : new ArrayList<Pair<String, JComponent>>(tabbedContent.getTabs())) {
74 if (Comparing.equal(tab.second, contentComponent)) {
75 tabbedContent.removeContent(tab.second);
79 manager.setSelectedContent(tabbedContent, true, true);
81 tabbedContent.addContent(contentComponent, tabName, true);
84 registerDisposable(tabbedContent, childDisposable, contentComponent);
87 private static void registerDisposable(@NotNull Content content,
88 @Nullable Disposable childDisposable,
89 @NotNull JComponent contentComponent) {
90 if (childDisposable != null) {
91 Disposer.register(content, childDisposable);
92 assert contentComponent.getClientProperty(DISPOSABLE_KEY) == null;
93 contentComponent.putClientProperty(DISPOSABLE_KEY, childDisposable);
94 Disposer.register(childDisposable, () -> contentComponent.putClientProperty(DISPOSABLE_KEY, null));
97 Object disposableByKey = contentComponent.getClientProperty(DISPOSABLE_KEY);
98 if (disposableByKey != null && disposableByKey instanceof Disposable) {
99 Disposer.register(content, (Disposable)disposableByKey);
105 public static TabbedContent findTabbedContent(ContentManager manager, String groupPrefix) {
106 TabbedContent tabbedContent = null;
107 for (Content content : manager.getContents()) {
108 if (content instanceof TabbedContent && content.getTabName().startsWith(getFullPrefix(groupPrefix))) {
109 tabbedContent = (TabbedContent)content;
113 return tabbedContent;
117 public static String getFullName(@NotNull String groupPrefix, @NotNull String tabName) {
118 return getFullPrefix(groupPrefix) + tabName;
122 private static String getFullPrefix(@NotNull String groupPrefix) {
123 return groupPrefix + ": ";
127 * Searches through all {@link Content simple} and {@link TabbedContent tabbed} contents of the given ContentManager,
128 * and selects the one which holds the specified {@code contentComponent}.
130 * @return true if the necessary content was found (and thus selected) among content components of the given ContentManager.
132 public static boolean selectContent(@NotNull ContentManager manager, @NotNull final JComponent contentComponent, boolean requestFocus) {
133 for (Content content : manager.getContents()) {
134 if (content instanceof TabbedContentImpl) {
135 boolean found = ((TabbedContentImpl)content).findAndSelectContent(contentComponent);
137 manager.setSelectedContent(content, requestFocus);
141 else if (Comparing.equal(content.getComponent(), contentComponent)) {
142 manager.setSelectedContent(content, requestFocus);
150 * Searches through all {@link Content simple} and {@link TabbedContent tabbed} contents of the given ContentManager,
151 * trying to find the first one which matches the given condition.
154 public static JComponent findContentComponent(@NotNull ContentManager manager, @NotNull Condition<JComponent> condition) {
155 for (Content content : manager.getContents()) {
156 if (content instanceof TabbedContentImpl) {
157 List<Pair<String, JComponent>> tabs = ((TabbedContentImpl)content).getTabs();
158 for (Pair<String, JComponent> tab : tabs) {
159 if (condition.value(tab.second)) {
164 else if (condition.value(content.getComponent())) {
165 return content.getComponent();
171 public static int getSelectedTab(@NotNull TabbedContent content) {
172 final JComponent current = content.getComponent();
174 for (Pair<String, JComponent> tab : content.getTabs()) {
175 if (tab.second == current) {