package org.jboss.errai.demo.summit2013.client.local;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.jboss.errai.common.client.api.Caller;
import org.jboss.errai.databinding.client.api.DataBinder;
import org.jboss.errai.databinding.client.api.PropertyChangeEvent;
import org.jboss.errai.databinding.client.api.PropertyChangeHandler;
import org.jboss.errai.demo.summit2013.client.shared.UserComplaint;
import org.jboss.errai.demo.summit2013.client.shared.UserComplaintEndpoint;
import org.jboss.errai.enterprise.client.jaxrs.api.ResponseCallback;
import org.jboss.errai.ui.client.widget.HasModel;
import org.jboss.errai.ui.client.widget.ValueImage;
import org.jboss.errai.ui.shared.api.annotations.AutoBound;
import org.jboss.errai.ui.shared.api.annotations.Bound;
import org.jboss.errai.ui.shared.api.annotations.DataField;
import org.jboss.errai.ui.shared.api.annotations.Templated;

import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;

@Templated("Admin.html#complaint")
public class ComplaintWidget extends Composite implements HasModel<UserComplaint>
{
   @Inject
   @AutoBound
   private DataBinder<UserComplaint> userComplaint;

   @Bound
   @DataField
   private Element id = DOM.createTD();
   @Inject
   @Bound
   @DataField
   private CheckBox done;
   @Bound
   @DataField
   private Element name = DOM.createTD();
   @Bound
   @DataField
   private Element email = DOM.createTD();
   @Bound
   @DataField
   private Element complaint = DOM.createTD();
   @Inject
   @Bound
   @DataField
   private ValueImage image;

   @Inject
   private Caller<UserComplaintEndpoint> endpoint;

   @PostConstruct
   private void init()
   {
      userComplaint.addPropertyChangeHandler("done", new PropertyChangeHandler<Boolean>() {
         @Override
         public void onPropertyChange(PropertyChangeEvent<Boolean> event)
         {
            updateDoneStyle();
            endpoint.call(new ResponseCallback() {
               @Override
               public void callback(Response response)
               {}
            }).update(getModel().getId(), getModel());
         }
      });
   }

   @Override
   public UserComplaint getModel()
   {
      return userComplaint.getModel();
   }

   @Override
   public void setModel(UserComplaint model)
   {
      userComplaint.setModel(model);
      updateDoneStyle();
   }
   
   private void updateDoneStyle() {
	   if (userComplaint.getModel().isDone())
       {
          removeStyleName("issue-open");
          addStyleName("issue-closed");
       }
       else
       {
          removeStyleName("issue-closed");
          addStyleName("issue-open");
       }
   }
}

@@ -507,77 +507,8 @@
 nt;%0A
-import org.jboss.errai.enterprise.client.jaxrs.api.ResponseCallback;%0A
 impo
@@ -553,16 +553,16 @@
 sModel;%0A
+
 import o
@@ -846,52 +846,8 @@
 d;%0A%0A
-import com.google.gwt.http.client.Response;%0A
 impo
@@ -1977,144 +1977,8 @@
 all(
-new ResponseCallback() %7B%0A               @Override%0A               public void callback(Response response)%0A               %7B%7D%0A            %7D
 ).up
@@ -2270,19 +2270,16 @@
 );%0A   %7D%0A
-   
 %0A   priv
@@ -2308,12 +2308,17 @@
 le()
- %7B%0A%09
+%0A   %7B%0A   
    i
@@ -2357,28 +2357,26 @@
 ne())%0A      
- 
 %7B%0A
- 
          rem
@@ -2404,33 +2404,32 @@
 pen%22);%0A         
- 
 addStyleName(%22is
@@ -2444,28 +2444,26 @@
 ed%22);%0A      
- 
 %7D%0A
- 
       else%0A 
@@ -2463,28 +2463,26 @@
  else%0A      
- 
 %7B%0A
- 
          rem
@@ -2512,33 +2512,32 @@
 sed%22);%0A         
- 
 addStyleName(%22is
@@ -2544,25 +2544,24 @@
 sue-open%22);%0A
- 
       %7D%0A   %7D

Fix styling issues