Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2009-2011 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jdal.swing.bind;
 
 
 import java.util.Map;
 import java.util.Set;
 
Do an automatic binding of a view using reflection. Bind controls with name equals to property names in model.

Author(s):
Jose Luis Martin
Since:
1.1
See also:
org.jdal.ui.Binder
 
 public class AutoBinder<T> implements Binder<T> {

Log
 
 	private static final Log log = LogFactory.getLog(AutoBinder.class);
View to bind on
 
 	private Object view;
Control accessor factory to use for create control accessors
 
PropertyAccessor for access view fields
 
PropertyAccessor for access model properties
 
Set with property names to ingnore on binding commands
 
 	private Set<StringignoredProperties = new HashSet<String>();
Binded model
 
 	private T model;
Command to execute on refresh
 
Command to execute on update
 
 	private UpdateCommand updateCommand = new UpdateCommand();
Process binding errors
 
binding errors
 
Hold property name -> ControlAccessor mapping
 
Create an AutoBinder for a View

Parameters:
view View to bind.
 
 	public AutoBinder(ModelHolder<T> view) {
 		this(viewview.getModel());
 	}

Create a Binder for view and model

Parameters:
view
model
 
 	public AutoBinder(Object view, T model) {
 		this. = view;
 		this. = model;
 		 = new BeanPropertyBindingResult(model"model");
 	}
 	
 	public void bind(String viewFieldString propertyNamethrows UndefinedAccessorException {
 		Object control = .getPropertyValue(propertyName);
		if (control != null) {
			if (accessor != null) {
				.put(propertyNameaccessor);
			}
			else {
				String msg = "Not found ControlAcessor for control class [" + 
						control.getClass().getName() +  "]";
			}
		}
	}

	public void refresh() {
	}

	public void update() {
	}

Execute BinderCommand (update or refresh) for all model properties

Parameters:
command Command to execute.
	private void  executeBinderCommand(BinderCommand command) {
		// iterate on model properties
			String propertyName = pd.getName();
			if ( !.contains(propertyName)) {
				ControlAccessor controlAccessor = getControlAccessor(propertyName);
				if (controlAccessor != null)
					command.execute(controlAccessorpropertyName);
			}
		}
	}

Gets control accessor

Parameters:
control to get accessor
Returns:
accessor or null if none found.
		// try map first
			return .get(name);
		// try matching view property
		ControlAccessor accessor = null;
			if (control != null) {
					.debug("Found control: " + control.getClass().getSimpleName() + " for property: " + name);
			}
		}
		return accessor;
	}

	public T getModel() {
		return ;
	}

	public void setModel(T model) {
		this. = model;
	}

Returns:
the controlAccessorFactory
		if ( == null)
	}

Parameters:
controlAccessorFactory the controlAccessorFactory to set
	public void setControlAccessorFactory(ControlAccessorFactory controlAccessorFactory) {
		this. = controlAccessorFactory;
	}

Add a property name to ignore on binding.

Parameters:
propertyName property name to ignore
	public void ignoreProperty(String propertyName) {
		.add(propertyName);
	}


Returns:
the ignoredProperties
	}

Parameters:
ignoredProperties the ignoredProperties to set
	public void setIgnoredProperties(Set<StringignoredProperties) {
		this. = ignoredProperties;
	}

Add a Collection of property names to ignore on binding

Parameters:
c Collection of property names.
	public void ignoreProperties(Collection<? extends Stringc) {
	}

Return the Binding result

Returns:
the binding result
	}

Binder Command Callback

Author(s):
Jose Luis Martin - (jlm@joseluismartin.info)
	interface BinderCommand {
		void execute(ControlAccessor accessorString propertyName);
	}

Update Command
	class UpdateCommand implements  BinderCommand {
		public void execute(ControlAccessor controlAccessorString name) {
			try {
			}
			catch (PropertyAccessException pae) { 
			}
		}
	}

Refresh Command
	class RefreshCommand implements BinderCommand {
		public void execute(ControlAccessor controlAccessorString name) {
		}
	}
New to GrepCode? Check out our FAQ X