/platform/platform-api/src/com/intellij/notification/Notifications.java

https://bitbucket.org/nbargnesi/idea · Java · 95 lines · 61 code · 11 blank · 23 comment · 9 complexity · 82968cc006b08b981d824d22a2a4b2cf MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. package com.intellij.notification;
  17. import com.intellij.openapi.application.Application;
  18. import com.intellij.openapi.application.ApplicationManager;
  19. import com.intellij.openapi.project.Project;
  20. import com.intellij.util.messages.Topic;
  21. import org.jetbrains.annotations.NotNull;
  22. import org.jetbrains.annotations.Nullable;
  23. import javax.swing.*;
  24. /**
  25. * @author spleaner
  26. */
  27. public interface Notifications {
  28. Topic<Notifications> TOPIC = Topic.create("Notifications", Notifications.class, Topic.BroadcastDirection.NONE);
  29. String SYSTEM_MESSAGES_GROUP_ID = "System Messages";
  30. void notify(@NotNull Notification notification);
  31. void register(@NotNull final String groupDisplayName, @NotNull final NotificationDisplayType defaultDisplayType);
  32. void register(@NotNull final String groupDisplayName, @NotNull final NotificationDisplayType defaultDisplayType, boolean shouldLog);
  33. @SuppressWarnings({"UtilityClassWithoutPrivateConstructor"})
  34. class Bus {
  35. /**
  36. * Registration is OPTIONAL: STICKY_BALLOON display type will be used by default.
  37. */
  38. @SuppressWarnings("JavaDoc")
  39. public static void register(@NotNull final String group_id, @NotNull final NotificationDisplayType defaultDisplayType) {
  40. if (ApplicationManager.getApplication().isUnitTestMode()) return;
  41. //noinspection SSBasedInspection
  42. SwingUtilities.invokeLater(new Runnable() {
  43. @Override
  44. public void run() {
  45. Application app = ApplicationManager.getApplication();
  46. if (!app.isDisposed()) {
  47. app.getMessageBus().syncPublisher(TOPIC).register(group_id, defaultDisplayType);
  48. }
  49. }
  50. });
  51. }
  52. @Deprecated
  53. public static void notify(@NotNull final Notification notification, @SuppressWarnings("UnusedParameters") final NotificationDisplayType displayType, @Nullable final Project project) {
  54. notify(notification, project);
  55. }
  56. public static void notify(@NotNull final Notification notification) {
  57. notify(notification, null);
  58. }
  59. public static void notify(@NotNull final Notification notification, @Nullable final Project project) {
  60. if (ApplicationManager.getApplication().isUnitTestMode()) {
  61. doNotify(notification, project);
  62. }
  63. else {
  64. //noinspection SSBasedInspection
  65. SwingUtilities.invokeLater(new Runnable() {
  66. @Override
  67. public void run() {
  68. doNotify(notification, project);
  69. }
  70. });
  71. }
  72. }
  73. private static void doNotify(Notification notification, Project project) {
  74. if (project != null && !project.isDisposed()) {
  75. project.getMessageBus().syncPublisher(TOPIC).notify(notification);
  76. } else {
  77. Application app = ApplicationManager.getApplication();
  78. if (!app.isDisposed()) {
  79. app.getMessageBus().syncPublisher(TOPIC).notify(notification);
  80. }
  81. }
  82. }
  83. }
  84. }