Resource

How to find if an icon is available in the directory … For example if i am setting an icon to a button.

Button b= new Button();
b.setIcon(new ThemeResource(“…/icons/remove.png”));

suppose if the icon remove.png is not available in the icons folder, is there any method that i can find that the icon is not available?

My need is if the icon is available in the directory i have the set the specific icon otherwise i need to set the default icon. For that i need to know whether the icon is available or not … using Resource class or ThemeResource or any other…

/*
if (icon available) {
// set the specific icon
} else {
// set the default icon
}
*/

Can anyone help me in this regard?

Don’t double post. It sometimes takes longer then 2 days till someone replies to a thread. If it then takes even longer till someone replies that’s probably because your question is for example missing information, got answered several times before, not well formatted, not understandable, …

Original Post

A solution which might work but isn’t that recommend would be to do something like this:

try{
  b.setIcon(new ThemeResource("../icons/remove.png"));
}catch(FileNotFoundException e){ //Or other type of exception. Depends on what is thrown if the png doesn't exist
  b.setIcon(new ThemeResource("../yourdefaulticon.png"));
}

It is not possible to create a general solution for this, as the when using proxies, front-end web servers etc. the theme resources might well not even come from the same server as the application and the server might not even be able to access the network from which they are loaded. Only the client browser can find out if it can access those resources.

This is also why there is no checking performed when creating an instance of a ThemeResource so Marius’ solution won’t work here.

That said, in the typical case where the same servlet serves also the theme, you can get the servlet context and try to check for the existence of the resource there. I didn’t test this anytime recently, but I think you should be able to use VaadinServlet.getCurrent().getServletContext() - you can also see VaadinServlet.serveStaticResourcesInVAADIN(…) for how it is used to serve theme (and other static) resources.

Hi. Thanks for your reply. Could you please post some example code to meet the requirement.

In general, if the person replying does not post example code, there is a reason for that - usually that the person is too busy to write it, and it should be possible to find out how to do it from the information already provided.

That said, I’ll make an exception this time and try to write some - the code could look something like (completely untested)

ServletContext sc = VaadinServlet.getCurrent().getServletContext();
// this assumes the file is directly in the project, not some other JAR
// otherwise see VaadinServlet.findResourceURL(...), use a (Http)URLConnection and see http://stackoverflow.com/questions/1378199/how-to-check-if-a-url-exists-or-returns-404-with-java - and make sure you close the connection properly
// the code will probably be 15-25 lines in that case
String path = sc.getRealPath(themeResourcePath);
boolean exists = (path != null) && new File(path).exists();

If you need more hands-on support, you can also purchase
Vaadin Pro Account
and commercial support hours for it.

Thank you very much Henri. It worked. Thanks for the extra effort.