Class ObjectToStringConverter
- java.lang.Object
-
- org.jdesktop.swingx.autocomplete.ObjectToStringConverter
-
public abstract class ObjectToStringConverter extends Object
This class is used to provide string representations for objects when doing automatic completion.
A class inherited from this class could be used, when the object's toString method is not appropriate for automatic completion.
An example for i18n:
public class I18NStringConverter extends ObjectToStringConverter { ResourceBundle bundle; public I18NStringConverter(ResourceBundle bundle) { this.bundle = bundle; } public String getPreferredStringForItem(Object item) { return item==null ? null : bundle.getString(item.toString()); } }
It's also possible to return more than one string representation. The following example shows a converter that will allow a user to choose an airport using either the airport's full description (toString()) or its ICAO/IATA code:
public class AirportConverter extends ObjectToStringConverter { public String[] getPossibleStringsForItem(Object item) { if (item==null) return new String[0]; if (!(item instanceof Airport)) throw new IllegalArgumentException(); Airport airport = (Airport) item; return new String[]{airport.toString(), airport.icaoCode, airport.iataCode}; } public String getPreferredStringForItem(Object item) { return item==null?null:getPossibleStringsForItem(item)[0]; } }
- Author:
- Thomas Bierhance
-
-
Field Summary
Fields Modifier and Type Field Description static ObjectToStringConverter
DEFAULT_IMPLEMENTATION
This field contains the default implementation, that returns item.toString() for any item !=null.
-
Constructor Summary
Constructors Constructor Description ObjectToStringConverter()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description String[]
getPossibleStringsForItem(Object item)
Returns all possible String representations for a given item.abstract String
getPreferredStringForItem(Object item)
Returns the preferred String representations for a given item.
-
-
-
Field Detail
-
DEFAULT_IMPLEMENTATION
public static final ObjectToStringConverter DEFAULT_IMPLEMENTATION
This field contains the default implementation, that returns item.toString() for any item !=null. For any item ==null, it returns null as well.
-
-
Method Detail
-
getPossibleStringsForItem
public String[] getPossibleStringsForItem(Object item)
Returns all possible String representations for a given item. The default implementation wraps the method getPreferredStringForItem. It returns an empty array, if the wrapped method returns null. Otherwise it returns a one dimensional array containing the wrapped method's return value.- Parameters:
item
- the item to convert- Returns:
- possible String representation for the given item.
-
-