View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.action.context;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import net.sf.magicproject.clickable.targetable.Targetable;
25  import net.sf.magicproject.stack.StackManager;
26  import net.sf.magicproject.test.Test;
27  
28  /***
29   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
30   * @since 0.86
31   */
32  public class TargetList implements ActionContext {
33  
34  	/***
35  	 * Create a new instance of this class.
36  	 */
37  	public TargetList() {
38  		super();
39  	}
40  
41  	/***
42  	 * Add yhe specified target to the list of this context.
43  	 * 
44  	 * @param target
45  	 *          the target to add
46  	 * @param test
47  	 *          the attached test validating the target
48  	 */
49  	public void add(Targetable target, Test test) {
50  		if (targetList == null) {
51  			targetList = new ArrayList<Targetable>();
52  			this.testList = new ArrayList<Test>();
53  		}
54  		targetList.add(target);
55  		testList.add(test);
56  	}
57  
58  	/***
59  	 * Add the specified targets to the list of this context.
60  	 * 
61  	 * @param targetList
62  	 *          the targets to add
63  	 * @param testList
64  	 *          the attached tests validating the targets
65  	 */
66  	public void addAll(List<Targetable> targetList, List<Test> testList) {
67  		if (this.targetList == null) {
68  			this.targetList = new ArrayList<Targetable>();
69  			this.testList = new ArrayList<Test>();
70  		}
71  		this.targetList.addAll(targetList);
72  		this.testList.addAll(testList);
73  	}
74  
75  	/***
76  	 * The targets added by this context.
77  	 */
78  	public List<Targetable> targetList;
79  
80  	/***
81  	 * The restriction test used to add the targets.
82  	 */
83  	public List<Test> testList;
84  
85  	/***
86  	 * Rollback the target list content.
87  	 */
88  	public void rollback() {
89  		for (int index = 0; index < targetList.size(); index++) {
90  			StackManager.getInstance().getTargetedList().removeTargeted(
91  					targetList.get(index));
92  		}
93  	}
94  
95  	/***
96  	 * Replay the target list operation.
97  	 * 
98  	 * @param eventOption
99  	 *          is the event mode : Force, default, none. Depending this value,
100 	 *          the added target will be rechecked before resolution process
101 	 * @param raiseEvent
102 	 *          if false, is any case, no event can be triggered.
103 	 */
104 	public void replay(int eventOption, boolean raiseEvent) {
105 		for (int index = 0; index < targetList.size(); index++) {
106 			StackManager.getInstance().getTargetedList().addTarget(eventOption,
107 					targetList.get(index), testList.get(index), raiseEvent);
108 		}
109 	}
110 }