File: /Users/shatabdi/Education/GitHub/se450_project/src/model/commands/ShapePasteCommand.java

1     package model.commands;
2     
3     import java.util.ArrayList;
4     import java.util.List;
5     
6     import controller.interfaces.ICommand;
7     import controller.interfaces.IUndoable;
8     import model.interfaces.IShape;
9     import view.interfaces.PaintCanvasBase;
10     
11     public class ShapePasteCommand implements ICommand, IUndoable {
12     	private PaintCanvasBase canvasBase;
13     
14     	List<IShape> clipBoardList = CommandHistory.shapesInClipboard.getList();
15     	List<IShape> pastedShapes = new ArrayList<>();
16     
17     	List<IShape> shapeList = CommandHistory.shapeCollection.getList();
18     
19     	public ShapePasteCommand(PaintCanvasBase canvasBase) {
20     		this.canvasBase = canvasBase;
21     	}
22     
23     	@Override
24     	public void redo() {
25     		for (IShape shape : pastedShapes) {
26     			shapeList.add(shape);
27     		}
28     		canvasBase.repaint();
29     	}
30     
31     	@Override
32     	public void run() {
33     		boolean paste = !CommandHistory.shapesInClipboard.getList().isEmpty();
34     		if (paste) {
35     			pastedShapes.clear();
36     			for (IShape iShape : clipBoardList) {
37     
38     				IShape shape = iShape.pasteShape();
39     				pastedShapes.add(shape);
40     				shapeList.add(shape);
41     			}
42     			canvasBase.repaint();
43     			CommandHistory.add(this);
44     		}
45     	}
46     
47     	@Override
48     	public void undo() {
49     
50     		for (IShape shape : pastedShapes) {
51     			shapeList.removeAll(shape.getNodeList());
52     		}
53     		canvasBase.repaint();
54     	}
55     }
56