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.Attribute;
25  import org.vectomatic.common.model.Shape;
26  import org.vectomatic.common.model.geometry.ShapeGroup;
27  import org.vectomatic.common.model.geometry.TransformMatrix;
28  
29  /**
30   * Command to group shapes
31   */
32  public class GroupCommand extends CommandBase {
33  	private ShapeGroup _group;
34  
35  	public GroupCommand(RepApplication app) {
36  		super(app);
37  		// Use the selection group to obtain a group with the proper
38  		// bounding box and position matrix
39  		_group = new ShapeGroup((ShapeGroup)_app.getSelection().getRootShape());
40  		_group.clearAttribute(Attribute.FILL_OPACITY);
41  		
42  		// Replace its contents with the actually select model shapes
43  		_group.getShapes().clear();
44  		_group.getShapes().addAll(_app.getSelection().getSelectedShapes());
45  	}
46  	
47  	public String getDescription() {
48  		return _app.getConstants().groupCommand();
49  	}
50  
51  	public void execute() {
52  		_app.getModel().addShape(_group);
53  		List<Shape> shapes = _group.getShapes();
54  		TransformMatrix groupInv = new TransformMatrix(_group.getTransform()).invert();
55  		TransformMatrix mTmp = new TransformMatrix();
56  		for (int i = 0, size = shapes.size(); i < size; i++) {
57  			Shape shape = shapes.get(i);
58  			shape.setTransform(shape.getTransform().preMultiply(groupInv, mTmp));
59  			_app.getModel().removeShape(shape);
60  		}
61  		List<Shape> selectedShapes = new ArrayList<Shape>();
62  		selectedShapes.add(_group);
63  		_app.getSelection().select(selectedShapes);
64  	}
65  
66  	public void unexecute() {
67  		_app.getModel().removeShape(_group);
68  		List<Shape> shapes = _group.getShapes();
69  		TransformMatrix mTmp = new TransformMatrix();
70  		for (int i = 0, size = shapes.size(); i < size; i++) {
71  			Shape shape = shapes.get(i);
72  			shape.setTransform(shape.getTransform().preMultiply(_group.getTransform(), mTmp));
73  			_app.getModel().addShape(shape);
74  		}
75  		_app.getSelection().select(_group.getShapes());
76  	}
77  }