root / tags / v1_0_1_RELEASE / libraries / libUI / src / org / gvsig / gui / beans / controls / dnd / JDnDList.java @ 9531
History | View | Annotate | Download (9.38 KB)
1 | 4520 | jaume | 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 | 4556 | jaume | |
27 | 4520 | jaume | /* 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$
|
||
71 | * $Log$
|
||
72 | 4556 | jaume | * Revision 1.2 2006-03-23 10:37:11 jaume
|
73 | 4520 | jaume | * *** empty log message ***
|
74 | *
|
||
75 | 4556 | jaume | * Revision 1.1 2006/03/22 11:18:29 jaume
|
76 | * *** empty log message ***
|
||
77 | *
|
||
78 | 4520 | jaume | * Revision 1.4 2006/02/28 15:25:14 jaume
|
79 | * *** empty log message ***
|
||
80 | *
|
||
81 | * Revision 1.2.2.3 2006/01/31 16:25:24 jaume
|
||
82 | * correcciones de bugs
|
||
83 | *
|
||
84 | * Revision 1.3 2006/01/26 16:07:14 jaume
|
||
85 | * *** empty log message ***
|
||
86 | *
|
||
87 | * Revision 1.2.2.1 2006/01/26 12:59:33 jaume
|
||
88 | * 0.5
|
||
89 | *
|
||
90 | * Revision 1.2 2006/01/24 14:36:33 jaume
|
||
91 | * This is the new version
|
||
92 | *
|
||
93 | * Revision 1.1.2.1 2006/01/10 13:11:38 jaume
|
||
94 | * *** empty log message ***
|
||
95 | *
|
||
96 | * Revision 1.1.2.1 2005/12/29 08:26:54 jaume
|
||
97 | * some gui issues where fixed
|
||
98 | *
|
||
99 | * Revision 1.1.2.1 2005/12/19 18:12:35 jaume
|
||
100 | * *** empty log message ***
|
||
101 | *
|
||
102 | *
|
||
103 | */
|
||
104 | /**
|
||
105 | * <p>
|
||
106 | * A JList that allows drag'n'drop elements. It accepts changing position of
|
||
107 | * one or more elements within the list, adding other from other JDnDList's,
|
||
108 | * removing sets of selected items, and other features.
|
||
109 | * </p>
|
||
110 | * <p>
|
||
111 | * In order to use this features you have to use a JDnDListModel as the list's
|
||
112 | * model.
|
||
113 | * </p>
|
||
114 | * jaume dominguez faus - jaume.dominguez@iver.es
|
||
115 | */
|
||
116 | public class JDnDList extends JList implements DragSourceListener, DragGestureListener, DropTargetListener{ |
||
117 | private DragSource dragSource; |
||
118 | private DropTarget dropTarget; |
||
119 | private boolean dragging; |
||
120 | private int overIndex; |
||
121 | private int[] selectedIndices; |
||
122 | |||
123 | |||
124 | public JDnDList() {
|
||
125 | dropTarget = new DropTarget (this, this); |
||
126 | dragSource = new DragSource(); |
||
127 | dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this); |
||
128 | } |
||
129 | |||
130 | public JDnDList( JDnDListModel model ) {
|
||
131 | super( model );
|
||
132 | // Configure ourselves to be a drag source
|
||
133 | dragSource = new DragSource(); |
||
134 | dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this); |
||
135 | |||
136 | // Configure ourselves to be a drop target
|
||
137 | dropTarget = new DropTarget( this, this ); |
||
138 | } |
||
139 | |||
140 | public void dragGestureRecognized(DragGestureEvent dge) { |
||
141 | this.selectedIndices = this.getSelectedIndices(); |
||
142 | Object[] selectedObjects = this.getSelectedValues(); |
||
143 | if( selectedObjects.length > 0 ) { |
||
144 | StringBuffer sb = new StringBuffer(); |
||
145 | for( int i=0; i<selectedObjects.length; i++ ) { |
||
146 | sb.append( selectedObjects[ i ].toString() + "\n" );
|
||
147 | } |
||
148 | |||
149 | // Build a StringSelection object that the Drag Source
|
||
150 | // can use to transport a string to the Drop Target
|
||
151 | StringSelection text = new StringSelection( sb.toString() ); |
||
152 | |||
153 | // Start dragging the object
|
||
154 | this.dragging = true; |
||
155 | dragSource.startDrag( dge, DragSource.DefaultMoveDrop, text, this ); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | public void dragDropEnd(DragSourceDropEvent dsde) { |
||
160 | this.dragging = false; |
||
161 | } |
||
162 | |||
163 | public void dragExit(DropTargetEvent dte) { |
||
164 | this.overIndex = -1; |
||
165 | } |
||
166 | public void dragEnter(DropTargetDragEvent dtde) { |
||
167 | this.overIndex = this.locationToIndex( dtde.getLocation() ); |
||
168 | this.setSelectedIndex( this.overIndex ); |
||
169 | } |
||
170 | public void dragOver(DropTargetDragEvent dtde) { |
||
171 | // See who we are over...
|
||
172 | int overIndex = this.locationToIndex( dtde.getLocation() ); |
||
173 | if( overIndex != -1 && overIndex != this.overIndex ) { |
||
174 | // If the value has changed from what we were previously over
|
||
175 | // then change the selected object to the one we are over; this
|
||
176 | // is a visual representation that this is where the drop will occur
|
||
177 | this.overIndex = overIndex;
|
||
178 | this.setSelectedIndex( this.overIndex ); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | public void drop(DropTargetDropEvent dtde) { |
||
183 | try {
|
||
184 | Transferable transferable = dtde.getTransferable();
|
||
185 | if( transferable.isDataFlavorSupported( DataFlavor.stringFlavor ) ) { |
||
186 | dtde.acceptDrop( DnDConstants.ACTION_MOVE );
|
||
187 | |||
188 | // Find out where the item was dropped
|
||
189 | int newIndex = this.locationToIndex( dtde.getLocation() ); |
||
190 | |||
191 | // Get the items out of the transferable object and build an
|
||
192 | // array out of them...
|
||
193 | String s = ( String )transferable.getTransferData( DataFlavor.stringFlavor ); |
||
194 | StringTokenizer st = new StringTokenizer( s ); |
||
195 | ArrayList items = new ArrayList(); |
||
196 | while( st.hasMoreTokens() ) {
|
||
197 | items.add( st.nextToken() ); |
||
198 | } |
||
199 | JDnDListModel model = ( JDnDListModel )this.getModel();
|
||
200 | |||
201 | // If we are dragging from our this to our list them move the items,
|
||
202 | // otherwise just add them...
|
||
203 | if( this.dragging ) { |
||
204 | //model.itemsMoved( newIndex, items );
|
||
205 | model.itemsMoved( newIndex, this.selectedIndices );
|
||
206 | } else {
|
||
207 | model.insertItems( newIndex, items ); |
||
208 | } |
||
209 | |||
210 | // Update the selected indicies
|
||
211 | int[] newIndicies = new int[ items.size() ]; |
||
212 | for( int i=0; i<items.size(); i++ ) { |
||
213 | newIndicies[ i ] = newIndex + i; |
||
214 | } |
||
215 | this.setSelectedIndices( newIndicies );
|
||
216 | |||
217 | // Reset the over index
|
||
218 | this.overIndex = -1; |
||
219 | |||
220 | dtde.getDropTargetContext().dropComplete( true );
|
||
221 | } else {
|
||
222 | dtde.rejectDrop(); |
||
223 | } |
||
224 | } catch( IOException exception ) { |
||
225 | exception.printStackTrace(); |
||
226 | System.err.println( "Exception" + exception.getMessage()); |
||
227 | dtde.rejectDrop(); |
||
228 | } catch( UnsupportedFlavorException ufException ) { |
||
229 | ufException.printStackTrace(); |
||
230 | System.err.println( "Exception" + ufException.getMessage()); |
||
231 | dtde.rejectDrop(); |
||
232 | } |
||
233 | } |
||
234 | |||
235 | /* (non-Javadoc)
|
||
236 | * @see java.awt.dnd.DragSourceListener#dragEnter(java.awt.dnd.DragSourceDragEvent)
|
||
237 | */
|
||
238 | public void dragEnter(DragSourceDragEvent dsde) { |
||
239 | |||
240 | } |
||
241 | |||
242 | /* (non-Javadoc)
|
||
243 | * @see java.awt.dnd.DragSourceListener#dragOver(java.awt.dnd.DragSourceDragEvent)
|
||
244 | */
|
||
245 | public void dragOver(DragSourceDragEvent dsde) { |
||
246 | |||
247 | } |
||
248 | |||
249 | /* (non-Javadoc)
|
||
250 | * @see java.awt.dnd.DragSourceListener#dropActionChanged(java.awt.dnd.DragSourceDragEvent)
|
||
251 | */
|
||
252 | public void dropActionChanged(DragSourceDragEvent dsde) { |
||
253 | |||
254 | } |
||
255 | |||
256 | /* (non-Javadoc)
|
||
257 | * @see java.awt.dnd.DragSourceListener#dragExit(java.awt.dnd.DragSourceEvent)
|
||
258 | */
|
||
259 | public void dragExit(DragSourceEvent dse) { |
||
260 | |||
261 | } |
||
262 | |||
263 | /* (non-Javadoc)
|
||
264 | * @see java.awt.dnd.DropTargetListener#dropActionChanged(java.awt.dnd.DropTargetDragEvent)
|
||
265 | */
|
||
266 | public void dropActionChanged(DropTargetDragEvent dtde) { |
||
267 | |||
268 | } |
||
269 | 4556 | jaume | |
270 | /**
|
||
271 | * Constructs a <b>JDnDListModel</b> from an array of objects and then applies setModel to it.
|
||
272 | * @param listData an array of Objects containing the items to display in the list
|
||
273 | * @see setModel
|
||
274 | * @author jaume
|
||
275 | */
|
||
276 | public void setListData(Object[] obj) { |
||
277 | JDnDListModel model = new JDnDListModel();
|
||
278 | for (int i = 0; i < obj.length; i++) { |
||
279 | model.addElement(obj[i]); |
||
280 | } |
||
281 | this.setModel(model);
|
||
282 | } |
||
283 | 4520 | jaume | } |