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.zone;
20  
21  import java.awt.Color;
22  import java.awt.Component;
23  import java.awt.Dimension;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import net.sf.magicproject.clickable.targetable.card.AbstractCard;
30  import net.sf.magicproject.clickable.targetable.card.MCard;
31  import net.sf.magicproject.stack.StackManager;
32  import net.sf.magicproject.token.IdZones;
33  import net.sf.magicproject.tools.Configuration;
34  import net.sf.magicproject.ui.layout.SectorLayout;
35  
36  /***
37   * Represents the play zone
38   * 
39   * @author Fabrice Daugan
40   * @since 0.2d
41   * @since 0.3 feature "reverseImage" implemented
42   * @since 0.4 you can now change wallpaper/color of this MZone and setting are
43   *        saved.
44   * @since 0.72 borderlayout added
45   * @since 0.91 use list instead of panel to represent a sector.
46   */
47  public class Play extends MZone {
48  
49  	/***
50  	 * The zone name.
51  	 */
52  	public static final String ZONE_NAME = "play";
53  
54  	/***
55  	 * create a new instance of Play
56  	 * 
57  	 * @param reverseImage
58  	 *          if true the backpicture will be reversed
59  	 * @since 0.3 feature "reverseImage" implemented
60  	 * @see IdZones
61  	 */
62  	public Play(boolean reverseImage) {
63  		super(IdZones.PLAY, null, null, reverseImage, ZONE_NAME);
64  		setMinimumSize(new Dimension(90, 60));
65  		sectors = new ArrayList<ZoneSector>();
66  	}
67  
68  	@Override
69  	public void updateReversed() {
70  		// need to update?
71  		if (oldReverse == (Configuration.getBoolean("reverseArt", true) && reverseImage)) {
72  			return;
73  		}
74  		synchronized (getTreeLock()) {
75  			oldReverse = !oldReverse;
76  			// TODO change the layout
77  		}
78  
79  	}
80  
81  	@Override
82  	public Dimension getPreferredSize() {
83  		return new Dimension(super.getPreferredSize().width,
84  				getParent().getSize().height);
85  	}
86  
87  	@Override
88  	public void remove(Component component) {
89  		if (component instanceof AbstractCard) {
90  			for (ZoneSector sector : sectors) {
91  				if (sector.remove((AbstractCard) component))
92  					break;
93  			}
94  			super.remove((AbstractCard) component);
95  			return;
96  		}
97  		throw new InternalError("should not be called");
98  	}
99  
100 	@Override
101 	public void reset() {
102 		for (ZoneSector sector : sectors) {
103 			sector.getCards().clear();
104 		}
105 		sectors.clear();
106 		super.reset();
107 	}
108 
109 	@Override
110 	public void remove(AbstractCard card) {
111 		for (ZoneSector sector : sectors) {
112 			if (sector.remove(card))
113 				break;
114 		}
115 		super.remove(card);
116 	}
117 
118 	@Override
119 	public void add(MCard card, int pIndex) {
120 		boolean found = false;
121 		for (ZoneSector sector : sectors) {
122 			if (sector.getTest().test(null, card)) {
123 				sector.add(card);
124 				found = true;
125 				break;
126 			}
127 		}
128 		if (!found)
129 			sectors.get(0).add(card);
130 		super.add(card, pIndex);
131 	}
132 
133 	@Override
134 	public final void highLight(Color targetColor) {
135 		// TODO Make a highlighted border
136 	}
137 
138 	@Override
139 	public void disHighLight() {
140 		// TODO Make a highlighted border
141 	}
142 
143 	@Override
144 	public void shuffle() {
145 		throw new InternalError("should not be called");
146 	}
147 
148 	/***
149 	 * Init the sector configurations.
150 	 * <ul>
151 	 * Structure of stream : Data[size]
152 	 * <li>sectors [SectorConfiguration[]]</li>
153 	 * </ul>
154 	 * <br>
155 	 * 
156 	 * @param inputStream
157 	 *          the stream containing the layout configuration of this zone for a
158 	 *          specified layout
159 	 * @throws IOException
160 	 *           error while reading.
161 	 */
162 	public static void initSectorConfigurations(InputStream inputStream)
163 			throws IOException {
164 		sectorConfigurations = new ArrayList<SectorConfiguration>();
165 		for (int i = inputStream.read(); i-- > 0;) {
166 			sectorConfigurations.add(new SectorConfiguration(inputStream));
167 		}
168 		StackManager.PLAYERS[0].zoneManager.play.initSectors();
169 		StackManager.PLAYERS[1].zoneManager.play.initSectors();
170 	}
171 
172 	/***
173 	 * Init the sectors of this instance.
174 	 */
175 	public void initSectors() {
176 		for (SectorConfiguration sectorConfiguration : sectorConfigurations) {
177 			sectors.add(new ZoneSector(sectorConfiguration));
178 		}
179 		setLayout(new SectorLayout(sectors));
180 	}
181 
182 	private final List<ZoneSector> sectors;
183 
184 	private static List<SectorConfiguration> sectorConfigurations;
185 
186 	private boolean oldReverse = Configuration.getBoolean("reverseArt", true)
187 			&& reverseImage;
188 
189 }