1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
78 separatorIndex = 0;
79 } else if (x >= 10 + getComponentCount() * (CardFactory.cardWidth + 10)) {
80
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
91 }
92
93 public void componentShown(ComponentEvent e) {
94
95 }
96
97 public void componentHidden(ComponentEvent e) {
98
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 }