Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
MessageNotifyUtil.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013-2015 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.coreutils;
20 
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.text.SimpleDateFormat;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.logging.Level;
29 import javax.swing.Icon;
30 import javax.swing.ImageIcon;
31 import org.openide.DialogDisplayer;
32 import org.openide.NotifyDescriptor;
33 import org.openide.awt.Notification;
34 import org.openide.awt.NotificationDisplayer;
35 import org.openide.util.ImageUtilities;
36 
47 public class MessageNotifyUtil {
48 
49  private MessageNotifyUtil() {
50  }
51 
52  public enum MessageType {
53 
54  INFO(NotifyDescriptor.INFORMATION_MESSAGE, "info-icon-16.png"), //NON-NLS
55  ERROR(NotifyDescriptor.ERROR_MESSAGE, "error-icon-16.png"), //NON-NLS
56  WARNING(NotifyDescriptor.WARNING_MESSAGE, "warning-icon-16.png"), //NON-NLS
57  CONFIRM(NotifyDescriptor.YES_NO_OPTION, "warning-icon-16.png"); //NON-NLS
58 
59  private final int notifyDescriptorType;
60  private final Icon icon;
61 
62  private MessageType(int notifyDescriptorType, String resourceName) {
63  this.notifyDescriptorType = notifyDescriptorType;
64  if (resourceName == null) {
65  icon = new ImageIcon();
66  } else {
67  icon = loadIcon(resourceName);
68  }
69  }
70 
71  private static Icon loadIcon(String resourceName) {
72  Icon icon = ImageUtilities.loadImageIcon("org/sleuthkit/autopsy/images/" + resourceName, false); //NON-NLS
73  if (icon == null) {
75  logger.log(Level.SEVERE, "Failed to load icon resource: " + resourceName + ". Using blank image."); //NON-NLS NON-NLS
76  icon = new ImageIcon();
77  }
78  return icon;
79  }
80 
82  return notifyDescriptorType;
83  }
84 
85  Icon getIcon() {
86  return icon;
87  }
88  }
89 
93  public static class Message {
94 
95  private Message() {
96  }
97 
101  public static DialogDisplayer getDialogDisplayer() {
102  return DialogDisplayer.getDefault();
103  }
104 
111  public static void show(String message, MessageType messageType) {
112  getDialogDisplayer().notify(new NotifyDescriptor.Message(message,
113  messageType.getNotifyDescriptorType()));
114  }
115 
123  public static boolean confirm(String message) {
124  return getDialogDisplayer().notify(new NotifyDescriptor.Confirmation(message,
125  MessageType.CONFIRM.getNotifyDescriptorType())) == NotifyDescriptor.YES_OPTION;
126  }
127 
133  public static void info(String message) {
134  show(message, MessageType.INFO);
135  }
136 
142  public static void error(String message) {
143  show(message, MessageType.ERROR);
144  }
145 
151  public static void warn(String message) {
152  show(message, MessageType.WARNING);
153  }
154 
155  }
156 
160  public static class Notify {
161 
162  private static final SimpleDateFormat TIME_STAMP_FORMAT = new SimpleDateFormat("MM/dd/yy HH:mm:ss z");
163 
164  //notifications to keep track of and to reset when case is closed
165  private static final List<Notification> notifications = Collections.synchronizedList(new ArrayList<Notification>());
166 
167  private Notify() {
168  }
169 
173  public static void clear() {
174  notifications.stream().forEach((n) -> {
175  n.clear();
176  });
177  notifications.clear();
178  }
179 
188  public static void show(String title, String message, MessageType type, ActionListener actionListener) {
189  Notification newNotification
190  = NotificationDisplayer.getDefault().notify(addTimeStampToTitle(title), type.getIcon(), message, actionListener);
191  notifications.add(newNotification);
192  }
193 
203  public static void show(String title, final String message, final MessageType type) {
204  ActionListener actionListener = (ActionEvent e) -> {
205  MessageNotifyUtil.Message.show(message, type);
206  };
207 
208  show(title, message, type, actionListener);
209  }
210 
217  public static void info(String title, String message) {
218  show(title, message, MessageType.INFO);
219  }
220 
227  public static void error(String title, String message) {
228  show(title, message, MessageType.ERROR);
229  }
230 
237  public static void warn(String title, String message) {
238  show(title, message, MessageType.WARNING);
239  }
240 
250  private static String addTimeStampToTitle(String title) {
251  return TIME_STAMP_FORMAT.format(new Date()) + " " + title;
252  }
253  }
254 }
MessageType(int notifyDescriptorType, String resourceName)
static void info(String title, String message)
static void show(String message, MessageType messageType)
static void error(String title, String message)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static void show(String title, String message, MessageType type, ActionListener actionListener)
static void show(String title, final String message, final MessageType type)
static void warn(String title, String message)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.