svn-gvsig-desktop / branches / v05 / extensions / extWMS / src / com / iver / cit / gvsig / gui / beans / controls / dnd / JDnDListModel.java @ 3855
History | View | Annotate | Download (6.08 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 3855 2006-01-31 16:25:24Z jaume $
|
45 |
* $Log$
|
46 |
* Revision 1.2.2.3 2006-01-31 16:25:24 jaume
|
47 |
* correcciones de bugs
|
48 |
*
|
49 |
* Revision 1.3 2006/01/26 16:07:14 jaume
|
50 |
* *** empty log message ***
|
51 |
*
|
52 |
* Revision 1.2.2.1 2006/01/26 12:59:33 jaume
|
53 |
* 0.5
|
54 |
*
|
55 |
* Revision 1.2 2006/01/24 14:36:33 jaume
|
56 |
* This is the new version
|
57 |
*
|
58 |
* Revision 1.1.2.2 2006/01/17 12:55:40 jaume
|
59 |
* *** empty log message ***
|
60 |
*
|
61 |
* Revision 1.1.2.1 2006/01/10 13:11:38 jaume
|
62 |
* *** empty log message ***
|
63 |
*
|
64 |
* Revision 1.1.2.1 2005/12/29 08:26:54 jaume
|
65 |
* some gui issues where fixed
|
66 |
*
|
67 |
* Revision 1.1.2.2 2005/12/22 16:46:00 jaume
|
68 |
* puede borrar m?ltiples intervalos de entradas dentro de la lista
|
69 |
*
|
70 |
* Revision 1.1.2.1 2005/12/19 18:12:35 jaume
|
71 |
* *** empty log message ***
|
72 |
*
|
73 |
*
|
74 |
*/
|
75 |
/**
|
76 |
*
|
77 |
*/
|
78 |
package com.iver.cit.gvsig.gui.beans.controls.dnd; |
79 |
|
80 |
import java.util.ArrayList; |
81 |
import java.util.Collection; |
82 |
import java.util.Iterator; |
83 |
|
84 |
import javax.swing.AbstractListModel; |
85 |
|
86 |
|
87 |
/**
|
88 |
*
|
89 |
* @author jaume
|
90 |
*
|
91 |
*/
|
92 |
public class JDnDListModel extends AbstractListModel { |
93 |
private ArrayList items = new ArrayList(); |
94 |
/**
|
95 |
* Inserts a collection of items before the specified index
|
96 |
*/
|
97 |
public void insertItems( int index, Collection objects ) |
98 |
{ |
99 |
// Handle the case where the items are being added to the end of the list
|
100 |
if( index == -1 ) |
101 |
{ |
102 |
// Add the items
|
103 |
for( Iterator i = objects.iterator(); i.hasNext(); ) |
104 |
{ |
105 |
String item = ( String )i.next(); |
106 |
addElement(items.size(), item ); |
107 |
} |
108 |
} |
109 |
else
|
110 |
{ |
111 |
// Insert the items
|
112 |
for( Iterator i = objects.iterator(); i.hasNext(); ) |
113 |
{ |
114 |
Object item = i.next();
|
115 |
insertElement( index++, item ); |
116 |
} |
117 |
} |
118 |
|
119 |
// Tell the list to update itself
|
120 |
this.fireContentsChanged( this, 0, this.items.size() - 1 ); |
121 |
} |
122 |
|
123 |
/**
|
124 |
* Inserts a new element into the list at the position mentioned in the index param.
|
125 |
* @param index
|
126 |
* @param item
|
127 |
*/
|
128 |
public boolean insertElement(int index, Object element) { |
129 |
if (element == null) { |
130 |
return false; |
131 |
} |
132 |
for (int i = 0; i < items.size(); i++) { |
133 |
|
134 |
if (items.get(i).equals(items)) {
|
135 |
return false; |
136 |
} |
137 |
} |
138 |
|
139 |
this.items.add(index, element);
|
140 |
return true; |
141 |
} |
142 |
|
143 |
/**
|
144 |
* Adds a new element at the position indicated by pos of the list.
|
145 |
* @param j
|
146 |
* @param pos
|
147 |
*/
|
148 |
public boolean addElement(int j, Object element) { |
149 |
if (element == null) { |
150 |
return false; |
151 |
} |
152 |
|
153 |
for (int i = 0; i < items.size(); i++) { |
154 |
if (items.get(i).equals(items)) {
|
155 |
return false; |
156 |
} |
157 |
} |
158 |
this.items.add(j, element);
|
159 |
fireContentsChanged(this, items.size() - 1, items.size() - 1); |
160 |
return true; |
161 |
} |
162 |
|
163 |
/**
|
164 |
* Adds a new element at the position indicated by pos of the list.
|
165 |
* @param j
|
166 |
* @param pos
|
167 |
*/
|
168 |
public boolean addElement(Object element) { |
169 |
return addElement(items.size(), element);
|
170 |
} |
171 |
|
172 |
/**
|
173 |
* Removes every elements contained in the collection from this list.
|
174 |
*/
|
175 |
public void delElements(Collection c) { |
176 |
items.removeAll(c); |
177 |
this.fireContentsChanged(this, 0, items.size()); |
178 |
} |
179 |
|
180 |
/**
|
181 |
* Removes the items of the list mentioned by the index array passed as argument.
|
182 |
* @param indices
|
183 |
*/
|
184 |
public void delIndices(int[] indices){ |
185 |
int removed = 0; |
186 |
for (int i = 0; i < indices.length; i++) { |
187 |
items.remove(indices[i]-removed); |
188 |
removed++; |
189 |
} |
190 |
} |
191 |
|
192 |
public void itemsMoved( int newIndex, int[] indicies ) |
193 |
{ |
194 |
|
195 |
// Copy the objects to a temporary ArrayList
|
196 |
ArrayList objects = new ArrayList(); |
197 |
for( int i=0; i<indicies.length; i++ ) |
198 |
{ |
199 |
objects.add( this.items.get( indicies[ i ] ) );
|
200 |
} |
201 |
|
202 |
// Delete the objects from the list
|
203 |
for( int i=indicies.length-1; i>=0; i-- ) |
204 |
{ |
205 |
this.items.remove( indicies[ i ] );
|
206 |
} |
207 |
|
208 |
// Insert the items at the new location
|
209 |
insertItems( newIndex, objects ); |
210 |
} |
211 |
|
212 |
/* (non-Javadoc)
|
213 |
* @see javax.swing.ListModel#getSize()
|
214 |
*/
|
215 |
public int getSize() { |
216 |
return items.size();
|
217 |
} |
218 |
|
219 |
/* (non-Javadoc)
|
220 |
* @see javax.swing.ListModel#getElementAt(int)
|
221 |
*/
|
222 |
public Object getElementAt(int index) { |
223 |
return items.get(index);
|
224 |
} |
225 |
|
226 |
/**
|
227 |
* Removes any item currently contained by this list.
|
228 |
*
|
229 |
*/
|
230 |
public void clear(){ |
231 |
items.clear(); |
232 |
fireContentsChanged(this, 0, 0); |
233 |
} |
234 |
|
235 |
/**
|
236 |
* Returns an ArrayList containing the elements of this list.
|
237 |
*/
|
238 |
public ArrayList getElements() { |
239 |
return items;
|
240 |
} |
241 |
|
242 |
|
243 |
} |