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