Combobox – native value and translated value

Hi all,

I have the POJO, that holds the enum (assume Enum1 { A, B}). On UI i use the Combobox to display the enum entry. The enum entry must be visualized according to the given (natural) language. Thus, I needed theoretical to storage into combobox that both values (the s.c. native value and translated value).

But I have no found the smart solution with using only combobox to save that values :–((

Currently I use the extra mapping with hasmaps between native value and translations for each needed language.

I’m tried to create a “smart” enum, that holds a native value with all corresponding translations and realizing ‘toString()” method on such enum, but no the translated values on such combobox were shown…

Does anyone have a good idea of ​​how it can be better realized?

Thank you!

You can override an items caption using the ComboBox.setItemCaption(Object, String) method. Also works on Trees and other Select components :slight_smile:

Hi Thomas,

i’m overrided setItemCaption(Object, String) method (of AbstractSelect class), but no needed translations have been occurred, because this method was not called. I’m tried all item caption modes…
The example:

package com.example.testvaadin;

import java.util.HashMap;
import java.util.Map;
import com.vaadin.Application;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class TestvaadinApplication extends Application {

	private static final String ISO_CODE = "en";

       /** The map used as translations holder **/
      /** Map<String as iso code, Map<Enum entry as item id, String as translation>
	private Map<String, Map<TestColor, String>> trans;

/** Enum it's entries shell be displayed as translated on UI **/
	private enum TestColor {
		RED, GREEN, BLUE
	}
/** own class to override  setItemCaption **/
	private class MyComboBox extends ComboBox {

		@Override
		public void setItemCaption(Object itemId, String caption) {
			
			TestColor color = (TestColor) itemId;
			
			caption = getTranslation(color, ISO_CODE);
			
			super.setItemCaption(itemId, caption);
		}
		
		

	}

	@Override
	public void init() {

		initInternal();

		Window mainWindow = new Window("Testvaadin Application");
		
		MyComboBox testCmbx = new MyComboBox();
		testCmbx.setCaption("TestCmbx:ITEM_CAPTION_MODE_EXPLICIT");
		prepareCombobox(testCmbx, ISO_CODE, AbstractSelect.ITEM_CAPTION_MODE_EXPLICIT);
		addItems(testCmbx);
		mainWindow.addComponent(testCmbx);
		
		testCmbx = new MyComboBox();
		testCmbx.setCaption("TestCmbx:ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID");
		prepareCombobox(testCmbx, ISO_CODE, AbstractSelect.ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID);
		addItems(testCmbx);
		mainWindow.addComponent(testCmbx);
		
		
		testCmbx = new MyComboBox();
		testCmbx.setCaption("TestCmbx:ITEM_CAPTION_MODE_ID");
		prepareCombobox(testCmbx, ISO_CODE, AbstractSelect.ITEM_CAPTION_MODE_ID);
		addItems(testCmbx);
		mainWindow.addComponent(testCmbx);
		
		testCmbx = new MyComboBox();
		testCmbx.setCaption("TestCmbx:ITEM_CAPTION_MODE_INDEX");
		prepareCombobox(testCmbx, ISO_CODE, AbstractSelect.ITEM_CAPTION_MODE_INDEX);
		addItems(testCmbx);
		mainWindow.addComponent(testCmbx);
		
		testCmbx = new MyComboBox();
		testCmbx.setCaption("TestCmbx:ITEM_CAPTION_MODE_ITEM");
		prepareCombobox(testCmbx, ISO_CODE, AbstractSelect.ITEM_CAPTION_MODE_ITEM);
		addItems(testCmbx);
		mainWindow.addComponent(testCmbx);
		
//		testCmbx = new MyComboBox();
//		testCmbx.setCaption("TestCmbx:ITEM_CAPTION_MODE_PROPERTY");
//		prepareCombobox(testCmbx, ISO_CODE, AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
//		addItems(testCmbx);
//		mainWindow.addComponent(testCmbx);
		
		Label label = new Label("Hello Vaadin user");
                setMainWindow(mainWindow);//edit
		mainWindow.getContent().setSizeUndefined(); // try to set a horizontal scroll bar

	}

	private void addItems(ComboBox testCmbx) {
		testCmbx.addItem(TestColor.RED);
		testCmbx.addItem(TestColor.GREEN);
		testCmbx.addItem(TestColor.BLUE);
		
	}

	private void initInternal() {

		trans = new HashMap<String, Map<TestColor, String>>();
		String isoCode = ISO_CODE;
		Map<TestColor, String> dm = new HashMap<TestColor, String>();

		dm = new HashMap<TestColor, String>();
		dm.put(TestColor.BLUE, "blue");
		dm.put(TestColor.GREEN, "green");
		dm.put(TestColor.RED, "red");
		trans.put(isoCode, dm);

	}

	private String getTranslation(TestColor color, String isoCode) {
		String result = "";
		Map<TestColor, String> dm = trans.get(isoCode);
		if (dm != null) {
			result = dm.get(color);
		}
		return result;
	}
	
	/**
	 * Prepares the given instance of the combobox.
	 * 
	 * @param combobox	The combobox, that will be prepared.
	 * @param isoCode	The given language iso code
	 */
	private static void prepareCombobox(ComboBox combobox, String isoCode, int itemCaptionMode) {
		combobox.setNullSelectionAllowed(true);
//		String nullItem = NLSTexts.getInstance().getDisplayNameTranslationFor(noneId, isoCode);
//		combobox.addItem(nullItem);
//		combobox.setNullSelectionItemId(noneId);
		combobox.setItemCaptionMode(itemCaptionMode);
		combobox.setNullSelectionAllowed(true);
		combobox.setNewItemsAllowed(false);
		combobox.setReadOnly(false);
		combobox.setInvalidAllowed(false);
		combobox.setImmediate(true);
	}

}

You shouldn’t override that method, just call it for every item in your container with the caption you want for that item.

I read wrong:—(((((( ;.-)… “override method” instead of “override an item caption”

But i’m found meanwhile an other solution using “smart” enums, that have customized "toString() method. And using combobox caption mode AbstractSelect.ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID or AbstractSelect.ITEM_CAPTION_MODE_ID.
Edit: the simplest solution imho is to overwrite the public String getItemCaption(Object itemId) {…} mehtod of the AbstractSelect class.
I thank you for your help!

P.S An Example for enum:


package com.example.testvaadin;

import java.util.HashMap;
import java.util.Map;
import com.vaadin.Application;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class TestvaadinApplication extends Application {

	private static final String ISO_CODE = "en";

	private Map<String, Map<TestColor, String>> trans;

	private enum TestColor {
		RED, GREEN, BLUE;

		private TestvaadinApplication parent;

		private void setParent(TestvaadinApplication parent) {
			this.parent = parent;
		}

		@Override
		public String toString() {
			return parent.getTranslation(this, ISO_CODE);
		}

	}

	private class MyComboBox extends ComboBox {

		@Override
		public void setItemCaption(Object itemId, String caption) {

			TestColor color = (TestColor) itemId;

			caption = getTranslation(color, ISO_CODE);

			super.setItemCaption(itemId, caption);
		}

	}


	@Override
	public void init() {

		initInternal();
;
		Window mainWindow = new Window("Testvaadin Application");

		MyComboBox testCmbx = new MyComboBox();
		testCmbx.setCaption("TestCmbx:ITEM_CAPTION_MODE_EXPLICIT");
		prepareCombobox(testCmbx, ISO_CODE, AbstractSelect.ITEM_CAPTION_MODE_EXPLICIT);
		addItems(testCmbx);
		mainWindow.addComponent(testCmbx);

		testCmbx = new MyComboBox();
		testCmbx.setCaption("TestCmbx:ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID");
		prepareCombobox(testCmbx, ISO_CODE, AbstractSelect.ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID);
		addItems(testCmbx);
		mainWindow.addComponent(testCmbx);

		testCmbx = new MyComboBox();
		testCmbx.setCaption("TestCmbx:ITEM_CAPTION_MODE_ID");
		prepareCombobox(testCmbx, ISO_CODE, AbstractSelect.ITEM_CAPTION_MODE_ID);
		addItems(testCmbx);
		mainWindow.addComponent(testCmbx);

		testCmbx = new MyComboBox();
		testCmbx.setCaption("TestCmbx:ITEM_CAPTION_MODE_INDEX");
		prepareCombobox(testCmbx, ISO_CODE, AbstractSelect.ITEM_CAPTION_MODE_INDEX);
		addItems(testCmbx);
		mainWindow.addComponent(testCmbx);

		testCmbx = new MyComboBox();
		testCmbx.setCaption("TestCmbx:ITEM_CAPTION_MODE_ITEM");
		prepareCombobox(testCmbx, ISO_CODE, AbstractSelect.ITEM_CAPTION_MODE_ITEM);
		addItems(testCmbx);
		mainWindow.addComponent(testCmbx);

		Label label = new Label("Hello Vaadin user");

		setMainWindow(mainWindow);

		mainWindow.getContent().setSizeUndefined(); // try to set a horizontal scroll bar

	}

	private void addItems(ComboBox testCmbx) {
		TestColor entry = TestColor.RED;
		entry.setParent(this);
		testCmbx.addItem(entry);
		
		entry = TestColor.GREEN;
		entry.setParent(this);
		testCmbx.addItem(entry);
		
		entry = TestColor.BLUE;
		entry.setParent(this);
		testCmbx.addItem(entry);

	}

	private void initInternal() {

		trans = new HashMap<String, Map<TestColor, String>>();
		String isoCode = ISO_CODE;

		dm = new HashMap<TestColor, String>();
		dm.put(TestColor.BLUE, "blue");
		dm.put(TestColor.GREEN, "green");
		dm.put(TestColor.RED, "red");
		trans.put(isoCode, dm);

	}

	private String getTranslation(TestColor color, String isoCode) {
		String result = "";
		Map<TestColor, String> dm = trans.get(isoCode);
		if (dm != null) {
			result = dm.get(color);
		}
		return result;
	}

	/**
	 * Prepares the given instance of the combobox.
	 * 
	 * @param combobox	The combobox, that will be prepared.
	 * @param isoCode	The given language iso code
	 */
	private static void prepareCombobox(ComboBox combobox, String isoCode, int itemCaptionMode) {
		combobox.setNullSelectionAllowed(true);
		combobox.setItemCaptionMode(itemCaptionMode);
		combobox.setNullSelectionAllowed(true);
		combobox.setNewItemsAllowed(false);
		combobox.setReadOnly(false);
		combobox.setInvalidAllowed(false);
		combobox.setImmediate(true);
	}

}