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.awt.LayoutManager;
25  import java.awt.Point;
26  
27  import javax.swing.JPanel;
28  import javax.swing.JScrollPane;
29  import javax.swing.ScrollPaneConstants;
30  
31  import net.sf.magicproject.clickable.targetable.card.CardFactory;
32  import net.sf.magicproject.clickable.targetable.card.MCard;
33  import net.sf.magicproject.token.IdConst;
34  import net.sf.magicproject.tools.Log;
35  import net.sf.magicproject.tools.MToolKit;
36  
37  /***
38   * Represents Graveyard or Library zone.<br>
39   * All indexes of this zone are inverted.
40   * 
41   * @author Fabrice Daugan
42   * @since 0.2c
43   * @since 0.3 feature "reverseImage" implemented
44   * @since 0.4 you can now change wallpaper/color of this MZone and setting are
45   *        saved.
46   * @since 0.54.16 new shuffle algorythm
47   */
48  public class ExpandableZone extends MZone {
49  
50  	/***
51  	 * create a new instance of ExpandableZone
52  	 * 
53  	 * @param zoneConfiguration
54  	 *          the zone configuration
55  	 * @param superPanel
56  	 *          scroll panel containing this panel
57  	 * @param reverseImage
58  	 *          if true the backpicture will be reversed
59  	 * @since 0.3 feature "reverseImage" implemented
60  	 */
61  	protected ExpandableZone(ZoneConfiguration zoneConfiguration,
62  			JScrollPane superPanel, boolean reverseImage) {
63  		super(zoneConfiguration.getZoneId(), null, superPanel, reverseImage,
64  				zoneConfiguration.getZoneName());
65  		if (zoneConfiguration.getLayoutClass() != null) {
66  			try {
67  				setLayout((LayoutManager) Class.forName(
68  						zoneConfiguration.getLayoutClass()).newInstance());
69  			} catch (Exception e) {
70  				Log.error("Unable to load the specified layout manager '"
71  						+ zoneConfiguration.getLayoutClass() + "'", e);
72  			}
73  		}
74  		superPanel
75  				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
76  
77  		superPanel
78  				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
79  		this.zoneConfiguration = zoneConfiguration;
80  	}
81  
82  	@Override
83  	public final void highLight(Color targetColor) {
84  		// deploy this panel
85  		if (ZoneManager.expandedZone != this) {
86  			deployPanel();
87  		}
88  	}
89  
90  	/***
91  	 * Toggle display of this zone
92  	 */
93  	public void toggle() {
94  		if (ZoneManager.expandedZone == this) {
95  			// Gather this zone
96  			ZoneManager.expandedZone = null;
97  			ZoneManager.expandedPanel.setVisible(false);
98  			gatheredParent
99  					.add(superPanel, zoneConfiguration.getLayoutConstraintYou());
100 			updatePanel();
101 		} else {
102 			// deploy this zone
103 			deployPanel();
104 		}
105 	}
106 
107 	/***
108 	 * seprarate cards of this zone
109 	 */
110 	public void deployPanel() {
111 		if (ZoneManager.expandedZone == this) {
112 			return;
113 		}
114 
115 		if (ZoneManager.expandedZone != null) {
116 			// gather the previous zone
117 			ZoneManager.expandedZone.toggle();
118 		}
119 
120 		final int count = getCardCount();
121 		if (count == 0)
122 			return;
123 		ZoneManager.expandedZone = this;
124 		Point location = MToolKit.getAbsoluteLocation(getTop());
125 		location.translate(-6, -6);
126 		ZoneManager.expandedPanel.setLocation(location);
127 		ZoneManager.expandedPanel.removeAll();
128 		gatheredParent = (JPanel) superPanel.getParent();
129 		ZoneManager.expandedPanel.add(superPanel);
130 
131 		for (int a = 0; a < count; a++) {
132 			getCard(a).setLocation(2 + (CardFactory.cardWidth + 2) * a, 2);
133 		}
134 
135 		setPreferredSize(new Dimension((CardFactory.cardWidth + 2) * count + 4,
136 				CardFactory.cardHeight + 10));
137 		ZoneManager.expandedPanel.setSize(new Dimension(Math.min(600,
138 				getPreferredSize().width + 10), CardFactory.cardHeight + 28));
139 		ZoneManager.expandedPanel.setMaximumSize(ZoneManager.expandedPanel
140 				.getSize());
141 		ZoneManager.expandedPanel.setPreferredSize(ZoneManager.expandedPanel
142 				.getSize());
143 		superPanel
144 				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
145 		superPanel.setViewportView(this);
146 		ZoneManager.expandedPanel.setVisible(true);
147 	}
148 
149 	/***
150 	 * update this library = hide library too
151 	 */
152 	@Override
153 	public void updatePanel() {
154 		final int count = getCardCount();
155 		if (ZoneManager.expandedZone == this) {
156 			toggle();
157 		} else {
158 			for (int a = 0; a < count; a++) {
159 				getCard(a).setLocation(18 + a / IdConst.CARD_3D_COUNT,
160 						2 + a / IdConst.CARD_3D_COUNT);
161 			}
162 			setPreferredSize(new Dimension((IdConst.STD_WIDTH + 5) / 2,
163 					CardFactory.cardHeight + 10));
164 			superPanel
165 					.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
166 			superPanel.setViewportView(this);
167 		}
168 	}
169 
170 	@Override
171 	public void disHighLight() {
172 		// gather this zone
173 		if (ZoneManager.expandedZone == this) {
174 			toggle();
175 		}
176 	}
177 
178 	/***
179 	 * Dishighlight all cards of this zone manager
180 	 */
181 	@Override
182 	public void disHighLightAll() {
183 		for (Component card : getComponents()) {
184 			((MCard) card).disHighLight();
185 		}
186 		disHighLight();
187 	}
188 
189 	@Override
190 	public boolean isMustBePaintedReversed(MCard card) {
191 		if (card != getTop() && ZoneManager.expandedZone != this)
192 			return false;
193 		return super.isMustBePaintedReversed(card);
194 	}
195 
196 	@Override
197 	public boolean isMustBePainted(MCard card) {
198 		if (card != getTop() && ZoneManager.expandedZone != this)
199 			return false;
200 		return super.isMustBePainted(card);
201 	}
202 
203 	@Override
204 	public boolean startDragAndDrop(MCard card, Point mousePoint) {
205 		dragAndDropComponent = null;
206 		return false;
207 	}
208 
209 	/***
210 	 * The configuration attached to this zone.
211 	 */
212 	public final ZoneConfiguration zoneConfiguration;
213 
214 	/***
215 	 * The panel containing this zone when it is not expanded.
216 	 */
217 	private JPanel gatheredParent;
218 
219 	/***
220 	 * Is this zone can be gathered or not.
221 	 * 
222 	 * @return <code>true</code> if this zone can be gathered.
223 	 */
224 	public boolean canBeGathered() {
225 		return zoneConfiguration.getLayoutClass() == null;
226 	}
227 
228 }