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 move a vertex in a polyline
33   */
34  public class MoveVertexCommand extends CommandBase {
35  	private EditPolylineController _editPolylineController;
36  	private Polyline _polyline;
37  	private List<Point> _vertices;
38  	private Set<Integer> _vertexIndices;
39  	
40  	public MoveVertexCommand(RepApplication app, EditPolylineController editPolylineController, Polyline polyline, Polyline clone, 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[] cloneVertices = clone.getVertices();
47  		Iterator<Integer> iterator = _vertexIndices.iterator();
48  		while (iterator.hasNext()) {
49  			int index = iterator.next().intValue();
50  			Point vertex = new Point(cloneVertices[index]);
51  			_vertices.add(vertex);
52  		}
53  	}
54  	
55  	public String getDescription() {
56  		return _app.getConstants().moveVertexCommand();
57  	}
58  
59  	public void execute() {
60  		_changeVertices();
61  	}
62  
63  	public void unexecute() {
64  		_changeVertices();
65  	}
66  	
67  	private void _changeVertices() {
68  		Point[] vertices = _polyline.getVertices();
69  		int index = 0;
70  		boolean closed = _polyline.isClosed();
71  		Iterator<Integer> iterator = _vertexIndices.iterator();
72  		while (iterator.hasNext()) {
73  			int pos = iterator.next().intValue();
74  			vertices[pos].swap(_vertices.get(index));
75  			if (pos == 0 && closed) {
76  				vertices[0].copyTo(vertices[vertices.length - 1]);
77  			}
78  			index++;
79  		}
80  		_polyline.setVertices(vertices);
81  		_app.getSelector().selectController(_editPolylineController.getButton());
82  		_editPolylineController.selectPolyline(_polyline, _vertexIndices);
83  	}
84  }