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