001 package org.springframework.richclient.exceptionhandling; 002 003 import org.hibernate.validator.InvalidStateException; 004 import org.hibernate.validator.InvalidValue; 005 006 import javax.swing.*; 007 import java.awt.*; 008 import java.util.ArrayList; 009 import java.util.List; 010 011 /** 012 * Displays the validation errors to the user. 013 * @author Geoffrey De Smet 014 * @since 0.3 015 */ 016 public class HibernateValidatorDialogExceptionHandler extends AbstractDialogExceptionHandler { 017 018 private static final String CAPTION_KEY = "hibernateValidatorDialogExceptionHandler.caption"; 019 private static final String EXPLANATION_KEY = "hibernateValidatorDialogExceptionHandler.explanation"; 020 021 public String resolveExceptionCaption(Throwable throwable) { 022 return messageSourceAccessor.getMessage(CAPTION_KEY, CAPTION_KEY); 023 } 024 025 public Object createExceptionContent(Throwable throwable) { 026 if (!(throwable instanceof InvalidStateException)) { 027 String ILLEGAL_THROWABLE_ARGUMENT 028 = "Could not handle exception: throwable is not an InvalidStateException:\n" 029 + throwable.getClass().getName(); 030 logger.error(ILLEGAL_THROWABLE_ARGUMENT); 031 return ILLEGAL_THROWABLE_ARGUMENT; 032 } 033 InvalidStateException invalidStateException = (InvalidStateException) throwable; 034 String explanation = messageSourceAccessor.getMessage(EXPLANATION_KEY, EXPLANATION_KEY); 035 JPanel panel = new JPanel(new BorderLayout()); 036 JLabel explanationLabel = new JLabel(explanation); 037 panel.add(explanationLabel, BorderLayout.NORTH); 038 List<String> invalidValueMessageList = new ArrayList<String>(); 039 for (InvalidValue invalidValue : invalidStateException.getInvalidValues()) { 040 StringBuffer messageBuffer = new StringBuffer(); 041 String propertyName = invalidValue.getPropertyName(); 042 messageBuffer.append(messageSourceAccessor.getMessage(propertyName + ".label", propertyName)); 043 messageBuffer.append(" "); 044 messageBuffer.append(invalidValue.getMessage()); 045 invalidValueMessageList.add(messageBuffer.toString()); 046 } 047 JList invalidValuesJList = new JList(invalidValueMessageList.toArray()); 048 JScrollPane invalidValuesScrollPane = new JScrollPane(invalidValuesJList, 049 ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 050 panel.add(invalidValuesScrollPane, BorderLayout.CENTER); 051 return panel; 052 } 053 054 }