17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
17 |
ilm |
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
/*
|
|
|
15 |
* MovableContainerComponent created on 23 sept. 2004
|
|
|
16 |
*
|
|
|
17 |
*/
|
|
|
18 |
package org.openconcerto.ui;
|
|
|
19 |
|
|
|
20 |
import org.openconcerto.utils.JImage;
|
|
|
21 |
|
|
|
22 |
import java.awt.Component;
|
|
|
23 |
import java.awt.Graphics;
|
|
|
24 |
import java.awt.GridLayout;
|
|
|
25 |
import java.awt.Image;
|
|
|
26 |
import java.awt.Point;
|
|
|
27 |
import java.awt.event.MouseEvent;
|
|
|
28 |
import java.awt.event.MouseListener;
|
|
|
29 |
import java.awt.event.MouseMotionListener;
|
|
|
30 |
import java.awt.image.BufferedImage;
|
|
|
31 |
import java.util.Vector;
|
|
|
32 |
|
|
|
33 |
import javax.swing.JComponent;
|
|
|
34 |
import javax.swing.JLayeredPane;
|
|
|
35 |
import javax.swing.SwingUtilities;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* @author ILM Informatique 23 sept. 2004
|
|
|
39 |
*
|
|
|
40 |
*/
|
|
|
41 |
public class MovableContainerComponent extends JComponent implements MouseListener, MouseMotionListener {
|
|
|
42 |
|
|
|
43 |
private int size;
|
|
|
44 |
private int offsetX;
|
|
|
45 |
int currentIndex;
|
|
|
46 |
private Vector list = new Vector();
|
|
|
47 |
private JComponent draggedComponent = null;
|
|
|
48 |
private int draggedComponentIndex;
|
|
|
49 |
private Image componentImage;
|
|
|
50 |
private JComponent imgComp;
|
|
|
51 |
private Point pOffset;
|
|
|
52 |
private JLayeredPane layerPane;
|
|
|
53 |
public MovableContainerComponent(int size) {
|
|
|
54 |
this.size = size;
|
|
|
55 |
this.setLayout(new GridLayout(1, size));
|
|
|
56 |
addMouseListener(this);
|
|
|
57 |
addMouseMotionListener(this);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/* (non-Javadoc)
|
|
|
61 |
* @see java.awt.Container#add(java.awt.Component)
|
|
|
62 |
*/
|
|
|
63 |
public Component add(Component comp) {
|
|
|
64 |
list.add(comp);
|
|
|
65 |
comp.addMouseListener(this);
|
|
|
66 |
comp.addMouseMotionListener(this);
|
|
|
67 |
return super.add(comp);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/* (non-Javadoc)
|
|
|
71 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
|
|
72 |
*/
|
|
|
73 |
public void mouseClicked(MouseEvent e) {
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/* (non-Javadoc)
|
|
|
77 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
|
|
78 |
*/
|
|
|
79 |
public void mousePressed(MouseEvent e) {
|
|
|
80 |
if(e.getY()<16)
|
|
|
81 |
for (int i = 0; i < list.size(); i++) {
|
|
|
82 |
JComponent obj = (JComponent) list.elementAt(i);
|
|
|
83 |
if (obj == e.getSource()) {
|
|
|
84 |
System.out.println("Le " + i + " eme est dragge");
|
|
|
85 |
this.draggedComponent = (JComponent) obj;
|
|
|
86 |
this.draggedComponentIndex = i;
|
|
|
87 |
this.currentIndex = i;
|
|
|
88 |
// La frame
|
|
|
89 |
//JComponent frame = SwingUtilities.getRoot(obj);
|
|
|
90 |
//this.remove(obj);
|
|
|
91 |
//glassPane = ((JFrame) frame).getGlassPane();
|
|
|
92 |
componentImage = new BufferedImage(obj.getWidth(), obj.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
|
|
93 |
|
|
|
94 |
//obj.setBackground(Color.RED);
|
|
|
95 |
Graphics gc = componentImage.getGraphics();
|
|
|
96 |
//gc.setColor(Color.RED);
|
|
|
97 |
|
|
|
98 |
obj.print(gc);
|
|
|
99 |
|
|
|
100 |
//glassPane.getGraphics().drawImage( componentImage, e.getX()+pOffset.x,pOffset.y,null );
|
|
|
101 |
layerPane = obj.getRootPane().getLayeredPane();
|
|
|
102 |
|
|
|
103 |
this.offsetX = e.getX();
|
|
|
104 |
pOffset = SwingUtilities.convertPoint(obj, 0, 0, layerPane);
|
|
|
105 |
System.out.println(pOffset);
|
|
|
106 |
|
|
|
107 |
imgComp = new JImage(componentImage);
|
|
|
108 |
|
|
|
109 |
//imgComp = new JLabel("dddddd");
|
|
|
110 |
imgComp.setLocation(pOffset);
|
|
|
111 |
imgComp.setSize(imgComp.getPreferredSize());
|
|
|
112 |
layerPane.add(imgComp, JLayeredPane.DRAG_LAYER);
|
|
|
113 |
|
|
|
114 |
draggedComponent.setVisible(false);
|
|
|
115 |
//this.remove(draggedComponent);
|
|
|
116 |
//this.validate();
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/* (non-Javadoc)
|
|
|
123 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
|
|
124 |
*/
|
|
|
125 |
public void mouseReleased(MouseEvent e) {
|
|
|
126 |
//System.out.println("mouseReleased");
|
|
|
127 |
this.draggedComponent.setVisible(true);
|
|
|
128 |
this.draggedComponent = null;
|
|
|
129 |
|
|
|
130 |
this.layerPane.remove(this.imgComp);
|
|
|
131 |
this.layerPane.validate();
|
|
|
132 |
this.layerPane.repaint();
|
|
|
133 |
componentImage = null;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/* (non-Javadoc)
|
|
|
137 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
|
|
138 |
*/
|
|
|
139 |
public void mouseEntered(MouseEvent e) {
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/* (non-Javadoc)
|
|
|
145 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
|
|
146 |
*/
|
|
|
147 |
public void mouseExited(MouseEvent e) {
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/* (non-Javadoc)
|
|
|
153 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
|
|
154 |
*/
|
|
|
155 |
public void mouseDragged(MouseEvent e) {
|
|
|
156 |
//System.out.println("mouseDragged: " + e.getSource());
|
|
|
157 |
|
|
|
158 |
if (componentImage != null) {
|
|
|
159 |
;
|
|
|
160 |
pOffset = SwingUtilities.convertPoint((JComponent) e.getSource(), 0, 0, layerPane);
|
|
|
161 |
|
|
|
162 |
int currentX = e.getX() + pOffset.x - this.offsetX;
|
|
|
163 |
|
|
|
164 |
Point p = SwingUtilities.convertPoint(this, 0, 0, layerPane);
|
|
|
165 |
|
|
|
166 |
// Min a droite
|
|
|
167 |
if (currentX < p.x)
|
|
|
168 |
currentX = p.x;
|
|
|
169 |
|
|
|
170 |
// Max droite
|
|
|
171 |
p = SwingUtilities.convertPoint(this, this.getWidth() - 2, 0, layerPane);
|
|
|
172 |
p.x -= this.draggedComponent.getWidth();
|
|
|
173 |
if (currentX >= p.x)
|
|
|
174 |
currentX = p.x;
|
|
|
175 |
|
|
|
176 |
int select = (currentX) / (this.getWidth() / size);
|
|
|
177 |
//System.out.println(" select: " + select);
|
|
|
178 |
if (/*select !=this.draggedComponentIndex &&*/
|
|
|
179 |
select != currentIndex) {
|
|
|
180 |
currentIndex = select;
|
|
|
181 |
//System.out.println("Move en " + select);
|
|
|
182 |
this.remove(this.draggedComponent);
|
|
|
183 |
for (int i = 0; i < list.size(); i++) {
|
|
|
184 |
JComponent obj = (JComponent) list.elementAt(i);
|
|
|
185 |
this.remove(obj);
|
|
|
186 |
}
|
|
|
187 |
swap(this.draggedComponentIndex, select);
|
|
|
188 |
|
|
|
189 |
for (int i = 0; i < list.size(); i++) {
|
|
|
190 |
JComponent obj = (JComponent) list.elementAt(i);
|
|
|
191 |
|
|
|
192 |
super.add(obj);
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
this.draggedComponentIndex = select;
|
|
|
196 |
this.draggedComponent = (JComponent) list.elementAt(select);
|
|
|
197 |
this.validate();
|
|
|
198 |
}
|
|
|
199 |
this.imgComp.setLocation(currentX, pOffset.y);
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
/**
|
|
|
205 |
* Inverse les composants i et j
|
|
|
206 |
* @param i
|
|
|
207 |
* @param select
|
|
|
208 |
*/
|
|
|
209 |
private void swap(int i, int j) {
|
|
|
210 |
System.out.println("Swap: " + i + "," + j);
|
|
|
211 |
JComponent c1 = (JComponent) this.list.get(i);
|
|
|
212 |
JComponent c2 = (JComponent) this.list.get(j);
|
|
|
213 |
this.list.set(j, c1);
|
|
|
214 |
this.list.set(i, c2);
|
|
|
215 |
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/* (non-Javadoc)
|
|
|
219 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
|
|
220 |
*/
|
|
|
221 |
public void mouseMoved(MouseEvent e) {
|
|
|
222 |
//System.out.println("mouseMoved");
|
|
|
223 |
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
}
|