GWT dropdown Fail

A colleague has asked me to post some of my own failures here. For one, because he thought it’s unfair otherwise but for second, because he found a good one. It occured when writing a straight-forward GWT mockup of some GUI which finally was deployed a couple of weeks ago.

Here is the code to initialize a dropdown with some values:

   @Override
   public void onLoad()
   {
       templatesList.clear();
       GWTClient.getServerStub().getTemplateList(new AsyncCallback<List<Template>>()
       {
           
           @Override
           public void onFailure(Throwable caught)
           {
               
               GWT.log("", caught);
           }
           
           @Override
           public void onSuccess(List<Template> result)
           {
               for (Template template : result)
               {
                   // only use enabled templates
                   if (template.getEnabled())
                   {
                       templatesList.addItem(template.getName(), template.getId().toString());
                   }
               }
           }
       });
       templatesList.setEnabled(true);
   }

And here is the code executed on selection of an entry of that dropdown:

   public void onTemplateSelection()
   {
       selectButton.setVisible(false);
       templatesList.setVisible(false);
       templatesList.setEnabled(false);
       
       GWTClient.getServerStub().getTemplateList(new AsyncCallback<List<Template>>()
       {
           
           @Override
           public void onFailure(Throwable caught)
           {
               
               GWT.log("", caught);
           }
           
           @Override
           public void onSuccess(List<Template> result)
           {
               Template template = result.get(templatesList.getSelectedIndex());
               templatePanel.init(template);
               rootPanel.add(templatePanel);
               rootPanel.add(buttonBar);
           }
       });
       
   }

I guess I got lazy when I realized I had to cast the ID from String to Integer again in order to make it right. Bad choice. Probably the enabled-property came into that code later than the dropdown. Here you see what you get from laziness – your code stinks.

The code seemed to work quite fine until someone actually used the enabled-property on those templates. The dropdown still looked alright…