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.ArrayList;
21  import java.util.Collections;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.vectomatic.svg.edit.client.engine.SVGModel;
27  import org.vectomatic.svg.edit.client.model.ModelConstants;
28  import org.vectomatic.svg.edit.client.model.svg.SVGElementModel;
29  import org.vectomatic.svg.edit.client.model.svg.SVGNamedElementModel;
30  
31  import com.extjs.gxt.ui.client.data.BeanModel;
32  import com.extjs.gxt.ui.client.data.BeanModelLookup;
33  import com.extjs.gxt.ui.client.util.Format;
34  
35  /**
36   * Class to represent the removal of one or many elements from
37   * the SVG model.
38   * @author laaglu
39   */
40  public class GenericRemoveCommand extends CommandBase {
41  	/**
42  	 * The models to remove
43  	 */
44  	protected List<SVGElementModel> models;
45  	/**
46  	 * The parent models of the models to remove
47  	 */
48  	protected List<SVGElementModel> parentModels;
49  	/**
50  	 * The next non-null siblings of the models to remove, or null if no such siblings exist
51  	 */
52  	protected List<SVGElementModel> refModels;
53  	/**
54  	 * The owner of the models to remove
55  	 */
56  	protected SVGModel owner;
57  	/**
58  	 * The command description
59  	 */
60  	protected String description;
61  
62  	public GenericRemoveCommand(ICommandFactory factory, List<SVGElementModel> models, String description) {
63  		super(factory);
64  		// Sort nodes according to their order in the tree.
65  		Collections.<SVGElementModel>sort(models, SVGElementModel.getAscendingCompataror());
66  		Set<SVGElementModel> modelSet = new HashSet<SVGElementModel>(models);
67  		this.models = models;
68  		this.description = description;
69  		parentModels = new ArrayList<SVGElementModel>();
70  		refModels = new ArrayList<SVGElementModel>();
71  		for (SVGElementModel model : models) {
72  			SVGModel owner = model.getOwner();
73  			if (this.owner == null) {
74  				this.owner = owner;
75  			} else {
76  				assert this.owner == owner : "Attempt to remove models from heterogeneous owners: " + this.owner + ", " + owner;
77  			}
78  			SVGElementModel parentModel = (SVGElementModel) model.getParent();
79  			parentModels.add(parentModel);
80  			// Retrieve the next sibling not in the set of models to delete
81  			SVGElementModel siblingModel = model;
82  			while (((siblingModel = siblingModel.getNextSibling()) != null) && modelSet.contains(siblingModel)) {
83  			}
84  			refModels.add(siblingModel);
85  		}
86  	}
87  
88  	@Override
89  	public String getDescription() {
90  		return description;
91  	}
92  
93  	@Override
94  	public void commit() {
95  		for (int i = models.size() - 1; i >= 0; i--) {
96  			SVGElementModel model = models.get(i);
97  			owner.remove(model);
98  		}
99  	}
100 
101 	@Override
102 	public void rollback() {
103 		for (int i = 0, size = models.size(); i < size; i++) {
104 			SVGElementModel model = models.get(i);
105 			SVGElementModel parentModel = parentModels.get(i);
106 			SVGElementModel refModel = refModels.get(i);
107 			owner.insertBefore(parentModel, model, refModel);
108 		}
109 	}
110 
111 	@Override
112 	public BeanModel asModel() {
113 		return BeanModelLookup.get().getFactory(GenericRemoveCommand.class).createModel(this);
114 	}
115 
116 }