On 12 juin, 12:36, ciukes <ciu...@gmail.com> wrote: > > My application will by displaying a country flag depending on a locale > selected form list. All the flags are gathered within LocaleFlag > (extends ImageBundle). > I want to be able to get flag image out of LocaleFlag having only a > locale name string in hand.
Er, let's start by saying that language/locale is distinct from country. Using a flag to represent a locale is hardly a good idea (it can be, in cases you're actually selecting a region/country and not a language, for instance have distinct choices for GB and US). > List selection -> Locale name string (e.g. uk) -> LocaleFlag -> > corresponding com.google.gwt.user.client.ui.Image instance. > > So far I came up with a bit brute force spaghetti code solution. I > created LocaleName enumeration with getImage():Image abstract method. > Every enum value implements the method to return corresponding image > instance. [...] > With this enum in hand I can call CountyCode.valueOf("UK").getImage() > and this satisfies my requirement. What I don't like is massive amount > of copy-paste code I have to create ( getImage() implementation have > to be developed for every single enum value). > > Can you guys come up with a better solution? Ideally that would be > LocaleFlag.getImage("UK") method that could internally discover, > create and return Image instance. The "best way" would be to make a new generator (which would extend or call the ImageBundleGenerator). In this case, I would personally add an ImageBundleWithLookup interface (extends ImageBundle) and a corresponding generator, same as how ConstantsWithLookup relates to Constants. Basically, the generator would implement the lookup(String) method (from ImageBundleWithLookup) as (for example): private HashMap<String,AbstractImagePrototype> lookupCache; public AbstractImagePrototype lookup(String key) { AbstractImagePrototype ret = null; if (lookupCache == null) { lookupCache = new HashMap<String, AbstractImagePrototype>(); } else { ret = lookupCache.get(key); if (ret != null) { return ret; } } if ("UK".equals(key)) { ret = this.UK(); lookupCache.put(key, ret); } else if (...) { ... } return ret; // would return null if not found } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to Google-Web-Toolkit@googlegroups.com To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/Google-Web-Toolkit?hl=en -~----------~----~----~----~------~----~------~--~---