001 /* 002 * Copyright 2002-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.text; 017 018 import javax.swing.JComponent; 019 import javax.swing.JSpinner; 020 import javax.swing.JTextField; 021 import javax.swing.JSpinner.DefaultEditor; 022 import javax.swing.text.JTextComponent; 023 024 import org.springframework.richclient.form.builder.support.AbstractFormComponentInterceptor; 025 026 /** 027 * Abstract base class for <code>FormComponentInterceptor</code>s that work on 028 * <code>JTextComponent</code>s. 029 * 030 * @author Peter De Bruycker 031 * 032 */ 033 public abstract class TextComponentInterceptor extends AbstractFormComponentInterceptor { 034 035 public final void processComponent( String propertyName, JComponent component ) { 036 JTextComponent textComponent = getTextComponent( getInnerComponent( component ) ); 037 if( textComponent != null ) { 038 processComponent( propertyName, textComponent ); 039 } 040 } 041 042 /** 043 * Process the text component. 044 * 045 * @param propertyName the name of the property 046 * @param textComponent the text component 047 */ 048 protected abstract void processComponent( String propertyName, JTextComponent textComponent ); 049 050 /** 051 * Converts the given component to a <code>JTextComponent</code>. This can be a 052 * simple cast if the component is already a text component, or an embedded component 053 * (for example a JSpinner). 054 * <p> 055 * This method is protected, and can be overridden when necessary. 056 * 057 * @param component the component 058 * @return a <code>JTextComponent</code>, or <code>null</code> 059 */ 060 protected JTextComponent getTextComponent( JComponent component ) { 061 if( component instanceof JTextField ) { 062 return (JTextField) component; 063 } 064 065 if( component instanceof JSpinner ) { 066 JSpinner spinner = (JSpinner) component; 067 if( spinner.getEditor() instanceof JSpinner.DefaultEditor ) { 068 return ((DefaultEditor) spinner.getEditor()).getTextField(); 069 } 070 if( spinner.getEditor() instanceof JTextField ) { 071 return (JTextField) spinner.getEditor(); 072 } 073 } 074 075 return null; 076 } 077 078 }