1 /* 2 * Copyright (c) 2022 Guangzhou Digitalchina Information Technology Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 package com.sk.utils; 16 17 import com.intellij.ide.actions.OpenFileAction; 18 import com.intellij.notification.NotificationType; 19 import com.intellij.notification.Notification; 20 import com.intellij.notification.NotificationGroup; 21 import com.intellij.notification.NotificationAction; 22 import com.intellij.notification.Notifications; 23 import com.intellij.notification.NotificationDisplayType; 24 import com.intellij.openapi.actionSystem.AnActionEvent; 25 import com.intellij.openapi.diagnostic.Logger; 26 import com.intellij.openapi.project.Project; 27 import org.jetbrains.annotations.NotNull; 28 29 import java.io.File; 30 31 /** 32 * 通知框 33 * 34 * @author: liulongc digitalchina.com 35 * @see: tool conversion plug-in 36 * @version: v1.0.0 37 * @since 2022-05-27 38 */ 39 public class GenNotification { 40 41 private static final Logger LOG = Logger.getInstance(FileUtil.class); 42 GenNotification()43 private GenNotification() { 44 } 45 46 /** 47 * 无action 通知 48 * 49 * @param project projectid 50 * @param content 提示内容 51 * @param title 提示栏内容 52 * @param type 提示类型 Error,Waring,info 53 */ notifyMessage(@avax.annotation.Nullable Project project, String content, String title, NotificationType type)54 public static void notifyMessage(@javax.annotation.Nullable Project project, 55 String content, 56 String title, 57 NotificationType type) { 58 notifyMessage(project, content, title, type, false); 59 } 60 61 /** 62 * 消息通知 63 * 64 * @param project projectid 65 * @param content 提示内容 66 * @param title 提示栏内容 67 * @param type 提示类型 Error,Waring,info 68 * @param addAct 是否添加action 69 */ notifyMessage(@avax.annotation.Nullable Project project, String content, String title, NotificationType type, boolean addAct)70 public static void notifyMessage(@javax.annotation.Nullable Project project, 71 String content, 72 String title, 73 NotificationType type, 74 boolean addAct) { 75 76 NotificationGroup notificationGroup = new NotificationGroup("Generate.Result.Group", 77 NotificationDisplayType.STICKY_BALLOON); 78 Notification notification = notificationGroup.createNotification(content, type); 79 notification.setTitle(title); 80 notification.setContent(content); 81 82 if (NotificationType.ERROR.equals(type)) { 83 LOG.error(content); 84 } else if (NotificationType.WARNING.equals(type)) { 85 LOG.warn(content); 86 } else { 87 LOG.info(content); 88 } 89 90 if (addAct) { 91 notification.setContent(null); 92 addAction(project, content, notification); 93 } 94 Notifications.Bus.notify(notification, project); 95 96 } 97 addAction(Project project, String dirPath, Notification notification)98 private static void addAction(Project project, String dirPath, Notification notification) { 99 File genResultPath = new File(dirPath); 100 if (!genResultPath.exists()) { 101 LOG.info(String.format("%s not exist", genResultPath.getPath())); 102 } 103 LOG.info("generated file list log:"); 104 105 File[] fa = genResultPath.listFiles(); 106 for (int i = 0; i < fa.length; i++) { 107 File fs = fa[i]; 108 String fileName = fs.getName(); 109 boolean dissFile = !fileName.endsWith(".log") || !fileName.endsWith(".txt") || !fileName.endsWith(".ts"); 110 if (!fs.isDirectory() && dissFile) { 111 String filePath = fs.getPath(); 112 NotificationAction action = new NotificationAction(filePath) { 113 @Override 114 public void actionPerformed(@NotNull AnActionEvent anActionEvent, 115 @NotNull Notification notification) { 116 OpenFileAction.openFile(filePath, project); 117 } 118 }; 119 notification.addAction(action); 120 } else { 121 LOG.info(String.format("%s is Directory", fs.getPath())); 122 } 123 } 124 } 125 } 126