title: custom Converter throws java.lang.ClassCastException when calling setPropertyDataSource on text field
body: <p>I am trying to implement a custom Converter for a TextField to a custom data type(CellElement). I am following the tutorial from the <a href="https://vaadin.com/book/vaadin7/-/page/datamodel.properties.html#datamodel.properties.converter" rel="nofollow">Book of Vaadin</a>.<br>
  When I call <code>setPropertyDataSource()</code> on the TextField the debugger throws <code>java.lang.ClassCastException: java.lang.String cannot be cast to com.example.demo.CellElement</code> </p>
  
  <pre><code>    board = new Board();//this initializes all the cells
  
      // connect the tile to the display
      for( int col = 0; col &lt; 9; col++ )
          for( int row = 0; row &lt; 9; row++ )
          {
              TextField cell_text = new TextField();
              cell_text.setConverter(new StringToCellElementConverter());
              Property temp_ce = board.getCellElement(col, row);
              //error on next line
              cell_text.setPropertyDataSource(temp_ce);
  
              grid.addComponent( cell_text, col, row );
          }
  </code></pre>
  
  <p>I think that I have made an error in implementing the <code>stringToCellElementConverter</code> class, but I cannot determine what. I followed the debugger step and it throws the error in this block of code inside <code>AbstractField</code>::</p>
  
  <pre><code>@Override
  public void setPropertyDataSource(Property newDataSource) {
  
      ...
  
      // Gets the value from source. This requires that a valid converter has
      // been set.
      try {
          if (dataSource != null) {
              //next line throws error!
              T fieldValue = convertFromModel(getDataSourceValue());
              setInternalValue(fieldValue);
          }
          setModified(false);
          if (getCurrentBufferedSourceException() != null) {
              setCurrentBufferedSourceException(null);
          }
      } catch (final Throwable e) {
          setCurrentBufferedSourceException(new Buffered.SourceException(
                  this, e));
          setModified(true);
          throw getCurrentBufferedSourceException();
      }
  </code></pre>
  
  <p>Here is the code for custom Converter::</p>
  
  <pre><code>public class StringToCellElementConverter implements Converter&lt;String, CellElement&gt; {
  
      private static final long serialVersionUID = -3138155389689170574L;
  
      @Override
      public CellElement convertToModel(String value,
              Class&lt;? extends CellElement&gt; targetType, Locale locale)
              throws com.vaadin.data.util.converter.Converter.ConversionException {
          return new CellElement(Integer.parseInt(value), true);
      }
  
      @Override
      public String convertToPresentation(CellElement value,
              Class&lt;? extends String&gt; targetType, Locale locale)
              throws com.vaadin.data.util.converter.Converter.ConversionException {
          String ret_val = value.getValue();
          return ret_val;
      }
  
      @Override
      public Class&lt;CellElement&gt; getModelType() {
          return CellElement.class;
      }
  
      @Override
      public Class&lt;String&gt; getPresentationType() {
          return String.class;
      }
  
  }
  </code></pre>
  
  <p>Code for CellElement. I may have some configuration issues inside this class. It was originally not designed for this purpose::</p>
  
  <pre><code>public class CellElement implements Property
  {
  
      private static final long serialVersionUID = -6205569838194226408L;
      private Integer value;
      private boolean readOnly;
      private boolean selected;
  
      public CellElement( Integer value, boolean readOnly )
      {
          this.value = value;
          this.readOnly = readOnly;
          this.selected = false;
      }
  
      public boolean isSelected()
      {
          return selected;
      }
  
      public void setSelected( boolean selected )
      {
          this.selected = selected;
      }
  
      public Integer getIntegerValue()
      {
          return value;
      }
  
      public int getIntValue()
      {
          return value.intValue();
      }
  
      @Override
      public String getValue()
      {
          return value.toString();
      }
  
      @Override
      public void setValue(Object newValue)
                  throws ReadOnlyException, ConversionException {
              if (readOnly)
                  throw new ReadOnlyException();
  
              // Already the same type as the internal representation
              if (newValue instanceof Integer)
                  value = (Integer) newValue;
  
              // Conversion from a string is required
              else if (newValue instanceof String)
                  try {
                      value = Integer.parseInt((String) newValue);
                  } catch (NumberFormatException e) {
                      throw new ConversionException();
                  }
              else
                   // Don't know how to convert any other types
                  throw new ConversionException();
          }        
  
      @Override
      public Class getType() {
          return this.getClass();
      }    
  
      @Override
      public boolean isReadOnly() {
          return this.readOnly;
      }    
  
      @Override
      public void setReadOnly(boolean newStatus) {
          this.readOnly = newStatus;  
      }
  }
  </code></pre>
  
  <p>Here is the stack trace::</p>
  
  <pre><code>com.vaadin.data.Buffered$SourceException
      at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:637)
      at com.example.demo.Sudoku.init(Sudoku.java:134)
      at com.vaadin.ui.UI.doInit(UI.java:639)
      at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222)
      at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
      at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
      at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402)
      at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Unknown Source)
  Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.example.demo.CellElement
      at com.example.demo.StringToCellElementConverter.convertToPresentation(StringToCellElementConverter.java:1)
      at com.vaadin.data.util.converter.ConverterUtil.convertFromModel(ConverterUtil.java:94)
      at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:708)
      at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:693)
      at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:629)
      ... 28 more
  
  Nov 21, 2014 4:01:31 PM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [com.example.demo.Sudoku$Servlet] in context with path [/VaadinUltimate] threw exception [com.vaadin.server.ServiceException: com.vaadin.data.Buffered$SourceException] with root cause
  java.lang.ClassCastException: java.lang.String cannot be cast to com.example.demo.CellElement
      at com.example.demo.StringToCellElementConverter.convertToPresentation(StringToCellElementConverter.java:1)
      at com.vaadin.data.util.converter.ConverterUtil.convertFromModel(ConverterUtil.java:94)
      at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:708)
      at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:693)
      at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:629)
      at com.example.demo.Sudoku.init(Sudoku.java:134)
      at com.vaadin.ui.UI.doInit(UI.java:639)
      at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222)
      at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
      at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
      at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402)
      at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>