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.ui.wizard;
20  
21  import java.awt.Component;
22  import java.awt.Point;
23  import java.awt.event.ActionEvent;
24  import java.awt.event.MouseEvent;
25  import java.awt.event.MouseListener;
26  import java.awt.event.MouseMotionListener;
27  import java.util.List;
28  
29  import javax.swing.JScrollPane;
30  import javax.swing.ScrollPaneConstants;
31  import javax.swing.WindowConstants;
32  
33  import net.sf.magicproject.clickable.targetable.card.MCard;
34  import net.sf.magicproject.clickable.targetable.card.VirtualCard;
35  import net.sf.magicproject.clickable.targetable.player.Player;
36  import net.sf.magicproject.token.Visibility;
37  import net.sf.magicproject.ui.i18n.LanguageManager;
38  
39  /***
40   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
41   * @since 0.82
42   */
43  public class Arrange extends Ok implements MouseMotionListener, MouseListener {
44  
45  	/***
46  	 * Creates a new instance of Replacement <br>
47  	 * 
48  	 * @param destinationZone
49  	 *          the destination zone.
50  	 * @param movingCards
51  	 *          the moving cards you can arrange
52  	 * @param order
53  	 *          the integer array that will contain the chosen order.
54  	 * @param owner
55  	 */
56  	public Arrange(int destinationZone, List<MCard> movingCards, int[] order,
57  			Player owner) {
58  		super(LanguageManager.getString("wiz_arrange.title"), LanguageManager
59  				.getString("wiz_arrange.description", owner.zoneManager
60  						.getContainer(destinationZone)), "wiz_arrange.png", null, 600, 230);
61  		final JScrollPane scroll = new JScrollPane(
62  				ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
63  				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
64  		container = new DropCardListener();
65  		this.movingCards = movingCards;
66  		this.order = order;
67  		this.owner = owner;
68  		for (int i = 0; i < movingCards.size(); i++) {
69  			final MCard card = movingCards.get(i);
70  			order[i] = i;
71  			container.add(card);
72  			card.getComponent(0).addMouseListener(this);
73  			card.getComponent(0).addMouseMotionListener(this);
74  			card.addComponentListener(container);
75  			card.returnCard(Visibility.PUBLIC);
76  			card.tap(false);
77  		}
78  		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
79  		scroll.setAutoscrolls(true);
80  		scroll.setViewportView(container);
81  		gameParamPanel.add(scroll);
82  	}
83  
84  	@Override
85  	public void actionPerformed(ActionEvent event) {
86  		if (event.getSource() == cancelBtn) {
87  			// remove mouse listeners
88  			movingCards.clear();
89  			for (int i = 0; i < container.getComponentCount(); i++) {
90  				final MCard card = (MCard) container.getComponent(i);
91  				card.getComponent(0).removeMouseListener(this);
92  				card.getComponent(0).removeMouseMotionListener(this);
93  				movingCards.add(card);
94  			}
95  		}
96  		super.actionPerformed(event);
97  	}
98  
99  	public void mouseClicked(MouseEvent e) {
100 		// Ignore this event
101 	}
102 
103 	public void mousePressed(MouseEvent e) {
104 		if (e.getSource() instanceof VirtualCard) {
105 			srcPoint = ((VirtualCard) e.getSource()).card.getLocation();
106 			dX = e.getPoint().getX();
107 			dY = e.getPoint().getY();
108 		}
109 	}
110 
111 	public void mouseReleased(MouseEvent e) {
112 		srcPoint = null;
113 		if (e.getSource() instanceof VirtualCard) {
114 			// now we move the card within the component list.
115 			int srcIndex = getIndexOf(e.getSource());
116 			if (srcIndex != container.separatorIndex
117 					&& srcIndex + 1 != container.separatorIndex) {
118 				if (container.separatorIndex == container.getComponentCount()) {
119 					container.remove(((Component) e.getSource()).getParent());
120 					container.add(((Component) e.getSource()).getParent());
121 				} else if (container.separatorIndex < srcIndex) {
122 					container.remove(((Component) e.getSource()).getParent());
123 					container.add(((Component) e.getSource()).getParent(),
124 							container.separatorIndex);
125 				} else if (container.separatorIndex > srcIndex) {
126 					container.remove(((Component) e.getSource()).getParent());
127 					container.add(((Component) e.getSource()).getParent(),
128 							container.separatorIndex - 1);
129 				}
130 				int idComp = order[srcIndex];
131 				if (srcIndex <= container.separatorIndex) {
132 					if (container.separatorIndex == container.getComponentCount()) {
133 						for (int i = srcIndex; i < container.separatorIndex - 2; i++) {
134 							order[i] = order[i + 1];
135 						}
136 						order[container.separatorIndex - 1] = idComp;
137 					} else {
138 						for (int i = srcIndex; i < container.separatorIndex - 1; i++) {
139 							order[i] = order[i + 1];
140 						}
141 						order[container.separatorIndex] = idComp;
142 					}
143 				} else { // if (srcIndex > container.separatorIndex) {
144 					for (int i = srcIndex; i > container.separatorIndex; i--) {
145 						order[i] = order[i - 1];
146 					}
147 					order[container.separatorIndex] = idComp;
148 				}
149 			}
150 			container.doLayout();
151 		}
152 	}
153 
154 	public void mouseEntered(MouseEvent e) {
155 		// Ignore this event
156 	}
157 
158 	public void mouseExited(MouseEvent e) {
159 		// Ignore this event
160 	}
161 
162 	public void mouseDragged(MouseEvent e) {
163 		if (srcPoint != null) {
164 			srcPoint.translate((int) (e.getPoint().getX() - dX), (int) (e.getPoint()
165 					.getY() - dY));
166 			((VirtualCard) e.getSource()).card.setLocation((int) (srcPoint.getX()),
167 					(int) (srcPoint.getY()));
168 		}
169 	}
170 
171 	/***
172 	 * Return the index of specified component within this container.
173 	 * 
174 	 * @param comp
175 	 *          the component to locate.
176 	 * @return the index of specified component within this container.
177 	 */
178 	private int getIndexOf(Object comp) {
179 		for (int i = container.getComponentCount(); i-- > 0;) {
180 			if (((MCard) container.getComponent(i)).getComponent(0) == comp) {
181 				return i;
182 			}
183 		}
184 		throw new InternalError("Could not find the component '" + comp + "'");
185 	}
186 
187 	public void mouseMoved(MouseEvent e) {
188 		// Ignore this event
189 	}
190 
191 	/***
192 	 * This is the panel containing the components and is listening drag&drop to
193 	 * make some graphic stufs.
194 	 */
195 	private DropCardListener container;
196 
197 	/***
198 	 * List of component to sort. This list is sorted each time a component has
199 	 * moved.
200 	 */
201 	private List<MCard> movingCards;
202 
203 	/***
204 	 * The associated component id to the given list of cards. When a card is
205 	 * moved from I to J indexes, we slice all values of <code>order</code> from
206 	 * I to J-1 indexes and we put to J the value of <code>order[I]</code>
207 	 * before the move.
208 	 * 
209 	 * @see #movingCards
210 	 */
211 	public int[] order;
212 
213 	/***
214 	 * The point of cursor when the drag has began.
215 	 */
216 	private Point srcPoint;
217 
218 	/***
219 	 * The distance between the left-top corner and thedragging point.
220 	 * 
221 	 * @see #srcPoint
222 	 */
223 	private double dX;
224 
225 	/***
226 	 * The distance between the left-top corner and thedragging point.
227 	 * 
228 	 * @see #srcPoint
229 	 */
230 	private double dY;
231 
232 	/***
233 	 * The player arranging the cards
234 	 */
235 	public Player owner;
236 }