Vaadin : Use a custom converter on grid

Here a simple Vaadin converter to format dateTime in GRID.
ConvertToModel method is not implemented because converter is not associated to any field.

How to use Grid

  CustomFormatDateStringConverter dateConverter = new CustomFormatDateStringConverter();
  grid.getColumn("beanDateAttr").setConverter(dateConverter);

Converter Implementation

package com.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.vaadin.data.util.converter.Converter;

public class CustomFormatDateStringConverter implements Converter<String, String> {
	SimpleDateFormat dateParser = new SimpleDateFormat("ddMMyyyy");
	SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");

	@Override
	public String convertToModel(String value,
			Class<? extends String> targetType, Locale locale)
			throws com.vaadin.data.util.converter.Converter.ConversionException {
			
			// If using in a grid this part can be omitted
			
			return value;
	}

	@Override
	public String convertToPresentation(String value,
			Class<? extends String> targetType, Locale locale)
			throws com.vaadin.data.util.converter.Converter.ConversionException {
			
			if (value!=null && !value.isEmpty()) {
				try {
					Date date = dateParser.parse(value);
					value = dateFormatter.format(date);
				} catch (ParseException e) {
					e.printStackTrace();

				}
			}else{
				value="";
			}
			return value;
	}

	@Override
	public Class<String> getModelType() {
		return String.class;
	}

	@Override
	public Class<String> getPresentationType() {
		return String.class;
	}

}

How to split String in words and populate an array

Here a simply method to split a string in words and generate an array.
In this case I use a Length param to limit substring size.

 


...

public static String[] splitStringInWordsAndCreateArrayWithLineInSize(String input,Integer lineLength){
		String[] words = input.split(" ");
 		List<String> list = new ArrayList<String>();
 		StringBuilder line = new StringBuilder();
 		for (int x = 0; x < words.length; x++) {
 			
 			
 			Boolean c = ((line.toString()+words[x]+" ").length()<lineLength);
 			if(c){
 				line.append(words[x]).append(" ");
 			}else{
 				list.add(line.toString());
 				line = new StringBuilder(words[x]).append(" ");
 			}
 		}
 		if(line.toString().length()>0){
 			list.add(line.toString());	
 		}
 		
 		Object[] arrObj = list.toArray();
 		String[] arrString = Arrays.copyOf(arrObj, arrObj.length, String[].class);
 		
		return arrString;
	}

...

And here unit test:


...

@Test
	 	public void testSplitStringInWordsAndCreateArrayWithLineInSize(){
	 		String s = "Prodotti tricologici per la cura e la bellezza dei capelli,quali shampoo, lozioni per capelli, preparati per ondulare icapelli, preparati per effettuare la permanente dei capelli,tinture per capelli, gel per capelli, lacche e fissatori percapelli,preparati per ravvivare il colore dei capelli . ";
	 		Integer lineLength = 62;
	 		String[] out = FYStringUtils.splitStringInWordsAndCreateArrayWithLineInSize(s, lineLength);
	 		StringBuilder rebuild = new StringBuilder();
	 		for(int x=0;x<out.length;x++){
	 			//System.out.println("x-"+out[x]);
	 			assertThat(out.length<lineLength).isTrue();
	 			rebuild.append(out[x]);
	 		}
	 		
	 		
	 		assertThat(rebuild.toString()).isEqualTo(s);
	 		
	 	}

...

iWidget – How To fight with Ibm Connection

This Week I’ve developed my first iWidget for Ibm Connections 5*.
Here you can find my first widget (an “html embedder” for Connections 5), it can be registered in connection using this parameters in “widget-config.xml” ** :

 

<widgetDef defId="htmlEmbed" url="https://<HostWhereYouPutWidget>/htmlEmbed/htmlembed20.xml?version=11" modes="view edit"
description="Use to embed html" showFullWidgetDeleteConfirmation="false" uniqueInstance="false"
primaryWidget="false"
iconUrl="https://<HostWhereYouPutWidget>/customWidget/htmlEmbed/htmlembed.png"
category="Social embedding"
/>

 

To Download iWidget Example Click Here – > htmlEmbed20

* This widget can be placed only in community.
** To know how to register widget you can follow official documentation.