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.client.rep.command;
19  
20  import java.util.ArrayList;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.vectomatic.client.rep.RepApplication;
27  import org.vectomatic.client.rep.controller.EditPolylineController;
28  import org.vectomatic.common.model.geometry.Point;
29  import org.vectomatic.common.model.geometry.Polyline;
30  
31  /**
32   * Command to delete a vertex from a polyline
33   */
34  public class DeleteVertexCommand extends CommandBase {
35  	private EditPolylineController _editPolylineController;
36  	private Polyline _polyline;
37  	private List<Point> _vertices;
38  	private Set<Integer> _vertexIndices;
39  	
40  	public DeleteVertexCommand(RepApplication app, EditPolylineController editPolylineController, Polyline polyline, Set<Integer> vertexIndices) {
41  		super(app);
42  		_editPolylineController = editPolylineController;
43  		_polyline = polyline;
44  		_vertexIndices = new HashSet<Integer>(vertexIndices);
45  		_vertices = new ArrayList<Point>(_vertexIndices.size());
46  		Point[] vertices = _polyline.getVertices();
47  		Iterator<Integer> iterator = _vertexIndices.iterator();
48  		while (iterator.hasNext()) {
49  			int index = iterator.next().intValue();
50  			Point vertex = new Point(vertices[index]);
51  			_vertices.add(vertex);
52  		}
53  	}
54  	
55  	public String getDescription() {
56  		return _app.getConstants().deleteVertexCommand();
57  	}
58  
59  	public void execute() {
60  		Point[] vertices = _polyline.getVertices();
61  		boolean isClosed = _polyline.isClosed();
62  		List<Point> newVertices = new ArrayList<Point>();
63  		for (int i = 0, size = vertices.length - (isClosed ? 1 : 0); i < size; i++) {
64  			if (!_vertexIndices.contains(new Integer(i))) {
65  				newVertices.add(vertices[i]);
66  			}
67  		}
68  		if (isClosed) {
69  			newVertices.add(newVertices.get(0));
70  		}
71  		_polyline.setVertices(newVertices.toArray(new Point[newVertices.size()]));
72  		_app.getSelector().selectController(_editPolylineController.getButton());
73  		_editPolylineController.selectPolyline(_polyline, new HashSet<Integer>());
74  	}
75  
76  	public void unexecute() {
77  		Point[] vertices = _polyline.getVertices();
78  		boolean isClosed = _polyline.isClosed();
79  		Point[] newVertices = new Point[vertices.length + _vertexIndices.size()];
80  		int index = 0;
81  		Iterator<Integer> iterator = _vertexIndices.iterator();
82  		while (iterator.hasNext()) {
83  			newVertices[iterator.next().intValue()] = _vertices.get(index);
84  			index++;
85  		}
86  		index = 0;
87  		for (int i = 0, size = vertices.length - (isClosed ? 1 : 0); i < size; i++) {
88  			while (newVertices[index] != null) {				
89  				index++;
90  			}
91  			newVertices[index] = vertices[i];
92  		}
93  		if (isClosed) {
94  			newVertices[newVertices.length - 1] = newVertices[0];
95  		}
96  		_polyline.setVertices(newVertices);
97  		_app.getSelector().selectController(_editPolylineController.getButton());
98  		_editPolylineController.selectPolyline(_polyline, _vertexIndices);
99  	}
100 }