Skip to content

Commit

Permalink
fix for MID-3972 (dropdown/enum for prism generated forms)
Browse files Browse the repository at this point in the history
  • Loading branch information
katkav committed Jun 15, 2017
1 parent afd360b commit 18e39ce
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 55 deletions.
Expand Up @@ -64,6 +64,7 @@
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.EnumChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
Expand Down Expand Up @@ -140,6 +141,7 @@
import com.evolveum.midpoint.web.component.data.BaseSortableDataProvider;
import com.evolveum.midpoint.web.component.data.Table;
import com.evolveum.midpoint.web.component.data.column.ColumnMenuAction;
import com.evolveum.midpoint.web.component.input.DisplayableValueChoiceRenderer;
import com.evolveum.midpoint.web.component.input.DropDownChoicePanel;
import com.evolveum.midpoint.web.component.util.Selectable;
import com.evolveum.midpoint.web.component.util.SelectableBean;
Expand Down Expand Up @@ -594,9 +596,11 @@ public static <E extends Enum> DropDownChoicePanel createEnumPanel(Class clazz,

public static <E extends Enum> DropDownChoicePanel createEnumPanel(Class clazz, String id,
IModel<List<E>> choicesList, final IModel<E> model, final Component component, boolean allowNull) {
return new DropDownChoicePanel(id, model, choicesList,
return new DropDownChoicePanel(id, model, choicesList,
new IChoiceRenderer<E>() {

private static final long serialVersionUID = 1L;

@Override
public E getObject(String id, IModel<? extends List<? extends E>> choices) {
if (StringUtils.isBlank(id)) {
Expand All @@ -623,60 +627,68 @@ public static DropDownChoicePanel createEnumPanel(final PrismPropertyDefinition
final Object o = model.getObject();

final IModel<List<DisplayableValue>> enumModelValues = new AbstractReadOnlyModel<List<DisplayableValue>>() {

private static final long serialVersionUID = 1L;

@Override
public List<DisplayableValue> getObject() {
List<DisplayableValue> values = null;
if (def.getAllowedValues() != null) {
values = new ArrayList<>(def.getAllowedValues().size());
for (Object v : def.getAllowedValues()) {
if (v instanceof DisplayableValue) {
values.add(((DisplayableValue) v));
}
}
}
return values;
return getDisplayableValues(def);
}

};

return new DropDownChoicePanel(id, model, enumModelValues, new IChoiceRenderer() {
return new DropDownChoicePanel(id, model, enumModelValues, new DisplayableValueChoiceRenderer(getDisplayableValues(def)), true);


// @Override
// public Object getObject(String id, IModel choices) {
// if (StringUtils.isBlank(id)) {
// return null;
// }
// return ((List) choices.getObject()).get(Integer.parseInt(id));
// }
//
// @Override
// public Object getDisplayValue(Object object) {
// if (object instanceof DisplayableValue) {
// return ((DisplayableValue) object).getLabel();
// }
// for (DisplayableValue v : enumModelValues.getObject()) {
// if (object.equals(v.getValue())) {
// return v.getLabel();
// }
// }
// return object;
//
// }
//
// @Override
// public String getIdValue(Object object, int index) {
// if (object instanceof String && enumModelValues != null && enumModelValues.getObject() != null) {
// List<DisplayableValue> enumValues = enumModelValues.getObject();
// for (DisplayableValue v : enumValues) {
// if (object.equals(v.getValue())) {
// return String.valueOf(enumValues.indexOf(v));
// }
// }
// }
// return String.valueOf(index);
// }
}

@Override
public Object getObject(String id, IModel choices) {
if (StringUtils.isBlank(id)) {
return null;
}
return ((List) choices.getObject()).get(Integer.parseInt(id));
}


@Override
public Object getDisplayValue(Object object) {
if (object instanceof DisplayableValue) {
return ((DisplayableValue) object).getLabel();
private static List<DisplayableValue> getDisplayableValues(PrismPropertyDefinition def) {
List<DisplayableValue> values = null;
if (def.getAllowedValues() != null) {
values = new ArrayList<>(def.getAllowedValues().size());
for (Object v : def.getAllowedValues()) {
if (v instanceof DisplayableValue) {
values.add(((DisplayableValue) v));
}
for (DisplayableValue v : enumModelValues.getObject()) {
if (object.equals(v.getValue())) {
return v.getLabel();
}
}
return object;

}

@Override
public String getIdValue(Object object, int index) {
if (object instanceof String && enumModelValues != null && enumModelValues.getObject() != null) {
List<DisplayableValue> enumValues = enumModelValues.getObject();
for (DisplayableValue v : enumValues) {
if (object.equals(v.getValue())) {
return String.valueOf(enumValues.indexOf(v));
}
}
}
return String.valueOf(index);
}

}, true);
}
return values;
}

public static <T> TextField<T> createAjaxTextField(String id, IModel<T> model) {
Expand Down
@@ -0,0 +1,58 @@
package com.evolveum.midpoint.web.component.input;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.IModel;

import com.evolveum.midpoint.util.DisplayableValue;

public class DisplayableValueChoiceRenderer<T> implements IChoiceRenderer<T> {

private static final long serialVersionUID = 1L;
private List<DisplayableValue> choices;


public DisplayableValueChoiceRenderer(List<DisplayableValue> choices) {
this.choices = choices;
}

@Override
public String getDisplayValue(T object) {
if (object == null) {
return null;
}

if (object instanceof DisplayableValue) {
return ((DisplayableValue) object).getLabel();
}

return object.toString();
}

@Override
public String getIdValue(T object, int index) {

if (object instanceof String && choices != null) {
for (DisplayableValue v : choices) {
if (object.equals(v.getValue())) {
return String.valueOf(choices.indexOf(v));
}

}
}

return Integer.toString(index);
}

@Override
public T getObject(String id, IModel<? extends List<? extends T>> choices) {
if (StringUtils.isBlank(id)){
return null;
}

return choices.getObject().get(Integer.parseInt(id));
}

}
Expand Up @@ -430,15 +430,15 @@ private Panel createTypedInputComponent(String id) {
return new TextAreaPanel(id, new PropertyModel(valueWrapperModel, baseExpression), null);
}

if (ActivationType.F_ADMINISTRATIVE_STATUS.equals(definition.getName())) {
return WebComponentUtil.createEnumPanel(ActivationStatusType.class, id, new PropertyModel<ActivationStatusType>(valueWrapperModel, baseExpression), this);
} else if(ActivationType.F_LOCKOUT_STATUS.equals(definition.getName())){
return new LockoutStatusPanel(id, valueWrapperModel.getObject(), new PropertyModel<LockoutStatusType>(valueWrapperModel, baseExpression));
} else {
if (definition.getTypeName().getLocalPart().equals(ActivationStatusType.class.getSimpleName())) {
return WebComponentUtil.createEnumPanel(ActivationStatusType.class, id, new PropertyModel<ActivationStatusType>(valueWrapperModel, baseExpression), this);
}
}
// if (ActivationType.F_ADMINISTRATIVE_STATUS.equals(definition.getName())) {
// return WebComponentUtil.createEnumPanel(ActivationStatusType.class, id, new PropertyModel<ActivationStatusType>(valueWrapperModel, baseExpression), this);
// } else if(ActivationType.F_LOCKOUT_STATUS.equals(definition.getName())){
// return new LockoutStatusPanel(id, valueWrapperModel.getObject(), new PropertyModel<LockoutStatusType>(valueWrapperModel, baseExpression));
// } else {
// if (definition.getTypeName().getLocalPart().equals(ActivationStatusType.class.getSimpleName())) {
// return WebComponentUtil.createEnumPanel(ActivationStatusType.class, id, new PropertyModel<ActivationStatusType>(valueWrapperModel, baseExpression), this);
// }
// }

if (DOMUtil.XSD_DATETIME.equals(valueType)) {
panel = new DatePanel(id, new PropertyModel<XMLGregorianCalendar>(valueWrapperModel, baseExpression));
Expand Down Expand Up @@ -545,7 +545,10 @@ public DeltaDto getObject() {
});
} else if (QueryType.COMPLEX_TYPE.equals(valueType) || CleanupPoliciesType.COMPLEX_TYPE.equals(valueType)) {
return new TextAreaPanel(id, new AbstractReadOnlyModel() {
@Override

private static final long serialVersionUID = 1L;

@Override
public Object getObject() {
if (valueWrapperModel.getObject() == null || valueWrapperModel.getObject().getValue() == null) {
return null;
Expand Down Expand Up @@ -577,6 +580,12 @@ public Object getObject() {
}

if (isEnum(property)) {
Class clazz = pageBase.getPrismContext().getSchemaRegistry().determineClassForType(definition.getTypeName());

if (clazz != null) {
return WebComponentUtil.createEnumPanel(clazz, id, new PropertyModel(valueWrapperModel, baseExpression), this);
}

return WebComponentUtil.createEnumPanel(definition, id, new PropertyModel<>(valueWrapperModel, baseExpression), this);
}
// // default QName validation is a bit weird, so let's treat QNames as strings [TODO finish this - at the parsing side]
Expand Down
Expand Up @@ -391,3 +391,6 @@ ResourceValidator.inboundMapping=inbound mapping number {0}
AdminGuiConfigurationType.adminGuiConfiguration=Admin GUI Configuration
midPointVersion=${pom.version}
midPointRevision=${git.describe}
IdempotenceType.CONSERVATIVE=Conservative
IdempotenceType.AGGRESSIVE=Aggressive
IdempotenceType.NONE=None

0 comments on commit 18e39ce

Please sign in to comment.