org.switchyard.tools.ui.editor.components.camel.mail.CamelMailProducerComposite.java Source code

Java tutorial

Introduction

Here is the source code for org.switchyard.tools.ui.editor.components.camel.mail.CamelMailProducerComposite.java

Source

/******************************************************************************* 
 * Copyright (c) 2012-2014 Red Hat, Inc. 
 *  All rights reserved. 
 * This program is made available under the terms of the 
 * Eclipse Public License v1.0 which accompanies this distribution, 
 * and is available at http://www.eclipse.org/legal/epl-v10.html 
 * 
 * Contributors: 
 * Red Hat, Inc. - initial API and implementation 
 *
 * @author bfitzpat
 ******************************************************************************/
package org.switchyard.tools.ui.editor.components.camel.mail;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.databinding.FeaturePath;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.databinding.fieldassist.ControlDecorationSupport;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.soa.sca.sca1_1.model.sca.Binding;
import org.eclipse.soa.sca.sca1_1.model.sca.ScaPackage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.switchyard.tools.models.switchyard1_0.camel.mail.CamelMailBindingType;
import org.switchyard.tools.models.switchyard1_0.camel.mail.MailPackage;
import org.switchyard.tools.ui.editor.Messages;
import org.switchyard.tools.ui.editor.databinding.EMFUpdateValueStrategyNullForEmptyString;
import org.switchyard.tools.ui.editor.databinding.EscapedPropertyIntegerValidator;
import org.switchyard.tools.ui.editor.databinding.ObservablesUtil;
import org.switchyard.tools.ui.editor.databinding.SWTValueUpdater;
import org.switchyard.tools.ui.editor.databinding.StringEmptyValidator;
import org.switchyard.tools.ui.editor.diagram.binding.AbstractSYBindingComposite;

/**
 * @author bfitzpat
 * 
 */
public class CamelMailProducerComposite extends AbstractSYBindingComposite {

    private Composite _panel;
    private CamelMailBindingType _binding = null;
    private Text _nameText;
    private Text _hostText;
    private Text _portText;
    private Text _usernameText;
    private Text _passwordText;
    private Text _subjectText;
    private Text _fromText;
    private Text _toText;
    private Text _ccText;
    private Text _bccText;
    private Text _replyToText;
    private Button _securedCheckbox;
    private WritableValue _bindingValue;

    CamelMailProducerComposite(FormToolkit toolkit) {
        super(toolkit);
    }

    @Override
    public String getTitle() {
        return Messages.title_mailBindingDetails;
    }

    @Override
    public String getDescription() {
        return Messages.description_mailBindingDetails;
    }

    @Override
    public void setBinding(Binding impl) {
        super.setBinding(impl);
        if (impl instanceof CamelMailBindingType) {
            _binding = (CamelMailBindingType) impl;

            _bindingValue.setValue(_binding);

        } else {
            _bindingValue.setValue(null);
        }
    }

    @Override
    public void createContents(Composite parent, int style, DataBindingContext context) {
        _panel = new Composite(parent, style);
        _panel.setLayout(new FillLayout());

        getProducerTabControl(_panel);

        bindControls(context);
    }

    private Control getProducerTabControl(Composite tabFolder) {
        Composite composite = new Composite(tabFolder, SWT.NONE);
        GridLayout gl = new GridLayout(2, false);
        composite.setLayout(gl);

        _nameText = createLabelAndText(composite, Messages.label_name);

        _hostText = createLabelAndText(composite, Messages.label_hostStar);
        _portText = createLabelAndText(composite, Messages.label_port);
        _usernameText = createLabelAndText(composite, Messages.label_userName);
        _passwordText = createLabelAndText(composite, Messages.label_password);
        _passwordText.setEchoChar('*');
        _securedCheckbox = createCheckbox(composite, Messages.label_secured);

        Group producerGroup = new Group(composite, SWT.NONE);
        producerGroup.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));
        producerGroup.setLayout(new GridLayout(2, false));
        producerGroup.setText(Messages.label_producerOptions);

        _subjectText = createLabelAndText(producerGroup, Messages.label_subject);
        _fromText = createLabelAndText(producerGroup, Messages.label_from);
        _toText = createLabelAndText(producerGroup, Messages.label_to);
        _ccText = createLabelAndText(producerGroup, Messages.label_cc);
        _bccText = createLabelAndText(producerGroup, Messages.label_bcc);
        _replyToText = createLabelAndText(producerGroup, Messages.label_replyTo);

        return composite;
    }

    @Override
    public Composite getPanel() {
        return this._panel;
    }

    protected void handleUndo(Control control) {
        if (_binding != null) {
            super.handleUndo(control);
        }
    }

    private void bindProducerControls(final DataBindingContext context, final EditingDomain domain,
            final Realm realm) {
        FeaturePath path = FeaturePath.fromList(MailPackage.Literals.CAMEL_MAIL_BINDING_TYPE__PRODUCE,
                MailPackage.Literals.CAMEL_MAIL_PRODUCER_TYPE__SUBJECT);

        org.eclipse.core.databinding.Binding binding = context.bindValue(
                SWTObservables.observeText(_subjectText, new int[] { SWT.Modify }),
                ObservablesUtil.observeDetailValue(domain, _bindingValue, path),
                new EMFUpdateValueStrategyNullForEmptyString(null, UpdateValueStrategy.POLICY_CONVERT), null);
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

        path = FeaturePath.fromList(MailPackage.Literals.CAMEL_MAIL_BINDING_TYPE__PRODUCE,
                MailPackage.Literals.CAMEL_MAIL_PRODUCER_TYPE__FROM);
        binding = context.bindValue(SWTObservables.observeText(_fromText, new int[] { SWT.Modify }),
                ObservablesUtil.observeDetailValue(domain, _bindingValue, path),
                new EMFUpdateValueStrategyNullForEmptyString(null, UpdateValueStrategy.POLICY_CONVERT), null);
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

        path = FeaturePath.fromList(MailPackage.Literals.CAMEL_MAIL_BINDING_TYPE__PRODUCE,
                MailPackage.Literals.CAMEL_MAIL_PRODUCER_TYPE__TO);
        binding = context.bindValue(SWTObservables.observeText(_toText, new int[] { SWT.Modify }),
                ObservablesUtil.observeDetailValue(domain, _bindingValue, path),
                new EMFUpdateValueStrategyNullForEmptyString("", UpdateValueStrategy.POLICY_CONVERT), null);
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

        path = FeaturePath.fromList(MailPackage.Literals.CAMEL_MAIL_BINDING_TYPE__PRODUCE,
                MailPackage.Literals.CAMEL_MAIL_PRODUCER_TYPE__CC);
        binding = context.bindValue(SWTObservables.observeText(_ccText, new int[] { SWT.Modify }),
                ObservablesUtil.observeDetailValue(domain, _bindingValue, path),
                new EMFUpdateValueStrategyNullForEmptyString(null, UpdateValueStrategy.POLICY_CONVERT), null);
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

        path = FeaturePath.fromList(MailPackage.Literals.CAMEL_MAIL_BINDING_TYPE__PRODUCE,
                MailPackage.Literals.CAMEL_MAIL_PRODUCER_TYPE__BCC);
        binding = context.bindValue(SWTObservables.observeText(_bccText, new int[] { SWT.Modify }),
                ObservablesUtil.observeDetailValue(domain, _bindingValue, path),
                new EMFUpdateValueStrategyNullForEmptyString(null, UpdateValueStrategy.POLICY_CONVERT), null);
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

        path = FeaturePath.fromList(MailPackage.Literals.CAMEL_MAIL_BINDING_TYPE__PRODUCE,
                MailPackage.Literals.CAMEL_MAIL_PRODUCER_TYPE__REPLY_TO);
        binding = context.bindValue(SWTObservables.observeText(_replyToText, new int[] { SWT.Modify }),
                ObservablesUtil.observeDetailValue(domain, _bindingValue, path),
                new EMFUpdateValueStrategyNullForEmptyString(null, UpdateValueStrategy.POLICY_CONVERT), null);
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

    }

    private void bindControls(final DataBindingContext context) {
        final EditingDomain domain = AdapterFactoryEditingDomain.getEditingDomainFor(getTargetObject());
        final Realm realm = SWTObservables.getRealm(_nameText.getDisplay());

        _bindingValue = new WritableValue(realm, null, CamelMailBindingType.class);

        org.eclipse.core.databinding.Binding binding = context.bindValue(
                SWTObservables.observeText(_nameText, new int[] { SWT.Modify }),
                ObservablesUtil.observeDetailValue(domain, _bindingValue, ScaPackage.eINSTANCE.getBinding_Name()),
                new EMFUpdateValueStrategyNullForEmptyString(null, UpdateValueStrategy.POLICY_CONVERT)
                        .setAfterConvertValidator(
                                new StringEmptyValidator("Mail binding name should not be empty", Status.WARNING)),
                null);
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

        /*
         * we also want to bind the name field to the binding name. note that
         * the model to target updater is configured to NEVER update. we want
         * the camel binding name to be the definitive source for this field.
         */
        binding = context.bindValue(SWTObservables.observeText(_nameText, new int[] { SWT.Modify }),
                ObservablesUtil.observeDetailValue(domain, _bindingValue, ScaPackage.eINSTANCE.getBinding_Name()),
                new EMFUpdateValueStrategyNullForEmptyString(null, UpdateValueStrategy.POLICY_CONVERT)
                        .setAfterConvertValidator(
                                new StringEmptyValidator("Mail binding name should not be empty", Status.WARNING)),
                new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER));
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

        binding = context.bindValue(SWTObservables.observeText(_hostText, new int[] { SWT.Modify }),
                ObservablesUtil.observeDetailValue(domain, _bindingValue,
                        MailPackage.Literals.CAMEL_MAIL_BINDING_TYPE__HOST),
                new EMFUpdateValueStrategyNullForEmptyString(null, UpdateValueStrategy.POLICY_CONVERT)
                        .setAfterConvertValidator(new StringEmptyValidator(Messages.error_emptyHost)),
                null);
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

        binding = context.bindValue(SWTObservables.observeText(_portText, new int[] { SWT.Modify }),
                ObservablesUtil.observeDetailValue(domain, _bindingValue,
                        MailPackage.Literals.CAMEL_MAIL_BINDING_TYPE__PORT),
                new EMFUpdateValueStrategyNullForEmptyString(
                        "Port must be a valid numeric value or follow the pattern for escaped properties (i.e. '${propName}').",
                        UpdateValueStrategy.POLICY_CONVERT)
                                .setAfterConvertValidator(new EscapedPropertyIntegerValidator(
                                        "Port must be a valid numeric value or follow the pattern for escaped properties (i.e. '${propName}').")),
                null);
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

        binding = context.bindValue(SWTObservables.observeText(_usernameText, new int[] { SWT.Modify }),
                ObservablesUtil.observeDetailValue(domain, _bindingValue,
                        MailPackage.Literals.CAMEL_MAIL_BINDING_TYPE__USERNAME),
                new EMFUpdateValueStrategyNullForEmptyString(null, UpdateValueStrategy.POLICY_CONVERT), null);
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

        binding = context.bindValue(SWTObservables.observeText(_passwordText, new int[] { SWT.Modify }),
                ObservablesUtil.observeDetailValue(domain, _bindingValue,
                        MailPackage.Literals.CAMEL_MAIL_BINDING_TYPE__PASSWORD),
                new EMFUpdateValueStrategyNullForEmptyString(null, UpdateValueStrategy.POLICY_CONVERT), null);
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

        binding = context.bindValue(SWTObservables.observeSelection(_securedCheckbox),
                ObservablesUtil.observeDetailValue(domain, _bindingValue,
                        MailPackage.Literals.CAMEL_MAIL_BINDING_TYPE__SECURE),
                new EMFUpdateValueStrategyNullForEmptyString(null, UpdateValueStrategy.POLICY_CONVERT), null);
        ControlDecorationSupport.create(SWTValueUpdater.attach(binding), SWT.TOP | SWT.LEFT);

        bindProducerControls(context, domain, realm);

    }

    /* (non-Javadoc)
     * @see org.switchyard.tools.ui.editor.diagram.shared.AbstractSwitchyardComposite#dispose()
     */
    @Override
    public void dispose() {
        _bindingValue.dispose();
        super.dispose();
    }
}