add note that NotificationAction is more preferable way

This commit is contained in:
Vladimir Krivosheev
2018-09-30 08:58:51 +02:00
parent 16e39ec16b
commit f453b03fe2
4 changed files with 23 additions and 38 deletions
@@ -33,6 +33,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
* <br><br>
* Warning: be careful not to use the links in HTML content, use {@link #addAction(AnAction)}
*
* @see NotificationAction
* @see com.intellij.notification.SingletonNotificationManager
*
* @author spleaner
* @author Alexander Lobas
*/
@@ -1,20 +1,7 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2018 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.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAwareAction;
import org.jetbrains.annotations.NotNull;
@@ -23,6 +10,8 @@ import org.jetbrains.annotations.Nullable;
import java.util.function.BiConsumer;
/**
* @see Notification#addAction(AnAction)
*
* @author Alexander Lobas
*/
public abstract class NotificationAction extends DumbAwareAction {
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2018 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.ide.BrowserUtil;
@@ -21,6 +7,11 @@ import org.jetbrains.annotations.NotNull;
import javax.swing.event.HyperlinkEvent;
import java.net.URL;
/**
* Consider using {@link NotificationAction} instead of links in HTML content.
*
* @see NotificationAction
*/
public interface NotificationListener {
void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event);
@@ -1,6 +1,7 @@
// Copyright 2000-2018 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.openapi.actionSystem.AnAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindowManager
@@ -9,21 +10,19 @@ import java.util.concurrent.atomic.AtomicReference
class SingletonNotificationManager(private val group: NotificationGroup, private val type: NotificationType, private val defaultListener: NotificationListener? = null) {
private val notification = AtomicReference<Notification>()
private val expiredListener by lazy {
Runnable {
val currentNotification = notification.get()
if (currentNotification != null && currentNotification.isExpired) {
notification.compareAndSet(currentNotification, null)
}
private val expiredListener = Runnable {
val currentNotification = notification.get()
if (currentNotification != null && currentNotification.isExpired) {
notification.compareAndSet(currentNotification, null)
}
}
fun notify(content: String, project: Project?): Boolean {
return notify("", content, project, defaultListener)
return notify("", content, project)
}
@JvmOverloads
fun notify(title: String, content: String, project: Project? = null, listener: NotificationListener? = defaultListener): Boolean {
fun notify(title: String = "", content: String, project: Project? = null, listener: NotificationListener? = defaultListener, action: AnAction? = null): Boolean {
val oldNotification = notification.get()
// !oldNotification.isExpired() is not enough - notification could be closed, but not expired
if (oldNotification != null) {
@@ -37,6 +36,9 @@ class SingletonNotificationManager(private val group: NotificationGroup, private
}
val newNotification = group.createNotification(title, content, type, listener)
if (action != null) {
newNotification.addAction(action)
}
newNotification.whenExpired(expiredListener)
notification.set(newNotification)
newNotification.notify(project)