001    package org.springframework.richclient.form.binding.swing;
002    
003    import org.springframework.richclient.form.binding.Binder;
004    import org.springframework.richclient.form.binding.Binding;
005    import org.springframework.richclient.components.FileChooser;
006    import org.springframework.binding.form.FormModel;
007    import org.springframework.util.Assert;
008    
009    import javax.swing.*;
010    import java.util.Map;
011    
012    public class FileChooserBinder implements Binder
013    {
014        public static final String BINDING_CLIENT_PROPERTY_KEY = "binding";
015    
016        private boolean useFile = false;
017    
018        private FileChooser.FileChooserMode mode = FileChooser.FileChooserMode.FILE;
019    
020        public FileChooserBinder()
021        {
022        }
023    
024        @SuppressWarnings("unchecked")
025        protected JComponent createControl(Map context)
026        {
027            return new FileChooser();
028        }
029    
030        @SuppressWarnings("unchecked")
031        protected Binding doBind(JComponent control, FormModel formModel,
032                                 String formPropertyPath, Map context)
033        {
034            final FileChooser chooser = (FileChooser) control;
035            if (useFile)
036            {
037                return new FileChooserBinding(formModel, formPropertyPath,
038                        java.io.File.class, chooser, mode, this.useFile);
039            }
040            else
041            {
042                return new FileChooserBinding(formModel, formPropertyPath,
043                        String.class, chooser, mode, this.useFile);
044            }
045        }
046    
047        @SuppressWarnings("unchecked")
048        public Binding bind(FormModel formModel, String formPropertyPath,
049                            Map context)
050        {
051            JComponent control = createControl(context);
052            Assert.notNull(control,
053                    "This binder does not support creating a default control.");
054            return bind(control, formModel, formPropertyPath, context);
055        }
056    
057        @SuppressWarnings("unchecked")
058        public Binding bind(JComponent control, FormModel formModel,
059                            String formPropertyPath, Map context)
060        {
061            Binding binding = (Binding) control
062                    .getClientProperty(BINDING_CLIENT_PROPERTY_KEY);
063            if (binding != null)
064            {
065                throw new IllegalStateException(
066                        "Component is already bound to property: "
067                                + binding.getProperty());
068            }
069            binding = doBind(control, formModel, formPropertyPath, context);
070            control.putClientProperty(BINDING_CLIENT_PROPERTY_KEY, binding);
071            return binding;
072        }
073    
074        /**
075         * @param useFile <code>true</code> when used with {@link java.io.File}, <code>false</code> when used with {@link
076         *                java.lang.String}
077         */
078        public void setUseFile(boolean useFile)
079        {
080            this.useFile = useFile;
081        }
082    
083        /** @return <code>true</code> when the binder uses {@link java.io.File}, otherwise false; */
084        public boolean isUseFile()
085        {
086            return useFile;
087        }
088    
089        /**
090         * @param mode Mode in which the control is to be used: <br/> <ul> <li>FileChooserMode.FILE: choose files</li>
091         *             <li>FileChooserMode.FOLDER: choose folders</li> </ul>
092         */
093        public void setMode(FileChooser.FileChooserMode mode)
094        {
095            this.mode = mode;
096        }
097    
098        /** @return The filechooser mode */
099        public FileChooser.FileChooserMode getMode()
100        {
101            return mode;
102        }
103    
104        protected Class<?> getPropertyType(FormModel formModel, String formPropertyPath)
105        {
106            return formModel.getFieldMetadata(formPropertyPath).getPropertyType();
107        }
108    }