IDEA-27223 (SVN lockout)
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / impl / GenericNotifierImpl.java
bloba66fa99b639628d9ee07ae657204d2743b854fa4
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 protected boolean getStateFor(final Key key) {
69 synchronized (myLock) {
70 return myState.containsKey(key);
74 public void clear() {
75 final List<MyNotification<T>> notifications;
76 synchronized (myLock) {
77 notifications = new ArrayList<MyNotification<T>>(myState.values());
78 myState.clear();
80 for (MyNotification<T> notification : notifications) {
81 notification.expire();
85 public void ensureNotify(final T obj) {
86 final MyNotification<T> notification;
87 synchronized (myLock) {
88 final Key key = getKey(obj);
89 if (myState.containsKey(key)) {
90 return;
92 notification = new MyNotification<T>(myGroupId, myTitle, getNotificationContent(obj), myType, myListener, obj);
93 myState.put(key, notification);
95 final Application application = ApplicationManager.getApplication();
96 if (application.isDispatchThread()) {
97 Notifications.Bus.notify(notification, myProject);
98 } else {
99 application.invokeLater(new Runnable() {
100 public void run() {
101 Notifications.Bus.notify(notification, myProject);
107 public void removeLazyNotificationByKey(final Key key) {
108 final MyNotification<T> notification;
109 synchronized (myLock) {
110 notification = myState.get(key);
111 if (notification != null) {
112 myState.remove(key);
115 if (notification != null) {
116 notification.expire();
120 public void removeLazyNotification(final T obj) {
121 final MyNotification<T> notification;
122 synchronized (myLock) {
123 final Key key = getKey(obj);
124 notification = myState.get(key);
125 if (notification != null) {
126 myState.remove(key);
129 if (notification != null) {
130 notification.expire();
134 private class MyListener implements NotificationListener {
135 public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) {
136 if (notification instanceof MyNotification) {
137 final MyNotification<T> concreteNotification = (MyNotification<T>) notification;
138 final T obj = concreteNotification.getObj();
139 final boolean state = ask(obj);
140 if (state) {
141 synchronized (myLock) {
142 final Key key = getKey(obj);
143 myState.remove(key);
145 notification.expire();
151 @Nullable
152 protected T getObj(final Key key) {
153 synchronized (myLock) {
154 final MyNotification<T> notification = myState.get(key);
155 return notification == null ? null : notification.getObj();
159 protected static class MyNotification<T> extends Notification {
160 private T myObj;
162 protected MyNotification(@NotNull String groupId,
163 @NotNull String title,
164 @NotNull String content,
165 @NotNull NotificationType type,
166 @Nullable NotificationListener listener,
167 @NotNull final T obj) {
168 super(groupId, title, content, type, listener);
169 myObj = obj;
172 public T getObj() {
173 return myObj;
177 private static void log(final String s) {
178 LOG.debug(s);