Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / controls / dnd / JDnDListModel.java @ 13136

History | View | Annotate | Download (6.34 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 13136 2007-08-20 08:38:34Z evercher $
45
* $Log$
46
* Revision 1.1  2007-08-20 08:34:46  evercher
47
* He fusionado LibUI con LibUIComponents
48
*
49
* Revision 1.1  2006/03/22 11:18:29  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.4  2006/02/28 15:25:14  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.2.2.3  2006/01/31 16:25:24  jaume
56
* correcciones de bugs
57
*
58
* Revision 1.3  2006/01/26 16:07:14  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.2.2.1  2006/01/26 12:59:33  jaume
62
* 0.5
63
*
64
* Revision 1.2  2006/01/24 14:36:33  jaume
65
* This is the new version
66
*
67
* Revision 1.1.2.2  2006/01/17 12:55:40  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.1.2.1  2006/01/10 13:11:38  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.1.2.1  2005/12/29 08:26:54  jaume
74
* some gui issues where fixed
75
*
76
* Revision 1.1.2.2  2005/12/22 16:46:00  jaume
77
* puede borrar m?ltiples intervalos de entradas dentro de la lista
78
*
79
* Revision 1.1.2.1  2005/12/19 18:12:35  jaume
80
* *** empty log message ***
81
*
82
*
83
*/
84
/**
85
 * 
86
 */
87
package org.gvsig.gui.beans.controls.dnd;
88

    
89
import java.util.ArrayList;
90
import java.util.Collection;
91
import java.util.Iterator;
92

    
93
import javax.swing.AbstractListModel;
94

    
95

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

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

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

    
243
    
244
}