Search in sources :

Example 1 with CEditor

use of org.compiere.swing.CEditor in project adempiere by adempiere.

the class SmallViewController method setEditor_RW.

private void setEditor_RW(int index, Boolean readWrite) {
    CEditor editor = (CEditor) getEditor(index);
    editor.setReadWrite(readWrite);
}
Also used : CEditor(org.compiere.swing.CEditor)

Example 2 with CEditor

use of org.compiere.swing.CEditor in project adempiere by adempiere.

the class SmallViewController method dynamicDisplay.

//	vetoableChange
/** 
	 *  This is a semi-recursive function to set field default values based on changes in other fields
	 *  or the context.  It cycles through all fields and changes the value to the updated defaults where
	 *  it makes sense to do so.  For read/write enabled fields, user input or initial default values will
	 *  not be overridden.  The recursive part of the function occurs when events are fired as a result of 
	 *  the change in default values which cause other fields to call this function again.<br>
	 *  <br>
	 *  There is a chance of an infinite loop if two or more read-only fields have defaults based on
	 *  the values of each other and the values are not stable.  
	 *
	 */
protected void dynamicDisplay() {
    for (int index = 0; index < fields.size(); index++) {
        GridField field = getField(index);
        if (field != null) {
            // r/w - check if field is Editable
            boolean rw = !field.isReadOnly();
            if (field.isDisplayed(true)) {
                // check context
                // visibility
                setComponentVisibility(index, true, field.isRange());
                rw = field.isEditablePara(true);
                setEditor_RW(index, rw);
                Boolean valueSet = false;
                Object value = field.getValue();
                // If the field is null, test the defaults again
                if (// If the field is not set 
                value == null || value.toString().length() == 0 || // or we are initializing
                !m_IsLoaded || !rw) {
                    // or the field is read only, then ...
                    // Try to reset the default value - could be null
                    // Don't want to overwrite a user input, so if the field is rw and has
                    // a value, don't change it.
                    Object defaultValue = field.getDefault();
                    if (defaultValue != null && field.getOldValue() == null && (value == null || !value.equals(defaultValue))) {
                        // Set the context and fire events if there is a change in value.
                        // Setting the field value to null fires events even if the field is
                        // already null so don't set null if the value is already null.
                        // Not inserting - overwriting the current value
                        field.setValue(defaultValue, false);
                        // The property change events fired will call this function recursively so
                        // we can bail out of this loop as it will be completed by the subsequent calls
                        valueSet = true;
                        // Check for valid values, mandatory (similar to GridController)
                        boolean manMissing = false;
                        boolean noValue = false;
                        noValue = field.getValue() == null || field.getValue().toString().length() == 0;
                        if (//  check context
                        noValue && field.isEditable(true) && field.isMandatory(true))
                            manMissing = true;
                        CEditor editor = getEditor(index);
                        if (editor != null)
                            editor.setBackground(manMissing || field.isError());
                    }
                    if (field.isRange() && getFieldTo(index) != null) {
                        GridField fieldTo = getFieldTo(index);
                        rw = fieldTo.isEditablePara(true);
                        setEditorTo_RW(index, rw);
                        Object valueTo = fieldTo.getValue();
                        if (valueTo == null || valueTo.toString().length() == 0 || !m_IsLoaded || (!rw && DisplayType.isNumeric(fieldTo.getVO().displayType))) {
                            Object defaultValueTo = fieldTo.getDefault();
                            if (defaultValueTo != null && fieldTo.getOldValue() == null && (valueTo == null || !valueTo.equals(defaultValueTo))) {
                                // Not inserting - overwriting the current value
                                fieldTo.setValue(defaultValueTo, false);
                                valueSet = true;
                                // Check for valid values, mandatory (similar to GridController)
                                boolean manMissing = false;
                                boolean noValue = false;
                                noValue = fieldTo.getValue() == null || fieldTo.getValue().toString().length() == 0;
                                if (//  check context
                                noValue && fieldTo.isEditable(true) && fieldTo.isMandatory(true))
                                    manMissing = true;
                                CEditor editorTo = getEditorTo(index);
                                if (editorTo != null)
                                    editorTo.setBackground(manMissing || fieldTo.isError());
                            }
                        }
                    }
                    if (valueSet)
                        break;
                }
            } else {
                // visibility
                setComponentVisibility(index, false, field.isRange());
            }
        }
    }
}
Also used : CEditor(org.compiere.swing.CEditor) GridField(org.compiere.model.GridField)

Example 3 with CEditor

use of org.compiere.swing.CEditor in project adempiere by adempiere.

the class SmallViewController method setEditorTo_RW.

private void setEditorTo_RW(int index, Boolean readWrite) {
    CEditor editorTo = (CEditor) getEditorTo(index);
    editorTo.setReadWrite(readWrite);
}
Also used : CEditor(org.compiere.swing.CEditor)

Example 4 with CEditor

use of org.compiere.swing.CEditor in project adempiere by adempiere.

the class SmallViewController method createEditors.

/**
	 * Create editors for each field/fieldTo pair.  Add event listeners as required
	 * by the various user interface types to ensure the fields and editors are synced. 
	 * @param field
	 * @param fieldTo
	 */
private void createEditors(GridField field, GridField fieldTo) {
    //	
    if (field == null) {
        editors.add(null);
        return;
    }
    //	Make sure the lookup has been fully loaded.
    field.lookupLoadComplete();
    //	Ask the View to create the Editor and give the controller
    //  a reference to it.
    CEditor editor = createEditor(field);
    if (editor == null) {
        editors.add(null);
        return;
    }
    //  Add to Editors ArrayList
    editors.add(editor);
    //  Add to the list of parameters
    setParameter(field.getColumnNameAlias(), editor);
    //	Set the Default Value to match the field default
    //  before the events are set to avoid unnecessary events.
    Object defaultObject = field.getDefault();
    field.setValue(defaultObject, false);
    field.validateValue();
    editor.setValue(field.getValue());
    //  ZK uses ValueChangeListeners.
    if (m_IsSwing) {
        editor.addVetoableChangeListener(this);
    } else {
        editor.addValueChangeListener(this);
    }
    //  GridFields use PropertyChange events to propagate changes
    //  back to the editors and controller.
    field.addPropertyChangeListener(editor);
    field.addPropertyChangeListener(this);
    //  If fieldTo is null, there is no range, set the editorTo to null and return.
    if (fieldTo == null) {
        editorsTo.add(null);
        return;
    }
    //	Ensure the lookup has been fully loaded.
    fieldTo.lookupLoadComplete();
    // Ask the View to create the appropriate editor
    CEditor editorTo = createEditor(fieldTo);
    if (editorTo == null) {
        editorsTo.add(null);
        return;
    }
    //	The To editor
    editorsTo.add(editorTo);
    //  Add to the search/process parameter list
    setParameter(fieldTo.getColumnNameAlias(), editorTo);
    //	Set Default Value
    Object defaultObject2 = fieldTo.getDefault();
    fieldTo.setValue(defaultObject2, false);
    fieldTo.validateValue();
    editorTo.setValue(fieldTo.getValue());
    //  ZK uses ValueChangeListeners.
    if (m_IsSwing) {
        editorTo.addVetoableChangeListener(this);
    } else {
        editorTo.addValueChangeListener(this);
    }
    //  GridFields use PropertyChange events to propagate changes
    //  back to the editors and controller.
    fieldTo.addPropertyChangeListener(editorTo);
    fieldTo.addPropertyChangeListener(this);
}
Also used : CEditor(org.compiere.swing.CEditor)

Example 5 with CEditor

use of org.compiere.swing.CEditor in project adempiere by adempiere.

the class SmallViewController method vetoableChange.

/**
	 *	Swing Editor Listener. Swing editors fire VetoableChange events when the user makes
	 *  a change.  The controller has the option of accepting the change and setting the 
	 *  field value accordingly or vetoing the change by throwing a PropertyVetoException.<br>
	 *  <br>
	 *  For ZK editors, see the equivalent {@link #valueChange(ValueChangeEvent)}
	 *	@param evt Event
	 * 	@exception PropertyVetoException if the controller wishes to roll back.
	 * 
	 */
public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
    log.fine(evt.getPropertyName() + "=" + evt.getNewValue() + " (" + evt.getOldValue() + ") " + (evt.getOldValue() == null ? "" : evt.getOldValue().getClass().getName()));
    //	Which GridField needs to know about this change?
    GridField changedField = null;
    if (evt.getSource() instanceof CEditor) {
        changedField = ((CEditor) evt.getSource()).getField();
    } else
        return;
    //  Deal with new null values. Some editors return "" instead of null
    if ((evt.getNewValue() == null || evt.getNewValue().toString().isEmpty()) && evt.getOldValue() != null && evt.getOldValue().toString().length() > 0) {
        //  #283 Set value to null - veto if the field is mandatory
        if (!changedField.getVO().IsMandatory)
            //	-> PropertyChanged -> dynamicDisplay
            changedField.setValue(null, false);
        else
            throw new PropertyVetoException("FillMandatory", evt);
    } else {
        // is different than the old value or ignore the change
        if (evt.getNewValue() != null && !evt.getNewValue().equals(evt.getOldValue()))
            //	-> PropertyChanged -> dynamicDisplay
            changedField.setValue(evt.getNewValue(), false);
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) CEditor(org.compiere.swing.CEditor) GridField(org.compiere.model.GridField)

Aggregations

CEditor (org.compiere.swing.CEditor)7 GridField (org.compiere.model.GridField)3 PropertyVetoException (java.beans.PropertyVetoException)1 ALayoutConstraint (org.compiere.apps.ALayoutConstraint)1