packagecom.vaadin.tests.components.abstractfield;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Collection;importjava.util.Collections;importjava.util.Date;importjava.util.LinkedHashMap;importjava.util.List;importjava.util.Locale;importcom.vaadin.data.Property;importcom.vaadin.data.Property.ReadOnlyStatusChangeEvent;importcom.vaadin.data.Property.ReadOnlyStatusChangeListener;importcom.vaadin.data.Property.ValueChangeListener;importcom.vaadin.tests.components.AbstractComponentTest;importcom.vaadin.ui.AbstractField;importcom.vaadin.ui.MenuBar;importcom.vaadin.ui.MenuBar.MenuItem;publicabstractclassAbstractFieldTest<TextendsAbstractField>extendsAbstractComponentTest<T>implementsValueChangeListener,ReadOnlyStatusChangeListener{privatebooleansortValueChanges=true;@OverrideprotectedvoidcreateActions(){super.createActions();createBooleanAction("Required",CATEGORY_STATE,false,requiredCommand);createRequiredErrorSelect(CATEGORY_DECORATIONS);createValueChangeListener(CATEGORY_LISTENERS);createReadOnlyStatusChangeListener(CATEGORY_LISTENERS);// * invalidcommitted// * commit()// * discard()// * writethrough// * readthrough// * addvalidator// * isvalid// * invalidallowed// * error indicator//// * validation visible// * ShortcutListener}@OverrideprotectedvoidpopulateSettingsMenu(MenuItemsettingsMenu){super.populateSettingsMenu(settingsMenu);if(AbstractField.class.isAssignableFrom(getTestClass())){MenuItemabstractField=settingsMenu.addItem("AbstractField",null);abstractField.addItem("Show value",newMenuBar.Command(){@OverridepublicvoidmenuSelected(MenuItemselectedItem){for(Ta:getTestComponents()){log(a.getClass().getSimpleName()+" value: "+getValue(a));}}});MenuItemsortValueChangesItem=abstractField.addItem("Show sorted value changes",newMenuBar.Command(){@OverridepublicvoidmenuSelected(MenuItemselectedItem){sortValueChanges=selectedItem.isChecked();log("Show sorted value changes: "+sortValueChanges);}});sortValueChangesItem.setCheckable(true);sortValueChangesItem.setChecked(true);}}privatevoidcreateRequiredErrorSelect(Stringcategory){LinkedHashMap<String,String>options=newLinkedHashMap<String,String>();options.put("-",null);options.put(TEXT_SHORT,TEXT_SHORT);options.put("Medium",TEXT_MEDIUM);options.put("Long",TEXT_LONG);options.put("Very long",TEXT_VERY_LONG);createSelectAction("Required error message",category,options,"-",requiredErrorMessageCommand);}privatevoidcreateValueChangeListener(Stringcategory){createBooleanAction("Value change listener",category,false,valueChangeListenerCommand);}privatevoidcreateReadOnlyStatusChangeListener(Stringcategory){createBooleanAction("Read only status change listener",category,false,readonlyStatusChangeListenerCommand);}protectedCommand<T,Boolean>valueChangeListenerCommand=newCommand<T,Boolean>(){@Overridepublicvoidexecute(Tc,Booleanvalue,Objectdata){if(value){c.addListener((ValueChangeListener)AbstractFieldTest.this);}else{c.removeListener((ValueChangeListener)AbstractFieldTest.this);}}};protectedCommand<T,Boolean>readonlyStatusChangeListenerCommand=newCommand<T,Boolean>(){@Overridepublicvoidexecute(Tc,Booleanvalue,Objectdata){if(value){c.addListener((ReadOnlyStatusChangeListener)AbstractFieldTest.this);}else{c.removeListener((ReadOnlyStatusChangeListener)AbstractFieldTest.this);}}};protectedCommand<T,Object>setValueCommand=newCommand<T,Object>(){@Overridepublicvoidexecute(Tc,Objectvalue,Objectdata){c.setValue(value);}};@OverridepublicvoidvalueChange(com.vaadin.data.Property.ValueChangeEventevent){log(event.getClass().getSimpleName()+", new value: "+getValue(event.getProperty()));}@SuppressWarnings({"rawtypes","unchecked"})privateStringgetValue(Propertyproperty){Objecto=property.getValue();if(oinstanceofCollection&&sortValueChanges){// Sort collections to avoid problems with values printed in// different ordertry{ArrayList<Comparable>c=newArrayList<Comparable>((Collection)o);Collections.sort(c);o=c;}catch(Exceptione){// continue with unsorted if sorting fails for some reasonlog("Exception while sorting value: "+e.getMessage());}}// Distinguish between null and 'null'Stringvalue="null";if(o!=null){if(oinstanceofDate){Dated=(Date)o;// Dec 31, 2068 23:09:26.531Stringpattern="MMM d, yyyy HH:mm:ss.SSS";SimpleDateFormatformat=newSimpleDateFormat(pattern,newLocale("en","US"));value=format.format(d);}else{value="'"+o.toString()+"'";}}returnvalue;}@OverridepublicvoidreadOnlyStatusChange(ReadOnlyStatusChangeEventevent){log(event.getClass().getSimpleName());}protectedvoidcreateSetTextValueAction(Stringcategory){StringsubCategory="Set text value";createCategory(subCategory,category);List<String>values=newArrayList<String>();values.add("Test");values.add("A little longer value");values.add("A very long value with very much text. All in all it is 74 characters long");createClickAction("(empty string)",subCategory,setValueCommand,"");createClickAction("(null)",subCategory,setValueCommand,null);for(Stringvalue:values){createClickAction(value,subCategory,setValueCommand,value);}}}