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