001    package org.springframework.richclient.exceptionhandling;
002    
003    import org.apache.commons.logging.LogFactory;
004    import org.springframework.context.MessageSourceResolvable;
005    import org.springframework.context.support.MessageSourceAccessor;
006    import org.springframework.richclient.application.ApplicationServicesLocator;
007    import org.springframework.richclient.application.ApplicationWindow;
008    import org.springframework.richclient.application.Application;
009    import org.springframework.richclient.application.config.ApplicationLifecycleAdvisor;
010    import org.springframework.richclient.core.DefaultMessage;
011    import org.springframework.richclient.core.Message;
012    import org.springframework.richclient.core.Severity;
013    import org.springframework.util.StringUtils;
014    
015    import javax.swing.JFrame;
016    import javax.swing.JOptionPane;
017    
018    /**
019     * @TODO extends AbstractRegisterableExceptionHandler as soon as spring-richclient is minimum 1.5
020     * @author Geoffrey De Smet
021     */
022    public class DefaultRegisterableExceptionHandler implements RegisterableExceptionHandler {
023    
024        /**
025         * Currently on registers for the event thread, not for other threads.
026         * @TODO remove as soon as this class extends AbstractRegisterableExceptionHandler
027         */
028        public void registerExceptionHandler() {
029            AwtExceptionHandlerAdapterHack.registerExceptionHandler(this);
030        }
031    
032        public void uncaughtException(Thread thread, Throwable throwable) {
033            LogFactory.getLog(ApplicationLifecycleAdvisor.class).error(throwable.getMessage(), throwable);
034            String exceptionMessage;
035            if (throwable instanceof MessageSourceResolvable) {
036                exceptionMessage = ((MessageSourceAccessor) ApplicationServicesLocator.services()
037                        .getService(MessageSourceAccessor.class))
038                        .getMessage((MessageSourceResolvable) throwable);
039            } else {
040                exceptionMessage = throwable.getLocalizedMessage();
041            }
042            if (!StringUtils.hasText(exceptionMessage)) {
043                String defaultMessage = "An application exception occurred.\nPlease contact your administrator.";
044                exceptionMessage = ((MessageSourceAccessor) ApplicationServicesLocator.services()
045                        .getService(MessageSourceAccessor.class))
046                        .getMessage("applicationDialog.defaultException", defaultMessage);
047            }
048    
049            Message message = new DefaultMessage(exceptionMessage, Severity.ERROR);
050            ApplicationWindow activeWindow = Application.instance().getActiveWindow();
051            JFrame parentFrame = (activeWindow == null) ? null : activeWindow.getControl();
052            JOptionPane.showMessageDialog(parentFrame, message.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
053        }
054    
055    }