Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.properties;
import com.codename1.io.Util;
import com.codename1.ui.Button;
import com.codename1.ui.CheckBox;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.RadioButton;
import com.codename1.ui.TextArea;
import com.codename1.ui.TextComponent;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.events.DataChangedListener;
import com.codename1.ui.spinner.Picker;
import com.codename1.ui.table.AbstractTableModel;
import com.codename1.ui.table.TableModel;
import com.codename1.ui.util.EventDispatcher;
import com.codename1.ui.validation.Constraint;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
The binding framework can implicitly bind UI elements to properties, this allow seamless
* model to UI mapping. Most cases allow simple binding by just using the
* {@link #bind(com.codename1.properties.Property, com.codename1.ui.Component)} method
* to seamlessly update a property/component based on changes.
*
*
It contains the following base concepts:
*
*
*
{@link com.codename1.properties.UiBinding.ObjectConverter} - a converter converts
* from one type to another. E.g. if we want a {@link com.codename1.ui.TextArea} to map
* to an {@code Integer} property we'd use an
* {@link com.codename1.properties.UiBinding.IntegerConverter} to indicate the desired
* destination value.
*
{@link com.codename1.properties.UiBinding.ComponentAdapter} - takes two
* {@link com.codename1.properties.UiBinding.ObjectConverter} to convert to/from the
* component & property. It provides the API for event binding and value extraction/setting
* on the component.
*
{@link com.codename1.properties.UiBinding.Binding} - the commit mode
*
{@link #bind(com.codename1.properties.Property, com.codename1.ui.Component) -
* the {@code bind} helper methods allow us to bind a component easily without exposure
* to these complexities.
*
*
* @author Shai Almog
*/
public class UiBinding {
private boolean autoCommit = true;
/**
* Default value for auto-commit mode, in auto-commit mode changes to the component/property
* are instantly reflected otherwise {@link com.codename1.properties.UiBinding.CommitMode#commit()}
* should be invoked explicitly
* @param b true to enable auto commit mode
*/
public void setAutoCommit(boolean b) {
autoCommit = b;
}
/**
* Is auto-commit mode on by default see {@link #setAutoCommit(boolean)}
* @return true if auto-commit is on
*/
public boolean isAutoCommit() {
return autoCommit;
}
/**
*
Binding allows us to have commit/auto-commit mode. This allows changes to properties
* to reflect immediately or only when committed, e.g. if a {@code Form} has "OK" &
* "Cancel" buttons you might want to do a commit on OK. We also provide a "rollback" method to
* reset to the original property values.
*
* {@code UiBinding} has a boolean auto commit flag that can be toggled to set the default for new
* bindings.
*
*
* Binding also provides the ability to disengage a "binding" between a property and a UI component
*
*/
public abstract class Binding {
private boolean autoCommit = UiBinding.this.autoCommit;
/**
* Toggles auto-commit mode and overrides the {@code UiBinding} autocommit default.
* Autocommit instantly reflects changes to the property or component values.
* @param b true to enable auto-commit
*/
public void setAutoCommit(boolean b) {
autoCommit = b;
}
/**
* Gets the autocommit value see {@link #setAutoCommit(boolean)}
* @return true if autocommit is on
*/
public boolean isAutoCommit() {
return autoCommit;
}
/**
* Set the value from the component into the property, note that this will throw an exception if
* autocommit is on
*/
public abstract void commit();
/**
* Sets the value from the property into the component, note that this will throw an exception if
* autocommit is on
*/
public abstract void rollback();
/**
* Clears the listeners and disengages binding, this can be important for GC as binding
* can keep object references in RAM
*/
public abstract void disconnect();
}
/**
* Allows us to unbind the property from binding, this is equivalent to calling
* {@link com.codename1.properties.UiBinding.Binding#disconnect()} on all
* bindings...
*
* @param prop the property
*/
public static void unbind(PropertyBase prop) {
if(prop.getListeners() != null) {
for(Object l : prop.getListeners()) {
if(l instanceof Binding) {
((Binding)l).disconnect();
// prevent a concurrent modification exception by returning and recursing
unbind(prop);
return;
}
}
}
}
/**
* Unbinds all the properties within the business object
* @param po the business object
*/
public static void unbind(PropertyBusinessObject po) {
for(PropertyBase pb : po.getPropertyIndex()) {
unbind(pb);
}
}
/**
* Object converter can convert an object from one type to another e.g. a String to an integer an
* array to a list model. Use this object converter to keep source/values the same e.g. when converting
* using a {@link com.codename1.properties.UiBinding.TextAreaAdapter} to a String property.
*/
public static class ObjectConverter {
/**
* Converts an object of source type to the type matching this class, the default
* implementation does nothing and can be used as a stand-in
* @param source an object or null
* @return null or a new object instance
*/
public Object convert(Object source) {
return source;
}
}
/**
* Converts the source value to a String
*/
public static class StringConverter extends ObjectConverter {
@Override
public Object convert(Object source) {
if(source == null) {
return null;
}
return source.toString();
}
}
/**
* Converts the source value to an Integer
*/
public static class IntegerConverter extends ObjectConverter {
@Override
public Object convert(Object source) {
if(source == null) {
return null;
}
return Util.toIntValue(source);
}
}
/**
* Converts the source value to a Date
*/
public static class DateConverter extends ObjectConverter {
@Override
public Object convert(Object source) {
if(source == null) {
return null;
}
if(source instanceof Date) {
return (Date)source;
}
return new Date(Util.toLongValue(source));
}
}
/**
* Converts the source value to a Long
*/
public static class LongConverter extends ObjectConverter {
@Override
public Object convert(Object source) {
if(source == null) {
return null;
}
return Util.toLongValue(source);
}
}
/**
* Converts the source value to a Float
*/
public static class FloatConverter extends ObjectConverter {
@Override
public Object convert(Object source) {
if(source == null) {
return null;
}
return Util.toFloatValue(source);
}
}
/**
* Converts the source value to a Double
*/
public static class DoubleConverter extends ObjectConverter {
@Override
public Object convert(Object source) {
if(source == null) {
return null;
}
return Util.toDoubleValue(source);
}
}
/**
* Converts the source value to a Boolean
*/
public static class BooleanConverter extends ObjectConverter {
@Override
public Object convert(Object source) {
if(source == null) {
return null;
}
if(source instanceof Boolean) {
return ((Boolean)source).booleanValue();
}
if(source instanceof String) {
String s = ((String)source).toLowerCase();
return s.indexOf("true") > 0 || s.indexOf("yes") > 0 || s.indexOf("1") > 0;
}
return Util.toIntValue(source) > 0;
}
}
/**
* Maps values to other values for conversion in a similar way to a Map this is pretty
* useful for API's like picker where we have a list of Strings and we might want a list
* of other objects to match every string
*/
public static class MappingConverter extends ObjectConverter {
private Map