Skip to content

Commit

Permalink
imrpoving enum extension - saving enum value, displaying enum label..
Browse files Browse the repository at this point in the history
  • Loading branch information
katkav committed Mar 9, 2015
1 parent 42a98c6 commit 4997efb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
Expand Up @@ -213,39 +213,63 @@ public String getIdValue(E object, int index) {
}, true);
}

public static <E extends Enum> DropDownChoicePanel createEnumPanel(final PrismPropertyDefinition def,
public static DropDownChoicePanel createEnumPanel(final PrismPropertyDefinition def,
String id, final IModel model, final Component component) {
// final Class clazz = model.getObject().getClass();
final Object o = model.getObject();

IModel<List<String>> enumModelValues = new AbstractReadOnlyModel<List<String>>() {
final IModel<List<DisplayableValue>> enumModelValues = new AbstractReadOnlyModel<List<DisplayableValue>>() {
@Override
public List<String> getObject() {
List<String> values = null;
public List<DisplayableValue> getObject() {
List<DisplayableValue> values = null;
if (def.getAllowedValues() != null){
values = new ArrayList<>(def.getAllowedValues().length);
for (Object v : def.getAllowedValues()){
if (v instanceof DisplayableValue){
values.add(((DisplayableValue) v).getLabel());
values.add(((DisplayableValue) v));
}
}
}
return values;
}


};

return new DropDownChoicePanel(id, model, enumModelValues,
new IChoiceRenderer<String>() {
new IChoiceRenderer() {



@Override
public Object getDisplayValue(String object) {
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(String object, int index) {
return Integer.toString(index);
public String getIdValue(Object object, int index) {
if (object instanceof DisplayableValue){
return ((DisplayableValue)object).getValue().toString();
}
return object.toString();
// for (DisplayableValue v : enumModelValues.getObject()){
// if (object.equals(v.getValue())){
// return v.getLabel();
// }
// }
// return object.getValue().toString();//Integer.toString(index);
}


}, true);
}

Expand Down
Expand Up @@ -481,7 +481,7 @@ private <T> T parseAtomicValueFromPrimitive(PrimitiveXNode<T> xprim, PrismProper

private <T> boolean isAllowed(T realValue, Object[] allowedValues){
for (Object o : allowedValues){
if (realValue.equals(((DisplayableValue)o).getLabel())){
if (realValue.equals(((DisplayableValue)o).getValue())){
return true;
}
}
Expand Down
Expand Up @@ -25,15 +25,18 @@
import com.evolveum.midpoint.prism.xml.XsdTypeMapper;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

import org.apache.commons.lang.StringUtils;

import com.evolveum.midpoint.prism.Visitor;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.DisplayableValue;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.SchemaException;

import org.apache.commons.lang.Validate;

public class PrimitiveXNode<T> extends XNode implements Serializable {
Expand Down Expand Up @@ -192,6 +195,10 @@ private String formatValue(T value) {
if (value instanceof QName) {
return QNameUtil.qNameToUri((QName) value);
}
if (value instanceof DisplayableValue) {
return ((DisplayableValue) value).getValue().toString();
}

return XmlTypeConverter.toXmlTextContent(value, null);
}

Expand Down

0 comments on commit 4997efb

Please sign in to comment.