root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / controls / dnd / JDnDList.java @ 13198
History | View | Annotate | Download (9.48 KB)
1 |
package org.gvsig.gui.beans.controls.dnd; |
---|---|
2 |
|
3 |
import java.awt.datatransfer.DataFlavor; |
4 |
import java.awt.datatransfer.StringSelection; |
5 |
import java.awt.datatransfer.Transferable; |
6 |
import java.awt.datatransfer.UnsupportedFlavorException; |
7 |
import java.awt.dnd.DnDConstants; |
8 |
import java.awt.dnd.DragGestureEvent; |
9 |
import java.awt.dnd.DragGestureListener; |
10 |
import java.awt.dnd.DragSource; |
11 |
import java.awt.dnd.DragSourceDragEvent; |
12 |
import java.awt.dnd.DragSourceDropEvent; |
13 |
import java.awt.dnd.DragSourceEvent; |
14 |
import java.awt.dnd.DragSourceListener; |
15 |
import java.awt.dnd.DropTarget; |
16 |
import java.awt.dnd.DropTargetDragEvent; |
17 |
import java.awt.dnd.DropTargetDropEvent; |
18 |
import java.awt.dnd.DropTargetEvent; |
19 |
import java.awt.dnd.DropTargetListener; |
20 |
import java.io.IOException; |
21 |
import java.util.ArrayList; |
22 |
import java.util.StringTokenizer; |
23 |
|
24 |
import javax.swing.JList; |
25 |
|
26 |
|
27 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
28 |
*
|
29 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
30 |
*
|
31 |
* This program is free software; you can redistribute it and/or
|
32 |
* modify it under the terms of the GNU General Public License
|
33 |
* as published by the Free Software Foundation; either version 2
|
34 |
* of the License, or (at your option) any later version.
|
35 |
*
|
36 |
* This program is distributed in the hope that it will be useful,
|
37 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
38 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
39 |
* GNU General Public License for more details.
|
40 |
*
|
41 |
* You should have received a copy of the GNU General Public License
|
42 |
* along with this program; if not, write to the Free Software
|
43 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
44 |
*
|
45 |
* For more information, contact:
|
46 |
*
|
47 |
* Generalitat Valenciana
|
48 |
* Conselleria d'Infraestructures i Transport
|
49 |
* Av. Blasco Ib??ez, 50
|
50 |
* 46010 VALENCIA
|
51 |
* SPAIN
|
52 |
*
|
53 |
* +34 963862235
|
54 |
* gvsig@gva.es
|
55 |
* www.gvsig.gva.es
|
56 |
*
|
57 |
* or
|
58 |
*
|
59 |
* IVER T.I. S.A
|
60 |
* Salamanca 50
|
61 |
* 46005 Valencia
|
62 |
* Spain
|
63 |
*
|
64 |
* +34 963163400
|
65 |
* dac@iver.es
|
66 |
*/
|
67 |
|
68 |
/* CVS MESSAGES:
|
69 |
*
|
70 |
* $Id: JDnDList.java 13198 2007-08-21 09:57:17Z bsanchez $
|
71 |
* $Log$
|
72 |
* Revision 1.2 2007-08-21 09:57:17 bsanchez
|
73 |
* - Variable no usada
|
74 |
*
|
75 |
* Revision 1.1 2007/08/20 08:34:46 evercher
|
76 |
* He fusionado LibUI con LibUIComponents
|
77 |
*
|
78 |
* Revision 1.2 2006/03/23 10:37:11 jaume
|
79 |
* *** empty log message ***
|
80 |
*
|
81 |
* Revision 1.1 2006/03/22 11:18:29 jaume
|
82 |
* *** empty log message ***
|
83 |
*
|
84 |
* Revision 1.4 2006/02/28 15:25:14 jaume
|
85 |
* *** empty log message ***
|
86 |
*
|
87 |
* Revision 1.2.2.3 2006/01/31 16:25:24 jaume
|
88 |
* correcciones de bugs
|
89 |
*
|
90 |
* Revision 1.3 2006/01/26 16:07:14 jaume
|
91 |
* *** empty log message ***
|
92 |
*
|
93 |
* Revision 1.2.2.1 2006/01/26 12:59:33 jaume
|
94 |
* 0.5
|
95 |
*
|
96 |
* Revision 1.2 2006/01/24 14:36:33 jaume
|
97 |
* This is the new version
|
98 |
*
|
99 |
* Revision 1.1.2.1 2006/01/10 13:11:38 jaume
|
100 |
* *** empty log message ***
|
101 |
*
|
102 |
* Revision 1.1.2.1 2005/12/29 08:26:54 jaume
|
103 |
* some gui issues where fixed
|
104 |
*
|
105 |
* Revision 1.1.2.1 2005/12/19 18:12:35 jaume
|
106 |
* *** empty log message ***
|
107 |
*
|
108 |
*
|
109 |
*/
|
110 |
/**
|
111 |
* <p>
|
112 |
* A JList that allows drag'n'drop elements. It accepts changing position of
|
113 |
* one or more elements within the list, adding other from other JDnDList's,
|
114 |
* removing sets of selected items, and other features.
|
115 |
* </p>
|
116 |
* <p>
|
117 |
* In order to use this features you have to use a JDnDListModel as the list's
|
118 |
* model.
|
119 |
* </p>
|
120 |
* jaume dominguez faus - jaume.dominguez@iver.es
|
121 |
*/
|
122 |
public class JDnDList extends JList implements DragSourceListener, DragGestureListener, DropTargetListener{ |
123 |
private DragSource dragSource; |
124 |
private boolean dragging; |
125 |
private int overIndex; |
126 |
private int[] selectedIndices; |
127 |
|
128 |
|
129 |
public JDnDList() {
|
130 |
new DropTarget (this, this); |
131 |
dragSource = new DragSource(); |
132 |
dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this); |
133 |
} |
134 |
|
135 |
public JDnDList( JDnDListModel model ) {
|
136 |
super( model );
|
137 |
// Configure ourselves to be a drag source
|
138 |
dragSource = new DragSource(); |
139 |
dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this); |
140 |
|
141 |
// Configure ourselves to be a drop target
|
142 |
new DropTarget( this, this ); |
143 |
} |
144 |
|
145 |
public void dragGestureRecognized(DragGestureEvent dge) { |
146 |
this.selectedIndices = this.getSelectedIndices(); |
147 |
Object[] selectedObjects = this.getSelectedValues(); |
148 |
if( selectedObjects.length > 0 ) { |
149 |
StringBuffer sb = new StringBuffer(); |
150 |
for( int i=0; i<selectedObjects.length; i++ ) { |
151 |
sb.append( selectedObjects[ i ].toString() + "\n" );
|
152 |
} |
153 |
|
154 |
// Build a StringSelection object that the Drag Source
|
155 |
// can use to transport a string to the Drop Target
|
156 |
StringSelection text = new StringSelection( sb.toString() ); |
157 |
|
158 |
// Start dragging the object
|
159 |
this.dragging = true; |
160 |
dragSource.startDrag( dge, DragSource.DefaultMoveDrop, text, this ); |
161 |
} |
162 |
} |
163 |
|
164 |
public void dragDropEnd(DragSourceDropEvent dsde) { |
165 |
this.dragging = false; |
166 |
} |
167 |
|
168 |
public void dragExit(DropTargetEvent dte) { |
169 |
this.overIndex = -1; |
170 |
} |
171 |
public void dragEnter(DropTargetDragEvent dtde) { |
172 |
this.overIndex = this.locationToIndex( dtde.getLocation() ); |
173 |
this.setSelectedIndex( this.overIndex ); |
174 |
} |
175 |
public void dragOver(DropTargetDragEvent dtde) { |
176 |
// See who we are over...
|
177 |
int overIndex = this.locationToIndex( dtde.getLocation() ); |
178 |
if( overIndex != -1 && overIndex != this.overIndex ) { |
179 |
// If the value has changed from what we were previously over
|
180 |
// then change the selected object to the one we are over; this
|
181 |
// is a visual representation that this is where the drop will occur
|
182 |
this.overIndex = overIndex;
|
183 |
this.setSelectedIndex( this.overIndex ); |
184 |
} |
185 |
} |
186 |
|
187 |
public void drop(DropTargetDropEvent dtde) { |
188 |
try {
|
189 |
Transferable transferable = dtde.getTransferable();
|
190 |
if( transferable.isDataFlavorSupported( DataFlavor.stringFlavor ) ) { |
191 |
dtde.acceptDrop( DnDConstants.ACTION_MOVE );
|
192 |
|
193 |
// Find out where the item was dropped
|
194 |
int newIndex = this.locationToIndex( dtde.getLocation() ); |
195 |
|
196 |
// Get the items out of the transferable object and build an
|
197 |
// array out of them...
|
198 |
String s = ( String )transferable.getTransferData( DataFlavor.stringFlavor ); |
199 |
StringTokenizer st = new StringTokenizer( s ); |
200 |
ArrayList items = new ArrayList(); |
201 |
while( st.hasMoreTokens() ) {
|
202 |
items.add( st.nextToken() ); |
203 |
} |
204 |
JDnDListModel model = ( JDnDListModel )this.getModel();
|
205 |
|
206 |
// If we are dragging from our this to our list them move the items,
|
207 |
// otherwise just add them...
|
208 |
if( this.dragging ) { |
209 |
//model.itemsMoved( newIndex, items );
|
210 |
model.itemsMoved( newIndex, this.selectedIndices );
|
211 |
} else {
|
212 |
model.insertItems( newIndex, items ); |
213 |
} |
214 |
|
215 |
// Update the selected indicies
|
216 |
int[] newIndicies = new int[ items.size() ]; |
217 |
for( int i=0; i<items.size(); i++ ) { |
218 |
newIndicies[ i ] = newIndex + i; |
219 |
} |
220 |
this.setSelectedIndices( newIndicies );
|
221 |
|
222 |
// Reset the over index
|
223 |
this.overIndex = -1; |
224 |
|
225 |
dtde.getDropTargetContext().dropComplete( true );
|
226 |
} else {
|
227 |
dtde.rejectDrop(); |
228 |
} |
229 |
} catch( IOException exception ) { |
230 |
exception.printStackTrace(); |
231 |
System.err.println( "Exception" + exception.getMessage()); |
232 |
dtde.rejectDrop(); |
233 |
} catch( UnsupportedFlavorException ufException ) { |
234 |
ufException.printStackTrace(); |
235 |
System.err.println( "Exception" + ufException.getMessage()); |
236 |
dtde.rejectDrop(); |
237 |
} |
238 |
} |
239 |
|
240 |
/* (non-Javadoc)
|
241 |
* @see java.awt.dnd.DragSourceListener#dragEnter(java.awt.dnd.DragSourceDragEvent)
|
242 |
*/
|
243 |
public void dragEnter(DragSourceDragEvent dsde) { |
244 |
|
245 |
} |
246 |
|
247 |
/* (non-Javadoc)
|
248 |
* @see java.awt.dnd.DragSourceListener#dragOver(java.awt.dnd.DragSourceDragEvent)
|
249 |
*/
|
250 |
public void dragOver(DragSourceDragEvent dsde) { |
251 |
|
252 |
} |
253 |
|
254 |
/* (non-Javadoc)
|
255 |
* @see java.awt.dnd.DragSourceListener#dropActionChanged(java.awt.dnd.DragSourceDragEvent)
|
256 |
*/
|
257 |
public void dropActionChanged(DragSourceDragEvent dsde) { |
258 |
|
259 |
} |
260 |
|
261 |
/* (non-Javadoc)
|
262 |
* @see java.awt.dnd.DragSourceListener#dragExit(java.awt.dnd.DragSourceEvent)
|
263 |
*/
|
264 |
public void dragExit(DragSourceEvent dse) { |
265 |
|
266 |
} |
267 |
|
268 |
/* (non-Javadoc)
|
269 |
* @see java.awt.dnd.DropTargetListener#dropActionChanged(java.awt.dnd.DropTargetDragEvent)
|
270 |
*/
|
271 |
public void dropActionChanged(DropTargetDragEvent dtde) { |
272 |
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* Constructs a <b>JDnDListModel</b> from an array of objects and then applies setModel to it.
|
277 |
* @param listData an array of Objects containing the items to display in the list
|
278 |
* @see setModel
|
279 |
* @author jaume
|
280 |
*/
|
281 |
public void setListData(Object[] obj) { |
282 |
JDnDListModel model = new JDnDListModel();
|
283 |
for (int i = 0; i < obj.length; i++) { |
284 |
model.addElement(obj[i]); |
285 |
} |
286 |
this.setModel(model);
|
287 |
} |
288 |
} |