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
.openapi
.vcs
.impl
;
18 import com
.intellij
.notification
.Notification
;
19 import com
.intellij
.notification
.NotificationListener
;
20 import com
.intellij
.notification
.NotificationType
;
21 import com
.intellij
.notification
.Notifications
;
22 import com
.intellij
.openapi
.diagnostic
.Logger
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import org
.jetbrains
.annotations
.NotNull
;
25 import org
.jetbrains
.annotations
.Nullable
;
27 import javax
.swing
.event
.HyperlinkEvent
;
30 public abstract class GenericNotifierImpl
<T
, Key
> {
31 private final static Logger LOG
= Logger
.getInstance("#com.intellij.openapi.vcs.impl.GenericNotifier");
32 private final Project myProject
;
34 private final String myGroupId
; //+- here
36 private final String myTitle
;
38 private final NotificationType myType
;
40 private final Map
<Key
, MyNotification
<T
>> myState
;
41 private final MyListener myListener
;
42 private final Object myLock
;
44 protected GenericNotifierImpl(final Project project
, @NotNull String groupId
, @NotNull String title
, final @NotNull NotificationType type
) {
49 myState
= new HashMap
<Key
, MyNotification
<T
>>();
50 myListener
= new MyListener();
51 myLock
= new Object();
54 protected abstract boolean ask(final T obj
);
56 protected abstract Key
getKey(final T obj
);
58 protected abstract String
getNotificationContent(final T obj
);
60 protected Collection
<Key
> getAllCurrentKeys() {
61 synchronized (myLock
) {
62 return new ArrayList
<Key
>(myState
.keySet());
67 final List
<MyNotification
<T
>> notifications
;
68 synchronized (myLock
) {
69 notifications
= new ArrayList
<MyNotification
<T
>>(myState
.values());
72 for (MyNotification
<T
> notification
: notifications
) {
73 notification
.expire();
77 public void ensureNotify(final T obj
) {
78 final MyNotification
<T
> notification
;
79 synchronized (myLock
) {
80 final Key key
= getKey(obj
);
81 if (myState
.containsKey(key
)) {
84 notification
= new MyNotification
<T
>(myGroupId
, myTitle
, getNotificationContent(obj
), myType
, myListener
, obj
);
85 myState
.put(key
, notification
);
87 Notifications
.Bus
.notify(notification
, myProject
);
90 public void removeLazyNotificationByKey(final Key key
) {
91 final MyNotification
<T
> notification
;
92 synchronized (myLock
) {
93 notification
= myState
.get(key
);
94 if (notification
!= null) {
98 if (notification
!= null) {
99 notification
.expire();
103 public void removeLazyNotification(final T obj
) {
104 final MyNotification
<T
> notification
;
105 synchronized (myLock
) {
106 final Key key
= getKey(obj
);
107 notification
= myState
.get(key
);
108 if (notification
!= null) {
112 if (notification
!= null) {
113 notification
.expire();
117 private class MyListener
implements NotificationListener
{
118 public void hyperlinkUpdate(@NotNull Notification notification
, @NotNull HyperlinkEvent event
) {
119 if (notification
instanceof MyNotification
) {
120 final MyNotification
<T
> concreteNotification
= (MyNotification
<T
>) notification
;
121 final T obj
= concreteNotification
.getObj();
122 final boolean state
= ask(obj
);
124 synchronized (myLock
) {
125 final Key key
= getKey(obj
);
128 notification
.expire();
135 protected T
getObj(final Key key
) {
136 synchronized (myLock
) {
137 final MyNotification
<T
> notification
= myState
.get(key
);
138 return notification
== null ?
null : notification
.getObj();
142 protected static class MyNotification
<T
> extends Notification
{
145 protected MyNotification(@NotNull String groupId
,
146 @NotNull String title
,
147 @NotNull String content
,
148 @NotNull NotificationType type
,
149 @Nullable NotificationListener listener
,
150 @NotNull final T obj
) {
151 super(groupId
, title
, content
, type
, listener
);
160 private static void log(final String s
) {