Search in sources :

Example 6 with IDelegate

use of com.servoy.j2db.util.IDelegate in project servoy-client by Servoy.

the class CellAdapter method getCellEditorValue.

public Object getCellEditorValue() {
    // test if currentEditing state isn't deleted already
    if (currentEditingState == null || dataProviderID == null || (currentEditingState != null && currentEditingState.getParentFoundSet() == null))
        return null;
    Object comp = editor;
    if ((comp instanceof IDisplay && ((IDisplay) comp).isReadOnly()) || gettingEditorValue) {
        return currentEditingState.getValue(getDataProviderID());
    }
    try {
        gettingEditorValue = true;
        if (comp instanceof IDelegate<?>) {
            comp = ((IDelegate<?>) comp).getDelegate();
        }
        // HACK:needed for commit value copied from other hack 'processfocus' in DataField
        if (comp instanceof DataField && ((DataField) comp).isEditable()) {
            DataField edit = (DataField) comp;
            boolean needEntireState = edit.needEntireState();
            try {
                edit.setNeedEntireState(false);
                int fb = edit.getFocusLostBehavior();
                if (fb == JFormattedTextField.COMMIT || fb == JFormattedTextField.COMMIT_OR_REVERT) {
                    try {
                        edit.commitEdit();
                        // Give it a chance to reformat.
                        edit.setValueObject(edit.getValue());
                    } catch (ParseException pe) {
                        return null;
                    }
                } else if (fb == JFormattedTextField.REVERT) {
                    edit.setValueObject(edit.getValue());
                }
            } finally {
                edit.setNeedEntireState(needEntireState);
            }
        }
        Object obj = null;
        if (editor instanceof IDisplayData) {
            IDisplayData displayData = (IDisplayData) editor;
            obj = Utils.removeJavascripLinkFromDisplay(displayData, null);
            if (!findMode) {
                // use UI converter to convert from UI value to record value
                obj = ComponentFormat.applyUIConverterFromObject(displayData, obj, dataProviderID, application.getFoundSetManager());
            }
            // if the editor is not enable or is readonly dont try to set any value.
            if (!displayData.isEnabled() || displayData.isReadOnly())
                return obj;
            // this can happen when toggeling with readonly. case 233226 or 232188
            if (!currentEditingState.isEditing() && !currentEditingState.startEditing())
                return obj;
            try {
                if (// $NON-NLS-1$
                currentEditingState != null && (obj == null || "".equals(obj)) && currentEditingState.getValue(dataProviderID) == null) {
                    return null;
                }
            } catch (IllegalArgumentException iae) {
                Debug.error(iae);
            }
            Object oldVal = null;
            if (currentEditingState instanceof FindState) {
                if (displayData instanceof IScriptableProvider && ((IScriptableProvider) displayData).getScriptObject() instanceof IFormatScriptComponent && ((IFormatScriptComponent) ((IScriptableProvider) displayData).getScriptObject()).getComponentFormat() != null) {
                    ((FindState) currentEditingState).setFormat(dataProviderID, ((IFormatScriptComponent) ((IScriptableProvider) displayData).getScriptObject()).getComponentFormat().parsedFormat);
                }
                try {
                    oldVal = currentEditingState.getValue(dataProviderID);
                } catch (IllegalArgumentException iae) {
                    // $NON-NLS-1$
                    Debug.error("Error getting the previous value", iae);
                    oldVal = null;
                }
                currentEditingState.setValue(dataProviderID, obj);
                if (!Utils.equalObjects(oldVal, obj)) {
                    // call notifyLastNewValue changed so that the onChangeEvent will be fired and called when attached.
                    displayData.notifyLastNewValueWasChange(oldVal, obj);
                    obj = dal.getValueObject(currentEditingState, dataProviderID);
                }
            } else {
                if (!displayData.isValueValid() && Utils.equalObjects(lastInvalidValue, obj)) {
                    // already validated
                    return obj;
                }
                try {
                    adjusting = true;
                    try {
                        oldVal = currentEditingState.getValue(dataProviderID);
                    } catch (IllegalArgumentException iae) {
                        // $NON-NLS-1$
                        Debug.error("Error getting the previous value", iae);
                    }
                    try {
                        if (oldVal == Scriptable.NOT_FOUND && dal.getFormScope().has(dataProviderID, dal.getFormScope())) {
                            oldVal = dal.getFormScope().get(dataProviderID);
                            dal.getFormScope().put(dataProviderID, obj);
                            IFoundSetInternal foundset = currentEditingState.getParentFoundSet();
                            if (foundset instanceof FoundSet)
                                ((FoundSet) foundset).fireFoundSetChanged();
                        } else
                            currentEditingState.setValue(dataProviderID, obj);
                    } catch (IllegalArgumentException e) {
                        Debug.trace(e);
                        displayData.setValueValid(false, oldVal);
                        application.handleException(null, new ApplicationException(ServoyException.INVALID_INPUT, e));
                        Object stateValue = null;
                        try {
                            stateValue = dal.getValueObject(currentEditingState, dataProviderID);
                        } catch (IllegalArgumentException iae) {
                            Debug.error(iae);
                        }
                        Object displayValue;
                        if (Utils.equalObjects(oldVal, stateValue)) {
                            // reset display to typed value
                            displayValue = obj;
                        } else {
                            // reset display to changed value in validator method
                            displayValue = stateValue;
                        }
                        convertAndSetValue(displayData, displayValue);
                        return displayValue;
                    }
                    if (!Utils.equalObjects(oldVal, obj)) {
                        fireModificationEvent(currentEditingState);
                        displayData.notifyLastNewValueWasChange(oldVal, obj);
                        obj = dal.getValueObject(currentEditingState, dataProviderID);
                        // we also want to reset the value in the current display if changed by script
                        convertAndSetValue(displayData, obj);
                    } else if (!displayData.isValueValid()) {
                        displayData.notifyLastNewValueWasChange(null, obj);
                    } else {
                        displayData.setValueValid(true, null);
                    }
                } finally {
                    adjusting = false;
                    if (displayData.isValueValid()) {
                        lastInvalidValue = NONE;
                    } else {
                        lastInvalidValue = obj;
                    }
                }
            }
        }
        return obj;
    } finally {
        gettingEditorValue = false;
    }
}
Also used : IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet) IDisplay(com.servoy.j2db.dataprocessing.IDisplay) Point(java.awt.Point) FindState(com.servoy.j2db.dataprocessing.FindState) ApplicationException(com.servoy.j2db.ApplicationException) EventObject(java.util.EventObject) IDisplayData(com.servoy.j2db.dataprocessing.IDisplayData) IFormatScriptComponent(com.servoy.j2db.ui.scripting.IFormatScriptComponent) ParseException(java.text.ParseException) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider) IDelegate(com.servoy.j2db.util.IDelegate)

Aggregations

IDelegate (com.servoy.j2db.util.IDelegate)6 Component (java.awt.Component)3 ApplicationException (com.servoy.j2db.ApplicationException)2 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)2 Date (java.util.Date)2 EventObject (java.util.EventObject)2 WrappedException (org.mozilla.javascript.WrappedException)2 Wrapper (org.mozilla.javascript.Wrapper)2 IJSFoundSet (com.servoy.base.scripting.api.IJSFoundSet)1 FindState (com.servoy.j2db.dataprocessing.FindState)1 IDataSet (com.servoy.j2db.dataprocessing.IDataSet)1 IDisplay (com.servoy.j2db.dataprocessing.IDisplay)1 IDisplayData (com.servoy.j2db.dataprocessing.IDisplayData)1 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)1 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)1 ISwingFoundSet (com.servoy.j2db.dataprocessing.ISwingFoundSet)1 ScriptVariable (com.servoy.j2db.persistence.ScriptVariable)1 IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)1 CellAdapter (com.servoy.j2db.smart.dataui.CellAdapter)1 PortalComponent (com.servoy.j2db.smart.dataui.PortalComponent)1