TokenField displays wrong item caption?

Hello,

I am a beginner of Vaadin. I use add-on TokenField as a multi-select in a Form, add-on AppFoundation
as JPA based persistence. A TokenField is binded to a user Set.

The problem is that until committed Form TokenField is ok. It displays right item caption(user name) ,
but after that it displays wrong item caption like “org.vaadin.appfoundation.authentication.data.User@1db80b” (see attachments).

I debugged my code. I found after method Form.setItemDataSource is executed,
the item caption of TokenField displays wrong result.

My code look more less like this.


public class MailForm extends Form implements
ClickListener{
	private TokenField addresSelect;
	public MailForm(final PaWebApplication app) {
		this.app = app;
		setWriteThrough(false);
		HorizontalLayout footer = new HorizontalLayout();		
		setFooter(footer);
		
		addresSelect = new TokenField("宛先");
		addresSelect.setStyleName(TokenField.STYLE_TOKENFIELD);
		addresSelect.setContainerDataSource(app.getUserDataSource());
		addresSelect.setRequired(true);
		addresSelect.setFilteringMode(ComboBox.FILTERINGMODE_CONTAINS);
		addresSelect.setTokenCaptionPropertyId("name");
		addresSelect.setTokenCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);
		addresSelect.setNewTokensAllowed(false);
		addresSelect.setRememberNewTokens(false);
		addresSelect.setInputPrompt("type or select tags here");		
		addresSelect.setWidth(350, UNITS_PIXELS);	
		addresSelect.setImmediate(true);
		addresSelect.addListener(new ValueChangeListener() {   
			public void valueChange(
					com.vaadin.data.Property.ValueChangeEvent event) {
				Set<User> tokens = (Set)event.getProperty().getValue();
				if (tokens!=null){
				       String name="";
				       for (User user : tokens)
					     name +=user.getName();
			               getWindow().showNotification("Tokens: "+ name);
				}
			}
		});
		
		setFormFieldFactory(new DefaultFieldFactory() {
			@Override
			public Field createField(Item item, Object propertyId,
					Component uiContext) {
				if (propertyId.equals("address")) {
					Field field= addresSelect;
					return field;
				}
			});
	}

In additon, on valueChange event of TokenField , it displays right user name.
12158.jpg
12159.jpg

I am disappointed that nobody has replied me for more than 2 moths:(

Did you ever find out how to resolve this problem, Zhongda? I’m having the same problem.

Paul

Ah looks like you need to override this method so that the caption comes from the POJO.

		            protected void configureTokenButton(Object tokenId, Button button) {
		                super.configureTokenButton(tokenId, button);
		                // custom caption
		                // button.setCaption(getTokenCaption(tokenId));
		                button.setCaption(((Role) tokenId).getName());
		                // width
		                button.setWidth("100%");

		                if (!cb.containsId(tokenId)) {
		                    // it's not in the role; style
		                    button.addStyleName(TokenField.STYLE_BUTTON_EMPHAZISED);
		                }
		            }

Hi Paul,

Thank your reply! You save me. I were disappointed for this discuss.

My solution just like yours.


@Override
	public String getTokenCaption(Object tokenId) {
		if (cb.containsId(tokenId)) {
			return cb.getItemCaption(tokenId);
		} else {
			return ((User) tokenId).getName();
		}
	}

I override the getTokenCaption method. I know this is not good as a last resort.