SVN auth
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / impl / GenericNotifierImpl.java
blob241e57b12c75138198f4f312f251d96701aa4ca0
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.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.application.Application;
23 import com.intellij.openapi.application.ApplicationManager;
24 import com.intellij.openapi.diagnostic.Logger;
25 import com.intellij.openapi.project.Project;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
29 import javax.swing.event.HyperlinkEvent;
30 import java.util.*;
32 public abstract class GenericNotifierImpl<T, Key> {
33 private final static Logger LOG = Logger.getInstance("#com.intellij.openapi.vcs.impl.GenericNotifier");
34 private final Project myProject;
35 @NotNull
36 private final String myGroupId; //+- here
37 @NotNull
38 private final String myTitle;
39 @NotNull
40 private final NotificationType myType;
41 @NotNull
42 private final Map<Key, MyNotification<T>> myState;
43 private final MyListener myListener;
44 private final Object myLock;
46 protected GenericNotifierImpl(final Project project, @NotNull String groupId, @NotNull String title, final @NotNull NotificationType type) {
47 myGroupId = groupId;
48 myTitle = title;
49 myType = type;
50 myProject = project;
51 myState = new HashMap<Key, MyNotification<T>>();
52 myListener = new MyListener();
53 myLock = new Object();
56 protected abstract boolean ask(final T obj);
57 @NotNull
58 protected abstract Key getKey(final T obj);
59 @NotNull
60 protected abstract String getNotificationContent(final T obj);
62 protected Collection<Key> getAllCurrentKeys() {
63 synchronized (myLock) {
64 return new ArrayList<Key>(myState.keySet());
68 public void clear() {
69 final List<MyNotification<T>> notifications;
70 synchronized (myLock) {
71 notifications = new ArrayList<MyNotification<T>>(myState.values());
72 myState.clear();
74 for (MyNotification<T> notification : notifications) {
75 notification.expire();
79 public void ensureNotify(final T obj) {
80 final MyNotification<T> notification;
81 synchronized (myLock) {
82 final Key key = getKey(obj);
83 if (myState.containsKey(key)) {
84 return;
86 notification = new MyNotification<T>(myGroupId, myTitle, getNotificationContent(obj), myType, myListener, obj);
87 myState.put(key, notification);
89 final Application application = ApplicationManager.getApplication();
90 if (application.isDispatchThread()) {
91 Notifications.Bus.notify(notification, myProject);
92 } else {
93 application.invokeLater(new Runnable() {
94 public void run() {
95 Notifications.Bus.notify(notification, myProject);
97 });
101 public void removeLazyNotificationByKey(final Key key) {
102 final MyNotification<T> notification;
103 synchronized (myLock) {
104 notification = myState.get(key);
105 if (notification != null) {
106 myState.remove(key);
109 if (notification != null) {
110 notification.expire();
114 public void removeLazyNotification(final T obj) {
115 final MyNotification<T> notification;
116 synchronized (myLock) {
117 final Key key = getKey(obj);
118 notification = myState.get(key);
119 if (notification != null) {
120 myState.remove(key);
123 if (notification != null) {
124 notification.expire();
128 private class MyListener implements NotificationListener {
129 public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) {
130 if (notification instanceof MyNotification) {
131 final MyNotification<T> concreteNotification = (MyNotification<T>) notification;
132 final T obj = concreteNotification.getObj();
133 final boolean state = ask(obj);
134 if (state) {
135 synchronized (myLock) {
136 final Key key = getKey(obj);
137 myState.remove(key);
139 notification.expire();
145 @Nullable
146 protected T getObj(final Key key) {
147 synchronized (myLock) {
148 final MyNotification<T> notification = myState.get(key);
149 return notification == null ? null : notification.getObj();
153 protected static class MyNotification<T> extends Notification {
154 private T myObj;
156 protected MyNotification(@NotNull String groupId,
157 @NotNull String title,
158 @NotNull String content,
159 @NotNull NotificationType type,
160 @Nullable NotificationListener listener,
161 @NotNull final T obj) {
162 super(groupId, title, content, type, listener);
163 myObj = obj;
166 public T getObj() {
167 return myObj;
171 private static void log(final String s) {
172 LOG.debug(s);