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