From b10e799f687837c95026da5f799be297cb7d2f89 Mon Sep 17 00:00:00 2001 From: Florian Hoelzl <hoelzl@fortiss.org> Date: Mon, 27 Jun 2011 13:03:48 +0000 Subject: [PATCH] added connection compositor base implementation --- ...archicElementConnectionCompositorBase.java | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/compose/HierarchicElementConnectionCompositorBase.java diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/compose/HierarchicElementConnectionCompositorBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/compose/HierarchicElementConnectionCompositorBase.java new file mode 100644 index 000000000..32a79b01f --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/compose/HierarchicElementConnectionCompositorBase.java @@ -0,0 +1,139 @@ +/*--------------------------------------------------------------------------+ +$Id$ +| | +| Copyright 2011 ForTISS GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.base.ui.compose; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.gef.RootEditPart; +import org.fortiss.tooling.base.model.element.IConnection; +import org.fortiss.tooling.base.model.element.IConnector; +import org.fortiss.tooling.base.model.element.IHierarchicElement; +import org.fortiss.tooling.base.ui.dnd.DragContext; +import org.fortiss.tooling.base.ui.dnd.DropContext; +import org.fortiss.tooling.base.ui.editpart.RootEditPartBase; +import org.fortiss.tooling.kernel.interfaces.IConnectionContext; +import org.fortiss.tooling.kernel.services.ICompositorService; + +/** + * Base implementation for supporting direct connection of two + * {@link IHierarchicElement}s. + * + * <p> + * Sub-classes must implement: + * <UL> + * <LI> + * {@link #createConnection(IHierarchicElement, IHierarchicElement, IConnectionContext)} + * <LI> + * {@link #createSourceConnector(IHierarchicElement, IHierarchicElement, IConnectionContext)} + * <LI> + * {@link #createTargetConnector(IHierarchicElement, IHierarchicElement, IConnectionContext)} + * <LI> + * {@link #getParent(IHierarchicElement, IHierarchicElement, IConnectionContext)} + * </UL> + * These methods are called in order to create specific model elements. + * + * <p> + * Sub-classes may implement {@link #canConnectInterally()} to support internal + * connections. By default connectors of the parent element cannot be connected + * in internal view of the element. + * + * @author hoelzl + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating RED Hash: + */ +public abstract class HierarchicElementConnectionCompositorBase<HE extends IHierarchicElement, S extends IHierarchicElement, T extends IHierarchicElement> + implements org.fortiss.tooling.kernel.interfaces.IConnector<S, T> { + + /** + * Determines whether connectors of the component may be connected + * internally. The default value is <code>false</code>. + */ + protected boolean canConnectInterally() { + return false; + } + + /** Creates the application specific {@link IConnection} model element. */ + protected abstract IConnection createConnection(S source, T target, + IConnectionContext context); + + /** + * Creates the application specific source {@link IConnector} model element. + */ + protected abstract IConnector createSourceConnector(S source, T target, + IConnectionContext context); + + /** + * Creates the application specific target {@link IConnector} model element. + */ + protected abstract IConnector createTargetConnector(S source, T target, + IConnectionContext context); + + /** Returns the parent model element used to compose the connection. */ + protected abstract HE getParent(S source, T target, + IConnectionContext context); + + /** {@inheritDoc} */ + @Override + public boolean canConnect(S source, T target, EObject connection, + IConnectionContext context) { + if (context instanceof DragContext) { + DragContext dc = (DragContext) context; + // Is an internal feedback connection allowed? + if (dc.getSource() instanceof RootEditPartBase + && dc.getTarget() instanceof RootEditPart) { + return canConnectInterally(); + } + } + // all other connections are allowed by default + return true; + } + + /** {@inheritDoc} */ + @Override + public void connect(S source, T target, EObject conn, + IConnectionContext context) { + + // create application specific connection + IConnection connection = createConnection(source, target, context); + // TODO (FH): use ID fixing here + getParent(source, target, context).getConnectionsList().add(connection); + + // create application specific connectors + IConnector sourceConnector = createSourceConnector(source, target, + context); + IConnector targetConnector = createTargetConnector(source, target, + context); + // TODO (FH): use ID fixing here + + // drop source connector + DropContext sourceDrop = context instanceof DragContext ? ((DragContext) context) + .getSourceDropContext() : null; + ICompositorService.INSTANCE + .compose(source, sourceConnector, sourceDrop); + + // drop target target connector + DropContext targetDrop = context instanceof DragContext ? ((DragContext) context) + .getTargetDropContext() : null; + ICompositorService.INSTANCE + .compose(target, targetConnector, targetDrop); + + // link connection to connectors + connection.setSource(sourceConnector); + connection.setTarget(targetConnector); + } +} -- GitLab