Search in sources :

Example 1 with Instance

use of com.haulmont.chile.core.model.Instance in project cuba by cuba-platform.

the class InstanceUtils method getInstanceName.

/**
 * @return Instance name as defined by {@link com.haulmont.chile.core.annotations.NamePattern}
 * or <code>toString()</code>.
 * @param instance  instance
 */
public static String getInstanceName(Instance instance) {
    checkNotNullArgument(instance, "instance is null");
    NamePatternRec rec = parseNamePattern(instance.getMetaClass());
    if (rec == null) {
        return instance.toString();
    } else {
        if (rec.methodName != null) {
            try {
                Method method = instance.getClass().getMethod(rec.methodName);
                Object result = method.invoke(instance);
                return (String) result;
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                throw new RuntimeException("Error getting instance name", e);
            }
        }
        // lazy initialized messages, used only for enum values
        Messages messages = null;
        Object[] values = new Object[rec.fields.length];
        for (int i = 0; i < rec.fields.length; i++) {
            Object value = instance.getValue(rec.fields[i]);
            if (value == null) {
                values[i] = "";
            } else if (value instanceof Instance) {
                values[i] = getInstanceName((Instance) value);
            } else if (value instanceof EnumClass) {
                if (messages == null) {
                    messages = AppBeans.get(Messages.NAME);
                }
                values[i] = messages.getMessage((Enum) value);
            } else {
                values[i] = value;
            }
        }
        return String.format(rec.format, values);
    }
}
Also used : Messages(com.haulmont.cuba.core.global.Messages) Instance(com.haulmont.chile.core.model.Instance) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass)

Example 2 with Instance

use of com.haulmont.chile.core.model.Instance in project cuba by cuba-platform.

the class TableModelAdapter method getValueExIgnoreUnfetched.

protected Object getValueExIgnoreUnfetched(Instance instance, String[] properties) {
    Object currentValue = null;
    Instance currentInstance = instance;
    for (String property : properties) {
        if (currentInstance == null) {
            break;
        }
        if (!PersistenceHelper.isLoaded(currentInstance, property)) {
            LoggerFactory.getLogger(TableModelAdapter.class).warn("Ignored unfetched attribute {} of instance {} in Table cell", property, currentInstance);
            return null;
        }
        currentValue = currentInstance.getValue(property);
        if (currentValue == null) {
            break;
        }
        currentInstance = currentValue instanceof Instance ? (Instance) currentValue : null;
    }
    return currentValue;
}
Also used : Instance(com.haulmont.chile.core.model.Instance)

Example 3 with Instance

use of com.haulmont.chile.core.model.Instance in project cuba by cuba-platform.

the class LinkColumnHelper method initColumn.

public static void initColumn(Table table, final String propertyName, final Handler handler) {
    final ComponentsFactory componentsFactory = AppBeans.get(ComponentsFactory.NAME);
    table.addGeneratedColumn(propertyName, new Table.ColumnGenerator() {

        @Override
        public Component generateCell(final Entity entity) {
            // //process properties like building.house.room
            String[] props = propertyName.split("\\.");
            Instance nestedEntity = entity;
            for (int i = 0; i < props.length - 1; i++) {
                nestedEntity = nestedEntity.getValue(props[i]);
                if (nestedEntity == null) {
                    break;
                }
            }
            final Object value = (nestedEntity == null) ? null : nestedEntity.getValue(props[props.length - 1]);
            if (value != null) {
                Button button = componentsFactory.createComponent(Button.class);
                button.setStyleName("link");
                button.setAction(new AbstractAction("open") {

                    @Override
                    public void actionPerform(Component component) {
                        handler.onClick(entity);
                    }

                    @Override
                    public String getCaption() {
                        String str;
                        Datatype datatype = Datatypes.get(value.getClass());
                        if (datatype != null) {
                            UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
                            str = datatype.format(value, sessionSource.getLocale());
                        } else {
                            str = value.toString();
                        }
                        return str;
                    }
                });
                button.setStyleName("link");
                return button;
            }
            return null;
        }
    });
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) Table(com.haulmont.cuba.gui.components.Table) Instance(com.haulmont.chile.core.model.Instance) Datatype(com.haulmont.chile.core.datatypes.Datatype) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) Button(com.haulmont.cuba.gui.components.Button) Component(com.haulmont.cuba.gui.components.Component) AbstractAction(com.haulmont.cuba.gui.components.AbstractAction)

Example 4 with Instance

use of com.haulmont.chile.core.model.Instance in project cuba by cuba-platform.

the class DesktopPickerField method updateComponent.

protected void updateComponent(Object value) {
    String text;
    if (value == null) {
        text = "";
    } else {
        if (value instanceof Instance) {
            if (captionMode.equals(CaptionMode.ITEM)) {
                text = ((Instance) value).getInstanceName();
            } else {
                Object propertyValue = ((Instance) value).getValue(captionProperty);
                MetaClass valueClass = metadata.getClassNN(value.getClass());
                MetaProperty property = valueClass.getProperty(captionProperty);
                text = metadataTools.format(propertyValue, property);
            }
        } else {
            text = value.toString();
        }
    }
    impl.setValue(text);
    prevTextValue = text;
    updateMissingValueState();
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) Instance(com.haulmont.chile.core.model.Instance) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 5 with Instance

use of com.haulmont.chile.core.model.Instance in project cuba by cuba-platform.

the class InstanceUtils method getValueEx.

/**
 * Get value of an attribute according to the rules described in {@link Instance#getValueEx(String)}.
 * @param instance      instance
 * @param properties    path to the attribute
 * @return              attribute value
 */
@Nullable
public static <T> T getValueEx(Instance instance, String[] properties) {
    if (properties == null) {
        return null;
    }
    Object currentValue = null;
    Instance currentInstance = instance;
    for (String property : properties) {
        if (currentInstance == null)
            break;
        currentValue = currentInstance.getValue(property);
        if (currentValue == null)
            break;
        currentInstance = currentValue instanceof Instance ? (Instance) currentValue : null;
    }
    // noinspection unchecked
    return (T) currentValue;
}
Also used : Instance(com.haulmont.chile.core.model.Instance) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) Nullable(javax.annotation.Nullable)

Aggregations

Instance (com.haulmont.chile.core.model.Instance)18 MetaClass (com.haulmont.chile.core.model.MetaClass)4 MetaProperty (com.haulmont.chile.core.model.MetaProperty)4 AbstractInstance (com.haulmont.chile.core.model.impl.AbstractInstance)4 Entity (com.haulmont.cuba.core.entity.Entity)4 Table (com.haulmont.cuba.gui.components.Table)3 Nullable (javax.annotation.Nullable)3 Logger (org.slf4j.Logger)3 Datatype (com.haulmont.chile.core.datatypes.Datatype)2 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)2 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)2 Messages (com.haulmont.cuba.core.global.Messages)2 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 java.util (java.util)2 Consumer (java.util.function.Consumer)2 LoggerFactory (org.slf4j.LoggerFactory)2 Sets (com.google.common.collect.Sets)1 EventHub (com.haulmont.bali.events.EventHub)1 Subscription (com.haulmont.bali.events.Subscription)1