You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TableWithCustomConverterFactory.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2012 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.components.table;
  17. import java.util.Locale;
  18. import com.vaadin.data.util.converter.Converter;
  19. import com.vaadin.data.util.converter.DefaultConverterFactory;
  20. import com.vaadin.server.VaadinRequest;
  21. import com.vaadin.tests.components.AbstractTestUI;
  22. import com.vaadin.ui.Table;
  23. public class TableWithCustomConverterFactory extends AbstractTestUI {
  24. public static class MyIntegerConverter implements
  25. Converter<String, Integer> {
  26. @Override
  27. public Integer convertToModel(String value,
  28. Class<? extends Integer> targetType, Locale locale)
  29. throws com.vaadin.data.util.converter.Converter.ConversionException {
  30. // TODO Auto-generated method stub
  31. return null;
  32. }
  33. @Override
  34. public String convertToPresentation(Integer value,
  35. Class<? extends String> targetType, Locale locale)
  36. throws com.vaadin.data.util.converter.Converter.ConversionException {
  37. return "Integer: " + value;
  38. }
  39. @Override
  40. public Class<Integer> getModelType() {
  41. return Integer.class;
  42. }
  43. @Override
  44. public Class<String> getPresentationType() {
  45. return String.class;
  46. }
  47. }
  48. public static class MyConverterFactory extends DefaultConverterFactory {
  49. @Override
  50. protected Converter<String, ?> createStringConverter(Class<?> sourceType) {
  51. if (Integer.class.isAssignableFrom(sourceType)) {
  52. return new MyIntegerConverter();
  53. } else {
  54. return super.createStringConverter(sourceType);
  55. }
  56. }
  57. }
  58. @Override
  59. protected void setup(VaadinRequest request) {
  60. getSession().setConverterFactory(new MyConverterFactory());
  61. Table t = new Table();
  62. t.addContainerProperty("String column", String.class, "");
  63. t.addContainerProperty("Integer column", Integer.class, "");
  64. t.addItem(new Object[] { "Second column is 1", 1 }, "item1");
  65. t.addItem(new Object[] { "Second column is 4589", 4589 }, "item2");
  66. addComponent(t);
  67. }
  68. @Override
  69. protected String getTestDescription() {
  70. // TODO Auto-generated method stub
  71. return null;
  72. }
  73. @Override
  74. protected Integer getTicketNumber() {
  75. // TODO Auto-generated method stub
  76. return null;
  77. }
  78. }