View Javadoc

1   /*
2    * Created on Aug 19, 2004 
3    * Original filename was AttachmentLayout.java
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   * 
22   */
23  package net.sf.magicproject.ui.layout;
24  
25  import java.awt.Component;
26  import java.awt.Container;
27  import java.awt.Dimension;
28  import java.awt.LayoutManager;
29  
30  import net.sf.magicproject.clickable.targetable.card.CardFactory;
31  import net.sf.magicproject.clickable.targetable.card.MCard;
32  import net.sf.magicproject.tools.Configuration;
33  
34  /***
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   */
37  public class AttachmentLayout implements LayoutManager {
38  
39  	/***
40  	 * Attachment dx position
41  	 */
42  	private int attachmentDx;
43  
44  	/***
45  	 * Attachment dy position
46  	 */
47  	private static int attachmentDy;
48  
49  	/***
50  	 * Amount of pixels increased/decreased for each mouse wheel unit.
51  	 */
52  	public static final int PIXEL_UNTIT = 5;
53  
54  	/***
55  	 * Creates a new instance of AttachmentLayout <br>
56  	 */
57  	public AttachmentLayout() {
58  		super();
59  		attachmentDy = (int) (CardFactory.cardHeight * 0.2f);
60  		attachmentDx = attachmentDy;
61  	}
62  
63  	public Dimension preferredLayoutSize(Container target) {
64  		synchronized (target.getTreeLock()) {
65  			final MCard card = (MCard) target;
66  			if (card.tapped) {
67  				return new Dimension(card.getMUI().tappedSize.width + attachmentDy
68  						* (card.getComponentCount() - 1), card.getMUI().tappedSize.height
69  						+ attachmentDx * (card.getComponentCount() - 1));
70  
71  			}
72  			return new Dimension(card.getMUI().untappedSize.width + attachmentDx
73  					* (card.getComponentCount() - 1), card.getMUI().untappedSize.height
74  					+ attachmentDy * (card.getComponentCount() - 1));
75  		}
76  	}
77  
78  	public Dimension minimumLayoutSize(Container target) {
79  		synchronized (target.getTreeLock()) {
80  			return preferredLayoutSize(target);
81  		}
82  	}
83  
84  	public void layoutContainer(Container target) {
85  		synchronized (target.getTreeLock()) {
86  			final MCard card = (MCard) target;
87  			final int nb = card.getComponentCount();
88  			if (card.tapped) {
89  				if (card.reversed && Configuration.getBoolean("reverseArt", true)) {
90  					for (int i = nb; i-- > 0;) {
91  						card.getComponent(i).setLocation((nb - i - 1) * attachmentDy,
92  								(nb - i - 1) * attachmentDx);
93  					}
94  				} else {
95  					for (int i = nb; i-- > 0;) {
96  						card.getComponent(i)
97  								.setLocation(i * attachmentDy, i * attachmentDx);
98  					}
99  				}
100 			} else if (card.reversed && Configuration.getBoolean("reverseArt", true)) {
101 				for (int i = nb; i-- > 0;) {
102 					card.getComponent(i).setLocation((nb - i - 1) * attachmentDx,
103 							i * attachmentDy);
104 				}
105 			} else {
106 				for (int i = nb; i-- > 0;) {
107 					card.getComponent(i).setLocation(i * attachmentDx,
108 							(nb - i - 1) * attachmentDy);
109 				}
110 			}
111 			card.setPreferredSize(preferredLayoutSize(card));
112 			card.setSize(card.getPreferredSize());
113 		}
114 	}
115 
116 	public void addLayoutComponent(String name, Component comp) {
117 		// Ignore this event
118 	}
119 
120 	public void removeLayoutComponent(Component comp) {
121 		// Ignore this event
122 	}
123 
124 	/***
125 	 * Decrease the amount of units used to separate nested cards.
126 	 * 
127 	 * @param amount
128 	 *          the amount of units.
129 	 */
130 	public void decreaseCardLayout(int amount) {
131 		attachmentDx -= amount * PIXEL_UNTIT;
132 		attachmentDy -= amount * PIXEL_UNTIT;
133 		if (attachmentDx < 0) {
134 			attachmentDx = 0;
135 		}
136 		if (attachmentDy < 0) {
137 			attachmentDy = 0;
138 		}
139 	}
140 
141 	/***
142 	 * Increase the amount of units used to separate nested cards.
143 	 * 
144 	 * @param amount
145 	 *          the amount of units.
146 	 */
147 	public void increaseCardLayout(int amount) {
148 		attachmentDx += amount * PIXEL_UNTIT;
149 		attachmentDy += amount * PIXEL_UNTIT;
150 		if (attachmentDx > CardFactory.cardWidth) {
151 			attachmentDx = CardFactory.cardWidth;
152 		}
153 		if (attachmentDy > CardFactory.cardHeight) {
154 			attachmentDy = CardFactory.cardHeight;
155 		}
156 	}
157 }