View Javadoc

1   /**********************************************
2    * Copyright (C) 2009 Lukas Laag
3    * This file is part of Vectomatic.
4    * 
5    * Vectomatic is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * Vectomatic is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with Vectomatic.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.common.model.geometry;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.vectomatic.common.model.IShapeVisitor;
24  import org.vectomatic.common.model.Shape;
25  
26  import com.google.gwt.user.client.rpc.IsSerializable;
27  
28  /**
29   * Class to represent a path. A path is made of
30   * a list of line segments and Bezier spline segments
31   */
32  public class Path extends Shape implements IsSerializable {
33  	private List<Segment> _segments;
34  	
35  	public Path() {
36  		// For GWT serialization
37  	}
38  
39  	public Path(List<Segment> segments) {
40  		super();
41  		_bbox = new BoundingBox();
42  		_segments = new ArrayList<Segment>(segments);
43  		_updateBoundingBox();
44  	}
45  
46  	public Path(Path path) {
47  		super(path);
48  		_segments = new ArrayList<Segment>();
49  		List<Segment> segments = path.getSegments();
50  		for (int i = 0, size = segments.size(); i < size; i++) {
51  			_segments.add(segments.get(i).clone());
52  		}
53  	}
54  
55  	@Override
56  	public void acceptVisitor(IShapeVisitor visitor) {
57  		visitor.visitPath(this);
58  	}
59  
60  	@Override
61  	public boolean isSame(Shape shape) {
62  		if (shape instanceof Path) {
63  			Path path = (Path)shape;
64  			return getTransform().equals(path.getTransform()) 
65  				&& _attributes.equals(path._attributes)
66  				&& _segments.equals(path._segments);
67  		}
68  		return false;
69  	}
70  
71  	public void addSegment(Segment segment) {
72  		_segments.add(segment);
73  	}
74  	
75  	public List<Segment> getSegments() {
76  		return _segments;
77  	}
78  	
79  	public boolean isClosed() {
80  		if (_segments.size() >= 2) {
81  			return _segments.get(0).getStartPoint().equals(_segments.get(_segments.size() - 1).getEndPoint());
82  		}
83  		return false;
84  	}
85  	
86  	private void _updateBoundingBox() {
87  		_bbox = new BoundingBox(_segments.get(0).getBoundingBox());
88  		for (int i = 1, size = _segments.size(); i < size; i++) {
89  			_bbox.union(_segments.get(i).getBoundingBox());
90  		}
91  		_bbox.getPoint(BoundingBox.PT_C, _t);
92  	}
93  
94  	@Override
95  	public String toString() {
96  		StringBuffer buffer = new StringBuffer();
97  		buffer.append("Path(m=");
98  		buffer.append(getTransform().toString());
99  		buffer.append(" bbox=");
100 		buffer.append(_bbox.toString());
101 		buffer.append(" segments=");
102 		buffer.append(_segments);
103 		buffer.append(" atts=");
104 		buffer.append(_attributes.toString());
105 		buffer.append(")");
106 		return buffer.toString();
107 	}
108 }