View Javadoc

1   /*
2    * Created on Jan 9, 2005
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package net.sf.magicproject.ui.wizard;
22  
23  import java.awt.Color;
24  import java.awt.Component;
25  import java.awt.FlowLayout;
26  import java.awt.Graphics;
27  import java.awt.event.ComponentEvent;
28  import java.awt.event.ComponentListener;
29  
30  import javax.swing.JPanel;
31  
32  import net.sf.magicproject.clickable.targetable.card.CardFactory;
33  import net.sf.magicproject.clickable.targetable.card.MCard;
34  
35  /***
36   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37   * @since 0.82
38   */
39  public class DropCardListener extends JPanel implements ComponentListener {
40  
41  	/***
42  	 * Creates a new instance of DropCardListener
43  	 */
44  	public DropCardListener() {
45  		super(new FlowLayout(FlowLayout.LEFT, 10, 5));
46  	}
47  
48  	@Override
49  	public void paint(Graphics g) {
50  		// paint components first
51  		super.paint(g);
52  		int index = 0;
53  		for (int i = 0; i < getComponentCount(); i++) {
54  			if (getComponent(i) == movingComponent) {
55  				index = i;
56  				break;
57  			}
58  		}
59  		g.setColor(Color.GREEN);
60  		g.draw3DRect(index * (CardFactory.cardWidth + 10) + 10, 0,
61  				CardFactory.cardWidth, getSize().height, true);
62  
63  		if (separatorIndex != index && separatorIndex != index + 1) {
64  			g.setColor(Color.RED);
65  			// draw an insertion point
66  			g.draw3DRect(separatorIndex * (10 + CardFactory.cardWidth), 0, 5,
67  					getSize().height, true);
68  		}
69  	}
70  
71  	public void componentMoved(ComponentEvent e) {
72  		Component comp = e.getComponent();
73  		if (comp instanceof MCard) {
74  			movingComponent = comp;
75  			int x = comp.getLocation().x;
76  			if (x <= 10) {
77  				// out of bounds (min)
78  				separatorIndex = 0;
79  			} else if (x >= 10 + getComponentCount() * (CardFactory.cardWidth + 10)) {
80  				// out of bounds (max)
81  				separatorIndex = getComponentCount();
82  			} else {
83  				separatorIndex = (x - 10) / (CardFactory.cardWidth + 10);
84  			}
85  			repaint();
86  		}
87  	}
88  
89  	public void componentResized(ComponentEvent e) {
90  		// Ignore this event
91  	}
92  
93  	public void componentShown(ComponentEvent e) {
94  		// Ignore this event
95  	}
96  
97  	public void componentHidden(ComponentEvent e) {
98  		// Ignore this event
99  	}
100 
101 	/***
102 	 * The moving component
103 	 */
104 	private Component movingComponent;
105 
106 	/***
107 	 * Location of rectangle representing insertion index of the moving card.
108 	 */
109 	int separatorIndex;
110 
111 }