Search in sources :

Example 21 with ScriptMethod

use of com.servoy.j2db.persistence.ScriptMethod in project servoy-client by Servoy.

the class JSValueList method getGlobalMethod.

/**
 * The global method of the valuelist is called to fill in or adjust the values of the valuelist.
 * The method returns a dataset with one or two columns, first column is the display value, second column is real value(if present).
 * The valuelist will be filled in with the dataset data. If second column is not present real value and display value will be the same.
 * The method has to handle three different scenarios:
 *  1. 'displayValue' parameter is not null, this parameter should be used to filter the list of values(in a typeahead fashion)
 *  2. 'realValue' parameter is specified, that means value was not found in current list, so must be specified manually.
 *  In this case method should return only one row in the dataset, with the missing value, that will be added to the valuelist
 *  3. 'realValue' and 'displayValue' are both null , in this case the complete list of values should be returned.
 *
 * Scenario 1 and 3 will completely replace any older results in the valuelist while scenario 2 will append results.
 *
 * In find mode the record will be the FindRecord which is just like a normal JSRecord (DataRecord) it has the same properties (column/dataproviders) but doesnt have its methods (like isEditing())
 *
 * The last argument is rawDisplayValue which contains the same text as displayValue but without converting it to lowercase.
 *
 * @sample
 * var listProvider = solutionModel.newGlobalMethod('globals', 'function getDataSetForValueList(displayValue, realValue, record, valueListName, findMode, rawDisplayValue) {' +
 * 		'	' +
 * 		'var args = null;' +
 * 		'var query = datasources.db.example_data.employees.createSelect();' +
 * 		'/** @type  {JSDataSet} */' +
 * 		'var result = null;' +
 * 		'query.result.add(query.columns.firstname.concat(' ').concat(query.columns.lastname)).add(query.columns.employeeid);' +
 * 		'if (displayValue == null && realValue == null) {' +
 * 		'  // TODO think about caching this result. can be called often!' +
 * 		'  // return the complete list' +
 * 		'  result = databaseManager.getDataSetByQuery(query,100);' +
 * 		'} else if (displayValue != null) {' +
 * 		'  // TYPE_AHEAD filter call, return a filtered list' +
 * 		'  args = [displayValue + "%", displayValue + "%"]' +
 * 		'  query.result.root.where.add(query.or.add(query.columns.firstname.lower.like(args[0] + '%')).add(query.columns.lastname.lower.like(args[1] + '%')));' +
 * 		'  result = databaseManager.getDataSetByQuery(query,100);' +
 * 		'} else if (realValue != null) {' +
 * 		'  // TODO think about caching this result. can be called often!' +
 * 		'  // real object not found in the current list, return 1 row with display,realvalue that will be added to the current list' +
 * 		'  // dont return a complete list in this mode because that will be added to the list that is already there' +
 * 		'  args = [realValue];' +
 * 		'  query.result.root.where.add(query.columns.employeeid.eq(args[0]));' +
 * 		'  result = databaseManager.getDataSetByQuery(query,1);' +
 * 		'}' +
 * 		'return result;' +
 * 		'}');
 * var vlist = solutionModel.newValueList('vlist', JSValueList.CUSTOM_VALUES);
 * vlist.globalMethod = listProvider;
 */
@JSGetter
public JSMethod getGlobalMethod() {
    String uuid = valuelist.getCustomValues();
    ScriptMethod scriptMethod = application.getFlattenedSolution().getScriptMethod(uuid);
    if (scriptMethod != null) {
        return new JSMethod(scriptMethod, application, true);
    }
    return null;
}
Also used : ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) JSGetter(org.mozilla.javascript.annotations.JSGetter)

Example 22 with ScriptMethod

use of com.servoy.j2db.persistence.ScriptMethod in project servoy-client by Servoy.

the class JSValueList method toString.

/**
 * @see java.lang.Object#toString()
 */
@SuppressWarnings("nls")
@Override
public String toString() {
    int type = valuelist.getValueListType();
    String typeString = "";
    switch(type) {
        case IValueListConstants.CUSTOM_VALUES:
            typeString = "Custom";
            break;
        case IValueListConstants.GLOBAL_METHOD_VALUES:
            ScriptMethod globalMethod = application.getFlattenedSolution().getScriptMethod(valuelist.getCustomValues());
            typeString = "GlobalMethod:" + globalMethod != null ? globalMethod.getPrefixedName() : valuelist.getCustomValues();
            break;
        case IValueListConstants.TABLE_VALUES:
            typeString = valuelist.getDatabaseValuesType() == IValueListConstants.TABLE_VALUES ? "Table:" + valuelist.getDataSource() : "Related:" + valuelist.getRelationName();
    }
    return "JSValueList[name:" + valuelist.getName() + ',' + typeString + ']';
}
Also used : ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Example 23 with ScriptMethod

use of com.servoy.j2db.persistence.ScriptMethod in project servoy-client by Servoy.

the class JSValueList method setGlobalMethod.

@JSSetter
public void setGlobalMethod(IBaseSMMethod method) {
    checkModification();
    if (method == null) {
        valuelist.setCustomValues(null);
        valuelist.setValueListType(IValueListConstants.CUSTOM_VALUES);
    } else {
        ScriptMethod scriptMethod = ((JSMethod) method).getScriptMethod();
        if (scriptMethod.getParent() instanceof Solution) {
            valuelist.setCustomValues(scriptMethod.getUUID().toString());
            valuelist.setValueListType(IValueListConstants.GLOBAL_METHOD_VALUES);
        } else {
            // $NON-NLS-1$
            throw new RuntimeException("Only global methods are supported for a valuelist");
        }
    }
}
Also used : ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Solution(com.servoy.j2db.persistence.Solution) JSSetter(org.mozilla.javascript.annotations.JSSetter)

Example 24 with ScriptMethod

use of com.servoy.j2db.persistence.ScriptMethod in project servoy-client by Servoy.

the class PropertyListCellRenderer method getListCellRendererComponent.

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    int align = LEFT;
    Font f = normalFont;
    // $NON-NLS-1$
    String newvalue = "";
    if (value != null)
        newvalue = value.toString();
    if (value instanceof ScriptMethod) {
        newvalue = ((ScriptMethod) value).getDisplayName();
    }
    if (value instanceof Pair && ((Pair) value).getLeft() != null) {
        newvalue = ((Pair) value).getLeft().toString();
    }
    if (// $NON-NLS-1$
    newvalue != null && newvalue.startsWith("*")) {
        newvalue = newvalue.substring(1);
        f = boldFont;
        align = CENTER;
    } else {
        f = normalFont;
        align = LEFT;
    }
    Component c = super.getListCellRendererComponent(list, newvalue, index, isSelected, cellHasFocus);
    if (showToolTips) {
        if (value instanceof ISupportHTMLToolTipText) {
            String html = ((ISupportHTMLToolTipText) value).toHTML();
            if (!html.startsWith("<html>"))
                html = "<html>" + html + "</html>";
            ((JComponent) c).setToolTipText(html);
        } else {
            ((JComponent) c).setToolTipText(null);
        }
    }
    if (normalFont == null) {
        normalFont = c.getFont();
        boldFont = normalFont.deriveFont(Font.BOLD);
        f = normalFont;
    }
    c.setFont(f);
    if (c instanceof JLabel) {
        ((JLabel) c).setHorizontalAlignment(align);
    }
    return c;
}
Also used : ISupportHTMLToolTipText(com.servoy.j2db.persistence.ISupportHTMLToolTipText) JComponent(javax.swing.JComponent) JLabel(javax.swing.JLabel) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) JComponent(javax.swing.JComponent) Component(java.awt.Component) Font(java.awt.Font) Pair(com.servoy.j2db.util.Pair)

Example 25 with ScriptMethod

use of com.servoy.j2db.persistence.ScriptMethod in project servoy-client by Servoy.

the class FormScope method get.

@Override
public Object get(String name, Scriptable start) {
    if (_fp == null) {
        Debug.warn("Error accessing a form " + formName + "  that is already destroyed for getting: " + name);
        throw new ExitScriptException("killing current script, client/solution already terminated");
    }
    _fp.touch();
    if (// $NON-NLS-1$
    "alldataproviders".equals(name)) {
        List<String> al = new ArrayList<String>();
        Table table = (Table) _fp.getTable();
        if (table != null) {
            al = getDataproviderIdList(table);
        }
        return new NativeJavaArray(this, al.toArray(new String[al.size()]));
    }
    if (// $NON-NLS-1$
    "allmethods".equals(name)) {
        List<String> al = new ArrayList<String>();
        Iterator<ScriptMethod> it = _fp.getForm().getScriptMethods(true);
        while (it.hasNext()) {
            ScriptMethod sm = it.next();
            al.add(sm.getName());
        }
        return new NativeJavaArray(this, al.toArray(new String[al.size()]));
    }
    if (// $NON-NLS-1$
    "allrelations".equals(name)) {
        List<String> al = getFormRelationsIdList(_fp.getForm());
        return new NativeJavaArray(this, al.toArray(new String[al.size()]));
    }
    if (// $NON-NLS-1$
    "allvariables".equals(name)) {
        List<String> al = getAllVariablesIdList(_fp.getForm());
        return new NativeJavaArray(this, al.toArray(new String[al.size()]));
    }
    Object object = super.get(name, start);
    if ((object == null || object == Scriptable.NOT_FOUND) && ("foundset".equals(name) || "elements".equals(name))) {
        Debug.error(Thread.currentThread().getName() + ": For form " + _fp + " the foundset/elements were asked for but that was not (or was no longer) set. ", new RuntimeException());
        if (name.equals("foundset"))
            return _fp.getFormModel();
    }
    return object;
}
Also used : Table(com.servoy.j2db.persistence.Table) ArrayList(java.util.ArrayList) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) ExitScriptException(com.servoy.j2db.ExitScriptException)

Aggregations

ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)55 JSFunction (org.mozilla.javascript.annotations.JSFunction)31 Function (org.mozilla.javascript.Function)27 FlattenedSolution (com.servoy.j2db.FlattenedSolution)14 Solution (com.servoy.j2db.persistence.Solution)14 RepositoryException (com.servoy.j2db.persistence.RepositoryException)12 Form (com.servoy.j2db.persistence.Form)11 TableNode (com.servoy.j2db.persistence.TableNode)9 ServoyException (com.servoy.j2db.util.ServoyException)8 ArrayList (java.util.ArrayList)7 GlobalScope (com.servoy.j2db.scripting.GlobalScope)6 Scriptable (org.mozilla.javascript.Scriptable)5 IMobileSMForm (com.servoy.base.solutionmodel.mobile.IMobileSMForm)4 ISMForm (com.servoy.j2db.solutionmodel.ISMForm)4 ServoyClientSupport (com.servoy.base.scripting.annotations.ServoyClientSupport)3 ApplicationException (com.servoy.j2db.ApplicationException)3 FlattenedForm (com.servoy.j2db.persistence.FlattenedForm)3 IPersist (com.servoy.j2db.persistence.IPersist)3 ISupportChilds (com.servoy.j2db.persistence.ISupportChilds)3 FormScope (com.servoy.j2db.scripting.FormScope)3