Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUI / src / org / gvsig / gui / beans / controls / dnd / JDnDList.java @ 4520

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