diff --git a/platform/platform-api/src/com/intellij/notification/Notifications.java b/platform/platform-api/src/com/intellij/notification/Notifications.java index 190de2a3e38f..c439bb198949 100644 --- a/platform/platform-api/src/com/intellij/notification/Notifications.java +++ b/platform/platform-api/src/com/intellij/notification/Notifications.java @@ -15,19 +15,14 @@ */ package com.intellij.notification; -import com.intellij.ide.FrameStateManager; import com.intellij.openapi.application.Application; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.project.Project; -import com.intellij.openapi.startup.StartupManager; -import com.intellij.util.Processor; -import com.intellij.util.messages.MessageBus; import com.intellij.util.messages.Topic; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; -import java.awt.*; /** * @author spleaner @@ -46,18 +41,22 @@ public interface Notifications { /** * Registration is OPTIONAL: STICKY_BALLOON display type will be used by default. */ + @SuppressWarnings("JavaDoc") public static void register(@NotNull final String group_id, @NotNull final NotificationDisplayType defaultDisplayType) { - invoke(null, new Processor() { + //noinspection SSBasedInspection + SwingUtilities.invokeLater(new Runnable() { @Override - public boolean process(final MessageBus bus) { - bus.syncPublisher(TOPIC).register(group_id, defaultDisplayType); - return false; + public void run() { + Application app = ApplicationManager.getApplication(); + if (!app.isDisposed()) { + app.getMessageBus().syncPublisher(TOPIC).register(group_id, defaultDisplayType); + } } }); } @Deprecated - public static void notify(@NotNull final Notification notification, final NotificationDisplayType displayType, @Nullable final Project project) { + public static void notify(@NotNull final Notification notification, @SuppressWarnings("UnusedParameters") final NotificationDisplayType displayType, @Nullable final Project project) { notify(notification, project); } @@ -66,61 +65,21 @@ public interface Notifications { } public static void notify(@NotNull final Notification notification, @Nullable final Project project) { - invoke(project, new Processor() { + //noinspection SSBasedInspection + SwingUtilities.invokeLater(new Runnable() { @Override - public boolean process(final MessageBus messageBus) { - messageBus.syncPublisher(TOPIC).notify(notification); - return false; + public void run() { + if (project != null && !project.isDisposed()) { + project.getMessageBus().syncPublisher(TOPIC).notify(notification); + } else { + Application app = ApplicationManager.getApplication(); + if (!app.isDisposed()) { + app.getMessageBus().syncPublisher(TOPIC).notify(notification); + } + } } }); } - private static void invoke(final Project project, final Processor fun) { - if (project != null && !project.isDisposed()) { - if (!project.isInitialized()) { - StartupManager.getInstance(project).runWhenProjectIsInitialized(new Runnable() { - public void run() { - fun.process(project.getMessageBus()); - } - }); - } - else { - final MessageBus bus = project.getMessageBus(); - notifyLaterIfNeeded(bus, fun); - } - - return; - } - - FrameStateManager frameStateManager = FrameStateManager.getInstance(); - if (frameStateManager != null) { - frameStateManager.getApplicationActive().doWhenDone(new Runnable() { - @Override - public void run() { - Application app = ApplicationManager.getApplication(); - final MessageBus bus = - project == null ? app.isDisposed() ? null : app.getMessageBus() : project.isDisposed() ? null : project.getMessageBus(); - if (bus != null) { - notifyLaterIfNeeded(bus, fun); - } - } - }); - } - } - - private static void notifyLaterIfNeeded(final MessageBus bus, - final Processor fun) { - if (EventQueue.isDispatchThread()) { - fun.process(bus); - } - else { - //noinspection SSBasedInspection - SwingUtilities.invokeLater(new Runnable() { - public void run() { - fun.process(bus); - } - }); - } - } } } diff --git a/platform/platform-impl/src/com/intellij/notification/impl/NotificationsManagerImpl.java b/platform/platform-impl/src/com/intellij/notification/impl/NotificationsManagerImpl.java index 2c2ea50e9880..c9e09f2a09e3 100644 --- a/platform/platform-impl/src/com/intellij/notification/impl/NotificationsManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/notification/impl/NotificationsManagerImpl.java @@ -15,12 +15,14 @@ */ package com.intellij.notification.impl; +import com.intellij.ide.FrameStateManager; import com.intellij.notification.*; import com.intellij.notification.impl.ui.NotificationsUtil; import com.intellij.openapi.Disposable; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.components.ApplicationComponent; import com.intellij.openapi.project.Project; +import com.intellij.openapi.startup.StartupManager; import com.intellij.openapi.ui.MessageType; import com.intellij.openapi.ui.popup.*; import com.intellij.openapi.util.text.StringUtil; @@ -84,6 +86,7 @@ public class NotificationsManagerImpl extends NotificationsManager implements No final List result = new ArrayList(); for (final Notification notification : notifications) { if (klass.isInstance(notification)) { + //noinspection unchecked result.add((T) notification); } } @@ -109,10 +112,6 @@ public class NotificationsManagerImpl extends NotificationsManager implements No myModel.clear(createFilter(project, true)); } - public void markRead() { - myModel.markRead(); - } - public NotificationModel getModel() { return myModel; } @@ -138,10 +137,34 @@ public class NotificationsManagerImpl extends NotificationsManager implements No myModel.add(notification, project); } - showNotification(notification, project); + showWhenVisible(notification, project); } - public static void showNotification(final Notification notification, @Nullable final Project project) { + private static void showWhenVisible(final Notification notification, final Project project) { + FrameStateManager.getInstance().getApplicationActive().doWhenDone(new Runnable() { + @Override + public void run() { + if (project != null) { + if (project.isDisposed()) { + return; + } + + if (!project.isInitialized()) { + StartupManager.getInstance(project).runWhenProjectIsInitialized(new Runnable() { + @Override + public void run() { + showWhenVisible(notification, project); + } + }); + return; + } + } + showNotification(notification, project); + } + }); + } + + private static void showNotification(final Notification notification, @Nullable final Project project) { String groupId = notification.getGroupId(); final NotificationSettings settings = NotificationsConfiguration.getSettings(groupId); @@ -180,6 +203,7 @@ public class NotificationsManagerImpl extends NotificationsManager implements No }; assert toolWindowId != null; String msg = StringUtil.isEmpty(notification.getTitle()) ? notification.getContent() : notification.getTitle(); + //noinspection SSBasedInspection ToolWindowManager.getInstance(project).notifyByBalloon(toolWindowId, messageType, msg, notification.getIcon(), listener); } } @@ -310,15 +334,6 @@ public class NotificationsManagerImpl extends NotificationsManager implements No return myModel.getByType(type, createFilter(project, false)); } - @Nullable - public NotificationType getMaximumType(@Nullable final Project project) { - return myModel.getMaximumType(createFilter(project, false)); - } - - public boolean wasRead(@NotNull final Notification notification) { - return myModel.wasRead(notification); - } - private static class ProjectFilter implements PairFunction { private final Project myProject; private final boolean myStrict;