| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.common.ui.client.widgets; | 
  | 17 |  |   | 
  | 18 |  |   | 
  | 19 |  |  import org.kuali.student.common.ui.client.application.Application; | 
  | 20 |  |  import org.kuali.student.common.ui.client.application.ApplicationContext; | 
  | 21 |  |  import org.kuali.student.common.ui.client.logging.Logger; | 
  | 22 |  |  import org.kuali.student.common.ui.client.mvc.Callback; | 
  | 23 |  |  import org.kuali.student.common.ui.client.widgets.buttongroups.OkGroup; | 
  | 24 |  |  import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations.OkEnum; | 
  | 25 |  |   | 
  | 26 |  |  import com.google.gwt.core.client.GWT; | 
  | 27 |  |  import com.google.gwt.user.client.Command; | 
  | 28 |  |  import com.google.gwt.user.client.DeferredCommand; | 
  | 29 |  |  import com.google.gwt.user.client.ui.SimplePanel; | 
  | 30 |  |  import com.google.gwt.user.client.ui.VerticalPanel; | 
  | 31 |  |   | 
  | 32 | 0 |  public class KSErrorDialog { | 
  | 33 |  |   | 
  | 34 | 0 |      final static ApplicationContext context = Application.getApplicationContext(); | 
  | 35 |  |   | 
  | 36 | 0 |      public enum MessagesRequired { | 
  | 37 | 0 |          DIALOG_TITLE("errorDialogTitle"), | 
  | 38 | 0 |          DESCRIBE_ACTION("describeAction"), | 
  | 39 | 0 |          ERROR_DESCRIPTION("errorDescription"); | 
  | 40 |  |   | 
  | 41 |  |          private String messageId; | 
  | 42 | 0 |          private MessagesRequired(String messageId) { | 
  | 43 | 0 |              this.messageId = messageId; | 
  | 44 | 0 |          } | 
  | 45 |  |          public String toString() { | 
  | 46 | 0 |              return this.messageId; | 
  | 47 |  |          } | 
  | 48 |  |      } | 
  | 49 |  |   | 
  | 50 |  |      public static void bindUncaughtExceptionHandler() { | 
  | 51 | 0 |          GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() { | 
  | 52 |  |              public void onUncaughtException(Throwable e) { | 
  | 53 | 0 |                  GWT.log(e.getMessage(), e); | 
  | 54 |  |                   | 
  | 55 | 0 |                      KSErrorDialog.show(e); | 
  | 56 | 0 |              } | 
  | 57 |  |          }); | 
  | 58 | 0 |      } | 
  | 59 |  |   | 
  | 60 |  |      private static String getStackTrace(Throwable t){ | 
  | 61 | 0 |              StringBuilder sb =  new StringBuilder(); | 
  | 62 | 0 |              appendStackTrace(t,sb); | 
  | 63 | 0 |              return sb.toString(); | 
  | 64 |  |      } | 
  | 65 |  |       | 
  | 66 |  |      private static void appendStackTrace(Throwable t, StringBuilder s) { | 
  | 67 | 0 |          s.append(t.toString()); | 
  | 68 | 0 |          s.append(": at\n"); | 
  | 69 | 0 |          StackTraceElement[] stack = t.getStackTrace(); | 
  | 70 | 0 |          for (StackTraceElement frame : stack) { | 
  | 71 | 0 |              s.append(frame.toString()); | 
  | 72 | 0 |              s.append("\n"); | 
  | 73 |  |          } | 
  | 74 | 0 |      } | 
  | 75 |  |       | 
  | 76 |  |      public static void show(final Throwable error) { | 
  | 77 | 0 |          final KSLightBox lightbox = new KSLightBox(); | 
  | 78 |  |   | 
  | 79 | 0 |          final VerticalPanel panel = new VerticalPanel(); | 
  | 80 | 0 |          panel.addStyleName("KS-Error-Dialog"); | 
  | 81 |  |   | 
  | 82 | 0 |          final KSLabel title = new KSLabel(context.getMessage(MessagesRequired.DIALOG_TITLE.toString())); | 
  | 83 | 0 |          title.addStyleName("KS-Error-Dialog-Title"); | 
  | 84 |  |   | 
  | 85 | 0 |          final KSLabel errorDescriptionLabel = new KSLabel(context.getMessage(MessagesRequired.ERROR_DESCRIPTION.toString())); | 
  | 86 | 0 |          errorDescriptionLabel.addStyleName("KS-Error-Dialog-Label"); | 
  | 87 |  |   | 
  | 88 | 0 |          final SimplePanel errorDescriptionPanel = new SimplePanel(); | 
  | 89 | 0 |          errorDescriptionPanel.addStyleName("KS-Error-Dialog-Panel"); | 
  | 90 |  |   | 
  | 91 | 0 |          final KSTextArea errorDescription = new KSTextArea(); | 
  | 92 | 0 |          errorDescription.setText(getErrorDescription(error)); | 
  | 93 | 0 |          errorDescription.addStyleName("KS-Error-Dialog-TextArea"); | 
  | 94 | 0 |          errorDescription.setReadOnly(true); | 
  | 95 |  |           | 
  | 96 | 0 |          errorDescriptionPanel.add(errorDescription); | 
  | 97 |  |   | 
  | 98 |  |   | 
  | 99 |  |   | 
  | 100 |  |   | 
  | 101 |  |   | 
  | 102 |  |   | 
  | 103 |  |   | 
  | 104 |  |   | 
  | 105 |  |   | 
  | 106 |  |   | 
  | 107 |  |   | 
  | 108 | 0 |          final OkGroup buttonPanel = new OkGroup(new Callback<OkEnum>(){ | 
  | 109 |  |   | 
  | 110 |  |              @Override | 
  | 111 |  |              public void exec(OkEnum result) { | 
  | 112 | 0 |                  switch(result){ | 
  | 113 |  |                      case Ok: | 
  | 114 | 0 |                          DeferredCommand.addCommand(new Command() { | 
  | 115 |  |                              public void execute() { | 
  | 116 | 0 |                                  sendReport(error); | 
  | 117 | 0 |                                  lightbox.hide(); | 
  | 118 | 0 |                              } | 
  | 119 |  |                          }); | 
  | 120 |  |                          break; | 
  | 121 |  |                  } | 
  | 122 | 0 |              } | 
  | 123 |  |          }); | 
  | 124 |  |   | 
  | 125 | 0 |          lightbox.setNonCaptionHeader(title); | 
  | 126 | 0 |          panel.add(errorDescriptionLabel); | 
  | 127 | 0 |          panel.add(errorDescriptionPanel); | 
  | 128 |  |   | 
  | 129 |  |   | 
  | 130 | 0 |          panel.add(buttonPanel); | 
  | 131 |  |   | 
  | 132 | 0 |          panel.setSize("100", "100"); | 
  | 133 |  |   | 
  | 134 | 0 |          lightbox.setWidget(panel); | 
  | 135 | 0 |          lightbox.setSize(440, 200); | 
  | 136 | 0 |          lightbox.show(); | 
  | 137 |  |   | 
  | 138 | 0 |      } | 
  | 139 |  |   | 
  | 140 |  |      private static String getErrorDescription(Throwable error) { | 
  | 141 |  |           | 
  | 142 | 0 |          return error.getMessage(); | 
  | 143 |  |      } | 
  | 144 |  |   | 
  | 145 |  |      private static void sendReport(final Throwable error) { | 
  | 146 |  |           | 
  | 147 | 0 |          Logger.getClientContextInfo().put("logType", "clientError"); | 
  | 148 |  |   | 
  | 149 | 0 |          Logger.error("Uncaught exception", error); | 
  | 150 | 0 |          Logger.sendLogs(); | 
  | 151 |  |   | 
  | 152 |  |           | 
  | 153 | 0 |          Logger.getClientContextInfo().clear(); | 
  | 154 | 0 |      } | 
  | 155 |  |   | 
  | 156 |  |  } |