1 56 package org.objectstyle.cayenne.swing; 57 58 import java.util.HashMap ; 59 import java.util.Map ; 60 61 import javax.swing.AbstractButton ; 62 import javax.swing.JComboBox ; 63 import javax.swing.JTextArea ; 64 import javax.swing.JTextField ; 65 66 72 public class BindingBuilder { 73 74 protected BindingFactory factory; 75 protected BindingDelegate delegate; 76 protected Object context; 77 protected Map actionsMap; 78 79 83 public BindingBuilder(BindingFactory factory, Object context) { 84 this.factory = factory; 85 this.context = context; 86 } 87 88 public BindingDelegate getDelegate() { 89 return delegate; 90 } 91 92 96 public void setDelegate(BindingDelegate delegate) { 97 this.delegate = delegate; 98 } 99 100 public Object getContext() { 101 return context; 102 } 103 104 108 public void setContext(Object context) { 109 this.context = context; 110 } 111 112 public BindingFactory getFactory() { 113 return factory; 114 } 115 116 121 public ObjectBinding bindToProperty( 122 BoundComponent component, 123 String property, 124 String boundProperty) { 125 ObjectBinding binding = factory 126 .bindToProperty(component, property, boundProperty); 127 return initBinding(binding, delegate); 128 } 129 130 public ObjectBinding bindToStateChange(AbstractButton button, String property) { 131 ObjectBinding binding = factory.bindToStateChange(button, property); 132 return initBinding(binding, delegate); 133 } 134 135 public ObjectBinding bindToStateChangeAndAction(AbstractButton button, String property, String action) { 136 ObjectBinding binding = factory.bindToStateChange(button, property); 137 return initBinding(binding, getActionDelegate(action)); 138 } 139 140 public ObjectBinding bindToAction(AbstractButton button, String action) { 141 ObjectBinding binding = factory.bindToAction(button, action); 142 return initBinding(binding, delegate); 143 } 144 145 public ObjectBinding bindToComboSelection(JComboBox component, String property) { 146 ObjectBinding binding = factory.bindToComboSelection(component, property, null); 147 return initBinding(binding, delegate); 148 } 149 150 public ObjectBinding bindToComboSelection( 151 JComboBox component, 152 String property, 153 String noSelectionValue) { 154 ObjectBinding binding = factory.bindToComboSelection(component, 155 property, 156 noSelectionValue); 157 return initBinding(binding, delegate); 158 } 159 160 public ObjectBinding bindToComboSelection( 161 JComboBox component, 162 String property, 163 String action, 164 String noSelectionValue) { 165 ObjectBinding binding = factory.bindToComboSelection(component, 166 property, 167 noSelectionValue); 168 return initBinding(binding, getActionDelegate(action)); 169 } 170 171 public ObjectBinding bindToTextArea(JTextArea component, String property) { 172 ObjectBinding binding = factory.bindToTextArea(component, property); 173 return initBinding(binding, delegate); 174 } 175 176 public ObjectBinding bindToTextField(JTextField component, String property) { 177 ObjectBinding binding = factory.bindToTextField(component, property); 178 return initBinding(binding, delegate); 179 } 180 181 protected ObjectBinding initBinding(ObjectBinding binding, BindingDelegate delegate) { 182 binding.setDelegate(delegate); 183 binding.setContext(context); 184 return binding; 185 } 186 187 protected BindingDelegate getActionDelegate(String action) { 188 BindingDelegate delegate = null; 189 190 if (actionsMap == null) { 191 actionsMap = new HashMap (); 192 } 193 else { 194 delegate = (BindingDelegate) actionsMap.get(action); 195 } 196 197 if (delegate == null) { 198 delegate = new ActionDelegate(action); 199 actionsMap.put(action, delegate); 200 } 201 202 return delegate; 203 } 204 } | Popular Tags |