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

1     package model.commands;
2     
3     import java.util.List;
4     import java.util.stream.Collectors;
5     
6     import controller.interfaces.ICommand;
7     import controller.interfaces.IUndoable;
8     import model.interfaces.IShape;
9     import model.shape.GroupShape;
10     import view.interfaces.PaintCanvasBase;
11     
12     public class ShapeUngroupCommand implements ICommand, IUndoable {
13     	PaintCanvasBase canvasBase;
14     	List<GroupShape> groupShapeCollectionList;
15     
16     	public ShapeUngroupCommand(PaintCanvasBase canvasBase) {
17     		this.canvasBase = canvasBase;
18     
19     		List<IShape> shapeCollectionList = CommandHistory.shapeCollection.getList();
20     
21     		groupShapeCollectionList = shapeCollectionList.stream().filter(ishape -> ishape instanceof GroupShape)
22     				.map(p -> (GroupShape) p).collect(Collectors.toList());
23     	}
24     
25     	@Override
26     	public void redo() {
27     		for (GroupShape groupShape : groupShapeCollectionList) {
28     			groupShape.unGroup();
29     		}
30     
31     		canvasBase.repaint();
32     	}
33     
34     	@Override
35     	public void run() {
36     
37     		for (GroupShape groupShape : groupShapeCollectionList) {
38     			groupShape.unGroup();
39     		}
40     
41     		canvasBase.repaint();
42     		CommandHistory.add(this);
43     	}
44     
45     	@Override
46     	public void undo() {
47     		for (GroupShape groupShape : groupShapeCollectionList) {
48     			groupShape.create();
49     		}
50     		canvasBase.repaint();
51     	}
52     
53     }
54