1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.enspire.gemini.commands;
25
26 import java.util.List;
27
28 import com.enspire.gemini.BidirectionalProperty;
29
30 /***
31 * Executes the <code>remove()</code> operation on a <code>BidirectionalList</code>.
32 * @author Dragan Djuric <dragand@dev.java.net>
33 * @since 1.0
34 */
35 public class BidirectionalListRemoveByIndex extends
36 BidirectionalListCommand {
37
38 Object removeValue;
39 int removeIndex;
40 boolean hasChanged = false;
41
42 /***
43 * Creates a command and sets its dependencies.
44 * @param bidirectional the bidirectional list that this command is tied to
45 * @param list the unidirectional list that this command is tied to
46 * @param removeIndex an index of an object that should be removed to the collection
47 * @param removeValue an object that should be added to the list
48 */
49 public BidirectionalListRemoveByIndex(BidirectionalProperty bidirectional,
50 List list, int removeIndex, Object removeValue) {
51 super(bidirectional, list);
52 this.removeValue = removeValue;
53 this.removeIndex = removeIndex;
54 }
55
56 /***
57 * @see com.enspire.gemini.commands.Command#execute()
58 */
59 public void execute() {
60 hasChanged = getList().remove(removeIndex) != null;
61 if (hasChanged) {
62 try {
63 getBidirectionalProperty().getRelationshipUpdater().
64 unset(removeValue,
65 getBidirectionalProperty().getOppositeName(),
66 getBidirectionalProperty().getOwner());
67 } catch(RuntimeException e) {
68 getList().add(removeIndex, removeValue);
69 throw e;
70 }
71 }
72 }
73
74 /***
75 * @see com.enspire.gemini.commands.Command#undo()
76 */
77 public void undo() {
78 if (!hasChanged) {
79 return;
80 }
81 getList().add(removeIndex, removeValue);
82 getBidirectionalProperty().getRelationshipUpdater().set(removeValue,
83 getBidirectionalProperty().getOppositeName(),
84 getBidirectionalProperty().getOwner());
85 }
86 }