use of com.codename1.rad.models.Property in project CodeRAD by shannah.
the class Properties method getName.
public static String getName(Property p) {
Name n = (Name) p.getAttribute(Name.class);
String nameStr = null;
if (n != null) {
nameStr = n.getValue();
if (nameStr != null && !nameStr.isEmpty()) {
return nameStr;
}
}
Tags tags = (Tags) p.getAttribute(Tags.class);
if (tags != null) {
for (Tag t : tags) {
String tname = t.getName();
if (tname != null && !tname.isEmpty()) {
return tname;
}
}
}
return p.toString();
}
use of com.codename1.rad.models.Property in project CodeRAD by shannah.
the class ButtonListPropertyView method updateMultiSelectionModel.
private void updateMultiSelectionModel() {
MultipleSelectionListModel model = getComponent().getMultiListModel();
Entity e = getPropertySelector().getLeafEntity();
Property p = getPropertySelector().getLeafProperty();
Object val = p.getValue(e.getEntity());
int len = model.getSize();
if (getPropertySelector().isEmpty()) {
if (model.getSelectedIndices().length > 0) {
model.setSelectedIndices(new int[0]);
}
} else {
List<Integer> selectedIndices = new ArrayList<>();
if (p.getContentType().isEntityList()) {
// Property contains an entity list
List selectedObjects = new ArrayList();
List<String> selectedIds = new ArrayList<>();
boolean useIds = true;
EntityList el = e.getEntity().getEntityList(p);
for (Object obj : el) {
selectedObjects.add(obj);
if (obj instanceof Entity) {
String id = ((Entity) obj).getEntity().getText(Thing.identifier);
if (id == null) {
useIds = false;
break;
}
selectedIds.add(id);
} else {
useIds = false;
}
}
if (useIds) {
// We will useIds to match rows in options list with rows in entity list
for (int i = 0; i < len; i++) {
Object rowVal = model.getItemAt(i);
if (rowVal instanceof Entity) {
Entity rowEnt = (Entity) rowVal;
String rowId = rowEnt.getEntity().getText(Thing.identifier);
if (rowId == null) {
throw new IllegalStateException("Attempt to use identifiers for matching items in ButtonListPropertyView, but row item " + rowEnt + " has no identifier. Property: " + p + " in entity " + e);
}
if (selectedIds.contains(rowId)) {
selectedIndices.add(i);
}
} else {
throw new IllegalStateException("Options for field should all be entities. Property " + p + " entity " + e);
}
}
} else {
// Not using IDS. We will use direct matching.
for (int i = 0; i < len; i++) {
if (el.contains(model.getItemAt(i))) {
selectedIndices.add(i);
}
}
}
} else if (Collection.class.isAssignableFrom(p.getContentType().getRepresentationClass())) {
// It's a collection
for (int i = 0; i < len; i++) {
if (((Collection) val).contains(model.getItemAt(i))) {
selectedIndices.add(i);
}
}
} else {
throw new IllegalStateException("Property " + p + " must contain either EntityList or Collection in order to be editable by ButtonListPropertyView with a multi-selection options model.");
}
java.util.Collections.sort(selectedIndices, (i1, i2) -> {
return i1 - i2;
});
List<Integer> existingSelectedIndices = new ArrayList<>();
for (int index : model.getSelectedIndices()) {
existingSelectedIndices.add(index);
}
java.util.Collections.sort(existingSelectedIndices, (i1, i2) -> {
return i1 - i2;
});
if (!Objects.deepEquals(selectedIndices, existingSelectedIndices)) {
int size0 = selectedIndices.size();
int[] selectedIndicesArr = new int[size0];
for (int i = 0; i < size0; i++) {
selectedIndicesArr[i] = selectedIndices.get(i);
}
model.setSelectedIndices(selectedIndicesArr);
}
}
}
use of com.codename1.rad.models.Property in project CodeRAD by shannah.
the class ButtonListPropertyView method commitMultiSelectionModel.
private void commitMultiSelectionModel() {
Entity e = getPropertySelector().getLeafEntity();
Property p = getPropertySelector().getLeafProperty();
int selectedIndex = getComponent().getModel().getSelectedIndex();
if (selectedIndex < 0) {
if (p.getContentType().isEntityList()) {
e.getEntity().getEntityList(p).clear();
e.getEntity().setChanged(p, true);
} else if (Collection.class.isAssignableFrom(p.getContentType().getRepresentationClass())) {
((Collection) e.getEntity().get(p)).clear();
e.getEntity().setChanged(p, true);
} else {
throw new IllegalStateException("Unsupported property content type for property");
}
} else {
ListModel model = getComponent().getModel();
if (model instanceof MultipleSelectionListModel) {
MultipleSelectionListModel multiModel = (MultipleSelectionListModel) model;
int[] selectedIndices = multiModel.getSelectedIndices();
boolean changed = false;
Set selectedObjects = new HashSet();
for (int i = 0; i < selectedIndices.length; i++) {
selectedObjects.add(multiModel.getItemAt(selectedIndices[i]));
}
Set oldSelectedObjects = new HashSet();
if (Iterable.class.isAssignableFrom(p.getContentType().getRepresentationClass())) {
Iterable it = e.getEntity().getAs(p, Iterable.class);
if (it != null) {
for (Object i : it) {
oldSelectedObjects.add(i);
}
}
}
changed = !(oldSelectedObjects.containsAll(selectedObjects) && selectedObjects.containsAll(oldSelectedObjects));
if (!changed)
return;
if (p.getContentType().isEntityList()) {
EntityList el = e.getEntity().getEntityList(p);
if (el == null) {
el = new EntityList();
e.getEntity().set(p, el);
}
for (Object o : selectedObjects) {
if (!(o instanceof Entity)) {
throw new IllegalStateException("Cannot add non-entity to entity list for property " + p);
}
if (!el.contains(o)) {
el.add((Entity) o);
}
}
List<Entity> toRemove = new ArrayList<>();
for (Object entity : el) {
if (!selectedObjects.contains(entity)) {
toRemove.add((Entity) entity);
}
}
if (!toRemove.isEmpty()) {
for (Entity entity : toRemove) {
el.remove(entity);
}
}
e.setChanged(p, true);
} else if (Collection.class.isAssignableFrom(p.getContentType().getRepresentationClass())) {
Collection c = e.getAs(p, Collection.class);
if (c == null) {
if (p.getContentType().getRepresentationClass().isAssignableFrom(List.class)) {
c = new ArrayList();
e.getEntity().set(p, c);
} else if (p.getContentType().getRepresentationClass().isAssignableFrom(Set.class)) {
c = new HashSet();
e.getEntity().set(p, c);
} else {
throw new IllegalStateException("Cannot set item in collection of property " + p + " because the collection is null.");
}
}
for (Object o : selectedObjects) {
if (!c.contains(o)) {
c.add(o);
}
}
List toRemove = new ArrayList<>();
for (Object entity : c) {
if (!selectedObjects.contains(entity)) {
toRemove.add(entity);
}
}
if (!toRemove.isEmpty()) {
for (Object entity : toRemove) {
c.remove(entity);
}
}
e.setChanged(p, true);
}
} else {
Object selectedObject = getComponent().getModel().getItemAt(selectedIndex);
if (p.getContentType().isEntityList()) {
if (!(selectedObject instanceof Entity)) {
throw new IllegalStateException("Attempt to add non-entity " + selectedObject + " to property of type entity list");
}
EntityList el = e.getEntity().getEntityList(p);
if (el == null) {
el = new EntityList();
e.getEntity().set(p, el);
}
if (el.size() == 1 && el.contains(selectedObject)) {
return;
}
if (el.size() != 0) {
el.clear();
}
el.add((Entity) selectedObject);
e.setChanged(p, true);
} else if (Collection.class.isAssignableFrom(p.getContentType().getRepresentationClass())) {
Collection c = (Collection) e.getEntity().getAs(p, Collection.class);
if (c == null) {
if (p.getContentType().getRepresentationClass().isAssignableFrom(List.class)) {
c = new ArrayList();
e.getEntity().set(p, c);
} else if (p.getContentType().getRepresentationClass().isAssignableFrom(Set.class)) {
c = new HashSet();
e.getEntity().set(p, c);
} else {
throw new IllegalStateException("Cannot set item in collection of property " + p + " because the collection is null.");
}
}
if (c.size() == 1 && c.contains(selectedObject)) {
return;
}
if (!c.isEmpty()) {
c.clear();
}
c.add(selectedObject);
e.setChanged(p, true);
}
e.getEntity().set(p, selectedObject);
}
}
}
use of com.codename1.rad.models.Property in project CodeRAD by shannah.
the class ButtonListPropertyView method commitSingleSelectionModel.
private void commitSingleSelectionModel() {
Entity e = getPropertySelector().getLeafEntity();
Property p = getPropertySelector().getLeafProperty();
int selectedIndex = getComponent().getModel().getSelectedIndex();
if (selectedIndex < 0) {
e.getEntity().set(p, null);
} else {
Object selectedObject = getComponent().getModel().getItemAt(selectedIndex);
e.getEntity().set(p, selectedObject);
}
}
use of com.codename1.rad.models.Property in project CodeRAD by shannah.
the class CheckBoxPropertyView method commit.
@Override
public void commit() {
if (getPropertySelector().isFalsey() == getComponent().isSelected()) {
Entity e = getPropertySelector().getLeafEntity();
Property p = getPropertySelector().getLeafProperty();
e.getEntity().setBoolean(p, getComponent().isSelected());
}
}
Aggregations