1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
37
38
39
40 public class GenericRemoveCommand extends CommandBase {
41
42
43
44 protected List<SVGElementModel> models;
45
46
47
48 protected List<SVGElementModel> parentModels;
49
50
51
52 protected List<SVGElementModel> refModels;
53
54
55
56 protected SVGModel owner;
57
58
59
60 protected String description;
61
62 public GenericRemoveCommand(ICommandFactory factory, List<SVGElementModel> models, String description) {
63 super(factory);
64
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
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 }