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 com.intellij.util.containers.ContainerUtil;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
31 import java.util.ArrayList;
32 import java.util.List;
35 * @author Konstantin Bulenkov
37 public class ContentUtilEx extends ContentsUtil {
38 public static final String DISPOSABLE_KEY = "TabContentDisposable";
40 public static void addTabbedContent(ContentManager manager, JComponent contentComponent, String groupPrefix, String tabName, boolean select) {
41 addTabbedContent(manager, contentComponent, groupPrefix, tabName, select, null);
44 public static void addTabbedContent(ContentManager manager, JComponent contentComponent, String groupPrefix, String tabName, boolean select, @Nullable Disposable childDisposable) {
45 if (PropertiesComponent.getInstance().getBoolean(TabbedContent.SPLIT_PROPERTY_PREFIX + groupPrefix)) {
46 final Content content = ContentFactory.SERVICE.getInstance().createContent(contentComponent, getFullName(groupPrefix, tabName), true);
47 content.putUserData(Content.TABBED_CONTENT_KEY, Boolean.TRUE);
48 content.putUserData(Content.TAB_GROUP_NAME_KEY, groupPrefix);
50 for (Content c : manager.getContents()) {
51 if (c.getComponent() == contentComponent) {
53 manager.setSelectedContent(c);
58 addContent(manager, content, select);
60 registerDisposable(content, childDisposable, contentComponent);
65 TabbedContent tabbedContent = findTabbedContent(manager, groupPrefix);
67 if (tabbedContent == null) {
68 final Disposable disposable = Disposer.newDisposable();
69 tabbedContent = new TabbedContentImpl(contentComponent, tabName, true, groupPrefix);
70 ContentsUtil.addOrReplaceContent(manager, tabbedContent, select);
71 Disposer.register(tabbedContent, disposable);
74 for (Pair<String, JComponent> tab : new ArrayList<Pair<String, JComponent>>(tabbedContent.getTabs())) {
75 if (Comparing.equal(tab.second, contentComponent)) {
76 tabbedContent.removeContent(tab.second);
80 manager.setSelectedContent(tabbedContent, true, true);
82 tabbedContent.addContent(contentComponent, tabName, true);
85 registerDisposable(tabbedContent, childDisposable, contentComponent);
88 private static void registerDisposable(@NotNull Content content,
89 @Nullable Disposable childDisposable,
90 @NotNull JComponent contentComponent) {
91 if (childDisposable != null) {
92 Disposer.register(content, childDisposable);
93 assert contentComponent.getClientProperty(DISPOSABLE_KEY) == null;
94 contentComponent.putClientProperty(DISPOSABLE_KEY, childDisposable);
95 Disposer.register(childDisposable, () -> contentComponent.putClientProperty(DISPOSABLE_KEY, null));
98 Object disposableByKey = contentComponent.getClientProperty(DISPOSABLE_KEY);
99 if (disposableByKey != null && disposableByKey instanceof Disposable) {
100 Disposer.register(content, (Disposable)disposableByKey);
106 public static TabbedContent findTabbedContent(@NotNull ContentManager manager, @NotNull String groupPrefix) {
107 TabbedContent tabbedContent = null;
108 for (Content content : manager.getContents()) {
109 if (content instanceof TabbedContent && content.getTabName().startsWith(getFullPrefix(groupPrefix))) {
110 tabbedContent = (TabbedContent)content;
114 return tabbedContent;
117 public static boolean isContentTab(@NotNull Content content, @NotNull String groupPrefix) {
118 return (content instanceof TabbedContent && content.getTabName().startsWith(getFullPrefix(groupPrefix))) ||
119 groupPrefix.equals(content.getUserData(Content.TAB_GROUP_NAME_KEY));
122 public static void closeContentTab(@NotNull ContentManager contentManager, @NotNull Content content) {
123 if (content instanceof TabbedContent) {
124 TabbedContent tabbedContent = (TabbedContent)content;
125 if (tabbedContent.getTabs().size() > 1) {
126 JComponent component = tabbedContent.getComponent();
127 tabbedContent.removeContent(component);
128 contentManager.setSelectedContent(tabbedContent, true, true);
132 contentManager.removeContent(content, true);
136 public static String getFullName(@NotNull String groupPrefix, @NotNull String tabName) {
137 return getFullPrefix(groupPrefix) + tabName;
141 private static String getFullPrefix(@NotNull String groupPrefix) {
142 return groupPrefix + ": ";
146 * Searches through all {@link Content simple} and {@link TabbedContent tabbed} contents of the given ContentManager,
147 * and selects the one which holds the specified {@code contentComponent}.
149 * @return true if the necessary content was found (and thus selected) among content components of the given ContentManager.
151 public static boolean selectContent(@NotNull ContentManager manager, @NotNull final JComponent contentComponent, boolean requestFocus) {
152 for (Content content : manager.getContents()) {
153 if (content instanceof TabbedContentImpl) {
154 boolean found = ((TabbedContentImpl)content).findAndSelectContent(contentComponent);
156 manager.setSelectedContent(content, requestFocus);
160 else if (Comparing.equal(content.getComponent(), contentComponent)) {
161 manager.setSelectedContent(content, requestFocus);
169 * Searches through all {@link Content simple} and {@link TabbedContent tabbed} contents of the given ContentManager,
170 * trying to find the first one which matches the given condition.
173 public static JComponent findContentComponent(@NotNull ContentManager manager, @NotNull Condition<JComponent> condition) {
174 for (Content content : manager.getContents()) {
175 if (content instanceof TabbedContentImpl) {
176 List<Pair<String, JComponent>> tabs = ((TabbedContentImpl)content).getTabs();
177 for (Pair<String, JComponent> tab : tabs) {
178 if (condition.value(tab.second)) {
183 else if (condition.value(content.getComponent())) {
184 return content.getComponent();
190 public static int getSelectedTab(@NotNull TabbedContent content) {
191 final JComponent current = content.getComponent();
193 for (Pair<String, JComponent> tab : content.getTabs()) {
194 if (tab.second == current) {