/**
  * faz a construção dos itens(listitem) da janela, pegando o conteudo da div listitem (que está na
  * definição da janela utilizando o componente crudWindow) e adiciona na jenela gerada pelo
  * components
  *
  * @param composer
  */
 private void buildListItem(ComposerController<?> composer) {
   if (this.divListitem != null && composer.getListitem() != null) {
     buildListItemFromComposer(composer);
   } else if (this.divListitem != null && composer.getListitem() == null) {
     buildListItemFromEntity(composer);
   }
 }
  /**
   * faz a construçãoo do cabeçalho(listheader) da janela, pegando o conteudo da div listhead (que
   * está na definição da janela utilizando o componente crudWindow) e adiciona na jenela gerada
   * pelo componente
   *
   * @param composer
   */
  private void buildListHearder(ComposerController<?> composer) {
    // Pega os campos configurados no template do componete para jogar na
    // janela construida
    if (this.divListhead != null && composer.getListhead() != null) {
      builderListHeaderFromComposer(composer);

    } else if (this.divListhead != null && composer.getListhead() == null) {
      builderListHeaderFromEntity(composer);
    }
  }
  /**
   * Faz a inicialização do compoente de FormCrudWindow
   *
   * @param composer
   */
  private void initFormCrudWindow(ComposerController<?> composer) {
    if (this.crudWindow != null) setTitle(this.crudWindow.getTitulo());

    if (composer.getForm() != null) {
      composer.getForm().setParent(divFields);
    } else {
      buildDivFieldsFromEntity(composer);
    }

    composer.getComponent().setAttribute("formCrudWindow", this);

    this.formCrudWindowBtnSave.setVisible(this.isCanSave());
  }
  private void buildDivFieldsFromEntity(ComposerController<?> composer) {
    //		AnnotateDataBinder binder = new AnnotateDataBinder(crudFormWindow);
    //		binder.bindBean("controller2", composer);

    if (divFields.getChildren().size() == 0) {
      List<Component> list = new ArrayList<Component>(divFields.getChildren());
      for (Component c : list) {
        c.detach();
      }
      d = new Div();
      g = new Grid();
      d.appendChild(g);

      binder = new AnnotateDataBinder(g);
      binder.bindBean("controller2", composer);
      Columns c = new Columns();
      Column col1 = new Column();
      col1.setWidth("120px");
      Column col2 = new Column();
      col2.setAlign("left");
      c.appendChild(col1);
      c.appendChild(col2);
      Rows r = new Rows();
      Textbox t;
      Checkbox checkbox;
      for (ManagedBeanField field : composer.getListColumns()) {
        if (field.isVisible()) {
          Row row = new Row();
          row.appendChild(new Label(Labels.getLabel(field.getFieldCaption())));
          String fieldName = "controller2." + field.getFieldName() + "";

          if (field.getFieldType().equalsIgnoreCase("boolean")) {
            checkbox = new Checkbox();
            binder.addBinding(checkbox, "checked", fieldName);
            row.appendChild(checkbox);

          } else {
            t = new Textbox();

            t.setWidth("98%");

            binder.addBinding(t, "value", fieldName);
            row.appendChild(t);
          }
          r.appendChild(row);
        }
      }
      g.appendChild(r);

      d.setParent(this.divFields);
      // binder.loadAll();
    }
    binder.loadComponent(g);
    // binder.saveAll();
  }
  /**
   * Metodo que constroi o ListHeader pegando o conteúdo da div listheader do composer
   *
   * @param composer
   */
  private void builderListHeaderFromComposer(ComposerController<?> composer) {
    if (this.divListhead.getChildren().size() == 1) {
      Listheader oper = (Listheader) this.divListhead.getChildren().get(0);
      this.divListhead.removeChild(oper);

      List<Component> list = new ArrayList<Component>(composer.getListhead().getChildren());
      for (Component l : list) {
        Listheader h = (Listheader) l;
        this.divListhead.appendChild(h);
      }
      this.divListhead.appendChild(oper);
    }
  }
  /**
   * Faz a construção dos itens(listemitem) da listagem, pegando o conteudo da div listitem (que
   * está na definição da janela utilizando o compoente crudWindow) e adiciona na janela gerada pela
   * componente
   *
   * @param composer
   */
  private void buildListItemFromComposer(ComposerController<?> composer) {
    if (this.divListitem.getChildren().size() == 1) {
      Listcell cell = (Listcell) this.divListitem.getChildren().get(0);
      this.divListitem.removeChild(cell);

      List<Component> list = new ArrayList<Component>(composer.getListitem().getChildren());
      for (Component l : list) {
        Listcell h = (Listcell) l;
        this.divListitem.appendChild(h);
      }
      this.divListitem.appendChild(cell);
    }
  }
 /**
  * Metodo que constroi o Listheader baseado nos campos da Entity
  *
  * @param composer composer para construir a lista de campos
  */
 private void builderListHeaderFromEntity(ComposerController<?> composer) {
   if (this.divListhead.getChildren().size() == 1) {
     Listheader oper = (Listheader) this.divListhead.getChildren().get(0);
     this.divListhead.removeChild(oper);
     for (ManagedBeanField field : composer.getListColumns()) {
       if (field.isVisible()) {
         Listheader l = new Listheader(Labels.getLabel(field.getFieldCaption()));
         l.setSort("auto(" + field.getKey() + ")");
         this.divListhead.appendChild(l);
       }
     }
     this.divListhead.appendChild(oper);
   }
 }
  /**
   * Faz a inicialização do compoente de CrudWindow
   *
   * @param composer
   */
  private void initCrudWindow(ComposerController<?> composer) {
    if (this.getChildren() != null) {
      setComposerRecursive(this.getChildren().get(0), composer);
    }

    composer.setEditForm(this.editFormCrudWindow);
    // composer.setUseCase(getTitulo());

    if (composer.getSearchForm() != null) {
      composer.getSearchForm().setParent(divSearch);
      // new AnnotateDataBinder(divSearch).loadAll();
    }
    if (this.divResult != null) {
      composer.getResult().setParent(divResult);
      new AnnotateDataBinder(divResult).loadAll();
    }
    buildListHearder(composer);
    buildListItem(composer);
    buildAcaoPainel(composer);

    // inicializar as variaveis de controle de operação baseado no atributo
    // crudOperation
    initCrudOpeartion();

    updateComponents();

    editFormCrudWindow.setCanSave(this.isCrudUpdateEnable());

    // composer.getForm().setParent(divForm);
    // new AnnotateDataBinder(divForm).loadAll();

    composer.getComponent().setAttribute("builderSoftCrudWindow", this);

    this.crudWindow.setWidth(this.getWidth());
    this.crudWindow.setHeight(this.getHeight());
    this.divListBox.setPageSize(Integer.parseInt(this.getListBoxSize()));
  }
    @Override
    public void render(Listitem listItem, Object data, int paramInt) throws Exception {
      Entity entityItem = (Entity) data;
      listItem.setValue(entityItem);

      for (ManagedBeanField field : composer.getListColumns()) {
        if (field.isVisible()) {
          Listcell l = new Listcell();

          l.setLabel(String.valueOf(Reflection.getFieldValue(entityItem, field.getKey())));
          l.setParent(listItem);
        }
      }
      ((Button) cellOper.getChildren().get(0)).setId(null);
      ((Button) cellOper.getChildren().get(1)).setId(null);
      Listcell vOper = ((Listcell) cellOper.clone());
      vOper.setParent(listItem);
      // this.divListBox.appendChild(cellOper);

      // new Listcell(prod.getName()).setParent(listItem);
      // new Listcell(""+prod.getPrice()).setParent(listItem);
      // new Listcell(""+cartItem.getAmount()).setParent(listItem);

    }
 /**
  * adiciona no painel de ação uma ação encontrada na proprieadade acaoPainel do composer
  *
  * @param composer
  */
 private void buildAcaoPainel(ComposerController<?> composer) {
   if (this.divAcao != null && composer.getAcaoPainel() != null) {
     composer.getAcaoPainel().setParent(this.divAcao);
   }
 }