Skip to content
Snippets Groups Projects
Commit 7c5fe944 authored by Lukas Ladenberger's avatar Lukas Ladenberger
Browse files

fixed copy/paste controls (correct handling of connections)

parent 385cadf8
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,7 @@ import java.util.Iterator;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.ui.actions.Clipboard;
import de.bmotionstudio.gef.editor.model.BConnection;
import de.bmotionstudio.gef.editor.model.BControl;
import de.bmotionstudio.gef.editor.model.Visualization;
......@@ -51,7 +52,8 @@ public class CopyCommand extends Command {
}
public boolean isCopyableControl(BControl control) {
if (!(control instanceof Visualization))
if (!(control instanceof Visualization)
&& !(control instanceof BConnection))
return true;
return false;
}
......
......@@ -7,12 +7,15 @@
package de.bmotionstudio.gef.editor.command;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import de.bmotionstudio.gef.editor.model.BControl;
public class CopyPasteHelper {
private ArrayList<BControl> list = new ArrayList<BControl>();
private Map<BControl, BControl> alreadyCloned = new HashMap<BControl, BControl>();
private int distance = 10;
public CopyPasteHelper(ArrayList<BControl> list, int distance) {
......@@ -36,4 +39,8 @@ public class CopyPasteHelper {
return distance;
}
public Map<BControl, BControl> getAlreadyClonedMap() {
return alreadyCloned;
}
}
......@@ -24,12 +24,11 @@ public class PasteCommand extends Command {
private CopyPasteHelper cHelper;
// List with mapping original BControl ==> cloned BControl
private HashMap<BControl, BControl> list = new HashMap<BControl, BControl>();
private HashMap<BControl, BControl> mappingControl = new HashMap<BControl, BControl>();
private HashMap<BConnection, BConnection> mappingConnection = new HashMap<BConnection, BConnection>();
private List<BControl> parentControls = new ArrayList<BControl>();
private List<ConnectionCreateCommand> connectionCreateCmds = new ArrayList<ConnectionCreateCommand>();
@Override
public boolean canExecute() {
cHelper = (CopyPasteHelper) Clipboard.getDefault().getContents();
......@@ -42,7 +41,7 @@ public class PasteCommand extends Command {
while (it.hasNext()) {
BControl node = (BControl) it.next();
if (isPastableControl(node)) {
list.put(node, null);
mappingControl.put(node, null);
}
}
return true;
......@@ -65,13 +64,15 @@ public class PasteCommand extends Command {
if (!canExecute())
return;
try {
for (BControl parent : parentControls) {
Iterator<BControl> it = list.keySet().iterator();
// Copy/Paste controls
Iterator<BControl> it = mappingControl.keySet().iterator();
while (it.hasNext()) {
BControl control = (BControl) it.next();
control.setParent(parent);
try {
BControl clone = (BControl) control.clone();
clone.setParent(parent);
int x = Integer.valueOf(Integer.valueOf(clone
......@@ -84,67 +85,119 @@ public class PasteCommand extends Command {
+ cHelper.getDistance());
clone.setAttributeValue(AttributeConstants.ATTRIBUTE_Y, y
+ cHelper.getDistance());
list.put(control, clone);
mappingControl.put(control, clone);
cHelper.setDistance(cHelper.getDistance() + 10);
}
// Copy/Paste connections
HashMap<BControl, BControl> helpMap = new HashMap<BControl, BControl>();
helpMap.putAll(cHelper.getAlreadyClonedMap());
helpMap.putAll(mappingControl);
Iterator<BControl> it2 = helpMap.keySet().iterator();
while (it2.hasNext()) {
BControl control = it2.next();
// Clone connections
for (BConnection c : control.getSourceConnections()) {
BConnection cb = (BConnection) c.clone();
cb.setSource(clone);
ConnectionCreateCommand connectionCreateCommand = new ConnectionCreateCommand(
clone);
connectionCreateCommand.setConnection(cb);
connectionCreateCmds.add(connectionCreateCommand);
BConnection newConnection = mappingConnection.get(c);
if (newConnection == null) {
newConnection = (BConnection) c.clone();
newConnection.disconnect();
mappingConnection.put(c, newConnection);
}
BControl s = helpMap.get(newConnection
.getSource());
if (s == null)
s = newConnection.getSource();
BControl t = helpMap.get(newConnection
.getTarget());
if (t == null)
t = newConnection.getTarget();
newConnection.setTarget(t);
newConnection.setSource(s);
}
for (BConnection c : control.getTargetConnections()) {
BConnection cb = (BConnection) c.clone();
cb.setTarget(clone);
ConnectionCreateCommand connectionCreateCommand = new ConnectionCreateCommand(
cb.getSource());
connectionCreateCommand.setConnection(cb);
connectionCreateCmds.add(connectionCreateCommand);
BConnection newConnection = mappingConnection.get(c);
if (newConnection == null) {
newConnection = (BConnection) c.clone();
newConnection.disconnect();
mappingConnection.put(c, newConnection);
}
} catch (CloneNotSupportedException e) {
e.printStackTrace();
BControl t = helpMap.get(newConnection
.getTarget());
if (t == null)
t = newConnection.getTarget();
BControl s = helpMap.get(newConnection
.getSource());
if (s == null)
s = newConnection.getSource();
newConnection.setTarget(t);
newConnection.setSource(s);
}
}
redo();
}
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
@Override
public void redo() {
Iterator<BControl> it = list.values().iterator();
Iterator<BControl> it = mappingControl.values().iterator();
while (it.hasNext()) {
BControl control = it.next();
if (isPastableControl(control)) {
control.getParent().addChild(control);
for (ConnectionCreateCommand cmd : connectionCreateCmds)
cmd.redo();
}
}
Iterator<BConnection> it2 = mappingConnection.values().iterator();
while (it2.hasNext()) {
BConnection connection = it2.next();
connection.reconnect();
}
}
@Override
public boolean canUndo() {
return !(list.isEmpty());
return !(mappingControl.isEmpty());
}
@Override
public void undo() {
Iterator<BControl> it = list.values().iterator();
Iterator<BControl> it = mappingControl.values().iterator();
while (it.hasNext()) {
BControl bcontrol = it.next();
if (isPastableControl(bcontrol)) {
bcontrol.getParent().removeChild(bcontrol);
for (ConnectionCreateCommand cmd : connectionCreateCmds)
cmd.undo();
}
}
Iterator<BConnection> it2 = mappingConnection.values().iterator();
while (it2.hasNext()) {
BConnection connection = it2.next();
connection.disconnect();
}
}
public boolean isPastableControl(BControl control) {
......@@ -154,7 +207,7 @@ public class PasteCommand extends Command {
}
public HashMap<BControl, BControl> getList() {
return this.list;
return this.mappingControl;
}
}
......@@ -156,11 +156,4 @@ public class BConnection extends BControl {
}
@Override
public BControl clone() throws CloneNotSupportedException {
BConnection clonedControl = (BConnection) super.clone();
clonedControl.isConnected = false;
return clonedControl;
}
}
\ No newline at end of file
......@@ -21,6 +21,7 @@ import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ui.actions.Clipboard;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.views.properties.IPropertySource;
......@@ -39,6 +40,7 @@ import de.bmotionstudio.gef.editor.attribute.BAttributeVisible;
import de.bmotionstudio.gef.editor.attribute.BAttributeWidth;
import de.bmotionstudio.gef.editor.attribute.BAttributeX;
import de.bmotionstudio.gef.editor.attribute.BAttributeY;
import de.bmotionstudio.gef.editor.command.CopyPasteHelper;
import de.bmotionstudio.gef.editor.internal.BControlPropertySource;
import de.bmotionstudio.gef.editor.observer.IObserverListener;
import de.bmotionstudio.gef.editor.observer.Observer;
......@@ -666,7 +668,13 @@ public abstract class BControl implements IAdaptable, Cloneable {
clonedControl.setChildrenArray(new BControlList());
Iterator<BControl> it = getChildrenArray().iterator();
while (it.hasNext()) {
clonedControl.addChild(((BControl) it.next()).clone());
BControl next = (BControl) it.next();
BControl childClone = next.clone();
CopyPasteHelper cHelper = (CopyPasteHelper) Clipboard.getDefault()
.getContents();
if (cHelper != null)
cHelper.getAlreadyClonedMap().put(next, childClone);
clonedControl.addChild(childClone);
}
// Clone observer
......
......@@ -36,18 +36,15 @@ public class SwitchPart extends BMSAbstractEditPart {
String aID = evt.getPropertyName();
Switch sw = (Switch) model;
if (aID.equals(AttributeConstants.ATTRIBUTE_SWITCH_POSITION)) {
Track track1 = sw.getTrack1();
Track track2 = sw.getTrack2();
if (aID.equals(AttributeConstants.ATTRIBUTE_SWITCH_POSITION)) {
if (track1 != null && track2 != null) {
track1.setAttributeValue(AttributeConstants.ATTRIBUTE_VISIBLE,
true);
track2.setAttributeValue(AttributeConstants.ATTRIBUTE_VISIBLE,
true);
if (value.equals(AttributeSwitchPosition.LEFT)) {
track1.setAttributeValue(
AttributeConstants.ATTRIBUTE_VISIBLE, false);
......@@ -55,22 +52,22 @@ public class SwitchPart extends BMSAbstractEditPart {
track2.setAttributeValue(
AttributeConstants.ATTRIBUTE_VISIBLE, false);
}
}
}
if (aID.equals(AttributeConstants.ATTRIBUTE_SWITCH_DIRECTION))
refreshEditLayout(figure, model);
if (aID.equals(AttributeConstants.ATTRIBUTE_VISIBLE)) {
if (track1 != null && track2 != null) {
Boolean visible = Boolean.valueOf(value.toString());
((SwitchFigure) figure).setVisible(visible);
sw.getTrack1().setAttributeValue(
track1.setAttributeValue(
AttributeConstants.ATTRIBUTE_VISIBLE, visible);
sw.getTrack2().setAttributeValue(
track2.setAttributeValue(
AttributeConstants.ATTRIBUTE_VISIBLE, visible);
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment