添加了列表对象的Property

This commit is contained in:
billkiller
2019-06-29 13:03:55 +08:00
parent 8b52a68083
commit e91322268a
22 changed files with 613 additions and 393 deletions

View File

@@ -1,38 +0,0 @@
package cn.edu.scau.biubiusuisui.entity;
import java.lang.reflect.Method;
/**
* @Author jack
* @Date:2019/6/28 10:03
*/
public class FXFieldMethodMapping {
private Method setMethod ;
private Method addMethod ;
private Method delMethod;
public Method getSetMethod() {
return setMethod;
}
public void setSetMethod(Method setMethod) {
this.setMethod = setMethod;
}
public Method getAddMethod() {
return addMethod;
}
public void setAddMethod(Method addMethod) {
this.addMethod = addMethod;
}
public Method getDelMethod() {
return delMethod;
}
public void setDelMethod(Method delMethod) {
this.delMethod = delMethod;
}
}

View File

@@ -0,0 +1,33 @@
package cn.edu.scau.biubiusuisui.entity;
import javafx.beans.property.Property;
import javafx.beans.value.ObservableValue;
import java.lang.reflect.Method;
/**
* @Author jack
* @Date:2019/6/28 10:03
*/
public class FXFieldViewFieldMapping {
private boolean readOnly;
private Class type;
public boolean isReadOnly() {
return readOnly;
}
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
public Class getType() {
return type;
}
public void setType(Class type) {
this.type = type;
}
}

View File

@@ -1,6 +1,7 @@
package cn.edu.scau.biubiusuisui.entity;
import cn.edu.scau.biubiusuisui.proxy.classProxy.FXEntityProxy;
import javafx.beans.property.Property;
import java.util.LinkedList;
import java.util.List;
@@ -19,9 +20,19 @@ public class FXPlusContext {
private FXPlusContext(){}
private static Map<String, List<FXBaseController>> controllerContext = new ConcurrentHashMap<>();
private static Map<String, List<FXBaseController>> controllerContext = new ConcurrentHashMap<>(); //FXController控制器注册表
private static Map<Object, FXEntityProxy> beanProxyMap = new ConcurrentHashMap<>();
private static Map<Object, FXEntityProxy> beanProxyMap = new ConcurrentHashMap<>(); // Object注册为FXEntityObject
public static Property getEntityPropertyByName(Object object, String fieldName){
FXEntityProxy fxEntityProxy = FXPlusContext.getProryByBeanObject(object);
if(fxEntityProxy == null){
return null;
}
return fxEntityProxy.getStringPropertyMap().get(fieldName);
}
private static Map<String, Object> session = new ConcurrentHashMap<>();
public static void addController(FXBaseController fxBaseController){
List<FXBaseController> controllers = controllerContext.get(fxBaseController.getName());
@@ -49,4 +60,20 @@ public class FXPlusContext {
public static void setBeanProxyMap(Map<Object, FXEntityProxy> beanProxyMap) {
FXPlusContext.beanProxyMap = beanProxyMap;
}
public static Map<String, List<FXBaseController>> getControllerContext() {
return controllerContext;
}
public static void setControllerContext(Map<String, List<FXBaseController>> controllerContext) {
FXPlusContext.controllerContext = controllerContext;
}
public static Map<String, Object> getSession() {
return session;
}
public static void setSession(Map<String, Object> session) {
FXPlusContext.session = session;
}
}

View File

@@ -1,17 +1,15 @@
package cn.edu.scau.biubiusuisui.example;
import cn.edu.scau.biubiusuisui.annotation.*;
import cn.edu.scau.biubiusuisui.annotation.FXController;
import cn.edu.scau.biubiusuisui.annotation.FXWindow;
import cn.edu.scau.biubiusuisui.entity.FXBaseController;
import cn.edu.scau.biubiusuisui.entity.FXPlusContext;
import cn.edu.scau.biubiusuisui.factory.FXEntityFactory;
import cn.edu.scau.biubiusuisui.factory.FXFactory;
import cn.edu.scau.biubiusuisui.proxy.classProxy.FXEntityProxy;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import java.net.URL;
import java.util.ResourceBundle;
@@ -25,31 +23,37 @@ import java.util.ResourceBundle;
public class MainController extends FXBaseController{
@FXML
Button btn;
private ResourceBundle resources;
@FXML
Label label;
private URL location;
@FXML
private Button addBtn;
@FXML
private Button delBtn;
@FXML
private ListView<String> list;
Student student;
int count = 1;
@Override
public void initialize() {
student = (Student) FXEntityFactory.getInstance().createJavaBeanProxy(Student.class); //工厂产生一个学生
student.setName("Jack"); //设置学生姓名
FXEntityProxy fxEntityProxy = FXPlusContext.getProryByBeanObject(student); //获取学生代理
Property nameProperty = fxEntityProxy.getPropertyByFieldName("name"); //获取Bean对应的Property
label.textProperty().bind(nameProperty); //属性绑定
@FXML
void addWord(ActionEvent event) {
System.out.println("click add");
student.addList("hello" );
}
@FXML
@FXSender
public String send(){
student.setName("Jack :" + count);
count++;
return "sending msg";
void delWord(ActionEvent event) {
student.delList("hello");
}
@Override
public void initialize() {
student = (Student) FXEntityFactory.createJavaBeanProxy(Student.class);
Property property = FXPlusContext.getEntityPropertyByName(student, "list");
list.itemsProperty().bind(property);
}
}

View File

@@ -3,10 +3,13 @@ package cn.edu.scau.biubiusuisui.example;
import cn.edu.scau.biubiusuisui.annotation.FXEntity;
import cn.edu.scau.biubiusuisui.annotation.FXField;
import java.util.ArrayList;
import java.util.List;
/**
* @Author jack
* @Date:2019/6/27 20:02
*/
@FXEntity
public class Student {
@@ -19,6 +22,9 @@ public class Student {
private String code;
@FXField
private List<String> list = new ArrayList<>();
public String getName() {
return name;
}
@@ -50,4 +56,11 @@ public class Student {
public void setCode(String code) {
this.code = code;
}
public void addList(String word){
list.add(word);
}
public void delList(String word){
list.remove(word);
}
}

View File

@@ -1,18 +1,18 @@
package cn.edu.scau.biubiusuisui.factory;
import cn.edu.scau.biubiusuisui.annotation.FXField;
import cn.edu.scau.biubiusuisui.entity.FXFieldViewFieldMapping;
import cn.edu.scau.biubiusuisui.entity.FXPlusContext;
import cn.edu.scau.biubiusuisui.function.DefaultEventFunction;
import cn.edu.scau.biubiusuisui.proxy.classProxy.FXEntityProxy;
import cn.edu.scau.biubiusuisui.utils.ClassUtils;
import javafx.beans.property.*;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
/**
* @Author jack
@@ -22,18 +22,7 @@ public class FXEntityFactory {
private FXEntityFactory(){}
private static FXEntityFactory instance = null;
ChangeListener propertyChangeEvent = new DefaultEventFunction();
public synchronized static FXEntityFactory getInstance() {
if(instance == null){
instance = new FXEntityFactory();
}
return instance;
}
public Object createJavaBeanProxy(Class clazz) {
public static Object createJavaBeanProxy(Class clazz) {
Object object = null;
try {
object = clazz.newInstance();
@@ -45,15 +34,13 @@ public class FXEntityFactory {
return createJavaBeanProxy(object);
}
public Object createJavaBeanProxy(Object object){
public static Object createJavaBeanProxy(Object object){
FXEntityProxy fxEntityProxy = new FXEntityProxy();
Object objectProxy = null;
try {
objectProxy = fxEntityProxy.getInstance(object);
Map<String, Property> stringPropertyMap = FXEntityFactory.getInstance().getEntityProperty(object,objectProxy);
fxEntityProxy.setStringPropertyMap(stringPropertyMap);
processFXEntityProxy(object,objectProxy,fxEntityProxy);
FXPlusContext.setProxyByBeanObject(objectProxy, fxEntityProxy);
} catch (IllegalAccessException e) {
e.printStackTrace();
@@ -62,47 +49,51 @@ public class FXEntityFactory {
}
public Map<String, Property> getEntityProperty(Object entity,Object proxy) throws IllegalAccessException {
public static void processFXEntityProxy(Object entity, Object proxy,FXEntityProxy fxEntityProxy) throws IllegalAccessException {
Map<String, Property> stringPropertyMap = new HashMap<>();
Map<String, FXFieldViewFieldMapping> stringFXFieldMethodMappingMap = new HashMap<>();
Field []fields = entity.getClass().getDeclaredFields();
for(Field field:fields){
Annotation annotation = ClassUtils.getAnnotationInList( FXField.class,field.getDeclaredAnnotations());
if(annotation != null){
Property property;
Property property = null;
field.setAccessible(true);
FXField fxField = (FXField)annotation;
FXFieldViewFieldMapping fieldMethodMapping = new FXFieldViewFieldMapping();
fieldMethodMapping.setReadOnly(fxField.readOnly());
fieldMethodMapping.setType(field.getType());
stringFXFieldMethodMappingMap.put(field.getName(), fieldMethodMapping);
if(field.get(entity) == null){
if(fxField.readOnly()){
property = getReadOnlyFieldDefalutProperty(field);
}else{
property = getFieldDefalutProperty(field);
}
}else{
if(fxField.readOnly()){
property = getReadOnlyProperty(entity,field);
}else{
property = getFieldProperty(entity, field);
}
}
if(property !=null) {
//添加时间;
property.addListener((object,oldVal,newVal)->{
try {
field.set(proxy, newVal);
} catch (IllegalAccessException e) {
e.printStackTrace();
if(!fxField.readOnly()) {
if(!field.getType().equals(List.class)) {
try {
field.set(proxy, newVal);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
});
stringPropertyMap.put(field.getName(), property);
}
}
}
return stringPropertyMap;
fxEntityProxy.setStringPropertyMap(stringPropertyMap);
fxEntityProxy.setStringFXFieldMethodMappingMap(stringFXFieldMethodMappingMap);
}
private static Property getFieldProperty(Object object,Field field) throws IllegalAccessException {
Class type = field.getType();
Object value = field.get(object);
@@ -119,6 +110,10 @@ public class FXEntityFactory {
property = new SimpleLongProperty((Long) value);
}else if(String.class.equals(type)){
property = new SimpleStringProperty((String) value);
}else if(List.class.equals(type)){
property = new SimpleListProperty(FXCollections.observableList((List)value));
}else if(Object.class.equals(type)){
property = new SimpleObjectProperty(value);
}
return property;
}
@@ -137,50 +132,12 @@ public class FXEntityFactory {
property = new SimpleLongProperty();
}else if(String.class.equals(type)){
property = new SimpleStringProperty();
}else if(List.class.equals(type)){
property = new SimpleListProperty();
}else if(Object.class.equals(type)){
property = new SimpleObjectProperty();
}
return property;
}
private static Property getReadOnlyFieldDefalutProperty(Field field) throws IllegalAccessException{
Class type = field.getType();
Property property = null;
if(Boolean.class.equals(type)){
property = new ReadOnlyBooleanWrapper();
}else if(Double.class.equals(type)){
property = new ReadOnlyDoubleWrapper();
}else if (Float.class.equals(type)){
property = new ReadOnlyFloatWrapper();
}else if(Integer.class.equals(type)){
property = new ReadOnlyIntegerWrapper();
}else if(Long.class.equals(type)){
property = new ReadOnlyLongWrapper();
}else if(String.class.equals(type)){
property = new ReadOnlyStringWrapper();
}
return property;
}
private static Property getReadOnlyProperty(Object object,Field field) throws IllegalAccessException {
Class type = field.getType();
Object value = field.get(object);
Property property = null;
if(Boolean.class.equals(type)){
property = new ReadOnlyBooleanWrapper((Boolean) value);
}else if(Double.class.equals(type)){
property = new ReadOnlyDoubleWrapper((Double) value);
}else if (Float.class.equals(type)){
property = new ReadOnlyFloatWrapper((Float) value);
}else if(Integer.class.equals(type)){
property = new ReadOnlyIntegerWrapper((Integer) value);
}else if(Long.class.equals(type)){
property = new ReadOnlyLongWrapper((Long) value);
}else if(String.class.equals(type)){
property = new ReadOnlyStringWrapper((String) value);
}
return property;
}
private static Method getFieldMethodMapping(Object object ,Field field){
Class clazz = object.getClass();
return null;
}
}

View File

@@ -114,4 +114,5 @@ public class FXFactory {
}
return fxControllerProxy;
}
}

View File

@@ -44,7 +44,6 @@ public class FXControllerProxy implements MethodInterceptor {
}else{
name += fxSender.name();
}
MessageQueue.getInstance().sendMsg(name,o1);
}
}

View File

@@ -1,15 +1,12 @@
package cn.edu.scau.biubiusuisui.proxy.classProxy;
import cn.edu.scau.biubiusuisui.entity.FXFieldMethodMapping;
import cn.edu.scau.biubiusuisui.entity.FXFieldViewFieldMapping;
import cn.edu.scau.biubiusuisui.utils.StringUtils;
import javafx.beans.property.ListProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.*;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;
@@ -22,7 +19,7 @@ public class FXEntityProxy implements MethodInterceptor {
Object target;
private Map<String, Property> stringPropertyMap;
private Map<String, FXFieldMethodMapping> stringFXFieldMethodMappingMap;
private Map<String, FXFieldViewFieldMapping> stringFXFieldMethodMappingMap;
public Object getInstance(Object target) {
this.target = target;
@@ -53,13 +50,33 @@ public class FXEntityProxy implements MethodInterceptor {
} else {
return o1;
}
SimpleStringProperty property = (SimpleStringProperty) stringPropertyMap.get(fieldName);
if (methodName.startsWith("set")) {
property.set((String) objects[0]);
FXFieldViewFieldMapping fieldMethodMapping = stringFXFieldMethodMappingMap.get(fieldName);
Property property = stringPropertyMap.get(fieldName);
if(fieldMethodMapping == null || property == null){
return o1;
}
Class type = fieldMethodMapping.getType();
if (methodName.startsWith("set")) {
if(Boolean.class.equals(type)){
((SimpleBooleanProperty)property).set((Boolean)objects[0]);
}else if(Double.class.equals(type)){
((SimpleDoubleProperty)property).set((Double)objects[0]);
}else if (Float.class.equals(type)){
((SimpleFloatProperty)property).set((Float) objects[0]);
}else if(Integer.class.equals(type)){
((SimpleIntegerProperty)property).set((Integer) objects[0]);
}else if(Long.class.equals(type)){
((SimpleLongProperty)property).set((Long)objects[0]);
}else if(String.class.equals(type)){
((SimpleStringProperty)property).set((String)objects[0]);
}
}else if (methodName.startsWith("add")){
((SimpleListProperty)(property)).add(objects[0]);
}else if(methodName.startsWith("del")){
((SimpleListProperty)(property)).remove(objects[0]);
}
//修改
return o1;
}
@@ -83,4 +100,12 @@ public class FXEntityProxy implements MethodInterceptor {
public void setStringPropertyMap(Map<String, Property> stringPropertyMap) {
this.stringPropertyMap = stringPropertyMap;
}
public Map<String, FXFieldViewFieldMapping> getStringFXFieldMethodMappingMap() {
return stringFXFieldMethodMappingMap;
}
public void setStringFXFieldMethodMappingMap(Map<String, FXFieldViewFieldMapping> stringFXFieldMethodMappingMap) {
this.stringFXFieldMethodMappingMap = stringFXFieldMethodMappingMap;
}
}