Example #1
0
  public ListaVentas(final Sgc_capa_web mainWindow) {

    String PATH_IMG = "/sgc_capa_web/images/";
    VLayout layout = new VLayout(10);
    layout.setBackgroundColor("#006633");
    final ListGrid facturaGrid = new ListGrid();

    facturaGrid.setWidth(500);
    facturaGrid.setHeight(224);
    facturaGrid.setShowAllRecords(true);
    facturaGrid.setAlternateRecordStyles(true);
    facturaGrid.setShowEdges(true);
    facturaGrid.setBorder("0px");
    facturaGrid.setBodyStyleName("normal");
    facturaGrid.setLeaveScrollbarGap(false);
    facturaGrid.setBackgroundColor("#99ffcc");

    /*-Buscar ------------------------------*/
    DynamicForm buscarFields = new DynamicForm();
    // buscarFields.setBackgroundColor("#99ffcc");
    buscarFields.setItemLayout(FormLayoutType.ABSOLUTE);

    final TextItem codigoText = new TextItem("codigo");
    codigoText.setWrapTitle(false);
    codigoText.setLeft(10);
    codigoText.setWidth(43);
    codigoText.addKeyPressHandler(
        new KeyPressHandler() {
          public void onKeyPress(KeyPressEvent event) {
            if ("Enter".equals(event.getKeyName())) {
              /* buscar por el campo correspondiente */
              if (codigoText.getValue() != null) {

                Factura factura = new Factura();
                factura.setId(Integer.parseInt(codigoText.getValue().toString()));
                listar(facturaGrid, factura, "codigo");
              }
            }
          }
        });

    ButtonItem buscarButton = new ButtonItem("find", "");
    buscarButton.setIcon("view.png");
    buscarButton.setWidth(50);
    buscarButton.setLeft(443);
    buscarButton.addClickHandler(
        new ClickHandler() {
          @Override
          public void onClick(ClickEvent event) {
            /* buscar por el campo correspondiente */
            Factura factura = new Factura();

            if (codigoText.getValue() != null)
              factura.setId(Integer.parseInt(codigoText.getValue().toString()));
            /*if(nombreusuarioText.getValue() != null)
            cliente.setNombreusuario(nombreusuarioText.getValue().toString());*/
            listar(facturaGrid, factura, "nombre");
          }
        });

    buscarFields.setFields(codigoText, buscarButton);
    /*--------------------------------------*/

    ListGridField codigoField = new ListGridField("codigo", "Codigo");
    ListGridField fechaField = new ListGridField("fecha", "Fecha");
    ListGridField numeroField = new ListGridField("numero", "Numero");
    ListGridField pendienteField = new ListGridField("pendiente", "Pendiente");
    ListGridField saldoField = new ListGridField("saldo", "Saldo");
    ListGridField editarField = new ListGridField("edit", "Editar");
    ListGridField borrarField = new ListGridField("remove", "Borrar");

    codigoField.setAlign(Alignment.CENTER);
    editarField.setAlign(Alignment.CENTER);
    borrarField.setAlign(Alignment.CENTER);

    editarField.setType(ListGridFieldType.IMAGE);
    borrarField.setType(ListGridFieldType.IMAGE);

    editarField.setImageURLPrefix(PATH_IMG);
    borrarField.setImageURLPrefix(PATH_IMG);

    editarField.setImageURLSuffix(".png");
    borrarField.setImageURLSuffix(".png");

    facturaGrid.addCellClickHandler(
        new CellClickHandler() {
          @Override
          public void onCellClick(CellClickEvent event) {
            ListGridRecord record = event.getRecord();
            int col = event.getColNum();
            if (col > 4) {

              Factura factura = new Factura();
              factura.setId(record.getAttributeAsInt("codigo"));
              factura.setFecha(record.getAttributeAsDate("fecha"));
              factura.setNumero(Integer.parseInt(record.getAttribute("numero")));
              factura.setPendiente(record.getAttribute("pendiente"));
              factura.setSaldo(Double.parseDouble(record.getAttribute("saldo")));

              if (col == 5) {
                /* Editar */

                new VentaDetalle(factura, mainWindow);

              } else {
                /* Borrar */

                FacturaServiceAsync service = GWT.create(FacturaService.class);
                ServiceDefTarget serviceDef = (ServiceDefTarget) service;
                serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL() + "facturaService");
                try {
                  service.eliminar(
                      record.getAttributeAsInt("codigo"),
                      new AsyncCallback<Void>() {

                        @Override
                        public void onFailure(Throwable caught) {
                          Window.alert(
                              "Ocurrio un error y no se puedo eliminar (objeto referenciado)"); // "
                          // +
                          // caught.getClass().getName() + " " + caught.getMessage()) ;
                        }

                        @Override
                        public void onSuccess(Void result) {
                          new ListaVentas(mainWindow);
                        }
                      });
                } catch (NumberFormatException e) {
                  e.printStackTrace();
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }
            }
          }
        });

    codigoField.setWidth(50);
    fechaField.setWidth(180);
    numeroField.setWidth(50);
    pendienteField.setWidth(50);
    saldoField.setWidth(50);
    facturaGrid.setFields(
        codigoField, fechaField, numeroField, pendienteField, saldoField, editarField, borrarField);
    facturaGrid.setCanResizeFields(true);

    ButtonItem button = new ButtonItem("add", "Agregar");
    button.setStartRow(false);
    button.setWidth(80);
    button.setIcon("add.png");
    button.setAlign(Alignment.CENTER);
    button.addClickHandler(
        new ClickHandler() {
          @Override
          public void onClick(ClickEvent event) {
            new VentaDetalle(mainWindow);
          }
        });

    listar(facturaGrid, new Factura(), "nombre");

    Label label = new Label();
    label.setBackgroundColor("#99ffcc");
    label.setHeight(30);
    label.setWidth(500);
    label.setPadding(10);
    label.setAlign(Alignment.CENTER);
    label.setValign(VerticalAlignment.CENTER);
    label.setWrap(false);
    label.setShowEdges(true);
    label.setContents("<div style='color:black;font-size:15'><b>Lista de Ventas</b></div>");

    layout.addMember(label);
    layout.addMember(buscarFields);
    layout.addMember(facturaGrid);

    DynamicForm form = new DynamicForm();
    // form.setBackgroundColor("#99ffcc");
    form.setWidth(300);
    form.setItems(button);
    layout.addMember(form);
    mainWindow.showDialog(layout);
  }