ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / plugins / cvs / cvs-plugin / src / com / intellij / cvsSupport2 / ui / CvsTabbedWindow.java
blobbc2384982b721453079ec5f1f0f5de9d0d524238
1 /*
2 * Copyright 2000-2009 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.cvsSupport2.ui;
18 import com.intellij.CvsBundle;
19 import com.intellij.openapi.Disposable;
20 import com.intellij.openapi.actionSystem.ActionGroup;
21 import com.intellij.openapi.components.ServiceManager;
22 import com.intellij.openapi.diagnostic.Logger;
23 import com.intellij.openapi.editor.Editor;
24 import com.intellij.openapi.editor.EditorFactory;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.util.Disposer;
27 import com.intellij.openapi.util.IconLoader;
28 import com.intellij.openapi.wm.ToolWindow;
29 import com.intellij.openapi.wm.ToolWindowAnchor;
30 import com.intellij.openapi.wm.ToolWindowId;
31 import com.intellij.openapi.wm.ToolWindowManager;
32 import com.intellij.ui.content.*;
33 import com.intellij.util.ui.ErrorTreeView;
34 import org.jetbrains.annotations.NonNls;
35 import org.jetbrains.annotations.Nullable;
37 import javax.swing.*;
39 /**
40 * author: lesya
42 public class CvsTabbedWindow {
43 private final Project myProject;
44 private Editor myOutput = null;
45 private ErrorTreeView myErrorsView;
46 private boolean myIsInitialized;
47 private boolean myIsDisposed;
49 private static final Logger LOG = Logger.getInstance("#com.intellij.cvsSupport2.ui.CvsTabbedWindow");
50 private ContentManager myContentManager;
52 public CvsTabbedWindow(Project project) {
53 myProject = project;
54 Disposer.register(project, new Disposable() {
55 public void dispose() {
56 if (myOutput != null) {
57 EditorFactory.getInstance().releaseEditor(myOutput);
58 myOutput = null;
60 if (myErrorsView != null) {
61 myErrorsView.dispose();
62 myErrorsView = null;
65 LOG.assertTrue(!myIsDisposed);
66 try {
67 if (!myIsInitialized) return;
68 ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(myProject);
69 toolWindowManager.unregisterToolWindow(ToolWindowId.CVS);
71 finally {
72 myIsDisposed = true;
75 });
78 private void initialize() {
79 if (myIsInitialized) {
80 return;
83 myIsInitialized = true;
84 myIsDisposed = false;
86 myContentManager = ContentFactory.SERVICE.getInstance().createContentManager(true, myProject);
87 myContentManager.addContentManagerListener(new ContentManagerAdapter() {
88 public void contentRemoved(ContentManagerEvent event) {
89 JComponent component = event.getContent().getComponent();
90 JComponent removedComponent = component instanceof CvsTabbedWindowComponent ?
91 ((CvsTabbedWindowComponent)component).getComponent() : component;
92 if (removedComponent == myErrorsView) {
93 myErrorsView.dispose();
94 myErrorsView = null;
96 else if (myOutput != null && removedComponent == myOutput.getComponent()) {
97 EditorFactory.getInstance().releaseEditor(myOutput);
98 myOutput = null;
103 ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(myProject);
104 ToolWindow toolWindow =
105 toolWindowManager.registerToolWindow(ToolWindowId.CVS, myContentManager.getComponent(), ToolWindowAnchor.BOTTOM);
106 toolWindow.setIcon(IconLoader.getIcon("/_cvs/cvs.png"));
107 toolWindow.installWatcher(myContentManager);
110 public static CvsTabbedWindow getInstance(Project project) {
111 return ServiceManager.getService(project, CvsTabbedWindow.class);
114 private int getComponentAt(int i, boolean select) {
115 if (select) getContentManager().setSelectedContent(getContentManager().getContent(i));
116 return i;
119 public interface DeactivateListener {
120 void deactivated();
123 public int addTab(String s,
124 JComponent component,
125 boolean selectTab,
126 boolean replaceContent,
127 boolean lockable,
128 boolean addDefaultToolbar,
129 @Nullable final ActionGroup toolbarActions,
130 @NonNls String helpId) {
132 int existing = getComponentNumNamed(s);
133 if (existing != -1) {
134 Content existingContent = getContentManager().getContent(existing);
135 final JComponent existingComponent = existingContent.getComponent();
136 if (existingComponent instanceof DeactivateListener) {
137 ((DeactivateListener) existingComponent).deactivated();
139 if (! replaceContent) {
140 getContentManager().setSelectedContent(existingContent);
141 return existing;
143 else if (!existingContent.isPinned()) {
144 getContentManager().removeContent(existingContent, true);
145 existingContent.release();
149 CvsTabbedWindowComponent newComponent = new CvsTabbedWindowComponent(component,
150 addDefaultToolbar, toolbarActions, getContentManager(), helpId);
151 Content content = ContentFactory.SERVICE.getInstance().createContent(newComponent.getShownComponent(), s, lockable);
152 newComponent.setContent(content);
153 getContentManager().addContent(content);
155 return getComponentAt(getContentManager().getContentCount() - 1, selectTab);
158 private int getComponentNumNamed(String s) {
159 for (int i = 0; i < getContentManager().getContentCount(); i++) {
160 if (s.equals(getContentManager().getContent(i).getDisplayName())) {
161 return i;
164 return -1;
167 public Editor addOutput(Editor output) {
168 LOG.assertTrue(myOutput == null);
169 if (myOutput == null) {
170 addTab(CvsBundle.message("tab.title.cvs.output"), output.getComponent(), false, false, false, true, null, "cvs.cvsOutput");
171 myOutput = output;
173 return myOutput;
176 public ErrorTreeView addErrorsTreeView(ErrorTreeView view) {
177 if (myErrorsView == null) {
178 addTab(CvsBundle.message("tab.title.errors"), view.getComponent(), true, false, true, false, null, "cvs.errors");
179 myErrorsView = view;
181 return myErrorsView;
184 public void hideErrors() {
185 //if (myErrorsView == null) return;
186 //Content content = getContent(myErrorsView);
187 //removeContent(content);
188 //myErrorsView = null;
191 public void ensureVisible(Project project) {
192 if (project == null) return;
193 ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
194 if (toolWindowManager != null) {
195 ToolWindow toolWindow = toolWindowManager.getToolWindow(ToolWindowId.CVS);
196 if (toolWindow != null) {
197 toolWindow.activate(null, false);
202 public ContentManager getContentManager() {
203 initialize();
204 return myContentManager;
207 public Editor getOutput() {
208 return myOutput;