Statistics
| Revision:

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

History | View | Annotate | Download (6.25 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
*
44
* $Id: JDnDListModel.java 4520 2006-03-22 11:18:29Z jaume $
45
* $Log$
46
* Revision 1.1  2006-03-22 11:18:29  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.4  2006/02/28 15:25:14  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.2.2.3  2006/01/31 16:25:24  jaume
53
* correcciones de bugs
54
*
55
* Revision 1.3  2006/01/26 16:07:14  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.2.2.1  2006/01/26 12:59:33  jaume
59
* 0.5
60
*
61
* Revision 1.2  2006/01/24 14:36:33  jaume
62
* This is the new version
63
*
64
* Revision 1.1.2.2  2006/01/17 12:55:40  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1.2.1  2006/01/10 13:11:38  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.1.2.1  2005/12/29 08:26:54  jaume
71
* some gui issues where fixed
72
*
73
* Revision 1.1.2.2  2005/12/22 16:46:00  jaume
74
* puede borrar m?ltiples intervalos de entradas dentro de la lista
75
*
76
* Revision 1.1.2.1  2005/12/19 18:12:35  jaume
77
* *** empty log message ***
78
*
79
*
80
*/
81
/**
82
 * 
83
 */
84
package org.gvsig.gui.beans.controls.dnd;
85

    
86
import java.util.ArrayList;
87
import java.util.Collection;
88
import java.util.Iterator;
89

    
90
import javax.swing.AbstractListModel;
91

    
92

    
93
/**
94
 * List model to use in junction the JDnDList. Contains some useful tools.
95
 *
96
 * @author jaume dominguez faus - jaume.dominguez@iver.es
97
 *
98
 */
99
public class JDnDListModel extends AbstractListModel {
100
    private ArrayList items = new ArrayList();
101
    /**
102
     * Inserts a collection of items before the specified index
103
     */
104
    public void insertItems( int index, Collection objects ) {
105
        // Handle the case where the items are being added to the end of the list
106
        if( index == -1 ) {
107
            // Add the items
108
            for( Iterator i = objects.iterator(); i.hasNext(); ) {
109
                String item = ( String )i.next();
110
                addElement(items.size(), item );
111
            }
112
        } else {
113
            // Insert the items
114
            for( Iterator i = objects.iterator(); i.hasNext(); ) {
115
                Object item = i.next();
116
                insertElement( index++, item );
117
            }
118
        }
119
        
120
        // Tell the list to update itself
121
        this.fireContentsChanged( this, 0, this.items.size() - 1 );
122
    }
123
    
124
    /**
125
     * Inserts a new element into the list at the position mentioned in the index param.
126
     * @param index
127
     * @param item
128
     */
129
    public boolean insertElement(int index, Object element) {
130
        if (element == null) {
131
            return false;
132
        }
133
        for (int i = 0; i < items.size(); i++) {
134
            
135
            if (items.get(i).equals(items)) {
136
                return false;
137
            }
138
        }
139
        
140
        this.items.add(index, element);
141
        return true;
142
    }
143

    
144
    /**
145
     * Adds a new element at the position indicated by pos of the list.
146
     * @param j 
147
     * @param pos 
148
     */
149
    public boolean addElement(int j, Object element) {
150
        if (element == null) {
151
            return false;
152
        }
153
        
154
        for (int i = 0; i < items.size(); i++) {
155
            if (items.get(i).equals(items)) {
156
                return false;
157
            }
158
        }
159
        this.items.add(j, element);
160
        fireContentsChanged(this, items.size() - 1, items.size() - 1);
161
        return true;
162
    }
163
    
164
    /**
165
     * Adds a new element at the position indicated by pos of the list.
166
     * @param j 
167
     * @param pos 
168
     */
169
    public boolean addElement(Object element) {
170
        return addElement(items.size(), element);
171
    }
172
    
173
    /**
174
     * Removes every elements contained in the collection from this list.
175
     */
176
    public void delElements(Collection c) {
177
        items.removeAll(c);
178
        this.fireContentsChanged(this, 0, items.size());
179
    }
180
    
181
    /**
182
     * Removes the items of the list mentioned by the index array passed as argument.
183
     * @param indices
184
     */
185
    public void delIndices(int[] indices){
186
        int removed = 0;
187
        for (int i = 0; i < indices.length; i++) {
188
            items.remove(indices[i]-removed);
189
            removed++;
190
        }
191
    }
192

    
193
    public void itemsMoved( int newIndex, int[] indicies ) {
194
        
195
        // Copy the objects to a temporary ArrayList
196
        ArrayList objects = new ArrayList();
197
        for( int i=0; i<indicies.length; i++ ) {
198
            objects.add( this.items.get( indicies[ i ] ) );
199
        }
200
        
201
        // Delete the objects from the list
202
        for( int i=indicies.length-1; i>=0; i-- ) {
203
            this.items.remove( indicies[ i ] );
204
        }
205
        
206
        // Insert the items at the new location
207
        insertItems( newIndex, objects );
208
    }
209
    
210
    /* (non-Javadoc)
211
     * @see javax.swing.ListModel#getSize()
212
     */
213
    public int getSize() {
214
        return items.size();
215
    }
216
    
217
    /* (non-Javadoc)
218
     * @see javax.swing.ListModel#getElementAt(int)
219
     */
220
    public Object getElementAt(int index) {
221
        return items.get(index);
222
    }
223
    
224
    /**
225
     * Removes any item currently contained by this list.
226
     *
227
     */
228
    public void clear() {
229
        items.clear();
230
        fireContentsChanged(this, 0, 0);
231
    }
232
    
233
    /**
234
     * Returns an ArrayList containing the elements of this list.
235
     */
236
    public ArrayList getElements() {
237
        return items;
238
    }
239

    
240
    
241
}