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.List;
22  
23  import org.vectomatic.client.rep.RepApplication;
24  import org.vectomatic.common.model.Shape;
25  import org.vectomatic.common.model.geometry.TransformMatrix;
26  
27  /**
28   * Command to delete a shape from the model
29   */
30  public class DeleteShapeCommand extends CommandBase {
31  	private TransformMatrix _m;
32  	private List<Shape> _shapes;
33  	
34  	public DeleteShapeCommand(RepApplication app) {
35  		super(app);
36  		_shapes = new ArrayList<Shape>(_app.getSelection().getSelectedShapes());
37  		_m = new TransformMatrix(_app.getSelection().getRootShape().getTransform());
38  	}
39  
40  	public String getDescription() {
41  		return _app.getConstants().deleteShapeCommand();
42  	}
43  
44  	public void execute() {
45  		for (int i = 0, size = _shapes.size(); i < size; i++) {
46  			Shape shape = _shapes.get(i);
47  			_app.getModel().removeShape(shape);
48  		}
49  		_app.getSelection().select(new ArrayList<Shape>());
50  	}
51  
52  	public void unexecute() {
53  		TransformMatrix mTmp = new TransformMatrix();
54  		TransformMatrix mInv = new TransformMatrix();
55  		_m.invert(mInv);
56  		_m.preMultiply(mInv, mTmp);
57  		for (int i = 0, size = _shapes.size(); i < size; i++) {
58  			Shape shape = _shapes.get(i);
59  			_app.getModel().addShape(shape);
60  			shape.setTransform(shape.getTransform().preMultiply(mInv, mTmp));
61  		}
62  		_app.getSelection().select(_shapes);
63  		for (int i = 0, size = _shapes.size(); i < size; i++) {
64  			Shape shape = _shapes.get(i);
65  			shape.setTransform(shape.getTransform().preMultiply(_m, mTmp));
66  		}
67  		_app.getSelection().getRootShape().setTransform(_m);
68  	}
69  
70  }