View Javadoc
1   package fr.ifremer.tutti.ui.swing.util;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import fr.ifremer.tutti.ui.swing.content.actions.AbstractChangeScreenAction;
26  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.nuiton.jaxx.application.ApplicationTechnicalException;
30  import org.nuiton.jaxx.application.swing.action.ApplicationActionException;
31  import org.nuiton.jaxx.application.swing.util.ApplicationErrorHelper;
32  import org.nuiton.jaxx.application.swing.util.ApplicationExceptionHandler;
33  
34  import java.beans.PropertyVetoException;
35  
36  /**
37   * Tutti global exception handler.
38   *
39   * Catch all application uncaught and display it in a custom JoptionPane
40   * or JXErrorPane.
41   *
42   * See http://stackoverflow.com/a/4448569/1165234 for details.
43   *
44   * @author Tony Chemit - chemit@codelutin.com
45   * @since 1.0
46   */
47  public class TuttiExceptionHandler extends ApplicationExceptionHandler {
48  
49      private static final Log log =
50              LogFactory.getLog(TuttiExceptionHandler.class);
51  
52      public TuttiExceptionHandler(ApplicationErrorHelper errorHelper) {
53          super(errorHelper);
54      }
55  
56      @Override
57      public void uncaughtException(Thread t, Throwable ex) {
58          if (t.getName().startsWith("AWT-EventQueue-") || ex.getCause() instanceof PropertyVetoException) {
59  
60              // Swallow some ui execption we can't deal with
61              // See https://forge.codelutin.com/issues/7489
62              if (log.isErrorEnabled()) {
63                  log.error("Swallow Swing error: " + ex.getMessage(), ex);
64              }
65  
66          } else {
67  
68              super.uncaughtException(t, ex);
69          }
70      }
71  
72      @Override
73      protected void handleException(String tname, Throwable ex) {
74          if (log.isErrorEnabled()) {
75              log.error("Global application exception [" + tname + "]", ex);
76          }
77  
78          Throwable cause = getCause(ex);
79  
80          boolean backToScreen = false;
81  
82          LongActionSupport action = null;
83  
84          if (cause instanceof ApplicationActionException) {
85  
86              ApplicationActionException actionException = (ApplicationActionException) cause;
87              cause = cause.getCause();
88  
89              if (log.isDebugEnabled()) {
90                  log.debug("Action error cause:", cause);
91              }
92  
93              action = (LongActionSupport) actionException.getAction();
94  
95              if (action instanceof AbstractChangeScreenAction) {
96                  backToScreen = true;
97              }
98          }
99  
100         showErrorDialog(cause.getMessage(), cause);
101 
102         if (backToScreen) {
103 
104             action.getContext().setFallBackScreen();
105         }
106     }
107 
108     @Override
109     protected Throwable getCause(Throwable ex) {
110 
111 
112         Throwable cause = super.getCause(ex);
113 
114         if (cause == null) {
115 
116             if (ex instanceof ApplicationTechnicalException) {
117                 cause = ex;
118             }
119         }
120 
121         return cause;
122 
123     }
124 }