View Javadoc

1   package org.xmlhammer.gui.util;
2   
3   import java.awt.Frame;
4   
5   import javax.swing.JOptionPane;
6   import javax.swing.SwingUtilities;
7   
8   import org.apache.log4j.AppenderSkeleton;
9   import org.apache.log4j.Level;
10  import org.apache.log4j.spi.LoggingEvent;
11  
12  public class UIAppender extends AppenderSkeleton {
13      private Frame frame = null;
14  
15      public UIAppender( Frame frame) {
16          super();
17  
18          this.frame = frame;
19      }
20  
21      @Override
22      protected void append(LoggingEvent event) {
23          if (event.getLevel() == Level.ERROR) {
24              if ( event.getThrowableInformation() != null && event.getThrowableInformation().getThrowable() != null) {
25                  Throwable t = event.getThrowableInformation().getThrowable();
26                  
27                  while (t.getCause() != null) {
28                      t = t.getCause();
29                  }
30  
31                  StringBuffer message = new StringBuffer();
32  
33                  if (t.getMessage() == null || t.getMessage().length() == 0) {
34                      message.append(t.getClass().toString());
35                      message.append("\nPlease see log for more information.");
36                  } else {
37                      message.append(t.getMessage());
38                      message.append("\nPlease see log for more information.");
39                  }
40  
41                  showMessage(frame, message.toString(), event.getRenderedMessage());
42              } else {
43                  showMessage(frame, event.getRenderedMessage(), "Module Error");
44              }
45          }
46      }
47  
48      private void showMessage(final Frame frame, final String message, final String title) {
49          if (SwingUtilities.isEventDispatchThread()) {
50              JOptionPane.showMessageDialog( frame, message, title, JOptionPane.ERROR_MESSAGE);
51          } else {
52              SwingUtilities.invokeLater(new Runnable() {
53                  public void run() {
54                      JOptionPane.showMessageDialog( frame, message, title, JOptionPane.ERROR_MESSAGE);
55                  }
56              });
57          }
58      }
59  
60      @Override
61      public boolean requiresLayout() {
62          return false;
63      }
64  
65      @Override
66      public void close() {
67      }
68  
69  }