View Javadoc
1 /* 2 Copyright (c) 2003, Laurent Caillette. 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without modifica- 6 tion, are permitted provided that the following conditions are met: 7 8 1. Redistributions of source code must retain the above copyright notice, 9 this list of conditions and the following disclaimer. 10 11 2. Redistributions in binary form must reproduce the above copyright notice, 12 this list of conditions and the following disclaimer in the documentation 13 and/or other materials provided with the distribution. 14 15 3. The end-user documentation included with the redistribution, if any, must 16 include the following acknowledgment: "This product includes software 17 written by Laurent Caillette." 18 Alternately, this acknowledgment may appear in the software itself, if 19 and wherever such third-party acknowledgments normally appear. 20 21 4. The name "Laurent Caillette" must not be used to endorse or 22 promote products derived from this software without 23 prior written permission. For written permission, please contact 24 laurent.caillette@laposte.net 25 26 5. Products derived from this software may not be called 27 "Laurent Caillette", nor may "Laurent Caillette" appear 28 in their name, without prior written permission of the 29 author. 30 31 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 32 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 33 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 34 AUTHOR (LAURENT CAILLETTE) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 36 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 37 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 40 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 41 */ 42 43 package tweed.binding; 44 import java.awt.Component; 45 import java.util.ArrayList; 46 import java.util.Date; 47 import java.util.Vector; 48 import org.apache.avalon.framework.logger.Logger; 49 import tweed.context.Context; 50 import tweed.message.Severity; 51 import tweed.message.UserMessage; 52 import tweed.message.UserMessageEvent; 53 import tweed.system.bus.EventBus; 54 55 /*** 56 * Holds the state of user input validation (bindings) and produces 57 * user messages, while a {@link tweed.action.ContextAction} is being 58 * performed. 59 * 60 * @author Laurent Caillette 61 * @version $Id$ 62 */ 63 public class InputValidationHub { 64 65 private final Context context ; 66 private final Logger logger ; 67 private final EventBus bus ; 68 69 public final static String CHILDLOGGER_NAME = "validation" ; 70 71 public InputValidationHub( Context context ) { 72 this.context = context ; 73 this.logger = context.getLogger().getChildLogger( CHILDLOGGER_NAME ) ; 74 this.bus = context.getBus() ; 75 } 76 77 protected Logger getLogger() { 78 return logger ; 79 } 80 81 protected Context getContext() { 82 return context ; 83 } 84 85 86 private boolean begun = false ; 87 88 private synchronized void checkBegun( boolean expected ) { 89 if( begun != expected ) { 90 RuntimeException ex = new IllegalStateException( 91 expected ? 92 "Binding has not begun" : 93 "Binding has already begun" 94 ) ; 95 getLogger().error( "Illegal state for validation", ex ) ; 96 throw ex ; 97 } 98 } 99 100 private synchronized void setBegun( boolean begun ) { 101 checkBegun( ! begun ) ; 102 this.begun = begun ; 103 } 104 105 public synchronized void beginBindings() { 106 checkBegun( false ) ; 107 messageBuilders.clear() ; 108 setBegun( true ) ; 109 } 110 111 public synchronized void endBindings() { 112 checkBegun( true ) ; 113 fireUserMessageEvents() ; 114 setBegun( false ) ; 115 } 116 117 private final Vector messageBuilders = new Vector() ; 118 119 public ValidationMessageBuilder createValidationMessage( String message ) { 120 checkBegun( true ) ; 121 ValidationMessageBuilder builder = new ValidationMessageBuilder( message ) ; 122 messageBuilders.add( builder ) ; 123 return builder ; 124 } 125 126 protected void fireUserMessageEvents() { 127 int messageCount = messageBuilders.size() ; 128 for( int i = 0 ; i < messageCount ; i++) { 129 ValidationMessageBuilder builder = 130 ( ValidationMessageBuilder ) messageBuilders.get( i ) ; 131 UserMessage message = builder.createValidationMessage() ; 132 UserMessageEvent event = new UserMessageEvent( this, message ) ; 133 bus.postEvent( event ) ; 134 } 135 } 136 137 public final class ValidationMessageBuilder { 138 139 private boolean built = false ; 140 private final String messageBody ; 141 private final Severity severity ; 142 143 private ValidationMessageBuilder( String messageBody ) { 144 checkBegun( true ) ; 145 if( messageBody == null ) { 146 throw new IllegalArgumentException( "messageBody == null " ) ; 147 } 148 this.messageBody = messageBody ; 149 this.severity = Severity.WARNING ; 150 } 151 152 private final ArrayList componentList = new ArrayList() ; 153 154 public ValidationMessageBuilder addComponent( Component component ) { 155 if( component == null ) { 156 throw new IllegalArgumentException( "component == null" ) ; 157 } 158 checkBegun( true ) ; 159 componentList.add( component ) ; 160 return this ; 161 } 162 163 </*package*/ ValidationMessage createValidationMessage() {/package-summary/html">color="#329900">package*/ ValidationMessage createValidationMessage() {/package-summary.html">font color="#329900">/*package*/ ValidationMessage createValidationMessage() {/package-summary.html">color="#329900">package*/ ValidationMessage createValidationMessage() { 164 165 if( built ) { 166 throw new IllegalStateException( "UserMessage already built" ) ; 167 } 168 169 final Date creationTime = new Date() ; 170 final Component[] components = ( Component[] ) 171 componentList.toArray( new Component[ componentList.size() ] ) ; 172 173 ValidationMessage validationMessage = new ValidationMessage() { 174 175 public Component[] getComponents() { 176 return components ; 177 } 178 179 public String getBody() { 180 return messageBody ; 181 } 182 183 public Severity getSeverity() { 184 return severity ; 185 } 186 187 public Date getTimestamp() { 188 return creationTime ; 189 } 190 191 public String toString() { 192 return messageBody ; 193 } 194 195 } ; 196 197 built = true ; 198 return validationMessage ; 199 } 200 } 201 202 }

This page was automatically generated by Maven