Statistics
| Revision:

root / branches / gvSIG_WMSv2 / extensions / extWMS / src / com / iver / cit / gvsig / gui / beans / JDnDListModel.java @ 3511

History | View | Annotate | Download (5.29 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 3511 2005-12-22 16:46:00Z jaume $
45
* $Log$
46
* Revision 1.1.2.2  2005-12-22 16:46:00  jaume
47
* puede borrar m?ltiples intervalos de entradas dentro de la lista
48
*
49
* Revision 1.1.2.1  2005/12/19 18:12:35  jaume
50
* *** empty log message ***
51
*
52
*
53
*/
54
/**
55
 * 
56
 */
57
package com.iver.cit.gvsig.gui.beans;
58

    
59
import java.util.ArrayList;
60
import java.util.Collection;
61
import java.util.Iterator;
62

    
63
import javax.swing.AbstractListModel;
64

    
65

    
66
/**
67
 * 
68
 * @author jaume
69
 *
70
 */
71
public class JDnDListModel extends AbstractListModel {
72
    private ArrayList items = new ArrayList();
73
    /**
74
     * Inserts a collection of items before the specified index
75
     */
76
    public void insertItems( int index, Collection objects )
77
    {
78
        // Handle the case where the items are being added to the end of the list
79
        if( index == -1 )
80
        {
81
            // Add the items
82
            for( Iterator i = objects.iterator(); i.hasNext(); )
83
            {
84
                String item = ( String )i.next();
85
                addElement( item );
86
            }
87
        }
88
        else
89
        {
90
            // Insert the items
91
            for( Iterator i = objects.iterator(); i.hasNext(); )
92
            {
93
                Object item = i.next();
94
                insertElement( index++, item );
95
            }
96
        }
97
        
98
        // Tell the list to update itself
99
        this.fireContentsChanged( this, 0, this.items.size() - 1 );
100
    }
101
    
102
    /**
103
     * Inserts a new element into the list at the position mentioned in the index param.
104
     * @param index
105
     * @param item
106
     */
107
    public boolean insertElement(int index, Object element) {
108
        if (element == null) {
109
            return false;
110
        }
111
        for (int i = 0; i < items.size(); i++) {
112
            
113
            if (items.get(i).equals(items)) {
114
                return false;
115
            }
116
        }
117
        
118
        this.items.add(index, element);
119
        return true;
120
    }
121

    
122
    /**
123
     * Adds a new element at the end of the list.
124
     * @param item
125
     */
126
    public boolean addElement(Object element) {
127
        if (element == null) {
128
            return false;
129
        }
130
        
131
        for (int i = 0; i < items.size(); i++) {
132
            if (items.get(i).equals(items)) {
133
                return false;
134
            }
135
        }
136
        this.items.add(element);
137
        fireContentsChanged(this, items.size() - 1, items.size() - 1);
138
        return true;
139
    }
140
    
141
    /**
142
     * Removes every elements contained in the collection from this list.
143
     */
144
    public void delElements(Collection c) {
145
        items.removeAll(c);
146
        this.fireContentsChanged(this, 0, items.size());
147
    }
148
    
149
    /**
150
     * Removes the items of the list mentioned by the index array passed as argument.
151
     * @param indices
152
     */
153
    public void delIndices(int[] indices){
154
        int removed = 0;
155
        for (int i = 0; i < indices.length; i++) {
156
            items.remove(indices[i]-removed);
157
            removed++;
158
        }
159
    }
160

    
161
    public void itemsMoved( int newIndex, int[] indicies )
162
    {
163
        
164
        // Copy the objects to a temporary ArrayList
165
        ArrayList objects = new ArrayList();
166
        for( int i=0; i<indicies.length; i++ )
167
        {
168
            objects.add( this.items.get( indicies[ i ] ) );
169
        }
170
        
171
        // Delete the objects from the list
172
        for( int i=indicies.length-1; i>=0; i-- )
173
        {
174
            this.items.remove( indicies[ i ] );
175
        }
176
        
177
        // Insert the items at the new location
178
        insertItems( newIndex, objects );
179
    }
180
    
181
    /* (non-Javadoc)
182
     * @see javax.swing.ListModel#getSize()
183
     */
184
    public int getSize() {
185
        return items.size();
186
    }
187
    
188
    /* (non-Javadoc)
189
     * @see javax.swing.ListModel#getElementAt(int)
190
     */
191
    public Object getElementAt(int index) {
192
        return items.get(index);
193
    }
194
    
195
    /**
196
     * Removes any item currently contained by this list.
197
     *
198
     */
199
    public void clear(){
200
        items.clear();
201
        fireContentsChanged(this, 0, 0);
202
    }
203
    
204
    /**
205
     * Returns an ArrayList containing the elements of this list.
206
     */
207
    public ArrayList getElements() {
208
        return items;
209
    }
210

    
211
    
212
}