package org.minimalj.frontend.edit.fields;

import java.util.List;

import org.minimalj.frontend.toolkit.ClientToolkit;
import org.minimalj.frontend.toolkit.ClientToolkit.IComponent;
import org.minimalj.frontend.toolkit.TextField;
import org.minimalj.model.PropertyInterface;
import org.minimalj.model.annotation.Code;
import org.minimalj.util.Codes;

public class CodeFormField<T extends Code> extends AbstractEditField<Object> {
	
	private final List<T> codes;
	private final TextField textFieldDisabled;

	public CodeFormField(PropertyInterface property) {
		super(property, true);
		codes = Codes.get((Class<T>) property.getFieldClazz());
		
		textFieldDisabled = ClientToolkit.getToolkit().createReadOnlyTextField();
	}
	
	@Override
	public IComponent getComponent() {
		return textFieldDisabled;
	}

	@Override
	public Object getObject() {
		throw new IllegalStateException();
	}

	@Override
	public void setObject(Object value) {
		Object code = Codes.findCode(codes, value);
		textFieldDisabled.setText(code != null ? code.toString() : null);
	}

}

@@ -59,16 +59,41 @@
 il.List;
+%0Aimport java.util.Locale;
 %0A%0Aimport
@@ -294,16 +294,101 @@
 erface;%0A
+import org.minimalj.model.Rendering;%0Aimport org.minimalj.model.Rendering.RenderType;%0A
 import o
@@ -611,16 +611,48 @@
 abled;%0A%0A
+%09@SuppressWarnings(%22unchecked%22)%0A
 %09public 
@@ -1074,22 +1074,20 @@
 ue) %7B%0A%09%09
-Object
+Code
  code = 
@@ -1122,71 +1122,256 @@
 ;%0A%09%09
-textFieldDisabled.setText(code != null ? code.toString() : null
+String text;%0A%09%09if (code instanceof Rendering) %7B%0A%09%09%09text = ((Rendering) code).render(RenderType.PLAIN_TEXT, Locale.getDefault());%0A%09%09%7D else if (code != null) %7B%0A%09%09%09text = code.toString();%0A%09%09%7D else %7B%0A%09%09%09text = null;%0A%09%09%7D%0A%09%09textFieldDisabled.setText(text
 );%0A%09

use rendering, not toString