예제 #1
0
 public void testNumberConverterCurrency() throws Exception {
   UIInput text = new UIInput();
   NumberConverter converter = (NumberConverter) application.createConverter("javax.faces.Number");
   converter.setType("currency");
   converter.setLocale(Locale.FRANCE);
   String toConv = "12 345,68 " + '\u20aC';
   Number number = (Number) converter.getAsObject(getFacesContext(), text, toConv);
   assertTrue(number != null);
 }
 // Create and configure a NumberConverter
 protected NumberConverter createNumberConverter() {
   NumberConverter nc = new NumberConverter();
   nc.setCurrencyCode("USD");
   nc.setCurrencySymbol("$");
   nc.setGroupingUsed(false);
   nc.setIntegerOnly(true);
   nc.setMaxFractionDigits(2);
   nc.setMaxIntegerDigits(10);
   nc.setMinFractionDigits(2);
   nc.setMinIntegerDigits(5);
   nc.setType("currency");
   return (nc);
 }
 protected void checkNumberConverters(NumberConverter nc1, NumberConverter nc2) {
   assertNotNull(nc1);
   assertNotNull(nc2);
   assertEquals(nc1.getCurrencyCode(), nc2.getCurrencyCode());
   assertEquals(nc1.getCurrencySymbol(), nc2.getCurrencySymbol());
   assertEquals(nc1.isGroupingUsed(), nc2.isGroupingUsed());
   assertEquals(nc1.isIntegerOnly(), nc2.isIntegerOnly());
   assertEquals(nc1.getMaxFractionDigits(), nc2.getMaxFractionDigits());
   assertEquals(nc1.getMaxIntegerDigits(), nc2.getMaxIntegerDigits());
   assertEquals(nc1.getMinFractionDigits(), nc2.getMinFractionDigits());
   assertEquals(nc1.getMinIntegerDigits(), nc2.getMinIntegerDigits());
   assertEquals(nc1.getLocale(), nc2.getLocale());
   assertEquals(nc1.getPattern(), nc2.getPattern());
   assertEquals(nc1.getType(), nc2.getType());
 }
  @SuppressWarnings("deprecation")
  public void testWidgetProcessor() throws Exception {

    StandardConverterProcessor processor = new StandardConverterProcessor();

    HtmlInputText htmlInputText = new HtmlInputText();

    // Actions get no Converters

    processor.processWidget(htmlInputText, ACTION, null, null);
    assertEquals(null, htmlInputText.getConverter());

    // Empty attributes get no Converters

    Map<String, String> attributes = CollectionUtils.newHashMap();
    attributes.put(NAME, "foo");
    processor.processWidget(htmlInputText, PROPERTY, attributes, null);
    assertEquals(null, htmlInputText.getConverter());
    assertEquals(null, htmlInputText.getLabel());

    // Explicit converter

    attributes.put(FACES_CONVERTER, "fooConverter");
    processor.processWidget(htmlInputText, PROPERTY, attributes, null);
    assertEquals("fooConverter", htmlInputText.getConverter().toString());

    // Explicit EL-based converter

    attributes.put(FACES_CONVERTER, "#{foo.converter}");
    htmlInputText = new HtmlInputText();
    processor.processWidget(htmlInputText, PROPERTY, attributes, null);
    assertEquals(
        "#{foo.converter}", htmlInputText.getValueBinding("converter").getExpressionString());
    attributes.remove(FACES_CONVERTER);

    // Implicit DateTimeConverter

    attributes.put(TYPE, Date.class.getName());
    htmlInputText = new HtmlInputText();
    processor.processWidget(htmlInputText, PROPERTY, attributes, new HtmlMetawidget());
    assertTrue(htmlInputText.getConverter() instanceof DateTimeConverter);
    htmlInputText = new HtmlInputText();

    // DateTimeConverter

    attributes.put(DATE_STYLE, "full");
    attributes.put(TIME_STYLE, "medium");
    attributes.put(LOCALE, "UK");
    attributes.put(DATETIME_PATTERN, "dd/MM/yyyy");
    attributes.put(TIME_ZONE, "Australia/Sydney");
    attributes.put(DATETIME_TYPE, "date");
    processor.processWidget(htmlInputText, PROPERTY, attributes, new HtmlMetawidget());
    assertEquals(null, htmlInputText.getLabel());

    DateTimeConverter dateTimeConverter = (DateTimeConverter) htmlInputText.getConverter();
    assertEquals("full", dateTimeConverter.getDateStyle());
    assertEquals("medium", dateTimeConverter.getTimeStyle());
    assertEquals("uk", dateTimeConverter.getLocale().getLanguage());
    assertEquals("dd/MM/yyyy", dateTimeConverter.getPattern());
    assertEquals("Australia/Sydney", dateTimeConverter.getTimeZone().getID());
    assertEquals("date", dateTimeConverter.getType());

    // NumberConverter

    attributes.clear();
    attributes.put(CURRENCY_CODE, "AUD");
    attributes.put(CURRENCY_SYMBOL, "$");
    attributes.put(NUMBER_USES_GROUPING_SEPARATORS, TRUE);
    attributes.put(LOCALE, "AU");
    attributes.put(MINIMUM_FRACTIONAL_DIGITS, "0");
    attributes.put(MAXIMUM_FRACTIONAL_DIGITS, "1");
    attributes.put(MINIMUM_INTEGER_DIGITS, "2");
    attributes.put(MAXIMUM_INTEGER_DIGITS, "5");
    attributes.put(NUMBER_PATTERN, "#0.00");
    attributes.put(NUMBER_TYPE, "currency");

    // (should not overwrite existing Converter)

    processor.processWidget(htmlInputText, PROPERTY, attributes, null);
    assertEquals(dateTimeConverter, htmlInputText.getConverter());
    htmlInputText.setConverter(null);
    processor.processWidget(htmlInputText, PROPERTY, attributes, null);

    NumberConverter numberConverter = (NumberConverter) htmlInputText.getConverter();
    assertEquals("AUD", numberConverter.getCurrencyCode());
    assertEquals("$", numberConverter.getCurrencySymbol());
    assertTrue(numberConverter.isGroupingUsed());
    assertEquals("au", numberConverter.getLocale().getLanguage());
    assertEquals(0, numberConverter.getMinFractionDigits());
    assertEquals(1, numberConverter.getMaxFractionDigits());
    assertEquals(2, numberConverter.getMinIntegerDigits());
    assertEquals(5, numberConverter.getMaxIntegerDigits());
    assertEquals("#0.00", numberConverter.getPattern());
    assertEquals("currency", numberConverter.getType());
  }
예제 #5
0
  protected Converter createConverter() throws JspException {
    FacesContext context = FacesContext.getCurrentInstance();
    ELContext elctx = context.getELContext();

    if (binding != null) {
      Converter boundConverter = (NumberConverter) binding.getValue(elctx);
      if (boundConverter != null) return boundConverter;
    }

    NumberConverter converter = new NumberConverter();

    if (currencyCode != null) converter.setCurrencyCode((String) currencyCode.getValue(elctx));
    if (currencySymbol != null)
      converter.setCurrencySymbol((String) currencySymbol.getValue(elctx));
    if (groupingUsed != null) converter.setGroupingUsed((Boolean) groupingUsed.getValue(elctx));
    if (integerOnly != null) converter.setIntegerOnly((Boolean) integerOnly.getValue(elctx));
    if (maxFractionDigits != null)
      converter.setMaxFractionDigits((Integer) maxFractionDigits.getValue(elctx));
    if (minFractionDigits != null)
      converter.setMinFractionDigits((Integer) minFractionDigits.getValue(elctx));
    if (minIntegerDigits != null)
      converter.setMinIntegerDigits((Integer) minIntegerDigits.getValue(elctx));
    if (pattern != null) converter.setPattern((String) pattern.getValue(elctx));
    if (type != null) converter.setType((String) type.getValue(elctx));

    Locale loc = null;
    if (locale != null) loc = FacesUtils.getLocaleFromExpression(context, locale);
    if (loc == null) loc = context.getViewRoot().getLocale();
    converter.setLocale(loc);

    if (binding != null) binding.setValue(elctx, converter);
    return converter;
  }
  public Converter getConverter(ValueHolder valueHolder, Map<String, String> attributes) {

    // Use existing Converter (if any)

    Converter converter = valueHolder.getConverter();

    if (converter != null) {
      return converter;
    }

    // Create from id

    FacesContext context = FacesContext.getCurrentInstance();
    String converterId = attributes.get(FACES_CONVERTER);

    if (converterId != null) {

      converter = context.getApplication().createConverter(converterId);

    } else if (valueHolder instanceof UISelectOne || valueHolder instanceof UISelectMany) {

      // Create from parameterized type (eg. a Date converter for List<Date>)

      String parameterizedType = WidgetBuilderUtils.getComponentType(attributes);

      if (parameterizedType != null) {
        Class<?> parameterizedClass = ClassUtils.niceForName(parameterizedType);

        // The parameterized type might be null, or might not be concrete
        // enough to be instantiatable (eg. List<? extends Foo>)

        if (parameterizedClass != null) {
          converter = context.getApplication().createConverter(parameterizedClass);
        }
      }

    } else {

      // Create implicit converters
      //
      // JSF does not appear to implicitly hook up DateTimeConverters without either an
      // explicit f:convertDateTime tag or a registered java.util.Date converter. Adding one
      // fixes both POSTback and display of read-only dates (otherwise JSF uses Date.toString)
      //
      // JSF *does* appear to implicitly hook up NumberConverters.

      String type = attributes.get(TYPE);

      if (type != null) {

        Class<?> clazz = ClassUtils.niceForName(type);

        if (clazz != null) {

          if (clazz.isAssignableFrom(Date.class)) {
            converter = getDateTimeConverter(converter);
          }
        }
      }
    }

    // Support for DateTimeConverter

    if (attributes.containsKey(DATE_STYLE)) {
      converter = getDateTimeConverter(converter);
      ((DateTimeConverter) converter).setDateStyle(attributes.get(DATE_STYLE));
    }

    if (attributes.containsKey(DATETIME_PATTERN)) {
      converter = getDateTimeConverter(converter);
      ((DateTimeConverter) converter).setPattern(attributes.get(DATETIME_PATTERN));
    }

    if (attributes.containsKey(TIME_STYLE)) {
      converter = getDateTimeConverter(converter);
      ((DateTimeConverter) converter).setTimeStyle(attributes.get(TIME_STYLE));
    }

    if (attributes.containsKey(TIME_ZONE)) {
      converter = getDateTimeConverter(converter);
      ((DateTimeConverter) converter).setTimeZone(TimeZone.getTimeZone(attributes.get(TIME_ZONE)));
    }

    if (attributes.containsKey(DATETIME_TYPE)) {
      converter = getDateTimeConverter(converter);
      ((DateTimeConverter) converter).setType(attributes.get(DATETIME_TYPE));
    }

    // Support for NumberConverter

    if (attributes.containsKey(CURRENCY_CODE)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter).setCurrencyCode(attributes.get(CURRENCY_CODE));
    }

    if (attributes.containsKey(CURRENCY_SYMBOL)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter).setCurrencySymbol(attributes.get(CURRENCY_SYMBOL));
    }

    if (attributes.containsKey(NUMBER_USES_GROUPING_SEPARATORS)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter)
          .setGroupingUsed(Boolean.parseBoolean(attributes.get(NUMBER_USES_GROUPING_SEPARATORS)));
    }

    if (attributes.containsKey(MINIMUM_INTEGER_DIGITS)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter)
          .setMinIntegerDigits(Integer.parseInt(attributes.get(MINIMUM_INTEGER_DIGITS)));
    }

    if (attributes.containsKey(MAXIMUM_INTEGER_DIGITS)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter)
          .setMaxIntegerDigits(Integer.parseInt(attributes.get(MAXIMUM_INTEGER_DIGITS)));
    }

    if (attributes.containsKey(MINIMUM_FRACTIONAL_DIGITS)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter)
          .setMinFractionDigits(Integer.parseInt(attributes.get(MINIMUM_FRACTIONAL_DIGITS)));
    }

    if (attributes.containsKey(MAXIMUM_FRACTIONAL_DIGITS)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter)
          .setMaxFractionDigits(Integer.parseInt(attributes.get(MAXIMUM_FRACTIONAL_DIGITS)));
    }

    if (attributes.containsKey(NUMBER_PATTERN)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter).setPattern(attributes.get(NUMBER_PATTERN));
    }

    if (attributes.containsKey(NUMBER_TYPE)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter).setType(attributes.get(NUMBER_TYPE));
    }

    // Locale (applies to both DateTimeConverter and NumberConverter)

    if (attributes.containsKey(LOCALE)) {
      if (converter instanceof NumberConverter) {
        ((NumberConverter) converter).setLocale(new Locale(attributes.get(LOCALE)));
      } else {
        converter = getDateTimeConverter(converter);
        ((DateTimeConverter) converter).setLocale(new Locale(attributes.get(LOCALE)));
      }
    }

    // Return it

    return converter;
  }
예제 #7
0
  public void wygeneruj(HashMap lista) throws Exception {
    FacesContext facesCtx = FacesContext.getCurrentInstance();
    ELContext elContext = facesCtx.getELContext();
    ExpressionFactory ef = ExpressionFactory.newInstance();

    akordeon = new AccordionPanel();
    // robienie glownej oprawy
    Set nazwyew = lista.keySet();
    Iterator it;
    it = nazwyew.iterator();
    int i = 0;
    while (it.hasNext()) {
      String nazwapj = (String) it.next();
      Tab tab = new Tab();
      tab.setId("tabek" + i);
      tab.setTitle("ewidencja: " + nazwapj);

      DataTable dataTable = new DataTable();
      dataTable.setId("tablica" + i);
      // dataTable.setResizableColumns(true);
      dataTable.setVar("var");
      dataTable.setValue(lista.get(nazwapj));
      dataTable.setStyle("width: 1000px;");
      // tak trzeba opisac kazda kolumne :)
      ArrayList<String> opisykolumn = new ArrayList<>();
      opisykolumn.addAll(EVatViewPola.getOpispol());
      Iterator itx;
      itx = opisykolumn.iterator();
      while (itx.hasNext()) {
        String wstawka = (String) itx.next();
        Column column = new Column();
        column.setHeaderText(wstawka);
        final String binding = "#{var." + wstawka + "}";
        ValueExpression ve = ef.createValueExpression(elContext, binding, String.class);
        HtmlOutputText ot = new HtmlOutputText();
        ot.setValueExpression("value", ve);
        switch (wstawka) {
          case "kontr":
            column.setWidth("350");
            break;
          case "id":
            column.setWidth("50");
            break;
          case "netto":
            ot.setStyle("float: right;");
            NumberConverter numx = new NumberConverter();
            numx.setMaxFractionDigits(2);
            numx.setMinFractionDigits(2);
            ot.setConverter(numx);
          case "vat":
            ot.setStyle("float: right;");
            NumberConverter numy = new NumberConverter();
            numy.setMaxFractionDigits(2);
            numy.setLocale(new Locale("PL"));
            numy.setGroupingUsed(true);
            ot.setConverter(numy);
        }
        column.getChildren().add(ot);
        dataTable.getChildren().add(column);
      }
      Separator sep = new Separator();
      CommandButton button = new CommandButton();
      button.setValue("PobierzPdf");
      button.setType("button");
      button.setId("przyciskpdf" + i);
      FacesContext context = FacesContext.getCurrentInstance();
      MethodExpression actionListener =
          context
              .getApplication()
              .getExpressionFactory()
              .createMethodExpression(
                  context.getELContext(),
                  "#{pdf.drukujewidencje('zakup')}",
                  null,
                  new Class[] {ActionEvent.class});
      button.addActionListener(new MethodExpressionActionListener(actionListener));
      //            MethodExpression methodExpressionX =
      // context.getApplication().getExpressionFactory().createMethodExpression(
      //            context.getELContext(), "#{pdf.drukujewidencje('"+nazwapj+"')}", null, new
      // Class[] {});
      //            button.setActionExpression(methodExpressionX);
      String nowanazwa;
      if (nazwapj.contains("sprzedaż")) {
        nowanazwa = nazwapj.substring(0, nazwapj.length() - 1);
      } else {
        nowanazwa = nazwapj;
      }
      String tablican =
          "PrimeFaces.ab({source:'form:przyciskpdf"
              + i
              + "',onsuccess:function(data,status,xhr){wydrukewidencje('"
              + wpisView.getPodatnikWpisu()
              + "','"
              + nowanazwa
              + "');;}});return false;";
      button.setOnclick(tablican);
      tab.getChildren().add(dataTable);
      tab.getChildren().add(sep);
      tab.getChildren().add(button);
      akordeon.getChildren().add(tab);

      i++;
    }

    // generowanie podsumowania
    List<EVatwpisSuma> suma2 = new ArrayList<>();
    suma2.addAll(sumaewidencji.values());
    Tab tab = new Tab();
    tab.setId("tabekdsuma");
    tab.setTitle("podsumowanie ewidencji");
    DataTable dataTable = new DataTable();
    dataTable.setId("tablicasuma");
    dataTable.setResizableColumns(true);
    dataTable.setVar("var");
    dataTable.setValue(suma2);
    dataTable.setStyle("width: 1000px;");
    List<String> opisykolumny = new ArrayList<>();
    opisykolumny.add("ewidencja");
    opisykolumny.add("netto");
    opisykolumny.add("vat");
    Iterator ity = opisykolumny.iterator();
    while (ity.hasNext()) {
      String wstawka = (String) ity.next();
      Column column = new Column();
      column.setHeaderText(wstawka);
      HtmlOutputText ot = new HtmlOutputText();
      if (!wstawka.equals("ewidencja")) {
        ot.setStyle("float: right;");
        NumberConverter numberconv = new NumberConverter();
        numberconv.setLocale(new Locale("PL"));
        numberconv.setMinFractionDigits(2);
        numberconv.setMaxFractionDigits(2);
        column.setWidth("200");
        ot.setConverter(numberconv);
      }
      final String binding = "#{var." + wstawka + "}";
      ValueExpression ve = ef.createValueExpression(elContext, binding, String.class);
      ot.setValueExpression("value", ve);
      column.getChildren().add(ot);
      dataTable.getChildren().add(column);
    }
    tab.getChildren().add(dataTable);
    akordeon.getChildren().add(tab);
  }