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.IAttributeValue;
26  import org.vectomatic.common.model.Shape;
27  
28  /**
29   * Command to set a graphical attribute on a shape
30   */
31  public class SetAttributeCommand extends CommandBase {
32  	private Attribute _attr;
33  	private List<Shape> _shapes;
34  	private List<IAttributeValue> _values;
35  
36  	public SetAttributeCommand(RepApplication app, Attribute attr, IAttributeValue value) {
37  		super(app);
38  		_attr = attr;
39  		_shapes = new ArrayList<Shape>(_app.getSelection().getSelectedShapes());
40  		_values = new ArrayList<IAttributeValue>();
41  		for (int i  = 0, count = _shapes.size(); i < count; i++) {
42  			_values.add(value);
43  		}
44  	}
45  	
46  	public String getDescription() {
47  		return _app.getConstants().setAttributeCommand() + _attr.getName();
48  	}
49  
50  
51  	public void execute() {
52  		applyAttributeChange();
53  	}
54  
55  	public void unexecute() {
56  		applyAttributeChange();
57  	}
58  
59  	private void applyAttributeChange() {
60  		for (int i  = 0, count = _shapes.size(); i < count; i++) {
61  			Shape shape = _shapes.get(i);
62  			_values.set(i, shape.setAttribute(_attr, _values.get(i)));
63  		}
64  		_shapes.get(0).getModel().fireModelHasChanged();
65  		_app.getSelection().fireSelectionHasChanged();
66  	}
67  }