Search in sources :

Example 1 with ObservableList

use of com.canoo.platform.remoting.ObservableList in project dolphin-platform by canoo.

the class DefaultJavaFXListBinder method to.

@Override
public <T> Binding to(final ObservableList<T> dolphinList, final Function<? super T, ? extends S> converter) {
    Assert.requireNonNull(dolphinList, "dolphinList");
    Assert.requireNonNull(converter, "converter");
    if (boundLists.containsKey(list)) {
        throw new UnsupportedOperationException("A JavaFX list can only be bound to one Dolphin Platform list!");
    }
    boundLists.put(list, list);
    final InternalListChangeListener<T> listChangeListener = new InternalListChangeListener<>(converter);
    final Subscription subscription = dolphinList.onChanged(listChangeListener);
    list.setAll(dolphinList.stream().map(converter).collect(Collectors.toList()));
    ListChangeListener<S> readOnlyListener = c -> {
        if (!listChangeListener.onChange) {
            throw new UnsupportedOperationException("A JavaFX list that is bound to a dolphin list can only be modified by the binding!");
        }
    };
    list.addListener(readOnlyListener);
    return () -> {
        subscription.unsubscribe();
        list.removeListener(readOnlyListener);
        boundLists.remove(list);
    };
}
Also used : Binding(com.canoo.platform.core.functional.Binding) Subscription(com.canoo.platform.core.functional.Subscription) IdentityHashMap(java.util.IdentityHashMap) ListChangeListener(javafx.collections.ListChangeListener) ObservableList(com.canoo.platform.remoting.ObservableList) Assert(com.canoo.dp.impl.platform.core.Assert) JavaFXListBinder(com.canoo.platform.remoting.client.javafx.binding.JavaFXListBinder) API(org.apiguardian.api.API) ListChangeEvent(com.canoo.platform.remoting.ListChangeEvent) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) Subscription(com.canoo.platform.core.functional.Subscription)

Example 2 with ObservableList

use of com.canoo.platform.remoting.ObservableList in project dolphin-platform by canoo.

the class GarbageCollector method gc.

/**
 * Calling this method triggers the garbage collection. For all dolphin beans (see {@link RemotingBean}) that
 * are not referenced by a root bean (see {@link RemotingModel}) the defined {@link GarbageCollectionCallback} (see constructor)
 * will be called.
 */
public synchronized void gc() {
    if (!configuration.isUseGc()) {
        LOG.trace("GC deactivated, no beans will be removed!");
        return;
    }
    LOG.trace("Garbage collection started! GC will remove {} beans!", removeOnGC.size());
    onRemoveCallback.onReject(removeOnGC.keySet());
    for (Map.Entry<Instance, Object> entry : removeOnGC.entrySet()) {
        Instance removedInstance = entry.getKey();
        for (Property property : removedInstance.getProperties()) {
            propertyToParent.remove(property);
        }
        for (ObservableList list : removedInstance.getLists()) {
            listToParent.remove(list);
        }
        allInstances.remove(entry.getValue());
    }
    removedBeansCount = removedBeansCount + removeOnGC.size();
    removeOnGC.clear();
    gcCalls = gcCalls + 1;
    LOG.trace("Garbage collection done! GC currently manages {} referenced beans!", allInstances.size());
}
Also used : ObservableList(com.canoo.platform.remoting.ObservableList) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map) Property(com.canoo.platform.remoting.Property)

Example 3 with ObservableList

use of com.canoo.platform.remoting.ObservableList in project dolphin-platform by canoo.

the class GarbageCollector method getAllLists.

private IdentitySet<ObservableList> getAllLists(Object bean) {
    IdentitySet<ObservableList> ret = new IdentitySet<>();
    List<Field> fields = listFieldCache.get(bean.getClass());
    if (fields == null) {
        fields = new ArrayList<>();
        for (Field field : ReflectionHelper.getInheritedDeclaredFields(bean.getClass())) {
            if (ObservableList.class.isAssignableFrom(field.getType())) {
                fields.add(field);
            }
        }
        listFieldCache.put(bean.getClass(), fields);
    }
    for (Field field : fields) {
        ret.add((ObservableList) ReflectionHelper.getPrivileged(field, bean));
    }
    return ret;
}
Also used : Field(java.lang.reflect.Field) ObservableList(com.canoo.platform.remoting.ObservableList) IdentitySet(com.canoo.dp.impl.platform.core.IdentitySet)

Example 4 with ObservableList

use of com.canoo.platform.remoting.ObservableList in project dolphin-platform by canoo.

the class GarbageCollector method removeFromGC.

private void removeFromGC(Instance instance) {
    LOG.trace("Bean of type {} removed from GC and will not be removed on next GC run", instance.getBean().getClass());
    Object removed = removeOnGC.remove(instance);
    LOG.trace("GC will remove {} beans at next GC run", removeOnGC.size());
    if (removed != null) {
        for (Property property : instance.getProperties()) {
            Object value = property.get();
            if (value != null && DolphinUtils.isDolphinBean(value.getClass())) {
                Instance childInstance = getInstance(value);
                removeFromGC(childInstance);
            }
        }
        for (ObservableList list : instance.getLists()) {
            for (Object value : list) {
                if (value != null && DolphinUtils.isDolphinBean(value.getClass())) {
                    Instance childInstance = getInstance(value);
                    removeFromGC(childInstance);
                }
            }
        }
    }
}
Also used : ObservableList(com.canoo.platform.remoting.ObservableList) Property(com.canoo.platform.remoting.Property)

Example 5 with ObservableList

use of com.canoo.platform.remoting.ObservableList in project dolphin-platform by canoo.

the class ModelJsonSerializer method toJson.

public static JsonObject toJson(Object dolphinModel) {
    if (dolphinModel == null) {
        return null;
    }
    JsonObject jsonObject = new JsonObject();
    for (Field field : ReflectionHelper.getInheritedDeclaredFields(dolphinModel.getClass())) {
        if (DolphinUtils.isProperty(field.getType())) {
            Property property = (Property) ReflectionHelper.getPrivileged(field, dolphinModel);
            Object value = property.get();
            if (value == null) {
                jsonObject.add(field.getName(), null);
            } else if (Number.class.isAssignableFrom(value.getClass()) || Double.TYPE.isAssignableFrom(value.getClass()) || Float.TYPE.isAssignableFrom(value.getClass()) || Long.TYPE.isAssignableFrom(value.getClass()) || Integer.TYPE.isAssignableFrom(value.getClass())) {
                jsonObject.add(field.getName(), new JsonPrimitive((Number) value));
            } else if (String.class.isAssignableFrom(value.getClass())) {
                jsonObject.add(field.getName(), new JsonPrimitive((String) value));
            } else if (Boolean.class.isAssignableFrom(value.getClass()) || Boolean.TYPE.isAssignableFrom(value.getClass())) {
                jsonObject.add(field.getName(), new JsonPrimitive((Boolean) value));
            } else {
                jsonObject.add(field.getName(), toJson(value));
            }
        } else if (DolphinUtils.isObservableList(field.getType())) {
            ObservableList list = (ObservableList) ReflectionHelper.getPrivileged(field, dolphinModel);
            JsonArray jsonArray = new JsonArray();
            for (Object value : list) {
                if (value == null) {
                // TODO
                // jsonArray.add(null);
                } else if (Number.class.isAssignableFrom(value.getClass()) || Double.TYPE.isAssignableFrom(value.getClass()) || Float.TYPE.isAssignableFrom(value.getClass()) || Long.TYPE.isAssignableFrom(value.getClass()) || Integer.TYPE.isAssignableFrom(value.getClass())) {
                    jsonArray.add(new JsonPrimitive((Number) value));
                } else if (String.class.isAssignableFrom(value.getClass())) {
                    jsonArray.add(new JsonPrimitive((String) value));
                } else if (Boolean.class.isAssignableFrom(value.getClass()) || Boolean.TYPE.isAssignableFrom(value.getClass())) {
                    jsonArray.add(new JsonPrimitive((Boolean) value));
                } else {
                    jsonArray.add(toJson(value));
                }
            }
            jsonObject.add(field.getName(), jsonArray);
        }
    }
    return jsonObject;
}
Also used : JsonArray(com.google.gson.JsonArray) Field(java.lang.reflect.Field) JsonPrimitive(com.google.gson.JsonPrimitive) ObservableList(com.canoo.platform.remoting.ObservableList) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) Property(com.canoo.platform.remoting.Property)

Aggregations

ObservableList (com.canoo.platform.remoting.ObservableList)8 Property (com.canoo.platform.remoting.Property)6 Field (java.lang.reflect.Field)2 IdentityHashMap (java.util.IdentityHashMap)2 Assert (com.canoo.dp.impl.platform.core.Assert)1 IdentitySet (com.canoo.dp.impl.platform.core.IdentitySet)1 Binding (com.canoo.platform.core.functional.Binding)1 Subscription (com.canoo.platform.core.functional.Subscription)1 ListChangeEvent (com.canoo.platform.remoting.ListChangeEvent)1 JavaFXListBinder (com.canoo.platform.remoting.client.javafx.binding.JavaFXListBinder)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 ListChangeListener (javafx.collections.ListChangeListener)1 API (org.apiguardian.api.API)1 INTERNAL (org.apiguardian.api.API.Status.INTERNAL)1