File: /Users/shatabdi/Education/GitHub/se450_project/src/model/shape/GroupShape.java

1     package model.shape;
2     
3     import java.awt.BasicStroke;
4     import java.awt.Color;
5     import java.awt.Font;
6     import java.awt.Graphics;
7     import java.awt.Graphics2D;
8     import java.awt.Point;
9     import java.awt.Shape;
10     import java.awt.Stroke;
11     import java.awt.geom.AffineTransform;
12     import java.util.ArrayList;
13     import java.util.List;
14     
15     import controller.interfaces.IUndoable;
16     import model.commands.CommandHistory;
17     import model.interfaces.IShape;
18     import model.others.BoundingBox;
19     import view.Enum.ShapeColor;
20     import view.Enum.ShapeShadingType;
21     import view.Enum.ShapeType;
22     import view.interfaces.PaintCanvasBase;
23     
24     public class GroupShape implements IShape, IUndoable {
25     
26     	private Shape boundingBox;
27     	private Graphics2D graphics2d;
28     	List<IShape> groupedShapes = new ArrayList<>();
29     
30     	private PaintCanvasBase paintCanvasBaseBase;
31     
32     	public GroupShape(GroupShape groupShape) {
33     		this.paintCanvasBaseBase = groupShape.paintCanvasBaseBase;
34     		this.graphics2d = groupShape.paintCanvasBaseBase.getGraphics2D();
35     
36     		for (IShape lGShapes : groupShape.getList()) {
37     			this.add(lGShapes.copyShape());
38     		}
39     		this.createBoundingBox();
40     	}
41     
42     	public GroupShape(PaintCanvasBase canvasBase) {
43     		this.paintCanvasBaseBase = canvasBase;
44     		this.graphics2d = canvasBase.getGraphics2D();
45     	}
46     
47     	public void add(IShape iShape) {
48     		if (!groupedShapes.contains(iShape)) {
49     			groupedShapes.add(iShape);
50     
51     			this.createBoundingBox();
52     		}
53     	}
54     
55     	@Override
56     	public void addX(int x) {
57     		// TODO Auto-generated method stub
58     
59     	}
60     
61     	@Override
62     	public void addY(int y) {
63     		// TODO Auto-generated method stub
64     
65     	}
66     
67     	@Override
68     	public Boolean contain(Point startpoint) {
69     		// TODO Auto-generated method stub
70     		return null;
71     	}
72     
73     	@Override
74     	public IShape copyShape() {
75     		return new GroupShape(this);
76     	}
77     
78     	@Override
79     	public void create() {
80     		CommandHistory.shapeCollection.add(this);
81     
82     		for (IShape iShape : this.groupedShapes) {
83     			CommandHistory.shapeCollection.remove(iShape);
84     		}
85     
86     		this.createBoundingBox();
87     	}
88     
89     	private void createBoundingBox() {
90     		List<Shape> groupShapeBounds = new ArrayList<>();
91     
92     		for (IShape group : groupedShapes) {
93     			Shape rect = group.getBoundingBox();
94     			if (!groupShapeBounds.contains(rect)) {
95     				groupShapeBounds.add(group.getBoundingBox());
96     			}
97     		}
98     		this.boundingBox = BoundingBox.createBoundingBox(groupShapeBounds);
99     	}
100     
101     	@Override
102     	public void deleteShape() {
103     		unGroup();
104     		for (IShape iShape : groupedShapes) {
105     			iShape.deleteShape();
106     		}
107     	}
108     
109     	@Override
110     	public void draw(Graphics g) {
111     		// TODO Auto-generated method stub
112     
113     	}
114     
115     	public void drawBoundingBox() {
116     		graphics2d.setFont(new Font(Messages.getString("GroupShape.0"), Font.BOLD, 16)); //$NON-NLS-1$
117     		Stroke dashed_outline = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 1, new float[] { 9 },
118     				0);
119     
120     		graphics2d.setStroke(dashed_outline);
121     
122     		graphics2d.setPaint(Color.BLACK);
123     		graphics2d.draw(this.getBoundingBox());
124     		graphics2d.setColor(Color.BLACK);
125     
126     		graphics2d.drawString(Messages.getString("GroupShape.1"), this.getBoundingBox().getBounds().x, //$NON-NLS-1$
127     				this.getBoundingBox().getBounds().y);
128     	}
129     
130     	@Override
131     	public Shape getBoundingBox() {
132     		return boundingBox;
133     	}
134     
135     	@Override
136     	public Point getEndPoint() {
137     		// TODO Auto-generated method stub
138     		return null;
139     	}
140     
141     	public List<IShape> getList() {
142     		return groupedShapes;
143     	}
144     
145     	@Override
146     	public List<IShape> getNodeList() {
147     		List<IShape> newList = new ArrayList<>();
148     
149     		for (IShape iShape : groupedShapes) {
150     			newList.addAll(iShape.getNodeList());
151     		}
152     		return newList;
153     	}
154     
155     	@Override
156     	public ShapeColor getPrimaryColor() {
157     		// TODO Auto-generated method stub
158     		return null;
159     	}
160     
161     	@Override
162     	public ShapeColor getSecondaryColor() {
163     		// TODO Auto-generated method stub
164     		return null;
165     	}
166     
167     	@Override
168     	public ShapeShadingType getShadingType() {
169     		// TODO Auto-generated method stub
170     		return null;
171     	}
172     
173     	@Override
174     	public ShapeType getShapeType() {
175     		// TODO Auto-generated method stub
176     		return null;
177     	}
178     
179     	@Override
180     	public Point getStartPoint() {
181     		// TODO Auto-generated method stub
182     		return null;
183     	}
184     
185     	@Override
186     	public Stroke getStroke() {
187     		// TODO Auto-generated method stub
188     		return null;
189     	}
190     
191     	public void group() {
192     
193     		List<IShape> selectedCollectionList = CommandHistory.shapesSelected.getList();
194     
195     		if (selectedCollectionList != null) {
196     			for (IShape iShape : selectedCollectionList) {
197     				this.add(iShape);
198     			}
199     			CommandHistory.shapesSelected.removeAll(this.groupedShapes);
200     			CommandHistory.shapesSelected.add(this);
201     
202     			create();
203     
204     			drawBoundingBox();
205     		}
206     	}
207     
208     	@Override
209     	public void highlightShape() {
210     		for (IShape iShape : groupedShapes) {
211     			iShape.highlightShape();
212     		}
213     	}
214     
215     	@Override
216     	public void moveShape(int X, int Y) {
217     		AffineTransform transform = new AffineTransform();
218     
219     		transform.translate(X, Y);
220     
221     		boundingBox = transform.createTransformedShape(boundingBox);
222     
223     		for (IShape iShape : groupedShapes) {
224     			iShape.moveShape(X, Y);
225     		}
226     	}
227     
228     	@Override
229     	public void paintShapeOnCanvas() {
230     		for (IShape iShape : groupedShapes) {
231     			iShape.setGraphics2d(graphics2d);
232     			iShape.paintShapeOnCanvas();
233     		}
234     	}
235     
236     	@Override
237     	public IShape pasteShape() {
238     		GroupShape gs = new GroupShape(paintCanvasBaseBase);
239     		for (IShape iShape : groupedShapes) {
240     			gs.add(iShape.pasteShape());
241     		}
242     		gs.create();
243     		return gs;
244     	}
245     
246     	@Override
247     	public void redo() {
248     		create();
249     	}
250     
251     	public void remove(IShape iShape) {
252     		groupedShapes.add(iShape);
253     		this.createBoundingBox();
254     	}
255     
256     	@Override
257     	public void selectMaxandMin(Point start, Point end) {
258     		// TODO Auto-generated method stub
259     
260     	}
261     
262     	@Override
263     	public void setGraphics2d(Graphics2D graphics2d) {
264     		this.graphics2d = graphics2d;
265     	}
266     
267     	@Override
268     	public void setPrimaryColor(ShapeColor primaryColor) {
269     		// TODO Auto-generated method stub
270     
271     	}
272     
273     	@Override
274     	public void setSecondaryColor(ShapeColor secondaryColor) {
275     		// TODO Auto-generated method stub
276     
277     	}
278     
279     	@Override
280     	public void setShadingColor(ShapeShadingType shapeShadingType) {
281     		// TODO Auto-generated method stub
282     
283     	}
284     
285     	@Override
286     	public void setStroke(Stroke stroke) {
287     		// TODO Auto-generated method stub
288     
289     	}
290     
291     	@Override
292     	public void undo() {
293     		unGroup();
294     	}
295     
296     	public void unGroup() {
297     		for (IShape iShape : groupedShapes) {
298     			CommandHistory.shapeCollection.add(iShape);
299     		}
300     		CommandHistory.shapeCollection.remove(this);
301     		CommandHistory.shapesSelected.remove(this);
302     	}
303     }
304