Skip to content

Commit

Permalink
paste in middle of screen
Browse files Browse the repository at this point in the history
  • Loading branch information
benjymin committed Mar 10, 2024
1 parent 38250d5 commit 329b563
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 76 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/gecko/actions/ActionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ public MoveSystemConnectionViewModelElementAction createMoveSystemConnectionView
elementScalerBlock, portViewModel, isVariableBlock);
}

public PastePositionableViewModelElementAction createPastePositionableViewModelElementAction() {
return new PastePositionableViewModelElementAction(geckoViewModel);
public PastePositionableViewModelElementAction createPastePositionableViewModelElementAction(Point2D center) {
return new PastePositionableViewModelElementAction(geckoViewModel, center);
}

public RenameViewModelElementAction createRenameViewModelElementAction(Renamable renamable, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
public class PastePositionableViewModelElementAction extends Action {
private final GeckoViewModel geckoViewModel;
private final Set<PositionableViewModelElement<?>> pastedElements;
private static final Point2D PASTE_OFFSET = new Point2D(50, 50);
private final Point2D pasteOffset;
private CopyPositionableViewModelElementVisitor copyVisitor;

PastePositionableViewModelElementAction(GeckoViewModel geckoViewModel) {
PastePositionableViewModelElementAction(GeckoViewModel geckoViewModel, Point2D center) {
this.geckoViewModel = geckoViewModel;
pastedElements = new HashSet<>();
this.pastedElements = new HashSet<>();
this.pasteOffset = center;
}

@Override
Expand All @@ -30,7 +31,7 @@ boolean run() throws GeckoException {
}

PastePositionableViewModelElementVisitor pasteVisitor =
new PastePositionableViewModelElementVisitor(geckoViewModel, copyVisitor);
new PastePositionableViewModelElementVisitor(geckoViewModel, copyVisitor, pasteOffset);
for (Element element : copyVisitor.getOriginalToClipboard().values()) {
element.accept(pasteVisitor);
}
Expand All @@ -41,6 +42,7 @@ boolean run() throws GeckoException {
element.accept(pasteVisitor);
}
}
pasteVisitor.updatePositions();
pastedElements.addAll(pasteVisitor.getPastedElements());
Action selectAction =
geckoViewModel.getActionManager().getActionFactory().createSelectAction(pastedElements, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.Getter;
import org.gecko.exceptions.MissingViewModelElementException;
import org.gecko.exceptions.ModelException;
import org.gecko.model.Automaton;
import org.gecko.model.Contract;
import org.gecko.model.Edge;
import org.gecko.model.Element;
Expand All @@ -31,20 +32,21 @@
public class PastePositionableViewModelElementVisitor implements ElementVisitor {
private final GeckoViewModel geckoViewModel;
private final CopyPositionableViewModelElementVisitor copyVisitor;
private final Point2D pasteOffset = new Point2D(50, 50);
private final Point2D pasteOffset;
@Getter
private final Set<PositionableViewModelElement<?>> pastedElements;
private final BiMap<Element, Element> clipboardToPasted;
@Getter
private final Set<Element> unsuccessfulPastes;

PastePositionableViewModelElementVisitor(
GeckoViewModel geckoViewModel, CopyPositionableViewModelElementVisitor copyVisitor) {
GeckoViewModel geckoViewModel, CopyPositionableViewModelElementVisitor copyVisitor, Point2D pasteOffset) {
this.geckoViewModel = geckoViewModel;
this.copyVisitor = copyVisitor;
pastedElements = new HashSet<>();
clipboardToPasted = HashBiMap.create();
unsuccessfulPastes = new HashSet<>();
this.pasteOffset = pasteOffset;
}

@Override
Expand All @@ -53,10 +55,13 @@ public void visit(State stateFromClipboard) throws ModelException {
geckoViewModel.getGeckoModel().getModelFactory().copyState(stateFromClipboard);
State stateToPaste = copyResult.getKey();
clipboardToPasted.putAll(copyResult.getValue());
geckoViewModel.getCurrentEditor().getCurrentSystem().getTarget().getAutomaton().addState(stateToPaste);
Automaton automaton = geckoViewModel.getCurrentEditor().getCurrentSystem().getTarget().getAutomaton();
automaton.addState(stateToPaste);
if (automaton.getStartState() == null) {
automaton.setStartState(stateToPaste);
}
StateViewModel stateViewModel = geckoViewModel.getViewModelFactory().createStateViewModelFrom(stateToPaste);
stateViewModel.setPosition(
copyVisitor.getElementToPosAndSize().get(stateFromClipboard).getKey().add(pasteOffset));
stateViewModel.setPosition(copyVisitor.getElementToPosAndSize().get(stateFromClipboard).getKey());
stateViewModel.setSize(copyVisitor.getElementToPosAndSize().get(stateFromClipboard).getValue());
clipboardToPasted.put(stateFromClipboard, stateToPaste);
pastedElements.add(stateViewModel);
Expand Down Expand Up @@ -100,7 +105,6 @@ public void visit(Variable variableFromClipboard) throws ModelException {
Variable variableToPaste = geckoViewModel.getGeckoModel().getModelFactory().copyVariable(variableFromClipboard);
geckoViewModel.getCurrentEditor().getCurrentSystem().getTarget().addVariable(variableToPaste);
PortViewModel portViewModel = geckoViewModel.getViewModelFactory().createPortViewModelFrom(variableToPaste);
//portViewModel.setPosition(copyVisitor.getElementToPosAndSize().get(variableFromClipboard).getKey().add(pasteOffset));
clipboardToPasted.put(variableFromClipboard, variableToPaste);
pastedElements.add(portViewModel);
}
Expand All @@ -120,6 +124,36 @@ public void visit(System systemFromClipboard) throws ModelException {
clipboardToPasted.put(systemFromClipboard, systemToPaste);
}

@Override
public void visit(Region regionFromClipboard) throws ModelException, MissingViewModelElementException {
Region regionToPaste = geckoViewModel.getGeckoModel().getModelFactory().copyRegion(regionFromClipboard);
clipboardToPasted.put(regionFromClipboard, regionToPaste);
geckoViewModel.getCurrentEditor().getCurrentSystem().getTarget().getAutomaton().addRegion(regionToPaste);
RegionViewModel regionViewModel = geckoViewModel.getViewModelFactory().createRegionViewModelFrom(regionToPaste);
regionViewModel.setPosition(
copyVisitor.getElementToPosAndSize().get(regionFromClipboard).getKey().add(pasteOffset));
regionViewModel.setSize(copyVisitor.getElementToPosAndSize().get(regionFromClipboard).getValue());
pastedElements.add(regionViewModel);
}

@Override
public void visit(Edge edge) throws ModelException, MissingViewModelElementException {
Edge copy = geckoViewModel.getGeckoModel().getModelFactory().copyEdge(edge);
State pastedSource = (State) clipboardToPasted.get(edge.getSource());
State pastedDestination = (State) clipboardToPasted.get(edge.getDestination());
Contract pastedContract = (Contract) clipboardToPasted.get(edge.getContract());
if (pastedSource == null || pastedDestination == null) {
unsuccessfulPastes.add(edge);
return;
}
geckoViewModel.getCurrentEditor().getCurrentSystem().getTarget().getAutomaton().addEdge(copy);
copy.setSource(pastedSource);
copy.setDestination(pastedDestination);
copy.setContract(pastedContract);
EdgeViewModel edgeViewModel = geckoViewModel.getViewModelFactory().createEdgeViewModelFrom(copy);
pastedElements.add(edgeViewModel);
}

private void createRecursiveSystemViewModels(System systemToPaste) {
System systemFromClipboard = (System) clipboardToPasted.inverse().get(systemToPaste);
SystemViewModel systemViewModel = geckoViewModel.getViewModelFactory().createSystemViewModelFrom(systemToPaste);
Expand All @@ -130,19 +164,15 @@ private void createRecursiveSystemViewModels(System systemToPaste) {
}
for (State state : systemToPaste.getAutomaton().getStates()) {
StateViewModel stateViewModel = geckoViewModel.getViewModelFactory().createStateViewModelFrom(state);
stateViewModel.setPosition(copyVisitor.getElementToPosAndSize()
.get(clipboardToPasted.inverse().get(state))
.getKey()
.add(pasteOffset));
stateViewModel.setPosition(
copyVisitor.getElementToPosAndSize().get(clipboardToPasted.inverse().get(state)).getKey());
}
for (Region region : systemToPaste.getAutomaton().getRegions()) {
try {
RegionViewModel regionViewModel =
geckoViewModel.getViewModelFactory().createRegionViewModelFrom(region);
regionViewModel.setPosition(copyVisitor.getElementToPosAndSize()
.get(clipboardToPasted.inverse().get(region))
.getKey()
.add(pasteOffset));
regionViewModel.setPosition(
copyVisitor.getElementToPosAndSize().get(clipboardToPasted.inverse().get(region)).getKey());
} catch (MissingViewModelElementException e) {
throw new RuntimeException(e);
}
Expand All @@ -166,33 +196,23 @@ private void createRecursiveSystemViewModels(System systemToPaste) {
}
}

@Override
public void visit(Region regionFromClipboard) throws ModelException, MissingViewModelElementException {
Region regionToPaste = geckoViewModel.getGeckoModel().getModelFactory().copyRegion(regionFromClipboard);
clipboardToPasted.put(regionFromClipboard, regionToPaste);
geckoViewModel.getCurrentEditor().getCurrentSystem().getTarget().getAutomaton().addRegion(regionToPaste);
RegionViewModel regionViewModel = geckoViewModel.getViewModelFactory().createRegionViewModelFrom(regionToPaste);
regionViewModel.setPosition(
copyVisitor.getElementToPosAndSize().get(regionFromClipboard).getKey().add(pasteOffset));
regionViewModel.setSize(copyVisitor.getElementToPosAndSize().get(regionFromClipboard).getValue());
pastedElements.add(regionViewModel);
}

@Override
public void visit(Edge edge) throws ModelException, MissingViewModelElementException {
Edge copy = geckoViewModel.getGeckoModel().getModelFactory().copyEdge(edge);
State pastedSource = (State) clipboardToPasted.get(edge.getSource());
State pastedDestination = (State) clipboardToPasted.get(edge.getDestination());
Contract pastedContract = (Contract) clipboardToPasted.get(edge.getContract());
if (pastedSource == null || pastedDestination == null) {
unsuccessfulPastes.add(edge);
return;
public void updatePositions() {
Point2D minPos = new Point2D(Double.MAX_VALUE, Double.MAX_VALUE);
Point2D maxPos = new Point2D(-Double.MAX_VALUE, -Double.MAX_VALUE);
for (PositionableViewModelElement<?> element : pastedElements) {
if (element.getSize().equals(Point2D.ZERO)) {
continue;
}
double x = element.getPosition().getX();
double y = element.getPosition().getY();
minPos = new Point2D(Math.min(minPos.getX(), x), Math.min(minPos.getY(), y));
maxPos = new Point2D(Math.max(maxPos.getX(), x + element.getSize().getX()),
Math.max(maxPos.getY(), y + element.getSize().getY()));
}
Point2D center = minPos.midpoint(maxPos);
for (PositionableViewModelElement<?> element : pastedElements) {
Point2D pos = element.getCenter();
element.setCenter(pos.subtract(center).add(pasteOffset));
}
geckoViewModel.getCurrentEditor().getCurrentSystem().getTarget().getAutomaton().addEdge(copy);
copy.setSource(pastedSource);
copy.setDestination(pastedDestination);
copy.setContract(pastedContract);
EdgeViewModel edgeViewModel = geckoViewModel.getViewModelFactory().createEdgeViewModelFrom(copy);
pastedElements.add(edgeViewModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javafx.scene.control.SeparatorMenuItem;
import org.gecko.actions.ActionManager;
import org.gecko.model.Kind;
import org.gecko.view.GeckoView;
import org.gecko.view.ResourceHandler;
import org.gecko.viewmodel.EdgeViewModel;

Expand All @@ -19,8 +20,8 @@ public class EdgeViewElementContextMenuBuilder extends ViewContextMenuBuilder {
private final EdgeViewModel edgeViewModel;

public EdgeViewElementContextMenuBuilder(
ActionManager actionManager, EdgeViewModel edgeViewModel) {
super(actionManager);
ActionManager actionManager, EdgeViewModel edgeViewModel, GeckoView geckoView) {
super(actionManager, geckoView);

this.edgeViewModel = edgeViewModel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import org.gecko.actions.ActionManager;
import org.gecko.view.GeckoView;
import org.gecko.view.ResourceHandler;
import org.gecko.viewmodel.RegionViewModel;

Expand All @@ -17,8 +18,8 @@ public class RegionViewElementContextMenuBuilder extends ViewContextMenuBuilder
private final RegionViewModel regionViewModel;

public RegionViewElementContextMenuBuilder(
ActionManager actionManager, RegionViewModel regionViewModel) {
super(actionManager);
ActionManager actionManager, RegionViewModel regionViewModel, GeckoView geckoView) {
super(actionManager, geckoView);

this.regionViewModel = regionViewModel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import org.gecko.actions.ActionManager;
import org.gecko.view.GeckoView;
import org.gecko.view.ResourceHandler;
import org.gecko.viewmodel.StateViewModel;

Expand All @@ -17,8 +18,8 @@ public class StateViewElementContextMenuBuilder extends ViewContextMenuBuilder {
private final StateViewModel stateViewModel;

public StateViewElementContextMenuBuilder(
ActionManager actionManager, StateViewModel stateViewModel) {
super(actionManager);
ActionManager actionManager, StateViewModel stateViewModel, GeckoView geckoView) {
super(actionManager, geckoView);
this.stateViewModel = stateViewModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import org.gecko.actions.ActionManager;
import org.gecko.view.GeckoView;
import org.gecko.view.ResourceHandler;
import org.gecko.viewmodel.SystemConnectionViewModel;

Expand All @@ -17,8 +18,8 @@ public class SystemConnectionViewElementContextMenuBuilder extends ViewContextMe
private final SystemConnectionViewModel systemConnectionViewModel;

public SystemConnectionViewElementContextMenuBuilder(
ActionManager actionManager, SystemConnectionViewModel systemConnectionViewModel) {
super(actionManager);
ActionManager actionManager, SystemConnectionViewModel systemConnectionViewModel, GeckoView geckoView) {
super(actionManager, geckoView);

this.systemConnectionViewModel = systemConnectionViewModel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import org.gecko.actions.ActionManager;
import org.gecko.view.GeckoView;
import org.gecko.view.ResourceHandler;
import org.gecko.view.views.shortcuts.Shortcuts;
import org.gecko.viewmodel.SystemViewModel;
Expand All @@ -18,8 +19,8 @@ public class SystemViewElementContextMenuBuilder extends ViewContextMenuBuilder
private final SystemViewModel systemViewModel;

public SystemViewElementContextMenuBuilder(
ActionManager actionManager, SystemViewModel systemViewModel) {
super(actionManager);
ActionManager actionManager, SystemViewModel systemViewModel, GeckoView geckoView) {
super(actionManager, geckoView);

this.systemViewModel = systemViewModel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import org.gecko.actions.ActionManager;
import org.gecko.view.GeckoView;
import org.gecko.view.ResourceHandler;
import org.gecko.viewmodel.PortViewModel;

Expand All @@ -17,8 +18,8 @@ public class VariableBlockViewElementContextMenuBuilder extends ViewContextMenuB
private final PortViewModel portViewModel;

public VariableBlockViewElementContextMenuBuilder(
ActionManager actionManager, PortViewModel portViewModel) {
super(actionManager);
ActionManager actionManager, PortViewModel portViewModel, GeckoView geckoView) {
super(actionManager, geckoView);
this.portViewModel = portViewModel;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.gecko.view.contextmenu;

import javafx.beans.binding.Bindings;
import javafx.geometry.Point2D;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import lombok.Getter;
import lombok.Setter;
import org.gecko.actions.ActionManager;
import org.gecko.view.GeckoView;
import org.gecko.view.ResourceHandler;
import org.gecko.view.views.EditorView;
import org.gecko.view.views.shortcuts.Shortcuts;
import org.gecko.viewmodel.EditorViewModel;

Expand All @@ -22,15 +25,18 @@ public class ViewContextMenuBuilder {
protected EditorViewModel editorViewModel;
@Getter
protected ContextMenu contextMenu;
private final EditorView editorView;

public ViewContextMenuBuilder(ActionManager actionManager) {
public ViewContextMenuBuilder(ActionManager actionManager, GeckoView geckoView) {
this.actionManager = actionManager;
this.editorViewModel = null;
this.editorView = geckoView.getCurrentView();
}

public ViewContextMenuBuilder(ActionManager actionManager, EditorViewModel editorViewModel) {
public ViewContextMenuBuilder(ActionManager actionManager, EditorViewModel editorViewModel, EditorView editorView) {
this.actionManager = actionManager;
this.editorViewModel = editorViewModel;
this.editorView = editorView;
}

public ContextMenu build() {
Expand Down Expand Up @@ -62,8 +68,10 @@ public ContextMenu build() {
}

MenuItem pasteMenuItem = new MenuItem(ResourceHandler.getString("Buttons", "paste"));
pasteMenuItem.setOnAction(
e -> actionManager.run(actionManager.getActionFactory().createPastePositionableViewModelElementAction()));
pasteMenuItem.setOnAction(e -> {
Point2D center = editorView.getViewElementPane().screenCenterWorldCoords();
actionManager.run(actionManager.getActionFactory().createPastePositionableViewModelElementAction(center));
});
pasteMenuItem.setAccelerator(Shortcuts.PASTE.get());

SeparatorMenuItem separatorMenuItem = new SeparatorMenuItem();
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/gecko/view/menubar/MenuBarBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.util.Set;
import javafx.beans.binding.Bindings;
import javafx.geometry.Point2D;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
Expand Down Expand Up @@ -142,8 +143,10 @@ private Menu setupEditMenu() {
copyMenuItem.setAccelerator(Shortcuts.COPY.get());

MenuItem pasteMenuItem = new MenuItem(ResourceHandler.getString("Buttons", "paste"));
pasteMenuItem.setOnAction(
e -> actionManager.run(actionManager.getActionFactory().createPastePositionableViewModelElementAction()));
pasteMenuItem.setOnAction(e -> {
Point2D center = view.getCurrentView().getViewElementPane().screenCenterWorldCoords();
actionManager.run(actionManager.getActionFactory().createPastePositionableViewModelElementAction(center));
});
pasteMenuItem.setAccelerator(Shortcuts.PASTE.get());

// General selection commands:
Expand Down
Loading

0 comments on commit 329b563

Please sign in to comment.