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.Collection;
27
28 import com.enspire.gemini.BidirectionalProperty;
29
30 /***
31 * Executes the <code>add()</code> operation on a <code>BidirectionalCollection</code>.
32 * @author Dragan Djuric <dragand@dev.java.net>
33 * @since 1.0
34 */
35 public class BidirectionalCollectionAdd extends
36 BidirectionalCollectionCommand {
37
38 Object oldOwner;
39 Object addValue;
40 boolean hasChanged = false;
41
42 /***
43 * Creates a command and sets its dependencies.
44 * @param bidirectional the bidirectional collection that this command is tied to
45 * @param collection the unidirectional collection that this command is tied to
46 * @param addValue an object that should be added to the collection
47 */
48 public BidirectionalCollectionAdd(BidirectionalProperty bidirectional,
49 Collection collection, Object addValue) {
50 super(bidirectional, collection);
51 this.addValue = addValue;
52 }
53
54 /***
55 * @see com.enspire.gemini.commands.Command#execute()
56 */
57 public void execute() {
58 hasChanged = getCollection().add(addValue);
59 if (hasChanged) {
60 try {
61 oldOwner = getBidirectionalProperty().getRelationshipUpdater().
62 set(addValue,
63 getBidirectionalProperty().getOppositeName(),
64 getBidirectionalProperty().getOwner());
65 } catch(RuntimeException e) {
66 getCollection().remove(addValue);
67 throw e;
68 }
69 }
70 }
71
72 /***
73 * @see com.enspire.gemini.commands.Command#undo()
74 */
75 public void undo() {
76 if (!hasChanged) {
77 return;
78 }
79 getCollection().remove(addValue);
80 getBidirectionalProperty().getRelationshipUpdater().unset(addValue,
81 getBidirectionalProperty().getOppositeName(),
82 getBidirectionalProperty().getOwner());
83 if(oldOwner != null) {
84 getBidirectionalProperty().getRelationshipUpdater().set(addValue,
85 getBidirectionalProperty().getOppositeName(), oldOwner);
86 }
87 }
88
89 }