001 /* 002 * Copyright 2002-2004 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.form.binding.swing; 017 018 import org.springframework.core.enums.LabeledEnum; 019 import org.springframework.richclient.form.binding.Binder; 020 import org.springframework.richclient.form.binding.support.AbstractBinderSelectionStrategy; 021 022 import javax.swing.*; 023 import javax.swing.text.JTextComponent; 024 import java.util.HashMap; 025 import java.util.Map; 026 027 /** 028 * @author Oliver Hutchison 029 */ 030 public class SwingBinderSelectionStrategy extends AbstractBinderSelectionStrategy 031 { 032 033 private Map<String, Binder> idBoundBinders = new HashMap<String, Binder>(); 034 035 /* 036 * Default constructor. 037 */ 038 public SwingBinderSelectionStrategy() 039 { 040 super(JTextField.class); 041 } 042 043 /** 044 * Set a map with predefined binders 045 * 046 * @param binders 047 */ 048 public void setIdBoundBinders(Map binders) 049 { 050 this.idBoundBinders = binders; 051 } 052 053 /** 054 * Register additional map of id bound binders 055 * 056 * @param binders 057 */ 058 public void registerIdBoundBinders(Map<String, Binder> binders) 059 { 060 idBoundBinders.putAll(binders); 061 } 062 063 /** 064 * Try to find a binder with a specified id. If no binder is found, try 065 * to locate it into the application context, check whether it's a binder and 066 * add it to the id bound binder map. 067 * 068 * @param id Id of the binder 069 * @return Binder or <code>null</code> if not found. 070 */ 071 public Binder getIdBoundBinder(String id) 072 { 073 Binder binder = idBoundBinders.get(id); 074 if (binder == null) // try to locate the binder bean 075 { 076 Object binderBean = getApplicationContext().getBean(id); 077 if (binderBean instanceof Binder) 078 { 079 if (binderBean != null) 080 { 081 idBoundBinders.put(id, (Binder) binderBean); 082 binder = (Binder) binderBean; 083 } 084 } 085 else 086 { 087 throw new IllegalArgumentException("Bean '" + id + "' was found, but was not a binder"); 088 } 089 } 090 return binder; 091 } 092 093 /** 094 * Select a binder based on a control type 095 * 096 * @param controlType Type of control 097 * @return The binder for that control 098 */ 099 public Binder selectBinder(Class controlType) 100 { 101 return findBinderByControlType(controlType); 102 } 103 104 protected void registerDefaultBinders() 105 { 106 registerBinderForPropertyType(String.class, new TextComponentBinder()); 107 registerBinderForPropertyType(boolean.class, new CheckBoxBinder()); 108 registerBinderForPropertyType(Boolean.class, new CheckBoxBinder()); 109 registerBinderForPropertyType(LabeledEnum.class, new LabeledEnumComboBoxBinder()); 110 registerBinderForControlType(JTextComponent.class, new TextComponentBinder()); 111 registerBinderForControlType(JFormattedTextField.class, new FormattedTextFieldBinder(null)); 112 registerBinderForControlType(JTextArea.class, new TextAreaBinder()); 113 registerBinderForControlType(JToggleButton.class, new ToggleButtonBinder()); 114 registerBinderForControlType(JCheckBox.class, new CheckBoxBinder()); 115 registerBinderForControlType(JComboBox.class, new ComboBoxBinder()); 116 registerBinderForControlType(JList.class, new ListBinder()); 117 registerBinderForControlType(JLabel.class, new LabelBinder()); 118 registerBinderForControlType(JScrollPane.class, new ScrollPaneBinder(this, JTextArea.class)); 119 } 120 }