-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
LogModel.java
152 lines (133 loc) · 5.22 KB
/
LogModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.notification;
import com.intellij.notification.impl.NotificationsConfigurationImpl;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.util.containers.CollectionFactory;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.messages.Topic;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public final class LogModel {
public static final Topic<EventLogListener> LOG_MODEL_CHANGED = Topic.create("LOG_MODEL_CHANGED", EventLogListener.class, Topic.BroadcastDirection.NONE);
public record StatusMessage(Notification notification, @NotNull @NlsContexts.StatusBarText String text, long stamp) {}
private final List<Notification> myNotifications = new ArrayList<>();
private final Map<Notification, @NlsContexts.StatusBarText String> myStatuses = CollectionFactory.createConcurrentWeakIdentityMap();
private StatusMessage myStatusMessage;
private final Project myProject;
final Map<Notification, Runnable> removeHandlers = new HashMap<>();
LogModel(@Nullable Project project) {
myProject = project;
}
void addNotification(Notification notification) {
long stamp = System.currentTimeMillis();
NotificationDisplayType type = NotificationsConfigurationImpl.getSettings(notification.getGroupId()).getDisplayType();
myStatuses.put(notification, EventLog.formatForLog(notification, "").status);
if (notification.isImportant() || (type != NotificationDisplayType.NONE && type != NotificationDisplayType.TOOL_WINDOW)) {
synchronized (myNotifications) {
myNotifications.add(notification);
}
}
setStatusMessage(notification, stamp);
fireModelChanged();
}
public static void fireModelChanged() {
ApplicationManager.getApplication().getMessageBus().syncPublisher(LOG_MODEL_CHANGED).modelChanged();
}
List<Notification> takeNotifications() {
final ArrayList<Notification> result;
synchronized (myNotifications) {
result = getNotifications();
myNotifications.clear();
}
if (!result.isEmpty()) {
fireModelChanged();
}
return result;
}
public void setStatusMessage(@Nullable Notification notification) {
if (notification == null) {
setStatusMessage(null, 0);
}
else {
myStatuses.put(notification, EventLog.formatForLog(notification, "").status);
setStatusMessage(notification, notification.getTimestamp());
}
}
void setStatusMessage(@Nullable Notification statusMessage, long stamp) {
synchronized (myNotifications) {
if (myStatusMessage != null && myStatusMessage.notification == statusMessage) return;
if (myStatusMessage == null && statusMessage == null) return;
myStatusMessage = statusMessage == null ? null :
new StatusMessage(statusMessage, Objects.requireNonNull(myStatuses.get(statusMessage)), stamp);
}
StatusBar.Info.set("", myProject, EventLog.LOG_REQUESTOR);
}
@Nullable StatusMessage getStatusMessage() {
synchronized (myNotifications) {
return myStatusMessage;
}
}
void logShown() {
for (Notification notification : getNotifications()) {
if (!notification.isImportant()) {
removeNotification(notification);
}
}
setStatusToImportant();
}
public void projectDispose(@Nullable LogModel applicationModel) {
if (applicationModel != null) {
synchronized (applicationModel.myNotifications) {
applicationModel.myNotifications.removeAll(myNotifications);
for (Notification notification : myNotifications) {
applicationModel.myStatuses.remove(notification);
applicationModel.removeHandlers.remove(notification);
}
}
}
synchronized (myNotifications) {
myNotifications.clear();
myStatuses.clear();
removeHandlers.clear();
myStatusMessage = null;
}
}
public @NotNull ArrayList<Notification> getNotifications() {
synchronized (myNotifications) {
return new ArrayList<>(myNotifications);
}
}
public void removeNotification(Notification notification) {
synchronized (myNotifications) {
myNotifications.remove(notification);
}
Runnable handler = removeHandlers.remove(notification);
if (handler != null) {
UIUtil.invokeLaterIfNeeded(handler);
}
@Nullable StatusMessage oldStatus = getStatusMessage();
if (oldStatus != null && notification == oldStatus.notification) {
setStatusToImportant();
}
fireModelChanged();
}
private void setStatusToImportant() {
ArrayList<Notification> notifications = getNotifications();
Collections.reverse(notifications);
Notification message = ContainerUtil.find(notifications, Notification::isImportant);
if (message == null) {
setStatusMessage(null, 0);
}
else {
setStatusMessage(message, message.getTimestamp());
}
}
public Project getProject() {
return myProject;
}
}