1 20 package org.enhydra.barracuda.core.comp; 21 22 import java.io.*; 23 import java.util.*; 24 25 import org.enhydra.barracuda.core.comp.model.*; 26 27 30 public class DefaultListSelectionModel implements ListSelectionModel, Cloneable , Serializable { 31 32 private static final int MIN = -1; 33 private static final int MAX = Integer.MAX_VALUE; 34 private int selectionMode = SINGLE_SELECTION; 35 private int minIndex = MAX; 36 private int maxIndex = MIN; 37 private int firstAdjustedIndex = MAX; 38 private int lastAdjustedIndex = MIN; 39 private BitSet value = new BitSet(32); 40 protected List listeners = new ArrayList(); 41 42 49 public void addModelListener(ModelListener ml) { 50 listeners.add(ml); 51 } 52 53 58 public void removeModelListener(ModelListener ml) { 59 listeners.remove(ml); 60 } 61 62 67 public void fireModelChanged() { 68 Iterator it = listeners.iterator(); 69 ModelListener ml = null; 70 while (it.hasNext()) { 71 ml = (ModelListener) it.next(); 72 ml.modelChanged(this); 73 } 74 } 75 76 89 public void setSelectionMode(int selectionMode) { 90 switch (selectionMode) { 91 case SINGLE_SELECTION: 92 case SINGLE_INTERVAL_SELECTION: 93 case MULTIPLE_INTERVAL_SELECTION: 94 this.selectionMode = selectionMode; 95 break; 96 default: 97 throw new IllegalArgumentException ("invalid selectionMode"); 98 } 99 } 100 101 111 public int getSelectionMode() { 112 return selectionMode; 113 } 114 115 124 public void setSelectionInterval(int index0, int index1) { 125 if (index0==-1 || index1==-1) return; 126 127 if (getSelectionMode()==SINGLE_SELECTION) index0 = index1; 128 129 int clearMin = minIndex; 130 int clearMax = maxIndex; 131 int setMin = Math.min(index0, index1); 132 int setMax = Math.max(index0, index1); 133 changeSelection(clearMin, clearMax, setMin, setMax); 134 } 135 136 146 public void addSelectionInterval(int index0, int index1) { 147 if (index0==-1 || index1==-1) return; 148 149 if (getSelectionMode()!=MULTIPLE_INTERVAL_SELECTION) { 150 setSelectionInterval(index0, index1); 151 return; 152 } 153 154 int clearMin = MAX; 155 int clearMax = MIN; 156 int setMin = Math.min(index0, index1); 157 int setMax = Math.max(index0, index1); 158 changeSelection(clearMin, clearMax, setMin, setMax); 159 } 160 161 171 public void removeSelectionInterval(int index0, int index1) { 172 if (index0==-1 || index1==-1) return; 173 174 int clearMin = Math.min(index0, index1); 175 int clearMax = Math.max(index0, index1); 176 int setMin = MAX; 177 int setMax = MIN; 178 179 if (getSelectionMode()!=MULTIPLE_INTERVAL_SELECTION && 182 clearMin>minIndex && clearMax<maxIndex) { 183 clearMax = maxIndex; 184 } 185 186 changeSelection(clearMin, clearMax, setMin, setMax); 187 } 188 189 192 public int getMinSelectionIndex() { 193 return isSelectionEmpty() ? -1 : minIndex; 194 } 195 196 199 public int getMaxSelectionIndex() { 200 return maxIndex; 201 } 202 203 206 public boolean isSelectedIndex(int index) { 207 return ((index < minIndex) || (index > maxIndex)) ? false : value.get(index); 208 } 209 210 213 public boolean isSelectionEmpty() { 214 return (minIndex > maxIndex); 215 } 216 217 221 public void clearSelection() { 222 removeSelectionInterval(minIndex, maxIndex); 223 } 224 225 private void markAsDirty(int r) { 227 firstAdjustedIndex = Math.min(firstAdjustedIndex, r); 228 lastAdjustedIndex = Math.max(lastAdjustedIndex, r); 229 } 230 231 private void set(int r) { 233 if (value.get(r)) return; 234 value.set(r); 235 markAsDirty(r); 236 237 minIndex = Math.min(minIndex, r); 239 maxIndex = Math.max(maxIndex, r); 240 } 241 242 private void clear(int r) { 244 if (!value.get(r)) return; 245 value.clear(r); 246 markAsDirty(r); 247 248 if (r==minIndex) { 255 for (minIndex=minIndex+1; minIndex<=maxIndex; minIndex++) { 256 if (value.get(minIndex)) { 257 break; 258 } 259 } 260 } 261 262 if (r==maxIndex) { 267 for (maxIndex=maxIndex-1; minIndex<=maxIndex; maxIndex--) { 268 if (value.get(maxIndex)) { 269 break; 270 } 271 } 272 } 273 274 if (isSelectionEmpty()) { 289 minIndex = MAX; 290 maxIndex = MIN; 291 } 292 } 293 294 private boolean contains(int a, int b, int i) { 295 return (i>=a) && (i<=b); 296 } 297 298 private void changeSelection(int clearMin, int clearMax, int setMin, int setMax, boolean clearFirst) { 299 for (int i=Math.min(setMin, clearMin); i<=Math.max(setMax, clearMax); i++) { 300 boolean shouldClear = contains(clearMin, clearMax, i); 301 boolean shouldSet = contains(setMin, setMax, i); 302 303 if (shouldSet && shouldClear) { 304 if (clearFirst) shouldClear = false; 305 else shouldSet = false; 306 } 307 308 if (shouldSet) set(i); 309 if (shouldClear) clear(i); 310 } 311 fireModelChanged(); 312 } 313 314 320 private void changeSelection(int clearMin, int clearMax, int setMin, int setMax) { 321 changeSelection(clearMin, clearMax, setMin, setMax, true); 322 } 323 324 private void setState(int index, boolean state) { 325 if (state) set(index); 326 else clear(index); 327 } 328 329 335 public String toString() { 336 String s = "="+value.toString(); 337 return getClass().getName()+" "+Integer.toString(hashCode())+" "+s; 338 } 339 340 348 public Object clone() throws CloneNotSupportedException { 349 DefaultListSelectionModel clone = (DefaultListSelectionModel)super.clone(); 350 clone.value = (BitSet)value.clone(); 351 clone.listeners = new ArrayList(); 352 return clone; 353 } 354 355 } 356 | Popular Tags |