root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / filterPanel / filterQueryPanel / jLabelAsCell / DefaultListModelForJLabelAsCell.java @ 13136
History | View | Annotate | Download (7.3 KB)
1 |
package org.gvsig.gui.beans.filterPanel.filterQueryPanel.jLabelAsCell; |
---|---|
2 |
|
3 |
import java.io.Serializable; |
4 |
|
5 |
import javax.swing.DefaultListModel; |
6 |
import javax.swing.JLabel; |
7 |
|
8 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
9 |
*
|
10 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
11 |
*
|
12 |
* This program is free software; you can redistribute it and/or
|
13 |
* modify it under the terms of the GNU General Public License
|
14 |
* as published by the Free Software Foundation; either version 2
|
15 |
* of the License, or (at your option) any later version.
|
16 |
*
|
17 |
* This program is distributed in the hope that it will be useful,
|
18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
* GNU General Public License for more details.
|
21 |
*
|
22 |
* You should have received a copy of the GNU General Public License
|
23 |
* along with this program; if not, write to the Free Software
|
24 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
25 |
*
|
26 |
* For more information, contact:
|
27 |
*
|
28 |
* Generalitat Valenciana
|
29 |
* Conselleria d'Infraestructures i Transport
|
30 |
* Av. Blasco Ib??ez, 50
|
31 |
* 46010 VALENCIA
|
32 |
* SPAIN
|
33 |
*
|
34 |
* +34 963862235
|
35 |
* gvsig@gva.es
|
36 |
* www.gvsig.gva.es
|
37 |
*
|
38 |
* or
|
39 |
*
|
40 |
* IVER T.I. S.A
|
41 |
* Salamanca 50
|
42 |
* 46005 Valencia
|
43 |
* Spain
|
44 |
*
|
45 |
* +34 963163400
|
46 |
* dac@iver.es
|
47 |
*/
|
48 |
|
49 |
|
50 |
/**
|
51 |
* This class reimplements the "contains()" method of "DefaultListModel" for
|
52 |
*
|
53 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
54 |
*
|
55 |
*/
|
56 |
public class DefaultListModelForJLabelAsCell extends DefaultListModel implements Serializable { |
57 |
private static final long serialVersionUID = 4410627738805243864L; |
58 |
|
59 |
/**
|
60 |
* Represents the start index of the group of not loaded values (that should be after the group of loaded values)
|
61 |
* -2 -> if elements aren't grouped (in two lists: first for loaded, after for not loaded)
|
62 |
* -1 -> if there isn't elements
|
63 |
* 0 -> if there are only not loaded elements
|
64 |
* = (capacity() -1) -> if there are only loaded elements
|
65 |
*/
|
66 |
private int startIndexOfNotLoadedValues; |
67 |
|
68 |
/**
|
69 |
* Default constructor without parameters
|
70 |
*/
|
71 |
public DefaultListModelForJLabelAsCell() {
|
72 |
super();
|
73 |
startIndexOfNotLoadedValues = -1;
|
74 |
} |
75 |
|
76 |
/**
|
77 |
* Method that returns true if there is a inner JLabel that has as inner text the same text as 'patternText'
|
78 |
*
|
79 |
* @param patternText
|
80 |
* @return A boolean value
|
81 |
*/
|
82 |
public boolean containsJLabelText(String patternText) { |
83 |
for (int i = 0; i < super.getSize(); i++) { |
84 |
if (((JLabel)super.get(i)).getText().compareTo(patternText) == 0) |
85 |
return true; |
86 |
} |
87 |
|
88 |
return false; |
89 |
} |
90 |
|
91 |
/**
|
92 |
* Returns the position of first JLabel that has as text the same text as 'patternText', or -1 if no JLabel has that text
|
93 |
*
|
94 |
* @param patternText An String
|
95 |
* @return An integer value
|
96 |
*/
|
97 |
public int getIndexOfJLabelText(String patternText) { |
98 |
for (int i = 0; i < super.getSize(); i++) { |
99 |
if (((JLabel)super.get(i)).getText().compareTo(patternText) == 0) |
100 |
return i;
|
101 |
} |
102 |
|
103 |
return -1; // If not found |
104 |
} |
105 |
|
106 |
/**
|
107 |
* Replaces first element which has the text 'patternText' to a JLabelValueLoaded new one element with the same text
|
108 |
*
|
109 |
* @param patternText Text of the component
|
110 |
*/
|
111 |
public void changeElementThatHasTextToJLabelLoadedValue(String patternText) { |
112 |
int index = getIndexOfJLabelText(patternText);
|
113 |
|
114 |
if (index != -1) |
115 |
super.set(index, new JLabelAsCellValueLoaded(patternText)); |
116 |
} |
117 |
|
118 |
/**
|
119 |
* Sets all elements to 'JLabelValueNotLoaded'
|
120 |
*/
|
121 |
public void setAllElementsToNotLoaded() { |
122 |
if (startIndexOfNotLoadedValues == -1) { |
123 |
for (int i = 0; i < super.getSize(); i++) { |
124 |
if (super.get(i) instanceof JLabelAsCellValueLoaded) { |
125 |
JLabelAsCellValueNotLoaded jL = new JLabelAsCellValueNotLoaded(((JLabelAsCellValueLoaded)super.get(i)).getText()); |
126 |
super.remove(i);
|
127 |
super.add(i, jL);///// REIMPLEMENTATION OF SOME METHODS OF 'DefaultListModel' ///// |
128 |
} |
129 |
} |
130 |
} |
131 |
else {
|
132 |
for (int i = 0; i < startIndexOfNotLoadedValues; i++) { |
133 |
if (super.get(i) instanceof JLabelAsCellValueLoaded) { |
134 |
JLabelAsCellValueNotLoaded jL = new JLabelAsCellValueNotLoaded(((JLabelAsCellValueLoaded)super.get(i)).getText()); |
135 |
super.set(i, jL); // replacement |
136 |
} |
137 |
} |
138 |
} |
139 |
|
140 |
startIndexOfNotLoadedValues = 0;
|
141 |
} |
142 |
|
143 |
///// REIMPLEMENTATION OF SOME METHODS OF 'DefaultListModel' /////
|
144 |
|
145 |
/*
|
146 |
* (non-Javadoc)
|
147 |
* @see javax.swing.DefaultListModel#clear()
|
148 |
*/
|
149 |
public void clear() { |
150 |
super.clear();
|
151 |
startIndexOfNotLoadedValues = -1;
|
152 |
} |
153 |
|
154 |
/*
|
155 |
* (non-Javadoc)
|
156 |
* @see javax.swing.DefaultListModel#insertElementAt(java.lang.Object, int)
|
157 |
*/
|
158 |
public void insertElementAt(Object obj, int index) { |
159 |
super.insertElementAt(obj, index);
|
160 |
|
161 |
if ((obj instanceof JLabelAsCellValueNotLoaded) && (index > -1) && (index < startIndexOfNotLoadedValues)) |
162 |
startIndexOfNotLoadedValues = index; |
163 |
} |
164 |
|
165 |
/*
|
166 |
* (non-Javadoc)
|
167 |
* @see javax.swing.DefaultListModel#remove(int)
|
168 |
*/
|
169 |
public Object remove(int index) { |
170 |
if ( (index > -1) && (index == startIndexOfNotLoadedValues) && (index == (super.capacity() -1)) ) { |
171 |
startIndexOfNotLoadedValues = -1;
|
172 |
} |
173 |
|
174 |
return super.remove(index); |
175 |
} |
176 |
|
177 |
/*
|
178 |
* (non-Javadoc)
|
179 |
* @see javax.swing.DefaultListModel#removeAllElements()
|
180 |
*/
|
181 |
public void removeAllElements() { |
182 |
super.removeAllElements();
|
183 |
startIndexOfNotLoadedValues = -1;
|
184 |
} |
185 |
|
186 |
/*
|
187 |
* (non-Javadoc)
|
188 |
* @see javax.swing.DefaultListModel#removeElement(java.lang.Object)
|
189 |
*/
|
190 |
public boolean removeElement(Object obj) { |
191 |
int index = super.indexOf(obj); |
192 |
|
193 |
if ( (index > -1) && (index == startIndexOfNotLoadedValues) && (index == (super.capacity() -1)) ) { |
194 |
startIndexOfNotLoadedValues = -1;
|
195 |
} |
196 |
|
197 |
return super.removeElement(obj); |
198 |
} |
199 |
|
200 |
/*
|
201 |
* (non-Javadoc)
|
202 |
* @see javax.swing.DefaultListModel#removeElementAt(int)
|
203 |
*/
|
204 |
public void removeElementAt(int index) { |
205 |
if ( (index > -1) && (index == startIndexOfNotLoadedValues) && (index == (super.capacity() -1)) ) { |
206 |
startIndexOfNotLoadedValues = -1;
|
207 |
} |
208 |
|
209 |
super.removeElementAt(index);
|
210 |
} |
211 |
|
212 |
/*
|
213 |
* (non-Javadoc)
|
214 |
* @see javax.swing.DefaultListModel#removeRange(int, int)
|
215 |
*/
|
216 |
public void removeRange(int fromIndex, int toIndex) { |
217 |
super.removeRange(fromIndex, toIndex);
|
218 |
|
219 |
// Try to find any element of type 'JLabelNotLoadedValue'
|
220 |
for (int i = 0; i < super.capacity(); i++) { |
221 |
if (super.get(i) instanceof JLabelAsCellValueNotLoaded) { |
222 |
if ( ( (i == (super.capacity() -1) || (i < (super.capacity() -1)) && (super.get(super.capacity() -1) instanceof JLabelAsCellValueNotLoaded) ) ) ) |
223 |
startIndexOfNotLoadedValues = i; |
224 |
else
|
225 |
startIndexOfNotLoadedValues = -1;
|
226 |
} |
227 |
} |
228 |
} |
229 |
|
230 |
/*
|
231 |
* (non-Javadoc)
|
232 |
* @see javax.swing.DefaultListModel#set(int, java.lang.Object)
|
233 |
*/
|
234 |
public Object set(int index, Object element) { |
235 |
if ((element instanceof JLabelAsCellValueNotLoaded) && (index > -1) && (index < startIndexOfNotLoadedValues)) |
236 |
startIndexOfNotLoadedValues = index; |
237 |
|
238 |
return super.set(index, element); |
239 |
} |
240 |
|
241 |
/*
|
242 |
* (non-Javadoc)
|
243 |
* @see javax.swing.DefaultListModel#setElementAt(java.lang.Object, int)
|
244 |
*/
|
245 |
public void setElementAt(Object obj, int index) { |
246 |
super.setElementAt(obj, index);
|
247 |
|
248 |
if ((obj instanceof JLabelAsCellValueNotLoaded) && (index > -1) && (index < startIndexOfNotLoadedValues)) |
249 |
startIndexOfNotLoadedValues = index; |
250 |
} |
251 |
|
252 |
///// END REIMPLEMENTATION OF SOME METHODS OF 'DefaultListModel' /////
|
253 |
} |