/******************************************************************************
* Copyright (c) 2008-2013, Linagora
*
* All rights reserved. This program and the accompanying materials
* are 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:
* Linagora - initial API and implementation
*******************************************************************************/
package com.ebmwebsourcing.petals.services.su.wizards.pages;
import java.util.Arrays;
import java.util.List;
import javax.xml.namespace.QName;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import com.ebmwebsourcing.petals.common.generation.Mep;
import com.ebmwebsourcing.petals.common.internal.provisional.swt.DefaultSelectionListener;
import com.ebmwebsourcing.petals.common.internal.provisional.swt.QNameText;
import com.ebmwebsourcing.petals.common.internal.provisional.utils.StringUtils;
import com.ebmwebsourcing.petals.common.internal.provisional.utils.SwtFactory;
import com.ebmwebsourcing.petals.services.su.extensions.SuWizardSettings;
import com.ebmwebsourcing.petals.services.su.ui.EnhancedConsumeDialog;
import com.sun.java.xml.ns.jbi.AbstractEndpoint;
/**
* JBI page for Binding Components in "consume".
* @author Vincent Zurczak - EBM WebSourcing
*/
public class JbiConsumePage extends JbiAbstractPage {
private Button invokeByServiceButton, invokeByEndpointButton;
private QNameText operationQText;
private ComboViewer mepViewer;
private List<Mep> supportedMep;
private boolean invokeByServiceName = false;
private boolean invokeByEndpointName = false;
/*
* (non-Javadoc)
* @see org.eclipse.jface.dialogs.DialogPage
* #getDescription()
*/
@Override
public String getDescription() {
return "Specify the invocation properties of a Petals service.";
}
/*
* (non-Javadoc)
* @see com.ebmwebsourcing.petals.tools.eclipse.su.main.pages.AbstractJbiPage
* #createCustomControls(org.eclipse.swt.widgets.Composite)
*/
@Override
public void createCustomControls( Composite container ) {
// Prepare the supported MEP
Mep[] meps = getWizard().getComponentVersionDescription().getSupportedMep();
if( meps == null )
meps = new Mep[ 0 ];
this.supportedMep = Arrays.asList( meps );
// Usual controls
addWorkspaceBrowser( container );
createCommonControls( container, 20 );
createCheckboxes( container );
// Disable invocation by end-point for auto-generated end-points
this.edptText.addModifyListener( new ModifyListener() {
@Override
public void modifyText( ModifyEvent e ) {
String autogenValue = getWizard().getComponentVersionDescription().getAutoGeneratedEndpointValue();
boolean autogen = autogenValue.equals( JbiConsumePage.this.edptText.getText().trim());
if( autogen ) {
JbiConsumePage.this.invokeByEndpointName = false;
JbiConsumePage.this.invokeByEndpointButton.setSelection( false );
}
}
});
}
/**
* The widgets to define the invocation properties.
* @param container
*/
private void createCheckboxes( Composite container ) {
// Add the button asking if we should generate the end-point.
SwtFactory.createLabel( container, "", null ); //$NON-NLS-1$
this.invokeByServiceButton = SwtFactory.createCheckBoxButton( container, "Invoke by service name", "Use this service name in the selection of a service provider", this.invokeByServiceName );
SwtFactory.createLabel( container, "", null ); //$NON-NLS-1$
this.invokeByEndpointButton = SwtFactory.createCheckBoxButton( container, "Invoke by end-point name", "Use this end-point name in the selection of a service provider", this.invokeByEndpointName );
this.invokeByEndpointButton.setEnabled( this.invokeByServiceName );
// Add the MEP and operation widgets
SuWizardSettings settings = getWizard().getSettings();
if( settings.showOperationName ) {
Label l = SwtFactory.createLabel( container, "Operation Name:", null ); //$NON-NLS-1$
GridDataFactory.swtDefaults().indent( 0, 20 ).applyTo( l );
this.operationQText = SwtFactory.createQNameTextField( container, false, "Operation", "http://Your.Operation.Namespace/" );
GridDataFactory.swtDefaults().align( SWT.FILL, SWT.CENTER ).indent( 0, 20 ).applyTo( this.operationQText );
this.operationQText.addModifyListener( new ModifyListener() {
@Override
public void modifyText( ModifyEvent e ) {
getWizard().getSettings().invokedOperation = JbiConsumePage.this.operationQText.getValue();
validate();
}
});
}
if( settings.showMep ) {
SwtFactory.createLabel( container, "Invocation MEP *:", "The Message Exchange Pattern" );
this.mepViewer = SwtFactory.createDefaultComboViewer( container, true, true, Mep.values());
this.mepViewer.addSelectionChangedListener( new ISelectionChangedListener() {
@Override
public void selectionChanged( SelectionChangedEvent event ) {
Mep mep = (Mep) ((IStructuredSelection) JbiConsumePage.this.mepViewer.getSelection()).getFirstElement();
getWizard().getSettings().invocationMep = mep;
validate();
}
});
}
// Listeners
this.invokeByServiceButton.addSelectionListener( new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// Update the UI
JbiConsumePage.this.invokeByServiceName = ((Button) e.widget).getSelection();
JbiConsumePage.this.srvQText.setEnabled( JbiConsumePage.this.invokeByServiceName );
if( ! JbiConsumePage.this.invokeByServiceName ) {
JbiConsumePage.this.invokeByEndpointName = false;
JbiConsumePage.this.invokeByEndpointButton.setSelection( false );
JbiConsumePage.this.edptText.setEnabled( false );
}
JbiConsumePage.this.invokeByEndpointButton.setEnabled( JbiConsumePage.this.invokeByServiceName );
JbiConsumePage.this.invokeByEndpointButton.notifyListeners( SWT.Selection, new Event());
// Update the model
if( JbiConsumePage.this.invokeByServiceName )
getNewlyCreatedEndpoint().setServiceName( JbiConsumePage.this.srvQText.getValue());
else
getNewlyCreatedEndpoint().setServiceName( null );
validate();
}
});
this.invokeByEndpointButton.addSelectionListener( new SelectionAdapter() {
@Override
public void widgetSelected( SelectionEvent e ) {
// Update the UI
JbiConsumePage.this.invokeByEndpointName = ((Button) e.widget).getSelection();
JbiConsumePage.this.edptText.setEnabled( JbiConsumePage.this.invokeByEndpointName );
// Update the model
if( JbiConsumePage.this.invokeByEndpointName )
getNewlyCreatedEndpoint().setEndpointName( JbiConsumePage.this.edptText.getText());
else
getNewlyCreatedEndpoint().setEndpointName( null );
validate();
}
});
this.invokeByServiceButton.notifyListeners( SWT.Selection, new Event());
this.invokeByEndpointButton.notifyListeners( SWT.Selection, new Event());
}
/**
* Adds a browser to look for "provide" end-points in the workspace.
* @param table
*/
private void addWorkspaceBrowser( final Composite table ) {
Composite container = SwtFactory.createComposite( table );
SwtFactory.applyNewGridLayout( container, 2, false, 5, 0, 5, 0 );
SwtFactory.applyHorizontalGridData( container, 2 );
new Label( container, SWT.NONE ).setImage( this.tipImage );
// Add an hyper link
Link selectLink = new Link( container, SWT.NONE );
selectLink.setText( "<A>Select a service</A> from the Petals Services view to fill in these fields automatically." );
selectLink.setToolTipText( "Select an end-point to consume among the currently referenced end-points" );
selectLink.addSelectionListener( new DefaultSelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
EnhancedConsumeDialog dlg = new EnhancedConsumeDialog( getShell());
dlg.setConstrainedMep( JbiConsumePage.this.supportedMep );
SuWizardSettings settings = getWizard().getSettings();
boolean needOperation = settings.showOperationName && settings.showMep;
dlg.setNeedOperation( needOperation );
if( dlg.open() == Window.OK ) {
JbiConsumePage.this.itfQText.setValue( dlg.getItfToInvoke());
JbiConsumePage.this.srvQText.setValue( dlg.getSrvToInvoke());
JbiConsumePage.this.edptText.setText( dlg.getEdptToInvoke() == null ? "" : dlg.getEdptToInvoke());
QName op = dlg.getOperationToInvoke();
if( EnhancedConsumeDialog.NO_OPERATION.equals( op ))
op = null;
if( JbiConsumePage.this.operationQText != null )
JbiConsumePage.this.operationQText.setValue( op );
if( JbiConsumePage.this.mepViewer != null ) {
JbiConsumePage.this.mepViewer.setSelection( new StructuredSelection( dlg.getInvocationMep()));
JbiConsumePage.this.mepViewer.getCombo().notifyListeners( SWT.Selection, new Event());
}
}
}
});
}
/*
* (non-Javadoc)
* @see com.ebmwebsourcing.petals.tools.eclipse.su.main.pages.AbstractSuPage
* #validate()
*/
@Override
public boolean validate() {
setMessage( null );
setMessage( null, IMessageProvider.WARNING );
// Interface name
AbstractEndpoint ae = getNewlyCreatedEndpoint();
SuWizardSettings settings = getWizard().getSettings();
if( settings.validateInterface && ae.getInterfaceName() == null ) {
updateStatus( "You have to provide the interface name." );
return false;
}
// Service name
if( this.invokeByServiceName && ae.getServiceName() == null ) {
updateStatus( "You have to provide the service name" );
return false;
}
// End-point name
if( this.invokeByEndpointName && StringUtils.isEmpty( ae.getEndpointName())) {
updateStatus( "You have to define the end-point name." );
return false;
}
if( this.invokeByEndpointName
&& getWizard().getComponentVersionDescription().getAutoGeneratedEndpointValue().equals( ae.getEndpointName())) {
updateStatus( "You cannot invoke by end-point name with autogenerated end-points." );
return false;
}
// MEP
if( getWizard().getSettings().invocationMep == null ) {
updateStatus( "You have to define the invocation MEP." );
return false;
}
// Update data.
updateStatus( null );
// Make sure this service can be invoked (MEP)
Mep invocationMep = getWizard().getSettings().invocationMep;
if( invocationMep != null
&& ! this.supportedMep.isEmpty()
&& ! this.supportedMep.contains( invocationMep )) {
String msg = "This invocation MEP is not supported by the component. Only " + Mep.listMep( this.supportedMep ) + " MEPs are supported.";
setMessage( msg, IMessageProvider.WARNING );
}
return true;
}
}