Search in sources :

Example 1 with CustomValueList

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());
}
Also used : CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) Form(com.servoy.j2db.persistence.Form) ValueList(com.servoy.j2db.persistence.ValueList) CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) IValueList(com.servoy.j2db.dataprocessing.IValueList) ValueListTypeSabloValue(com.servoy.j2db.server.ngclient.property.types.ValueListTypeSabloValue) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) Point(java.awt.Point) FormElement(com.servoy.j2db.server.ngclient.FormElement) Field(com.servoy.j2db.persistence.Field) WebFormUI(com.servoy.j2db.server.ngclient.WebFormUI) ServoyDataConverterContext(com.servoy.j2db.server.ngclient.ServoyDataConverterContext) IDataAdapterList(com.servoy.j2db.server.ngclient.IDataAdapterList) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) Test(org.junit.Test)

Example 2 with CustomValueList

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;
}
Also used : RuntimeDataLookupField(com.servoy.j2db.ui.scripting.RuntimeDataLookupField) ValueList(com.servoy.j2db.persistence.ValueList) LookupValueList(com.servoy.j2db.dataprocessing.LookupValueList) CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) IValueList(com.servoy.j2db.dataprocessing.IValueList) LookupValueList(com.servoy.j2db.dataprocessing.LookupValueList) IFieldComponent(com.servoy.j2db.ui.IFieldComponent) IValueList(com.servoy.j2db.dataprocessing.IValueList) RuntimeDataField(com.servoy.j2db.ui.scripting.RuntimeDataField) JSONException(org.json.JSONException) IOException(java.io.IOException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Example 3 with CustomValueList

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;
}
Also used : CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) IApplication(com.servoy.j2db.IApplication) SoftReference(java.lang.ref.SoftReference) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) UUID(com.servoy.j2db.util.UUID) IValueList(com.servoy.j2db.dataprocessing.IValueList)

Example 4 with CustomValueList

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();
            }
        }
    }
}
Also used : CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) ValueList(com.servoy.j2db.persistence.ValueList) CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) IValueList(com.servoy.j2db.dataprocessing.IValueList) IValueList(com.servoy.j2db.dataprocessing.IValueList)

Example 5 with CustomValueList

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;
    }
}
Also used : CustomValueList(com.servoy.j2db.dataprocessing.CustomValueList) IValueList(com.servoy.j2db.dataprocessing.IValueList)

Aggregations

CustomValueList (com.servoy.j2db.dataprocessing.CustomValueList)11 IValueList (com.servoy.j2db.dataprocessing.IValueList)8 ValueList (com.servoy.j2db.persistence.ValueList)6 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)3 Point (java.awt.Point)3 IDataSet (com.servoy.j2db.dataprocessing.IDataSet)2 JSDataSet (com.servoy.j2db.dataprocessing.JSDataSet)2 LookupValueList (com.servoy.j2db.dataprocessing.LookupValueList)2 INGApplication (com.servoy.j2db.server.ngclient.INGApplication)2 ParsedFormat (com.servoy.j2db.util.FormatParser.ParsedFormat)2 SoftReference (java.lang.ref.SoftReference)2 IApplication (com.servoy.j2db.IApplication)1 DBValueList (com.servoy.j2db.dataprocessing.DBValueList)1 GlobalMethodValueList (com.servoy.j2db.dataprocessing.GlobalMethodValueList)1 Field (com.servoy.j2db.persistence.Field)1 Form (com.servoy.j2db.persistence.Form)1 ITable (com.servoy.j2db.persistence.ITable)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 JSValueList (com.servoy.j2db.scripting.solutionmodel.JSValueList)1 MaskBehavior (com.servoy.j2db.server.headlessclient.mask.MaskBehavior)1