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);
}
}
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;
}
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;
}
});
}
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();
}
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;
}
Aggregations