Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / controls / dnd / JDnDList.java @ 40561

History | View | Annotate | Download (10.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gui.beans.controls.dnd;
25

    
26
import java.awt.datatransfer.DataFlavor;
27
import java.awt.datatransfer.StringSelection;
28
import java.awt.datatransfer.Transferable;
29
import java.awt.datatransfer.UnsupportedFlavorException;
30
import java.awt.dnd.DnDConstants;
31
import java.awt.dnd.DragGestureEvent;
32
import java.awt.dnd.DragGestureListener;
33
import java.awt.dnd.DragSource;
34
import java.awt.dnd.DragSourceDragEvent;
35
import java.awt.dnd.DragSourceDropEvent;
36
import java.awt.dnd.DragSourceEvent;
37
import java.awt.dnd.DragSourceListener;
38
import java.awt.dnd.DropTarget;
39
import java.awt.dnd.DropTargetDragEvent;
40
import java.awt.dnd.DropTargetDropEvent;
41
import java.awt.dnd.DropTargetEvent;
42
import java.awt.dnd.DropTargetListener;
43
import java.io.IOException;
44
import java.util.ArrayList;
45
import java.util.StringTokenizer;
46

    
47
import javax.swing.JList;
48

    
49

    
50
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
51
 *
52
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
53
 *
54
 * This program is free software; you can redistribute it and/or
55
 * modify it under the terms of the GNU General Public License
56
 * as published by the Free Software Foundation; either version 2
57
 * of the License, or (at your option) any later version.
58
 *
59
 * This program is distributed in the hope that it will be useful,
60
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
61
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
62
 * GNU General Public License for more details.
63
 *
64
 * You should have received a copy of the GNU General Public License
65
 * along with this program; if not, write to the Free Software
66
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
67
 *
68
 * For more information, contact:
69
 *
70
 *  Generalitat Valenciana
71
 *   Conselleria d'Infraestructures i Transport
72
 *   Av. Blasco Ib??ez, 50
73
 *   46010 VALENCIA
74
 *   SPAIN
75
 *
76
 *      +34 963862235
77
 *   gvsig@gva.es
78
 *      www.gvsig.gva.es
79
 *
80
 *    or
81
 *
82
 *   IVER T.I. S.A
83
 *   Salamanca 50
84
 *   46005 Valencia
85
 *   Spain
86
 *
87
 *   +34 963163400
88
 *   dac@iver.es
89
 */
90

    
91
/* CVS MESSAGES:
92
 *
93
 * $Id: JDnDList.java 13655 2007-09-12 16:28:55Z bsanchez $
94
 * $Log$
95
 * Revision 1.3  2007-09-12 16:28:23  bsanchez
96
 * *** empty log message ***
97
 *
98
 * Revision 1.2  2007/08/21 09:57:17  bsanchez
99
 * - Variable no usada
100
 *
101
 * Revision 1.1  2007/08/20 08:34:46  evercher
102
 * He fusionado LibUI con LibUIComponents
103
 *
104
 * Revision 1.2  2006/03/23 10:37:11  jaume
105
 * *** empty log message ***
106
 *
107
 * Revision 1.1  2006/03/22 11:18:29  jaume
108
 * *** empty log message ***
109
 *
110
 * Revision 1.4  2006/02/28 15:25:14  jaume
111
 * *** empty log message ***
112
 *
113
 * Revision 1.2.2.3  2006/01/31 16:25:24  jaume
114
 * correcciones de bugs
115
 *
116
 * Revision 1.3  2006/01/26 16:07:14  jaume
117
 * *** empty log message ***
118
 *
119
 * Revision 1.2.2.1  2006/01/26 12:59:33  jaume
120
 * 0.5
121
 *
122
 * Revision 1.2  2006/01/24 14:36:33  jaume
123
 * This is the new version
124
 *
125
 * Revision 1.1.2.1  2006/01/10 13:11:38  jaume
126
 * *** empty log message ***
127
 *
128
 * Revision 1.1.2.1  2005/12/29 08:26:54  jaume
129
 * some gui issues where fixed
130
 *
131
 * Revision 1.1.2.1  2005/12/19 18:12:35  jaume
132
 * *** empty log message ***
133
 *
134
 *
135
 */
136
/**
137
 * <p>
138
 * A JList that allows drag'n'drop elements. It accepts changing position of
139
 * one or more elements within the list, adding other from other JDnDList's,
140
 * removing sets of selected items, and other features.
141
 * </p>
142
 * <p>
143
 * In order to use this features you have to use a JDnDListModel as the list's
144
 * model.
145
 * </p>
146
 * jaume dominguez faus - jaume.dominguez@iver.es
147
 */
148
public class JDnDList extends JList implements DragSourceListener, DragGestureListener, DropTargetListener{
149
  private static final long serialVersionUID = -9071985815215584362L;
150
                private DragSource dragSource;
151
    private boolean    dragging;
152
    private int overIndex;
153
    private int[] selectedIndices;
154
    
155
    
156
    public JDnDList() {
157
        new DropTarget (this, this);
158
        dragSource = new DragSource();
159
        dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this);
160
      }
161
    
162
    public JDnDList( JDnDListModel model ) {
163
        super( model );
164
        // Configure ourselves to be a drag source
165
        dragSource = new DragSource();
166
        dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this);
167
        
168
        // Configure ourselves to be a drop target
169
        new DropTarget( this, this );
170
    }
171
    
172
    public void dragGestureRecognized(DragGestureEvent dge)  {
173
        this.selectedIndices = this.getSelectedIndices();
174
        Object[] selectedObjects = this.getSelectedValues();
175
        if( selectedObjects.length > 0 ) {
176
            StringBuffer sb = new StringBuffer();
177
            for( int i=0; i<selectedObjects.length; i++ ) {
178
                sb.append( selectedObjects[ i ].toString() + "\n" );
179
            }
180
            
181
            // Build a StringSelection object that the Drag Source
182
            // can use to transport a string to the Drop Target
183
            StringSelection text = new StringSelection( sb.toString() ); 
184
            
185
            // Start dragging the object 
186
            this.dragging = true;
187
            dragSource.startDrag( dge, DragSource.DefaultMoveDrop, text, this );
188
        }
189
    }
190
    
191
    public void dragDropEnd(DragSourceDropEvent dsde) {
192
        this.dragging = false;
193
    }
194
    
195
    public void dragExit(DropTargetEvent dte) {
196
        this.overIndex = -1;
197
    }
198
    public void dragEnter(DropTargetDragEvent dtde) {
199
        this.overIndex = this.locationToIndex( dtde.getLocation() );
200
        this.setSelectedIndex( this.overIndex );
201
    }
202
    public void dragOver(DropTargetDragEvent dtde) {
203
        // See who we are over...
204
        int overIndex = this.locationToIndex( dtde.getLocation() );
205
        if( overIndex != -1 && overIndex != this.overIndex ) {
206
            // If the value has changed from what we were previously over
207
            // then change the selected object to the one we are over; this 
208
            // is a visual representation that this is where the drop will occur
209
            this.overIndex = overIndex;
210
            this.setSelectedIndex( this.overIndex );
211
        }
212
    }
213
    
214
    public void drop(DropTargetDropEvent dtde) {
215
        try {
216
            Transferable transferable = dtde.getTransferable();
217
            if( transferable.isDataFlavorSupported( DataFlavor.stringFlavor ) ) {
218
                dtde.acceptDrop( DnDConstants.ACTION_MOVE );
219
                
220
                // Find out where the item was dropped
221
                int newIndex = this.locationToIndex( dtde.getLocation() );
222
                
223
                // Get the items out of the transferable object and build an
224
                // array out of them...
225
                String s = ( String )transferable.getTransferData( DataFlavor.stringFlavor );
226
                StringTokenizer st = new StringTokenizer( s );
227
                ArrayList items = new ArrayList();
228
                while( st.hasMoreTokens() ) {
229
                    items.add( st.nextToken() );
230
                }
231
                JDnDListModel model = ( JDnDListModel )this.getModel();
232
                
233
                // If we are dragging from our this to our list them move the items,
234
                // otherwise just add them...
235
                if( this.dragging ) {
236
                    //model.itemsMoved( newIndex, items );
237
                    model.itemsMoved( newIndex, this.selectedIndices );
238
                } else {
239
                    model.insertItems( newIndex, items );
240
                }
241
                
242
                // Update the selected indicies
243
                int[] newIndicies = new int[ items.size() ];
244
                for( int i=0; i<items.size(); i++ ) {
245
                    newIndicies[ i ] = newIndex + i;
246
                }
247
                this.setSelectedIndices( newIndicies );
248
                
249
                // Reset the over index
250
                this.overIndex = -1;
251
                
252
                dtde.getDropTargetContext().dropComplete( true );
253
            } else {
254
                dtde.rejectDrop();
255
            }
256
        } catch( IOException exception ) {
257
            exception.printStackTrace();
258
            System.err.println( "Exception" + exception.getMessage());
259
            dtde.rejectDrop();
260
        } catch( UnsupportedFlavorException ufException ) {
261
            ufException.printStackTrace();
262
            System.err.println( "Exception" + ufException.getMessage());
263
            dtde.rejectDrop();
264
        }
265
    }
266

    
267
    /* (non-Javadoc)
268
     * @see java.awt.dnd.DragSourceListener#dragEnter(java.awt.dnd.DragSourceDragEvent)
269
     */
270
    public void dragEnter(DragSourceDragEvent dsde) {
271
        
272
    }
273

    
274
    /* (non-Javadoc)
275
     * @see java.awt.dnd.DragSourceListener#dragOver(java.awt.dnd.DragSourceDragEvent)
276
     */
277
    public void dragOver(DragSourceDragEvent dsde) {
278
        
279
    }
280

    
281
    /* (non-Javadoc)
282
     * @see java.awt.dnd.DragSourceListener#dropActionChanged(java.awt.dnd.DragSourceDragEvent)
283
     */
284
    public void dropActionChanged(DragSourceDragEvent dsde) {
285
        
286
    }
287

    
288
    /* (non-Javadoc)
289
     * @see java.awt.dnd.DragSourceListener#dragExit(java.awt.dnd.DragSourceEvent)
290
     */
291
    public void dragExit(DragSourceEvent dse) {
292
        
293
    }
294

    
295
    /* (non-Javadoc)
296
     * @see java.awt.dnd.DropTargetListener#dropActionChanged(java.awt.dnd.DropTargetDragEvent)
297
     */
298
    public void dropActionChanged(DropTargetDragEvent dtde) {
299
        
300
    }
301
    
302
    /**
303
     * Constructs a <b>JDnDListModel</b> from an array of objects and then applies setModel to it. 
304
     * @param listData an array of Objects containing the items to display in the list
305
     * @see        setModel
306
     * @author jaume
307
     */
308
    public void setListData(Object[] obj) {
309
            JDnDListModel model = new JDnDListModel();
310
                for (int i = 0; i < obj.length; i++) {
311
                        model.addElement(obj[i]);
312
                }
313
                this.setModel(model);
314
    }
315
}