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 org.springframework.binding.form.FormModel;
019    import org.springframework.richclient.form.builder.FormComponentInterceptor;
020    import org.springframework.richclient.form.builder.FormComponentInterceptorFactory;
021    
022    import javax.swing.*;
023    import javax.swing.text.JTextComponent;
024    import java.awt.event.FocusAdapter;
025    import java.awt.event.FocusEvent;
026    import java.awt.event.FocusListener;
027    
028    /**
029     * Implements "select all" behaviour for form components. If the form component is a text
030     * field, or a spinner, the contents of the component are selected if it receives focus.
031     * 
032     * @author Peter De Bruycker
033     */
034    public class SelectAllFormComponentInterceptorFactory implements FormComponentInterceptorFactory {
035    
036        public FormComponentInterceptor getInterceptor( FormModel formModel ) {
037            return new SelectAllFormComponentInterceptor();
038        }
039    
040        public class SelectAllFormComponentInterceptor extends TextComponentInterceptor {
041            private FocusListener selector = new FocusAdapter() {
042    
043                public void focusGained( FocusEvent e ) {
044                    if( !e.isTemporary() ) {
045                        final JTextComponent textComponent = (JTextComponent) e.getComponent();
046                        // using invokeLater as fix for bug 4740914
047                        SwingUtilities.invokeLater( new Runnable() {
048                            public void run() {
049                                textComponent.selectAll();
050                            }
051                        } );
052                    }
053                }
054    
055                @Override
056                public void focusLost(FocusEvent e)
057                {
058                    if( !e.isTemporary() ) {
059                        final JTextComponent textComponent = (JTextComponent) e.getComponent();
060                        SwingUtilities.invokeLater( new Runnable() {
061                            public void run() {
062                                textComponent.select(0,0);
063                            }
064                        } );
065                    }
066                }
067            };
068    
069            protected void processComponent( String propertyName, JTextComponent textComponent ) {
070                textComponent.addFocusListener( selector );
071            }
072        }
073    
074    }