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 / JDnDListModel.java @ 40561

History | View | Annotate | Download (6.28 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
/* CVS MESSAGES:
25
*
26
* $Id: JDnDListModel.java 13655 2007-09-12 16:28:55Z bsanchez $
27
* $Log$
28
* Revision 1.2  2007-09-12 16:28:23  bsanchez
29
* *** empty log message ***
30
*
31
* Revision 1.1  2007/08/20 08:34:46  evercher
32
* He fusionado LibUI con LibUIComponents
33
*
34
* Revision 1.1  2006/03/22 11:18:29  jaume
35
* *** empty log message ***
36
*
37
* Revision 1.4  2006/02/28 15:25:14  jaume
38
* *** empty log message ***
39
*
40
* Revision 1.2.2.3  2006/01/31 16:25:24  jaume
41
* correcciones de bugs
42
*
43
* Revision 1.3  2006/01/26 16:07:14  jaume
44
* *** empty log message ***
45
*
46
* Revision 1.2.2.1  2006/01/26 12:59:33  jaume
47
* 0.5
48
*
49
* Revision 1.2  2006/01/24 14:36:33  jaume
50
* This is the new version
51
*
52
* Revision 1.1.2.2  2006/01/17 12:55:40  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.1.2.1  2006/01/10 13:11:38  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.1.2.1  2005/12/29 08:26:54  jaume
59
* some gui issues where fixed
60
*
61
* Revision 1.1.2.2  2005/12/22 16:46:00  jaume
62
* puede borrar m?ltiples intervalos de entradas dentro de la lista
63
*
64
* Revision 1.1.2.1  2005/12/19 18:12:35  jaume
65
* *** empty log message ***
66
*
67
*
68
*/
69
/**
70
 * 
71
 */
72
package org.gvsig.gui.beans.controls.dnd;
73

    
74
import java.util.ArrayList;
75
import java.util.Collection;
76
import java.util.Iterator;
77

    
78
import javax.swing.AbstractListModel;
79

    
80

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

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

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

    
229
    
230
}