use of com.servoy.j2db.dataprocessing.CustomValueList in project servoy-client by Servoy.
the class PersistFieldInstanceTest method testFieldWithValueList.
@Test
public void testFieldWithValueList() throws RepositoryException {
Form form = solution.getForm("test");
Assert.assertNotNull(form);
ValueList vl = solution.getValueList("test");
Assert.assertNotNull(vl);
Field field = form.createNewField(new Point(0, 0));
field.setDataProviderID("mycolumn");
field.setFormat("#,###.00");
field.setDisplayType(Field.TYPE_AHEAD);
field.setValuelistID(vl.getID());
// needed for a valuelist property type that searches it's form's table via the webform ui
WebFormUI formUI = new WebFormUI(client.getFormManager().getForm(form.getName()));
IDataAdapterList dataAdapterList = formUI.getDataAdapterList();
List<FormElement> formElements = FormElementHelper.INSTANCE.getFormElements(form.getAllObjects(), new ServoyDataConverterContext(client));
Assert.assertEquals(1, formElements.size());
WebFormComponent wc = ComponentFactory.createComponent(client, dataAdapterList, formElements.get(0), formUI, form);
Object property = wc.getProperty("valuelistID");
Assert.assertTrue(property != null ? property.getClass().getName() : "null", property instanceof ValueListTypeSabloValue && ((ValueListTypeSabloValue) property).getValueList() instanceof CustomValueList);
Assert.assertEquals("#,###.00", ((CustomValueList) ((ValueListTypeSabloValue) property).getValueList()).getFormat().getDisplayFormat());
}
use of com.servoy.j2db.dataprocessing.CustomValueList in project servoy-client by Servoy.
the class ComponentFactory method createTypeAheadWithValueList.
/**
* @param application
* @param field
* @param dataProviderLookup
* @param fl
* @return
*/
private static IFieldComponent createTypeAheadWithValueList(IApplication application, Form form, Field field, IDataProviderLookup dataProviderLookup, int type, ParsedFormat format, IStylePropertyChangesRecorder jsChangeRecorder) {
RuntimeDataField scriptable;
IFieldComponent fl;
ValueList valuelist = application.getFlattenedSolution().getValueList(field.getValuelistID());
if (valuelist == null) {
scriptable = new RuntimeDataField(jsChangeRecorder, application);
fl = application.getItemFactory().createDataField(scriptable, getWebID(form, field));
} else {
scriptable = new RuntimeDataLookupField(jsChangeRecorder, application);
if (valuelist.getValueListType() == IValueListConstants.DATABASE_VALUES) {
try {
IValueList secondLookup = getFallbackValueList(application, field.getDataProviderID(), type, format, valuelist);
LookupValueList lookupValueList = new LookupValueList(valuelist, application, secondLookup, format != null ? format.getDisplayFormat() : null);
fl = application.getItemFactory().createDataLookupField((RuntimeDataLookupField) scriptable, getWebID(form, field), lookupValueList);
} catch (Exception e1) {
Debug.error(e1);
return null;
}
} else if (valuelist.getValueListType() == IValueListConstants.CUSTOM_VALUES || valuelist.getValueListType() == IValueListConstants.GLOBAL_METHOD_VALUES) {
fl = application.getItemFactory().createDataLookupField((RuntimeDataLookupField) scriptable, getWebID(form, field), (CustomValueList) getRealValueList(application, valuelist, true, type, format, field.getDataProviderID()));
} else {
return null;
}
}
scriptable.setComponent(fl, field);
return fl;
}
use of com.servoy.j2db.dataprocessing.CustomValueList in project servoy-client by Servoy.
the class ComponentFactory method getRealValueList.
@SuppressWarnings("unchecked")
public static IValueList getRealValueList(IServiceProvider application, ValueList valuelist, boolean useSoftCacheForCustom, int type, ParsedFormat format, String dataprovider, boolean readOnlyCache) {
if (application == null) {
application = J2DBGlobals.getServiceProvider();
}
IValueList list = null;
if (valuelist != null && (valuelist.getValueListType() == IValueListConstants.CUSTOM_VALUES || // reuse,those are static,OTHERS not!
(valuelist.getValueListType() == IValueListConstants.DATABASE_VALUES && valuelist.getDatabaseValuesType() == IValueListConstants.TABLE_VALUES))) {
WeakHashMap<UUID, Object> hmValueLists = null;
if (application != null) {
hmValueLists = (WeakHashMap<UUID, Object>) application.getRuntimeProperties().get(IServiceProvider.RT_VALUELIST_CACHE);
if (hmValueLists == null) {
hmValueLists = new WeakHashMap<UUID, Object>();
application.getRuntimeProperties().put(IServiceProvider.RT_VALUELIST_CACHE, hmValueLists);
}
Object object = hmValueLists.get(valuelist.getUUID());
if (object instanceof SoftReference<?>) {
SoftReference<IValueList> sr = (SoftReference<IValueList>) object;
list = sr.get();
// if it was inserted by a soft reference but now it can't be softly referenced, put it back in hard.
if (list != null && !useSoftCacheForCustom && !readOnlyCache) {
hmValueLists.put(valuelist.getUUID(), list);
}
} else if (object instanceof IValueList) {
list = (IValueList) object;
}
}
// as it may have been changed via solution model (that creates a new persist copy ), and then we need to replace the cached value
if (list != null && list.getValueList() != valuelist) {
list = null;
}
if (list == null) {
list = ValueListFactory.createRealValueList(application, valuelist, type, format);
if (valuelist.getFallbackValueListID() > 0 && valuelist.getFallbackValueListID() != valuelist.getID()) {
list.setFallbackValueList(getFallbackValueList(application, dataprovider, type, format, valuelist));
}
if (!useSoftCacheForCustom && valuelist.getValueListType() == IValueListConstants.CUSTOM_VALUES) {
if (hmValueLists != null && !readOnlyCache)
hmValueLists.put(valuelist.getUUID(), list);
if (dataprovider != null) {
((CustomValueList) list).addDataProvider(dataprovider);
}
} else {
if (hmValueLists != null && !readOnlyCache)
hmValueLists.put(valuelist.getUUID(), new SoftReference<IValueList>(list));
if (dataprovider != null && valuelist.getValueListType() == IValueListConstants.CUSTOM_VALUES) {
((CustomValueList) list).addDataProvider(dataprovider);
}
}
} else if (valuelist.getValueListType() == IValueListConstants.CUSTOM_VALUES) {
if (application instanceof IApplication && ((IApplication) application).isInDeveloper()) {
int currentType = ((CustomValueList) list).getValueType();
if (currentType == Types.OTHER) {
((CustomValueList) list).setValueType(type);
if (dataprovider != null) {
((CustomValueList) list).addDataProvider(dataprovider);
}
} else if (type != Types.OTHER && type != currentType && !((Column.mapToDefaultType(type) == IColumnTypes.INTEGER && Column.mapToDefaultType(currentType) == IColumnTypes.NUMBER) || (Column.mapToDefaultType(type) == IColumnTypes.NUMBER && Column.mapToDefaultType(currentType) == IColumnTypes.INTEGER))) {
List<String> lst = ((CustomValueList) list).getDataProviders();
StringBuffer message = new StringBuffer("The valuelist was already created for type: " + Column.getDisplayTypeString(((CustomValueList) list).getValueType()));
message.append("\n for the dataproviders: ");
for (String previousProviders : lst) {
message.append(previousProviders);
message.append(",");
}
message.setLength(message.length() - 1);
message.append("\nSo it can't be used also for type: " + Column.getDisplayTypeString(type) + " for the dataprovider: " + dataprovider);
message.append("\nPlease edit these dataprovider(s) (using table editor for database column or Edit variable context menu action for variables) of this valuelist: " + valuelist.getName() + " so that they have the same type.");
application.reportError("Valuelist: " + list.getName() + " used with different types", message);
}
}
}
} else {
list = ValueListFactory.createRealValueList(application, valuelist, type, format);
if (valuelist != null && valuelist.getFallbackValueListID() > 0 && valuelist.getFallbackValueListID() != valuelist.getID()) {
list.setFallbackValueList(getFallbackValueList(application, dataprovider, type, format, valuelist));
}
}
return list;
}
use of com.servoy.j2db.dataprocessing.CustomValueList in project servoy-client by Servoy.
the class ClientState method setValueListItems.
/**
* @param name
* @param displayValues
* @param realValues
* @param autoconvert
*/
public void setValueListItems(String name, Object[] displayValues, Object[] realValues, boolean autoconvert) {
ValueList vl = getFlattenedSolution().getValueList(name);
if (vl != null && vl.getValueListType() == IValueListConstants.CUSTOM_VALUES) {
// TODO should getValueListItems not specify type and format??
IValueList valuelist = ComponentFactory.getRealValueList(this, vl, false, Types.OTHER, null, null);
if (valuelist instanceof CustomValueList) {
int guessedType = Types.OTHER;
if (autoconvert && realValues != null) {
guessedType = guessValuelistType(realValues);
} else if (autoconvert && displayValues != null) {
guessedType = guessValuelistType(displayValues);
}
if (guessedType != Types.OTHER) {
((CustomValueList) valuelist).setValueType(guessedType);
}
((CustomValueList) valuelist).fillWithArrayValues(displayValues, realValues);
((CustomValueList) valuelist).setRuntimeChanged(true);
IBasicFormManager fm = getFormManager();
List<IFormController> cachedFormControllers = fm.getCachedFormControllers();
for (IFormController form : cachedFormControllers) {
form.refreshView();
}
}
}
}
use of com.servoy.j2db.dataprocessing.CustomValueList in project servoy-client by Servoy.
the class DataLookupField method setValidationEnabled.
/**
* @see com.servoy.j2db.smart.dataui.DataField#setValidationEnabled(boolean)
*/
@Override
public void setValidationEnabled(boolean b) {
if (eventExecutor.getValidationEnabled() == b)
return;
if (dataProviderID != null && ScopesUtils.isVariableScope(dataProviderID))
return;
if (list != null && list.getFallbackValueList() != null) {
IValueList vlist = list;
if (!b) {
vlist = list.getFallbackValueList();
}
if (vlist instanceof CustomValueList) {
createCustomListModel((CustomValueList) vlist);
} else {
createLookupListModel((LookupValueList) vlist);
}
if (jlist != null) {
jlist.setModel(dlm);
}
}
try {
focusGainedOrValidationChange = true;
eventExecutor.setValidationEnabled(b);
consumeEnterReleased = false;
boolean prevEditState = editState;
if (b) {
setEditable(wasEditable);
if (editProvider != null) {
editProvider.setAdjusting(true);
}
try {
// prevent errors
setValue(null);
} finally {
if (editProvider != null) {
editProvider.setAdjusting(false);
}
}
} else {
wasEditable = isEditable();
if (!Boolean.TRUE.equals(application.getClientProperty(IApplication.LEAVE_FIELDS_READONLY_IN_FIND_MODE))) {
setEditable(true);
}
}
editState = prevEditState;
} finally {
focusGainedOrValidationChange = false;
}
}
Aggregations