Example usage for org.eclipse.jface.databinding.swt ISWTObservable getWidget

List of usage examples for org.eclipse.jface.databinding.swt ISWTObservable getWidget

Introduction

In this page you can find the example usage for org.eclipse.jface.databinding.swt ISWTObservable getWidget.

Prototype

public Widget getWidget();

Source Link

Document

Returns the widget of this observable

Usage

From source file:com.netxforge.screens.editing.base.util.DecorationService.java

License:Open Source License

public void registerBindingContext(final DataBindingContext ctx) {

    if (this.ctx == null) {
        this.ctx = ctx;
    } else {/*  ww  w  .  j ava  2  s .  co  m*/
        return;
    }

    AggregateValidationStatus aggregateStatus = new AggregateValidationStatus(
            ctx.getValidationStatusProviders(), AggregateValidationStatus.MAX_SEVERITY);

    aggregateStatus.addValueChangeListener(new IValueChangeListener() {
        public void handleValueChange(ValueChangeEvent event) {
            // Get the severity type, converted for the new status.
            List<IMessage> messages = getMessages(event);
            fireFormValidationEvent(messages);
        }
    });

    // Deprecated, use the MessageManager to do the decoration, do not use
    // our own
    // control registry! See the Validation
    aggregateStatus.addChangeListener(new IChangeListener() {
        public void handleChange(ChangeEvent event) {
            // Loop through the bindings.
            for (Object o : ctx.getBindings()) {
                Binding binding = (Binding) o;
                IStatus status = (IStatus) binding.getValidationStatus().getValue();
                Control control = null;

                // Note updating targets for writables, will not work here.
                if (binding.getTarget() instanceof ISWTObservable) {
                    ISWTObservable swtObservable = (ISWTObservable) binding.getTarget();
                    control = (Control) swtObservable.getWidget();
                }
                if (binding.getTarget() instanceof WritableValue) {
                    // FIXME We can't determine control for writables.
                }
                ControlDecoration decoration = getDecoration(control);
                if (decoration != null) {
                    if (status.isOK()) {
                        decoration.hide();
                    } else {
                        decoration.setDescriptionText(status.getMessage());
                        decoration.show();
                    }
                } else {
                    System.out.println("Error: Decorator not set for control:" + control);
                }
            }
        }

    });

    observablesMgr.addObservable(aggregateStatus);
}

From source file:com.netxforge.screens.editing.base.util.DecorationService.java

License:Open Source License

/**
 * Get all IMessages for this context.//from   w  ww .jav a 2s . c o m
 * 
 * @deprecated
 * @param ctx
 * @return
 */
protected List<IMessage> getMessages(DataBindingContext ctx) {

    // Iterate over the messages.
    List<IMessage> iMessages = new ArrayList<IMessage>();

    for (Object o : ctx.getBindings()) {
        Binding binding = (Binding) o;
        final IStatus status = (IStatus) binding.getValidationStatus().getValue();

        Control control = null;
        if (binding.getTarget() instanceof ISWTObservable) {
            ISWTObservable swtObservable = (ISWTObservable) binding.getTarget();
            control = (Control) swtObservable.getWidget();
        } else {
            if (binding.getTarget() instanceof DateChooserComboObservableValue) {
                DateChooserComboObservableValue dcObverable = (DateChooserComboObservableValue) binding
                        .getTarget();
                control = dcObverable.combo;
            }
            System.out.println(binding.getTarget().toString());

        }
        // if (!status.isOK()) {
        System.out.println("Creating message" + status.toString());
        iMessages.add(new MessageFromStatus(control, status));
        // CB Let MessageManager do the deoration.
        // ControlDecoration decoration
        // = decoratorMap.get(control);
        // if (decoration != null) {
        // if (status.isOK()) {
        // decoration.hide();
        // } else {
        // decoration
        // .setDescriptionText(status.getMessage());
        // decoration.show();
        // }
        // }
        // }
    }
    // Iterator<?> it = ctx.getValidationStatusProviders().iterator();
    // while (it.hasNext()) {
    // ValidationStatusProvider validationStatusProvider =
    // (ValidationStatusProvider) it
    // .next();
    // final IStatus status = (IStatus) validationStatusProvider
    // .getValidationStatus().getValue();
    //
    // if (!status.isOK()) {
    // iMessages.add(new IMessage() {
    // public Control getControl() {
    // return null;
    // }
    //
    // public Object getData() {
    // return null;
    // }
    //
    // public Object getKey() {
    // return null;
    // }
    //
    // public String getPrefix() {
    // return null;
    // }
    //
    // public String getMessage() {
    // return status.getMessage();
    // }
    //
    // public int getMessageType() {
    // return convertType(status.getSeverity());
    // }
    // });
    // }
    // }
    return iMessages;
}

From source file:com.netxforge.screens.editing.base.util.DecorationService.java

License:Open Source License

/**
 * Creates {@link IMessage}s from a {@link ValueChangeEvent }. The
 * {@link Control} is derived from the the {@link IObservableValue } which is
 * part of the event. </br></br> Currently supported observables:
 * <ul>//from w  ww .  java  2 s  .  co  m
 * <li>{@link ISWTObservable}</li>
 * <li>{@link DateChooserComboObservableValue}</li>
 * </ul>
 * The {@link IMessage} implementation is a {@link MessageFromStatus}, which
 * wraps the {@link IStatus} from the the event.
 * 
 * @param event
 * @return a collection of {@link IMessage}.
 */
protected List<IMessage> getMessages(ValueChangeEvent event) {
    // Iterate over the messages.
    List<IMessage> iMessages = new ArrayList<IMessage>();

    if (event.diff.getNewValue() instanceof IStatus) {
        IStatus status = (IStatus) event.diff.getNewValue();

        IObservableValue observableValue = event.getObservableValue();

        Control control = null;
        if (observableValue instanceof ISWTObservable) {
            ISWTObservable swtObservable = (ISWTObservable) observableValue;
            control = (Control) swtObservable.getWidget();
        } else {
            if (observableValue instanceof DateChooserComboObservableValue) {
                DateChooserComboObservableValue dcObverable = (DateChooserComboObservableValue) observableValue;
                control = dcObverable.combo;
            }
        }
        iMessages.add(new MessageFromStatus(control, status));
    }
    return iMessages;
}

From source file:de.uniluebeck.itm.spyglass.gui.configuration.DatabindingErrorHandler.java

License:Open Source License

@Override
public void handleValueChange(final ValueChangeEvent event) {

    // I found no way to find the specific widget/Binding which caused
    // this change event. Alas, we have to iterate through all of them...
    for (final Object o : dbc.getValidationStatusProviders()) {
        final ValidationStatusProvider prov = (ValidationStatusProvider) o;

        final Status status = (Status) prov.getValidationStatus().getValue();

        for (final Object o2 : prov.getTargets()) {
            final IObservable observable = (IObservable) o2;

            ///*from w  ww.  j a  v  a  2s  .  c o  m*/
            if (observable instanceof ISWTObservable) {
                final ISWTObservable observable2 = (ISWTObservable) observable;
                final Widget w = observable2.getWidget();

                assert w instanceof Control;
                if (w instanceof Control) {
                    final Control c = (Control) w;

                    updateToolTip(status, c);
                }
            } else {
                if (!status.isOK()) {
                    log.warn(status.getMessage(), status.getException());
                }
            }
        }

    }

}

From source file:de.uniluebeck.itm.spyglass.gui.configuration.DatabindingErrorHandler.java

License:Open Source License

@Override
public void handleListChange(final ListChangeEvent event) {

    // if a binding gets disposed while it contains a validation error, we have to notice this
    // to remove the information bubble
    for (final ListDiffEntry e : event.diff.getDifferences()) {
        if (!e.isAddition()) {
            final Binding b = (Binding) e.getElement();
            if (b.getTarget() instanceof ISWTObservable) {
                final ISWTObservable obs = (ISWTObservable) b.getTarget();
                assert obs.getWidget() instanceof Control;
                final Control c = (Control) obs.getWidget();
                if (tipMap.containsKey(c)) {
                    destroyToolTip(c, tipMap.get(c));
                }/* w  w w  . ja  v  a 2s .com*/
            }
        }
    }

}

From source file:gov.redhawk.internal.ui.editor.validation.ValidatingService34.java

License:Open Source License

private boolean checkBindingFor34(Object observed, Object valueType, final ISWTObservable swtObservable,
        final Diagnostic diagnostic, final IMessageManager messageManager) {
    final List<?> diagnosticData = diagnostic.getData();
    if (diagnosticData.size() >= 2) {
        if (diagnosticData.get(0) == observed) {
            if (diagnosticData.get(1) == valueType) {
                Widget widget = swtObservable.getWidget();
                if (widget instanceof Control && !(widget instanceof Button)) {
                    final Control control = (Control) widget;
                    messageManager.addMessage(swtObservable, diagnostic.getMessage(), null,
                            ValidatingService.KEY_MAP.getMessageProviderKey(diagnostic.getSeverity()), control);
                    return true;
                }//from ww w  . j  a v a 2 s . com
            }
        }
    }
    return false;
}

From source file:gov.redhawk.ui.editor.SCAFormEditor.java

License:Open Source License

/**
 * Sets focus on the correct control./*from w ww.ja  va  2 s.co  m*/
 * 
 * @param emfObservable
 * @param swtObservable
 * @param object
 * @param featureID
 * @return
 */
private boolean selectFeature(final EditingDomainEObjectObservableValue emfObservable,
        final ISWTObservable swtObservable, final EObject object, final int featureID) {
    boolean retVal = false;
    final Object observed = emfObservable.getObserved();
    if (emfObservable.getValueType() instanceof EStructuralFeature) {
        final int myId = ((EStructuralFeature) emfObservable.getValueType()).getFeatureID();
        if (object == observed) {
            if (featureID == myId) {
                if (swtObservable.getWidget() instanceof Control) {
                    final Control control = (Control) swtObservable.getWidget();
                    control.setFocus();
                    retVal = true;
                }
            }
        }
    }
    return retVal;
}

From source file:org.eclipse.etrice.ui.common.dialogs.AbstractPropertyDialog.java

License:Open Source License

@Override
protected void createFormContent(IManagedForm mform) {
    toolkit = mform.getToolkit();//from ww  w.  j  a  va 2s.  com
    bindingContext = new DataBindingContext();

    Form form = mform.getForm().getForm();
    form.setText(title);

    form.setImage(getImage());
    mform.getToolkit().decorateFormHeading(form);

    Composite body = form.getBody();
    body.setLayout(new GridLayout(2, false));
    body.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    validationLabel = toolkit.createLabel(body, "", SWT.NONE);
    validationLabel.setText("ERROR:");
    validationLabel.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_RED));

    validationText = toolkit.createLabel(body, "", SWT.NONE);
    validationText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    createContent(mform, body, bindingContext);

    aggregateValidationStatus = new AggregateValidationStatus(bindingContext.getBindings(),
            AggregateValidationStatus.MAX_SEVERITY);

    bindingContext.bindValue(SWTObservables.observeText(validationText), aggregateValidationStatus, null, null);

    aggregateValidationStatus.addChangeListener(new IChangeListener() {
        public void handleChange(ChangeEvent event) {

            boolean ok = true;
            for (Object o : bindingContext.getBindings()) {
                Binding binding = (Binding) o;
                IStatus status = (IStatus) binding.getValidationStatus().getValue();
                Control control = null;
                if (binding.getTarget() instanceof ISWTObservable) {
                    ISWTObservable swtObservable = (ISWTObservable) binding.getTarget();
                    control = (Control) swtObservable.getWidget();
                }
                ControlDecoration decoration = decoratorMap.get(control);
                if (decoration != null) {
                    if (status.isOK()) {
                        decoration.hide();
                    } else {
                        ok = false;
                        decoration.setDescriptionText(status.getMessage());
                        decoration.show();
                    }
                }
            }
            updateValidationFeedback(ok);
        }
    });
}

From source file:org.eclipse.pde.emfforms.internal.validation.ValidatingService35.java

License:Open Source License

private boolean checkBinding(IObserving emfObservable, ISWTObservable swtObservable, Diagnostic diagnostic,
        IMessageManager messageManager) {
    List<?> diagnosticData = diagnostic.getData();
    if (diagnosticData.size() >= 2) {
        if (diagnosticData.get(0) == emfObservable.getObserved()) {
            if ((emfObservable instanceof IObservableValue
                    && (diagnosticData.get(1) == ((IObservableValue) emfObservable).getValueType()))
                    || (emfObservable instanceof IObservableList
                            && (diagnosticData.get(1) == ((IObservableList) emfObservable).getElementType()))) {
                if (swtObservable.getWidget() instanceof Control) {
                    Control control = (Control) swtObservable.getWidget();
                    messageManager.addMessage(swtObservable, diagnostic.getMessage(), null,
                            keyMap.getMessageProviderKey(diagnostic.getSeverity()), control);

                    return true;
                }// w  w w  .  j  a  v a  2 s. co  m
            }
        }
    }
    return false;
}

From source file:org.goko.common.bindings.AbstractController.java

License:Open Source License

/**
 * Adds binding for the header displaying validation messages
 * @param source the label in which the errors will be displayed
 * @throws GkException GkException/*from   www.  j a v  a 2  s  .  c  om*/
 */
public void addValidationMessagesBinding(final Label source) throws GkException {

    AggregateValidationStatus aggValidationStatus = new AggregateValidationStatus(bindingContext.getBindings(),
            AggregateValidationStatus.MAX_SEVERITY);
    aggValidationStatus.addChangeListener(new IChangeListener() {

        @Override
        public void handleChange(ChangeEvent event) {
            StringBuffer errorBuffer = new StringBuffer();
            source.setText(StringUtils.EMPTY);
            source.setVisible(false);
            for (Object o : bindingContext.getBindings()) {
                Binding binding = (Binding) o;
                IStatus status = (IStatus) binding.getValidationStatus().getValue();
                Control control = null;
                if (binding.getTarget() instanceof ISWTObservable) {
                    ISWTObservable swtObservable = (ISWTObservable) binding.getTarget();
                    control = (Control) swtObservable.getWidget();
                }
                if (!status.isOK()) {
                    if (StringUtils.isNotEmpty(status.getMessage())) {
                        errorBuffer.append(status.getMessage());
                        errorBuffer.append(System.lineSeparator());
                        source.setText(errorBuffer.toString().trim());
                        source.setVisible(true);
                        return;
                    }
                }
            }

        }
    });
    bindingContext.bindValue(SWTObservables.observeText(source), aggValidationStatus, null, null);

}