Duplicate selection option in Edit menu and popup menu (Ctrl+D)

This commit is contained in:
Branislav Stojkovic
2017-05-06 13:56:54 +02:00
parent c7d8e8e0f0
commit d84f7dadf5
12 changed files with 132 additions and 37 deletions
@@ -183,6 +183,11 @@ public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyP
*/
void pasteComponents(Collection<IDIYComponent<?>> components);
/**
* Duplicates selected components and places them nearby.
*/
void duplicateSelection();
/**
* Deletes all the selected components from the project.
*/
@@ -164,7 +164,7 @@ public class InstantiationManager {
// Update the location according to mouse location
updateSingleClick(scaledPoint, snapToGrid, gridSpacing);
}
/**
* Updates location of component slot based on the new mouse location.
*
@@ -1410,6 +1410,38 @@ public class Presenter implements IPlugInPort {
instantiationManager.getFirstControlPoint());
}
@Override
public void duplicateSelection() {
LOG.info("duplicateSelection()");
if (selectedComponents.isEmpty()) {
LOG.debug("Nothing to duplicate");
return;
}
Project oldProject = currentProject.clone();
Set<IDIYComponent<?>> newSelection = new HashSet<IDIYComponent<?>>();
int grid = (int) currentProject.getGridSpacing().convertToPixels();
for (IDIYComponent<?> component : this.selectedComponents) {
try {
IDIYComponent<?> cloned = component.clone();
newSelection.add(cloned);
for (int i = 0; i < component.getControlPointCount(); i++) {
Point p = component.getControlPoint(i);
Point newPoint = new Point(p.x + grid, p.y + grid);
cloned.setControlPoint(newPoint, i);
}
currentProject.getComponents().add(cloned);
} catch (Exception e) {
}
}
updateSelection(newSelection);
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, currentProject.clone(), "Duplicate");
projectFileManager.notifyFileChange();
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
@Override
public void deleteSelectedComponents() {
LOG.info("deleteSelectedComponents()");