Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// SPDX-FileCopyrightText: The openTCS Authors
// SPDX-License-Identifier: MIT
package org.opentcs.modeleditor.util;
import static java.util.Objects.requireNonNull;
import jakarta.inject.Inject;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import org.jhotdraw.draw.Figure;
import org.jhotdraw.draw.connector.Connector;
import org.opentcs.guing.base.model.ConnectableModelComponent;
import org.opentcs.guing.base.model.ModelComponent;
import org.opentcs.guing.base.model.elements.AbstractConnection;
import org.opentcs.guing.common.components.drawing.OpenTCSDrawingEditor;
import org.opentcs.guing.common.components.drawing.OpenTCSDrawingView;
import org.opentcs.guing.common.components.drawing.figures.BitmapFigure;
import org.opentcs.guing.common.components.drawing.figures.FigureConstants;
import org.opentcs.guing.common.components.drawing.figures.LabeledFigure;
import org.opentcs.guing.common.components.drawing.figures.SimpleLineConnection;
import org.opentcs.guing.common.persistence.ModelManager;
/**
* A helper class for cloning figures.
*/
public class FigureCloner {
/**
* The application's model manager.
*/
private final ModelManager fModelManager;
/**
* The application's drawing editor.
*/
private final OpenTCSDrawingEditor fDrawingEditor;
/**
* Creates a new instance.
*
* @param modelManager The application's model manager.
* @param drawingEditor The application's drawing editor.
*/
@Inject
public FigureCloner(ModelManager modelManager, OpenTCSDrawingEditor drawingEditor) {
this.fModelManager = requireNonNull(modelManager, "modelManager");
this.fDrawingEditor = requireNonNull(drawingEditor, "drawingEditor");
}
public List cloneFigures(List figuresToClone) {
requireNonNull(figuresToClone, "figuresToClone");
// Buffer for Links and Paths associated with the cloned Points and Locations
TreeSet bufferedConnections
= new TreeSet<>(Comparator.comparing(AbstractConnection::getName));
// References the prototype Points and Locations to their clones
Map mClones = new HashMap<>();
List clonedFigures = new ArrayList<>();
for (Figure figure : figuresToClone) {
if (figure instanceof LabeledFigure) {
// Location or Point
ConnectableModelComponent model
= (ConnectableModelComponent) figure.get(FigureConstants.MODEL);
if (model != null) {
bufferedConnections.addAll(model.getConnections());
}
LabeledFigure clonedFigure = (LabeledFigure) figure.clone();
ModelComponent clonedModel = clonedFigure.get(FigureConstants.MODEL);
if (model != null) {
mClones.put(model, clonedModel);
}
// Paste cloned figure to the drawing
AffineTransform tx = new AffineTransform();
// TODO: Make the duplicate's distance configurable.
// TODO: With multiple pastes, place the inserted figure relative to
// the predecessor, not the original.
tx.translate(50, 50);
clonedFigure.transform(tx);
getActiveDrawingView().getDrawing().add(clonedFigure);
// The new tree component will be created by "figureAdded()"
clonedFigures.add(clonedFigure);
}
else if (figure instanceof BitmapFigure) {
BitmapFigure clonedFigure
= new BitmapFigure(new File(((BitmapFigure) figure).getImagePath()));
AffineTransform tx = new AffineTransform();
// TODO: Make the duplicate's distance configurable.
// TODO: With multiple pastes, place the inserted figure relative to
// the predecessor, not the original.
tx.translate(50, 50);
clonedFigure.transform(tx);
getActiveDrawingView().addBackgroundBitmap(clonedFigure);
}
}
for (Figure figure : figuresToClone) {
if (figure instanceof SimpleLineConnection) {
// Link or Path
SimpleLineConnection clonedFigure = (SimpleLineConnection) figure.clone();
AbstractConnection model = (AbstractConnection) figure.get(FigureConstants.MODEL);
AbstractConnection clonedModel
= (AbstractConnection) clonedFigure.get(FigureConstants.MODEL);
if (bufferedConnections.contains(model)) {
if (model != null) {
ModelComponent sourcePoint = model.getStartComponent();
ModelComponent clonedSource = mClones.get(sourcePoint);
Iterator iConnectors
= fModelManager.getModel().getFigure(clonedSource).getConnectors(null).iterator();
clonedFigure.setStartConnector(iConnectors.next());
ModelComponent destinationPoint = model.getEndComponent();
ModelComponent clonedDestination = mClones.get(destinationPoint);
iConnectors = fModelManager.getModel().getFigure(clonedDestination)
.getConnectors(null).iterator();
clonedFigure.setEndConnector(iConnectors.next());
clonedModel.setConnectedComponents(clonedSource, clonedDestination);
clonedModel.updateName();
}
}
getActiveDrawingView().getDrawing().add(clonedFigure);
// The new tree component will be created by "figureAdded()"
clonedFigures.add(clonedFigure);
}
}
return clonedFigures;
}
private OpenTCSDrawingView getActiveDrawingView() {
return fDrawingEditor.getActiveView();
}
}