SVN: authentication for free-read-access repositories to work also
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / impl / GenericNotifierImpl.java
blobe0d9a9e7498ef175eb89ef4c3ef3bdafb0dadd0d
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.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;
28 import java.util.*;
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;
33 @NotNull
34 private final String myGroupId; //+- here
35 @NotNull
36 private final String myTitle;
37 @NotNull
38 private final NotificationType myType;
39 @NotNull
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) {
45 myGroupId = groupId;
46 myTitle = title;
47 myType = type;
48 myProject = project;
49 myState = new HashMap<Key, MyNotification<T>>();
50 myListener = new MyListener();
51 myLock = new Object();
54 protected abstract boolean ask(final T obj);
55 @NotNull
56 protected abstract Key getKey(final T obj);
57 @NotNull
58 protected abstract String getNotificationContent(final T obj);
60 protected Collection<Key> getAllCurrentKeys() {
61 synchronized (myLock) {
62 return new ArrayList<Key>(myState.keySet());
66 public void clear() {
67 final List<MyNotification<T>> notifications;
68 synchronized (myLock) {
69 notifications = new ArrayList<MyNotification<T>>(myState.values());
70 myState.clear();
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)) {
82 return;
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) {
95 myState.remove(key);
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) {
109 myState.remove(key);
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);
123 if (state) {
124 synchronized (myLock) {
125 final Key key = getKey(obj);
126 myState.remove(key);
128 notification.expire();
134 @Nullable
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 {
143 private T myObj;
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);
152 myObj = obj;
155 public T getObj() {
156 return myObj;
160 private static void log(final String s) {
161 LOG.debug(s);