View Javadoc

1   /*
2   * E-nspire Gemini.
3   * A Java and AspectJ based framework that enables transparent 
4   * bidirectional relationships between Plain Old Java Objects.
5   * 
6   * Copyright (C) 2005 Dragan Djuric
7   * 
8   * This program is free software; you can redistribute it and/or
9   * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  * 
22  * Contact the author at dragand@dev.java.net
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      }