2 * Copyright 2000-2014 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.notification;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.ui.popup.Balloon;
21 import com.intellij.openapi.ui.popup.JBPopupAdapter;
22 import com.intellij.openapi.ui.popup.LightweightWindowEvent;
23 import com.intellij.openapi.util.text.StringUtil;
24 import com.intellij.reference.SoftReference;
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
29 import java.lang.ref.WeakReference;
34 public class Notification {
35 private static final Logger LOG = Logger.getInstance("#com.intellij.notification.Notification");
37 private final String myGroupId;
38 private final String myContent;
39 private final NotificationType myType;
40 private final NotificationListener myListener;
41 private final String myTitle;
42 private boolean myExpired;
43 private Runnable myWhenExpired;
44 private Boolean myImportant;
45 private WeakReference<Balloon> myBalloonRef;
47 public Notification(@NotNull String groupDisplayId, @NotNull String title, @NotNull String content, @NotNull NotificationType type) {
48 this(groupDisplayId, title, content, type, null);
52 * @param groupDisplayId this should be a human-readable, capitalized string like "Facet Detector".
53 * It will appear in "Notifications" configurable.
54 * @param title notification title
55 * @param content notification content
56 * @param type notification type
57 * @param listener notification lifecycle listener
59 public Notification(@NotNull String groupDisplayId,
60 @NotNull String title,
61 @NotNull String content,
62 @NotNull NotificationType type,
63 @Nullable NotificationListener listener) {
64 myGroupId = groupDisplayId;
68 myListener = listener;
70 LOG.assertTrue(!StringUtil.isEmptyOrSpaces(myContent), "Notification should have content, groupId: " + myGroupId);
73 @SuppressWarnings("MethodMayBeStatic")
75 public Icon getIcon() {
80 public String getGroupId() {
85 public String getTitle() {
90 public String getContent() {
95 public NotificationListener getListener() {
100 public NotificationType getType() {
104 public boolean isExpired() {
108 public void expire() {
109 NotificationsManager.getNotificationsManager().expire(this);
113 Runnable whenExpired = myWhenExpired;
114 if (whenExpired != null) whenExpired.run();
117 public Notification whenExpired(@Nullable Runnable whenExpired) {
118 myWhenExpired = whenExpired;
122 public void hideBalloon() {
123 if (myBalloonRef != null) {
124 final Balloon balloon = myBalloonRef.get();
125 if (balloon != null) {
132 public void setBalloon(@NotNull final Balloon balloon) {
134 myBalloonRef = new WeakReference<Balloon>(balloon);
135 balloon.addListener(new JBPopupAdapter() {
137 public void onClosed(LightweightWindowEvent event) {
138 if (SoftReference.dereference(myBalloonRef) == balloon) {
146 public Balloon getBalloon() {
147 return SoftReference.dereference(myBalloonRef);
150 public void notify(@Nullable Project project) {
151 Notifications.Bus.notify(this, project);
154 public Notification setImportant(boolean important) {
155 myImportant = important;
159 public boolean isImportant() {
160 if (myImportant != null) {
164 return getListener() != null;