Search in sources :

Example 1 with SelectedRecordScope

use of com.servoy.j2db.scripting.SelectedRecordScope in project servoy-client by Servoy.

the class BasicFormController method setModel.

protected boolean setModel(IFoundSetInternal newModel) throws ServoyException {
    if (newModel == formModel || adjustingModel) {
        // same or adjusting do nothing
        return true;
    }
    ITable formTable = application.getFoundSetManager().getTable(form.getDataSource());
    if (newModel != null && ((formTable == null && newModel.getTable() != null) || (formTable != null && !formTable.equals(newModel.getTable())))) {
        throw new IllegalArgumentException(application.getI18NMessage("servoy.formPanel.error.wrongFoundsetTable", new Object[] { // $NON-NLS-1$
        newModel.getTable() == null ? "NONE" : newModel.getTable().getName(), // $NON-NLS-1$
        form.getTableName() }));
    }
    try {
        IView view = getViewComponent();
        if (view != null && view.isEditing()) {
            // TODO if save fails don't set the newModel??
            int stopped = application.getFoundSetManager().getEditRecordList().stopEditing(false);
            if (stopped != ISaveConstants.STOPPED && stopped != ISaveConstants.AUTO_SAVE_BLOCKED) {
                return false;
            }
        }
        adjustingModel = true;
        if (formModel != null) {
            try {
                ((ISwingFoundSet) formModel).getSelectionModel().removeListSelectionListener(this);
                ((ISwingFoundSet) formModel).getSelectionModel().removeFormController(this);
                ((ISwingFoundSet) formModel).removeTableModelListener(this);
                // to make sure all data is gc'ed
                if (formModel instanceof FoundSet)
                    ((FoundSet) formModel).flushAllCachedItems();
            } catch (Exception ex) {
                Debug.error(ex);
            }
        }
        setFormModelInternal(newModel == null ? ((FoundSetManager) application.getFoundSetManager()).getEmptyFoundSet(this) : newModel);
        if (formScope != null) {
            // $NON-NLS-1$
            formScope.putWithoutFireChange("foundset", formModel);
            if (formScope.getPrototype() == null) {
                formScope.setPrototype(new SelectedRecordScope(this, formTable == null ? null : application.getScriptEngine().getTableScope(formTable)));
            }
        }
        if (isFormVisible) {
            ((ISwingFoundSet) formModel).getSelectionModel().addListSelectionListener(this);
            ((ISwingFoundSet) formModel).getSelectionModel().addFormController(this);
            ((ISwingFoundSet) formModel).addTableModelListener(this);
            if (// it may not yet exist
            view != null) {
                view.setModel(formModel);
            }
            // this was former a call to aggregateChange, but now does now unwanted parent traverse...
            int[] idx = null;
            if (getView() == RECORD_VIEW || getView() == LOCKED_RECORD_VIEW) {
                int selIdx = formModel.getSelectedIndex();
                if (selIdx != -1)
                    idx = new int[] { selIdx };
            } else {
                idx = formModel.getSelectedIndexes();
            }
            if (idx == null || idx.length == 0) {
                refreshAllPartRenderers(new IRecordInternal[] { formModel.getPrototypeState() });
            } else {
                IRecordInternal[] row = new IRecordInternal[idx.length];
                for (int i = 0; i < idx.length; i++) row[i] = formModel.getRecord(idx[i]);
                refreshAllPartRenderers(row);
            }
        }
    } finally {
        adjustingModel = false;
    }
    return true;
}
Also used : FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) IJSFoundSet(com.servoy.base.scripting.api.IJSFoundSet) IFoundSet(com.servoy.j2db.dataprocessing.IFoundSet) RelatedFoundSet(com.servoy.j2db.dataprocessing.RelatedFoundSet) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) ViewFoundSet(com.servoy.j2db.dataprocessing.ViewFoundSet) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) SelectedRecordScope(com.servoy.j2db.scripting.SelectedRecordScope) ITable(com.servoy.j2db.persistence.ITable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject)

Example 2 with SelectedRecordScope

use of com.servoy.j2db.scripting.SelectedRecordScope in project servoy-client by Servoy.

the class BasicFormController method initForJSUsage.

/**
 * Initialize this FormController(or related classes/methods) to be used in javascript
 */
public synchronized JSForm initForJSUsage(CreationalPrototype creationalPrototype) {
    if (formScope == null) {
        try {
            // make scope for state delegation via prototype mechanism
            List<Form> forms = application.getFlattenedSolution().getFormHierarchy(form);
            List<ISupportScriptProviders> scriptProviders = new ArrayList<ISupportScriptProviders>();
            for (Form frm : forms) {
                scriptProviders.add(new RuntimeSupportScriptProviders(application, frm));
            }
            formScope = new FormScope(this, scriptProviders.toArray(new ISupportScriptProviders[scriptProviders.size()]));
            if (formModel != null) {
                ITable formTable = application.getFoundSetManager().getTable(form.getDataSource());
                formScope.setPrototype(new SelectedRecordScope(this, formTable == null ? null : application.getScriptEngine().getTableScope(formTable)));
                // $NON-NLS-1$
                formScope.putWithoutFireChange("foundset", formModel);
            }
            // create JS place holder for this object
            scriptableForm = new BasicFormController.JSForm(this);
            // set parent scope
            NativeJavaObject formObject = new NativeJavaObject(formScope, scriptableForm, ScriptObjectRegistry.getJavaMembers(FormController.JSForm.class, formScope));
            // $NON-NLS-1$
            formScope.putWithoutFireChange("controller", formObject);
            // register the place holder 'scriptableForm' in CreationalPrototype scope
            creationalPrototype.setLocked(false);
            // $NON-NLS-1$
            creationalPrototype.put(((Integer) creationalPrototype.get("length", creationalPrototype)).intValue(), creationalPrototype, formScope);
            creationalPrototype.put(getName(), creationalPrototype, formScope);
            creationalPrototype.setLocked(true);
            formScope.createVars();
        } catch (Exception ex) {
            // $NON-NLS-1$
            application.reportError(application.getI18NMessage("servoy.formPanel.error.initFormScope") + ": " + getName(), ex);
            return null;
        }
    }
    return scriptableForm;
}
Also used : Form(com.servoy.j2db.persistence.Form) ArrayList(java.util.ArrayList) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) FormScope(com.servoy.j2db.scripting.FormScope) ISupportScriptProviders(com.servoy.j2db.persistence.ISupportScriptProviders) RuntimeSupportScriptProviders(com.servoy.j2db.FormController.RuntimeSupportScriptProviders) SelectedRecordScope(com.servoy.j2db.scripting.SelectedRecordScope) ITable(com.servoy.j2db.persistence.ITable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject)

Aggregations

ITable (com.servoy.j2db.persistence.ITable)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 ITwoNativeJavaObject (com.servoy.j2db.scripting.ITwoNativeJavaObject)2 SelectedRecordScope (com.servoy.j2db.scripting.SelectedRecordScope)2 ServoyException (com.servoy.j2db.util.ServoyException)2 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)2 IJSFoundSet (com.servoy.base.scripting.api.IJSFoundSet)1 RuntimeSupportScriptProviders (com.servoy.j2db.FormController.RuntimeSupportScriptProviders)1 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)1 FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)1 IFoundSet (com.servoy.j2db.dataprocessing.IFoundSet)1 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)1 ISwingFoundSet (com.servoy.j2db.dataprocessing.ISwingFoundSet)1 RelatedFoundSet (com.servoy.j2db.dataprocessing.RelatedFoundSet)1 ViewFoundSet (com.servoy.j2db.dataprocessing.ViewFoundSet)1 Form (com.servoy.j2db.persistence.Form)1 ISupportScriptProviders (com.servoy.j2db.persistence.ISupportScriptProviders)1 FormScope (com.servoy.j2db.scripting.FormScope)1 ArrayList (java.util.ArrayList)1