ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / actions / AnnotationFieldGutter.java
blob2ac37f45dd8cbf8a0d3b0671509ef1392250b063
1 /*
2 * Copyright 2000-2010 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.openapi.vcs.actions;
18 import com.intellij.openapi.actionSystem.AnAction;
19 import com.intellij.openapi.editor.Editor;
20 import com.intellij.openapi.editor.EditorGutterAction;
21 import com.intellij.openapi.editor.colors.ColorKey;
22 import com.intellij.openapi.editor.colors.EditorFontType;
23 import com.intellij.openapi.vcs.annotate.AnnotationListener;
24 import com.intellij.openapi.vcs.annotate.FileAnnotation;
25 import com.intellij.openapi.vcs.annotate.LineAnnotationAspect;
26 import com.intellij.openapi.vcs.annotate.TextAnnotationPresentation;
27 import com.intellij.xml.util.XmlStringUtil;
28 import org.jetbrains.annotations.Nullable;
30 import java.awt.*;
31 import java.util.List;
32 import java.util.Map;
34 /**
35 * @author Irina Chernushina
36 * @author Konstantin Bulenkov
38 class AnnotationFieldGutter implements ActiveAnnotationGutter {
39 protected final FileAnnotation myAnnotation;
40 private final Editor myEditor;
41 protected final LineAnnotationAspect myAspect;
42 private final TextAnnotationPresentation myPresentation;
43 private final AnnotationListener myListener;
44 private final boolean myIsGutterAction;
45 private Map<String, Color> myColorScheme;
46 private boolean myShowBg = true;
48 AnnotationFieldGutter(FileAnnotation annotation, Editor editor, LineAnnotationAspect aspect, final TextAnnotationPresentation presentation) {
49 myAnnotation = annotation;
50 myEditor = editor;
51 myAspect = aspect;
52 myPresentation = presentation;
53 myIsGutterAction = myAspect instanceof EditorGutterAction;
55 myListener = new AnnotationListener() {
56 public void onAnnotationChanged() {
57 myEditor.getGutter().closeAllAnnotations();
61 myAnnotation.addListener(myListener);
64 public boolean isGutterAction() {
65 return myIsGutterAction;
68 public String getLineText(int line, Editor editor) {
69 return myAspect.getValue(line);
72 @Nullable
73 public String getToolTip(final int line, final Editor editor) {
74 return XmlStringUtil.escapeString(myAnnotation.getToolTip(line));
77 public void doAction(int line) {
78 if (myIsGutterAction) {
79 ((EditorGutterAction)myAspect).doAction(line);
83 public Cursor getCursor(final int line) {
84 if (myIsGutterAction) {
85 return ((EditorGutterAction)myAspect).getCursor(line);
86 } else {
87 return Cursor.getDefaultCursor();
92 public EditorFontType getStyle(final int line, final Editor editor) {
93 return myPresentation.getFontType(line);
96 @Nullable
97 public ColorKey getColor(final int line, final Editor editor) {
98 return myPresentation.getColor(line);
101 public List<AnAction> getPopupActions(final Editor editor) {
102 return myPresentation.getActions();
105 public void gutterClosed() {
106 myAnnotation.removeListener(myListener);
107 myAnnotation.dispose();
108 myEditor.getUserData(AnnotateToggleAction.KEY_IN_EDITOR).remove(this);
111 @Nullable
112 public Color getBgColor(int line, Editor editor) {
113 if (myColorScheme == null || !myShowBg) return null;
114 final String s = getLineText(line, editor);
115 if (s == null) return null;
116 final Color bg = myColorScheme.get(s);
117 return bg == null ? findBgColor(s) : bg;
120 @Nullable
121 private Color findBgColor(String s) {
122 if (myColorScheme != null) {
123 for (String key : myColorScheme.keySet()) {
124 if (key.startsWith(s)) {
125 return myColorScheme.get(key);
129 return null;
132 public void setAspectValueToBgColorMap(Map<String, Color> colorScheme) {
133 myColorScheme = colorScheme;
136 public void setShowBg(boolean show) {
137 myShowBg = show;