blob: a2097d75f74b4af07b33db7ba32b46f34fd28f0d [file] [log] [blame]
/*******************************************************************************
* <copyright>
*
* Copyright (c) 2013, 2013 SAP AG.
* 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:
* SAP AG - initial API, implementation and documentation
*
* </copyright>
*
*******************************************************************************/
package org.eclipse.fmc.blockdiagram.editor.property;
import java.math.BigDecimal;
import java.math.MathContext;
import org.eclipse.fmc.blockdiagram.editor.algorithm.connection.FMCConnectionAlgorithm;
import org.eclipse.fmc.blockdiagram.editor.algorithm.connection.FMCConnectionAlgorithmFactory;
import org.eclipse.fmc.mm.DataflowDirection;
import org.eclipse.fmc.mm.RequestDirection;
import org.eclipse.graphiti.mm.pictograms.CompositeConnection;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.ManhattanConnection;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
/**
*
* @author Benjamin Schmeling
*
*/
public class ConnectionPropertySection extends FMCPropertySection {
private static final String TOOLTIP_DATA_FLOW = "Sets the direction of the data flow of the selected connection";
private static final String TOOLTIP_REQUEST_DIRECTION = "Sets the request direction of the channel";
private static final String TOOLTIP_CONNECTION_TYPE = "Sets the typeof the direction";
private static final String TOOLTIP_CHANNEL_LOCATION = "Moves the channel symbol along the given relative position";
private FMCConnectionAlgorithmFactory conFactory = FMCConnectionAlgorithmFactory
.getInstance();
private Spinner channelLocation;
private CCombo dataFlowDirection;
private CCombo requestDirection;
private CCombo connectionType;
private static final BigDecimal HUNDRET = new BigDecimal(100);
private void createConnectionProperties(TabbedPropertySheetWidgetFactory fac) {
createDataFlowDirection(fac);
createRequestDirection(fac);
createChannelLocation(fac);
createConnectionType(fac);
}
@Override
public void createControls(Composite parent,
TabbedPropertySheetPage aTabbedPropertySheetPage) {
super.createControls(parent, aTabbedPropertySheetPage);
TabbedPropertySheetWidgetFactory fac = getWidgetFactory();
comp = fac.createComposite(parent, SWT.NONE);
comp.setLayout(new GridLayout(2, false));
createConnectionProperties(fac);
refresh();
}
protected void createConnectionType(TabbedPropertySheetWidgetFactory fac) {
Label label = fac.createLabel(comp, "Connection Type:");
label.setToolTipText(TOOLTIP_CONNECTION_TYPE);
connectionType = fac.createCCombo(comp, SWT.DROP_DOWN);
connectionType.setToolTipText(TOOLTIP_REQUEST_DIRECTION);
connectionType.setEditable(false);
connectionType
.setItems(new String[] { "Auto Routing", "Manual Routing" });
connectionType.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
PictogramElement el = getSingleInput();
getActions().changeConnectionType((Connection) el,
connectionType.getSelectionIndex());
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
protected void createDataFlowDirection(TabbedPropertySheetWidgetFactory fac) {
Label label = fac.createLabel(comp, "DataFlow:");
label.setToolTipText(TOOLTIP_DATA_FLOW);
dataFlowDirection = fac.createCCombo(comp, SWT.DROP_DOWN);
dataFlowDirection.setToolTipText(TOOLTIP_DATA_FLOW);
dataFlowDirection.setEditable(false);
dataFlowDirection.setItems(new String[] { "Unspecified", "Default",
"Other" });
dataFlowDirection.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
PictogramElement el = getSingleInput();
getActions().changeDataFlowDirection((Connection) el,
dataFlowDirection.getSelectionIndex());
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
protected void createRequestDirection(TabbedPropertySheetWidgetFactory fac) {
Label label = fac.createLabel(comp, "Channel Type:");
label.setToolTipText(TOOLTIP_REQUEST_DIRECTION);
requestDirection = fac.createCCombo(comp, SWT.DROP_DOWN);
requestDirection.setToolTipText(TOOLTIP_REQUEST_DIRECTION);
requestDirection.setEditable(false);
requestDirection.setItems(new String[] { "Unspecified", "Request",
"Response", "Request/Response" });
requestDirection.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
PictogramElement el = getSingleInput();
getActions().changeRequestDirection((Connection) el,
requestDirection.getSelectionIndex());
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
protected void createChannelLocation(TabbedPropertySheetWidgetFactory fac) {
Label label = fac.createLabel(comp, "ChannelLocation:");
label.setToolTipText(TOOLTIP_CHANNEL_LOCATION);
channelLocation = new Spinner(comp, SWT.BORDER);
channelLocation.setToolTipText(TOOLTIP_CHANNEL_LOCATION);
channelLocation.addModifyListener(new ModifyListener() {
@Override
public void modifyText(final ModifyEvent e) {
int location = ((Spinner) e.getSource()).getSelection();
PictogramElement el = getSingleInput();
if (el != null && el instanceof Connection) {
Connection con = (Connection) el;
FMCConnectionAlgorithm algorithm = conFactory
.getAlgorithm(con);
BigDecimal decimal = new BigDecimal(algorithm
.getChannelLocation(con), new MathContext(2));
BigDecimal locationDecimal = new BigDecimal(location,
new MathContext(2));
if (!locationDecimal.divide(HUNDRET).equals(decimal))
getActions().changeChannelLocation(con,
location / 100.0);
}
}
});
}
private void refreshConnection(Connection con) {
connectionType.setEnabled(!(con instanceof CompositeConnection));
connectionType.select(con instanceof ManhattanConnection ? 0 : 1);
if (dataFlowDirection != null)
dataFlowDirection.setEnabled(!helper.isModifyAccess(con));
if (helper.isCommunicationChannel(con)) {
FMCConnectionAlgorithm connectionAlgorithm = conFactory
.getAlgorithm(con);
if (requestDirection != null)
requestDirection.setEnabled(true);
channelLocation.setEnabled(true);
channelLocation.setSelection((int) (connectionAlgorithm
.getChannelLocation(con) * 100));
} else {
channelLocation.setEnabled(false);
if (requestDirection != null)
requestDirection.setEnabled(false);
}
FMCConnectionAlgorithm algorithm = conFactory.getAlgorithm(con);
if (dataFlowDirection != null) {
DataflowDirection direction = algorithm.getDirection(con);
switch (direction) {
case UNSPECIFIED:
dataFlowDirection.select(0);
break;
case DEFAULT:
dataFlowDirection.select(1);
break;
case OTHER:
dataFlowDirection.select(2);
break;
}
}
if (requestDirection != null) {
RequestDirection reqDirection = algorithm.getRequestDirection(con);
switch (reqDirection) {
case UNSPECIFIED:
requestDirection.select(0);
break;
case REQUEST:
requestDirection.select(1);
break;
case RESPONSE:
requestDirection.select(2);
break;
case REQUESTRESPONSE:
requestDirection.select(3);
break;
}
}
}
@Override
public void refresh() {
if (!this.isDisposed()) {
PictogramElement el = this.getSingleInput();
if (el != null && el instanceof Connection) {
refreshConnection((Connection) el);
}
ScrolledComposite tabbedPropertyComposite = findTabbedPropertyComposite(comp);
if (tabbedPropertyComposite != null) {
int cols = tabbedPropertyComposite.getSize().x / 150 * 2;
cols = cols < 2 ? 2 : cols;
comp.setLayout(new GridLayout(cols > 8 ? 8 : cols, false));
comp.layout();
}
}
}
}