001    package org.springframework.richclient.form.binding.swing;
002    
003    import org.springframework.binding.form.FormModel;
004    import org.springframework.richclient.form.binding.support.CustomBinding;
005    import org.springframework.richclient.components.FileChooser;
006    
007    import javax.swing.*;
008    import java.awt.event.FocusListener;
009    import java.awt.event.FocusEvent;
010    
011    public class FileChooserBinding extends CustomBinding
012    {
013        private final FileChooser field;
014        private final boolean useFile;
015    
016        public FileChooserBinding(FormModel model, String path, Class<?> class1, FileChooser field,
017                                  FileChooser.FileChooserMode mode, boolean useFile)
018        {
019            super(model, path, class1);
020            this.field = field;
021            this.field.setMode(mode);
022            this.useFile = useFile;
023        }
024    
025        protected void valueModelChanged(Object newValue)
026        {
027            if (!useFile)
028            {
029                field.setText((String) newValue);
030            }
031            else
032            {
033                field.setText(((java.io.File) newValue).getAbsolutePath());
034            }
035            readOnlyChanged();
036        }
037    
038        protected JComponent doBindControl()
039        {
040            if (!useFile && getValue() != null)
041            {
042                field.setText((String) getValue());
043            }
044            else if (useFile && getValue() != null)
045            {
046                field.setText(((java.io.File) getValue()).getAbsolutePath());
047            }
048            else
049            {
050                field.setText("");
051            }
052            field.addFocusListener(new FocusListener()
053            {
054    
055                public void focusGained(FocusEvent e)
056                {
057                }
058    
059                public void focusLost(FocusEvent e)
060                {
061                    if (field.isEditable())
062                    {
063                        if (useFile)
064                        {
065                            controlValueChanged(new java.io.File(field.getText()));
066                        }
067                        else
068                        {
069                            controlValueChanged(field.getText());
070                        }
071                    }
072                }
073            });
074            return field;
075        }
076    
077        protected void readOnlyChanged()
078        {
079            field.setEditable(isEnabled() && !isReadOnly());
080        }
081    
082        protected void enabledChanged()
083        {
084            field.setEnabled(isEnabled());
085            readOnlyChanged();
086        }
087    }