diff options
Diffstat (limited to 'xml/impl/src/org/jetbrains/notification/SingletonNotificationManager.java')
-rw-r--r-- | xml/impl/src/org/jetbrains/notification/SingletonNotificationManager.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/xml/impl/src/org/jetbrains/notification/SingletonNotificationManager.java b/xml/impl/src/org/jetbrains/notification/SingletonNotificationManager.java new file mode 100644 index 000000000000..4ea4a10dcf85 --- /dev/null +++ b/xml/impl/src/org/jetbrains/notification/SingletonNotificationManager.java @@ -0,0 +1,86 @@ +package org.jetbrains.notification; + +import com.intellij.notification.*; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.wm.ToolWindowManager; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.concurrent.atomic.AtomicReference; + +public final class SingletonNotificationManager { + private final AtomicReference<Notification> notification = new AtomicReference<Notification>(); + + private final NotificationGroup group; + private final NotificationType type; + @Nullable + private final NotificationListener listener; + + private Runnable expiredListener; + + public SingletonNotificationManager(@NotNull String groupId, @NotNull NotificationType type, @Nullable NotificationListener listener) { + this(new NotificationGroup(groupId, NotificationDisplayType.STICKY_BALLOON, true), type, listener); + } + + public SingletonNotificationManager(@NotNull NotificationGroup group, @NotNull NotificationType type, @Nullable NotificationListener listener) { + this.group = group; + this.type = type; + this.listener = listener; + } + + public boolean notify(@NotNull String title, @NotNull String content) { + return notify(title, content, null); + } + + public boolean notify(@NotNull String title, @NotNull String content, @Nullable Project project) { + return notify(title, content, listener, project); + } + + public boolean notify(@NotNull String content, @Nullable Project project) { + return notify("", content, listener, project); + } + + public boolean notify(@NotNull String title, + @NotNull String content, + @Nullable NotificationListener listener, + @Nullable Project project) { + Notification oldNotification = notification.get(); + // !oldNotification.isExpired() is not enough - notification could be closed, but not expired + if (oldNotification != null) { + if (!oldNotification.isExpired() && (oldNotification.getBalloon() != null || + (project != null && + group.getDisplayType() == NotificationDisplayType.TOOL_WINDOW && + ToolWindowManager.getInstance(project).getToolWindowBalloon(group.getToolWindowId()) != null))) { + return false; + } + oldNotification.whenExpired(null); + oldNotification.expire(); + } + + if (expiredListener == null) { + expiredListener = new Runnable() { + @Override + public void run() { + Notification currentNotification = notification.get(); + if (currentNotification != null && currentNotification.isExpired()) { + notification.compareAndSet(currentNotification, null); + } + } + }; + } + + Notification newNotification = group.createNotification(title, content, type, listener); + newNotification.whenExpired(expiredListener); + notification.set(newNotification); + newNotification.notify(project); + return true; + } + + public void clear() { + Notification oldNotification = notification.getAndSet(null); + if (oldNotification != null) { + oldNotification.whenExpired(null); + oldNotification.expire(); + } + } +}
\ No newline at end of file |