View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal 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   * svgreal 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 svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.command;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.vectomatic.dom.svg.utils.SVGConstants;
24  import org.vectomatic.svg.edit.client.model.svg.SVGElementModel;
25  
26  import com.extjs.gxt.ui.client.data.BeanModel;
27  import com.extjs.gxt.ui.client.data.BeanModelLookup;
28  import com.extjs.gxt.ui.client.store.Record;
29  import com.extjs.gxt.ui.client.util.Format;
30  
31  /**
32   * Generic class to record the edition of a property of an
33   * svg element.
34   * @author laaglu
35   */
36  public class GenericEditCommand extends CommandBase {
37  	protected SVGElementModel model;
38  	protected Map<String, Object> oldValues;
39  	protected Map<String, Object> newValues;
40  	protected String description;
41  	
42  	public GenericEditCommand(CommandFactoryBase factory, SVGElementModel model, Map<String, Object> oldValues, String description) {
43  		super(factory);
44  		this.model = model;
45  		this.oldValues = oldValues;
46  		this.description = description;
47  		newValues = new HashMap<String, Object>();
48  		for (String attrName : oldValues.keySet()) {
49  			newValues.put(attrName, model.get(attrName));
50  		}
51  	}
52  	
53  	@Override
54  	public String getDescription() {
55  		String changes = newValues.toString();
56  		return Format.substitute(description, model.get(SVGConstants.SVG_TITLE_TAG), changes.substring(1, changes.length() - 1));
57  	}
58  	
59  	@Override
60  	public void commit() {
61  		changeAttributes(newValues);
62  	}
63  
64  	@Override
65  	public void rollback() {
66  		changeAttributes(oldValues);
67  	}
68  	
69  	private void changeAttributes(Map<String, Object> values) {
70  		Record record = model.getRecord();
71  		record.beginEdit();
72  		for (Map.Entry<String, Object> entry : values.entrySet()) {
73  			record.set(entry.getKey(), entry.getValue());
74  		}
75  		record.endEdit();
76  		record.commit(false);
77  	}
78  	
79  	@Override
80  	public String toString() {
81  		return getDescription();
82  	}
83  	
84  	@Override
85  	public BeanModel asModel() {
86  		return BeanModelLookup.get().getFactory(GenericEditCommand.class).createModel(this);
87  	}
88  
89  }