1. /*
  2. * @(#)DefaultListSelectionModel.java 1.75 04/05/05
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.util.EventListener;
  9. import java.util.BitSet;
  10. import java.io.Serializable;
  11. import javax.swing.event.*;
  12. /**
  13. * Default data model for list selections.
  14. * <p>
  15. * <strong>Warning:</strong>
  16. * Serialized objects of this class will not be compatible with
  17. * future Swing releases. The current serialization support is
  18. * appropriate for short term storage or RMI between applications running
  19. * the same version of Swing. As of 1.4, support for long term storage
  20. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  21. * has been added to the <code>java.beans</code> package.
  22. * Please see {@link java.beans.XMLEncoder}.
  23. *
  24. * @version 1.75 05/05/04
  25. * @author Philip Milne
  26. * @author Hans Muller
  27. * @see ListSelectionModel
  28. */
  29. public class DefaultListSelectionModel implements ListSelectionModel, Cloneable, Serializable
  30. {
  31. private static final int MIN = -1;
  32. private static final int MAX = Integer.MAX_VALUE;
  33. private int selectionMode = MULTIPLE_INTERVAL_SELECTION;
  34. private int minIndex = MAX;
  35. private int maxIndex = MIN;
  36. private int anchorIndex = -1;
  37. private int leadIndex = -1;
  38. private int firstAdjustedIndex = MAX;
  39. private int lastAdjustedIndex = MIN;
  40. private boolean isAdjusting = false;
  41. private int firstChangedIndex = MAX;
  42. private int lastChangedIndex = MIN;
  43. private BitSet value = new BitSet(32);
  44. protected EventListenerList listenerList = new EventListenerList();
  45. protected boolean leadAnchorNotificationEnabled = true;
  46. // implements javax.swing.ListSelectionModel
  47. public int getMinSelectionIndex() { return isSelectionEmpty() ? -1 : minIndex; }
  48. // implements javax.swing.ListSelectionModel
  49. public int getMaxSelectionIndex() { return maxIndex; }
  50. // implements javax.swing.ListSelectionModel
  51. public boolean getValueIsAdjusting() { return isAdjusting; }
  52. // implements javax.swing.ListSelectionModel
  53. /**
  54. * Returns the selection mode.
  55. * @return one of the these values:
  56. * <ul>
  57. * <li>SINGLE_SELECTION
  58. * <li>SINGLE_INTERVAL_SELECTION
  59. * <li>MULTIPLE_INTERVAL_SELECTION
  60. * </ul>
  61. * @see #getSelectionMode
  62. */
  63. public int getSelectionMode() { return selectionMode; }
  64. // implements javax.swing.ListSelectionModel
  65. /**
  66. * Sets the selection mode. The default is
  67. * MULTIPLE_INTERVAL_SELECTION.
  68. * @param selectionMode one of three values:
  69. * <ul>
  70. * <li>SINGLE_SELECTION
  71. * <li>SINGLE_INTERVAL_SELECTION
  72. * <li>MULTIPLE_INTERVAL_SELECTION
  73. * </ul>
  74. * @exception IllegalArgumentException if <code>selectionMode</code>
  75. * is not one of the legal values shown above
  76. * @see #setSelectionMode
  77. */
  78. public void setSelectionMode(int selectionMode) {
  79. switch (selectionMode) {
  80. case SINGLE_SELECTION:
  81. case SINGLE_INTERVAL_SELECTION:
  82. case MULTIPLE_INTERVAL_SELECTION:
  83. this.selectionMode = selectionMode;
  84. break;
  85. default:
  86. throw new IllegalArgumentException("invalid selectionMode");
  87. }
  88. }
  89. // implements javax.swing.ListSelectionModel
  90. public boolean isSelectedIndex(int index) {
  91. return ((index < minIndex) || (index > maxIndex)) ? false : value.get(index);
  92. }
  93. // implements javax.swing.ListSelectionModel
  94. public boolean isSelectionEmpty() {
  95. return (minIndex > maxIndex);
  96. }
  97. // implements javax.swing.ListSelectionModel
  98. public void addListSelectionListener(ListSelectionListener l) {
  99. listenerList.add(ListSelectionListener.class, l);
  100. }
  101. // implements javax.swing.ListSelectionModel
  102. public void removeListSelectionListener(ListSelectionListener l) {
  103. listenerList.remove(ListSelectionListener.class, l);
  104. }
  105. /**
  106. * Returns an array of all the list selection listeners
  107. * registered on this <code>DefaultListSelectionModel</code>.
  108. *
  109. * @return all of this model's <code>ListSelectionListener</code>s
  110. * or an empty
  111. * array if no list selection listeners are currently registered
  112. *
  113. * @see #addListSelectionListener
  114. * @see #removeListSelectionListener
  115. *
  116. * @since 1.4
  117. */
  118. public ListSelectionListener[] getListSelectionListeners() {
  119. return (ListSelectionListener[])listenerList.getListeners(
  120. ListSelectionListener.class);
  121. }
  122. /**
  123. * Notifies listeners that we have ended a series of adjustments.
  124. */
  125. protected void fireValueChanged(boolean isAdjusting) {
  126. if (lastChangedIndex == MIN) {
  127. return;
  128. }
  129. /* Change the values before sending the event to the
  130. * listeners in case the event causes a listener to make
  131. * another change to the selection.
  132. */
  133. int oldFirstChangedIndex = firstChangedIndex;
  134. int oldLastChangedIndex = lastChangedIndex;
  135. firstChangedIndex = MAX;
  136. lastChangedIndex = MIN;
  137. fireValueChanged(oldFirstChangedIndex, oldLastChangedIndex, isAdjusting);
  138. }
  139. /**
  140. * Notifies <code>ListSelectionListeners</code> that the value
  141. * of the selection, in the closed interval <code>firstIndex</code>,
  142. * <code>lastIndex</code>, has changed.
  143. */
  144. protected void fireValueChanged(int firstIndex, int lastIndex) {
  145. fireValueChanged(firstIndex, lastIndex, getValueIsAdjusting());
  146. }
  147. /**
  148. * @param firstIndex the first index in the interval
  149. * @param lastIndex the last index in the interval
  150. * @param isAdjusting true if this is the final change in a series of
  151. * adjustments
  152. * @see EventListenerList
  153. */
  154. protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)
  155. {
  156. Object[] listeners = listenerList.getListenerList();
  157. ListSelectionEvent e = null;
  158. for (int i = listeners.length - 2; i >= 0; i -= 2) {
  159. if (listeners[i] == ListSelectionListener.class) {
  160. if (e == null) {
  161. e = new ListSelectionEvent(this, firstIndex, lastIndex, isAdjusting);
  162. }
  163. ((ListSelectionListener)listeners[i+1]).valueChanged(e);
  164. }
  165. }
  166. }
  167. private void fireValueChanged() {
  168. if (lastAdjustedIndex == MIN) {
  169. return;
  170. }
  171. /* If getValueAdjusting() is true, (eg. during a drag opereration)
  172. * record the bounds of the changes so that, when the drag finishes (and
  173. * setValueAdjusting(false) is called) we can post a single event
  174. * with bounds covering all of these individual adjustments.
  175. */
  176. if (getValueIsAdjusting()) {
  177. firstChangedIndex = Math.min(firstChangedIndex, firstAdjustedIndex);
  178. lastChangedIndex = Math.max(lastChangedIndex, lastAdjustedIndex);
  179. }
  180. /* Change the values before sending the event to the
  181. * listeners in case the event causes a listener to make
  182. * another change to the selection.
  183. */
  184. int oldFirstAdjustedIndex = firstAdjustedIndex;
  185. int oldLastAdjustedIndex = lastAdjustedIndex;
  186. firstAdjustedIndex = MAX;
  187. lastAdjustedIndex = MIN;
  188. fireValueChanged(oldFirstAdjustedIndex, oldLastAdjustedIndex);
  189. }
  190. /**
  191. * Returns an array of all the objects currently registered as
  192. * <code><em>Foo</em>Listener</code>s
  193. * upon this model.
  194. * <code><em>Foo</em>Listener</code>s
  195. * are registered using the <code>add<em>Foo</em>Listener</code> method.
  196. * <p>
  197. * You can specify the <code>listenerType</code> argument
  198. * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
  199. * For example, you can query a <code>DefaultListSelectionModel</code>
  200. * instance <code>m</code>
  201. * for its list selection listeners
  202. * with the following code:
  203. *
  204. * <pre>ListSelectionListener[] lsls = (ListSelectionListener[])(m.getListeners(ListSelectionListener.class));</pre>
  205. *
  206. * If no such listeners exist,
  207. * this method returns an empty array.
  208. *
  209. * @param listenerType the type of listeners requested;
  210. * this parameter should specify an interface
  211. * that descends from <code>java.util.EventListener</code>
  212. * @return an array of all objects registered as
  213. * <code><em>Foo</em>Listener</code>s
  214. * on this model,
  215. * or an empty array if no such
  216. * listeners have been added
  217. * @exception ClassCastException if <code>listenerType</code> doesn't
  218. * specify a class or interface that implements
  219. * <code>java.util.EventListener</code>
  220. *
  221. * @see #getListSelectionListeners
  222. *
  223. * @since 1.3
  224. */
  225. public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
  226. return listenerList.getListeners(listenerType);
  227. }
  228. // Updates first and last change indices
  229. private void markAsDirty(int r) {
  230. firstAdjustedIndex = Math.min(firstAdjustedIndex, r);
  231. lastAdjustedIndex = Math.max(lastAdjustedIndex, r);
  232. }
  233. // Sets the state at this index and update all relevant state.
  234. private void set(int r) {
  235. if (value.get(r)) {
  236. return;
  237. }
  238. value.set(r);
  239. markAsDirty(r);
  240. // Update minimum and maximum indices
  241. minIndex = Math.min(minIndex, r);
  242. maxIndex = Math.max(maxIndex, r);
  243. }
  244. // Clears the state at this index and update all relevant state.
  245. private void clear(int r) {
  246. if (!value.get(r)) {
  247. return;
  248. }
  249. value.clear(r);
  250. markAsDirty(r);
  251. // Update minimum and maximum indices
  252. /*
  253. If (r > minIndex) the minimum has not changed.
  254. The case (r < minIndex) is not possible because r'th value was set.
  255. We only need to check for the case when lowest entry has been cleared,
  256. and in this case we need to search for the first value set above it.
  257. */
  258. if (r == minIndex) {
  259. for(minIndex = minIndex + 1; minIndex <= maxIndex; minIndex++) {
  260. if (value.get(minIndex)) {
  261. break;
  262. }
  263. }
  264. }
  265. /*
  266. If (r < maxIndex) the maximum has not changed.
  267. The case (r > maxIndex) is not possible because r'th value was set.
  268. We only need to check for the case when highest entry has been cleared,
  269. and in this case we need to search for the first value set below it.
  270. */
  271. if (r == maxIndex) {
  272. for(maxIndex = maxIndex - 1; minIndex <= maxIndex; maxIndex--) {
  273. if (value.get(maxIndex)) {
  274. break;
  275. }
  276. }
  277. }
  278. /* Performance note: This method is called from inside a loop in
  279. changeSelection() but we will only iterate in the loops
  280. above on the basis of one iteration per deselected cell - in total.
  281. Ie. the next time this method is called the work of the previous
  282. deselection will not be repeated.
  283. We also don't need to worry about the case when the min and max
  284. values are in their unassigned states. This cannot happen because
  285. this method's initial check ensures that the selection was not empty
  286. and therefore that the minIndex and maxIndex had 'real' values.
  287. If we have cleared the whole selection, set the minIndex and maxIndex
  288. to their cannonical values so that the next set command always works
  289. just by using Math.min and Math.max.
  290. */
  291. if (isSelectionEmpty()) {
  292. minIndex = MAX;
  293. maxIndex = MIN;
  294. }
  295. }
  296. /**
  297. * Sets the value of the leadAnchorNotificationEnabled flag.
  298. * @see #isLeadAnchorNotificationEnabled()
  299. */
  300. public void setLeadAnchorNotificationEnabled(boolean flag) {
  301. leadAnchorNotificationEnabled = flag;
  302. }
  303. /**
  304. * Returns the value of the <code>leadAnchorNotificationEnabled</code> flag.
  305. * When <code>leadAnchorNotificationEnabled</code> is true the model
  306. * generates notification events with bounds that cover all the changes to
  307. * the selection plus the changes to the lead and anchor indices.
  308. * Setting the flag to false causes a narrowing of the event's bounds to
  309. * include only the elements that have been selected or deselected since
  310. * the last change. Either way, the model continues to maintain the lead
  311. * and anchor variables internally. The default is true.
  312. * <p>
  313. * Note: It is possible for the lead or anchor to be changed without a
  314. * change to the selection. Notification of these changes is often
  315. * important, such as when the new lead or anchor needs to be updated in
  316. * the view. Therefore, caution is urged when changing the default value.
  317. *
  318. * @return the value of the <code>leadAnchorNotificationEnabled</code> flag
  319. * @see #setLeadAnchorNotificationEnabled(boolean)
  320. */
  321. public boolean isLeadAnchorNotificationEnabled() {
  322. return leadAnchorNotificationEnabled;
  323. }
  324. private void updateLeadAnchorIndices(int anchorIndex, int leadIndex) {
  325. if (leadAnchorNotificationEnabled) {
  326. if (this.anchorIndex != anchorIndex) {
  327. if (this.anchorIndex != -1) { // The unassigned state.
  328. markAsDirty(this.anchorIndex);
  329. }
  330. markAsDirty(anchorIndex);
  331. }
  332. if (this.leadIndex != leadIndex) {
  333. if (this.leadIndex != -1) { // The unassigned state.
  334. markAsDirty(this.leadIndex);
  335. }
  336. markAsDirty(leadIndex);
  337. }
  338. }
  339. this.anchorIndex = anchorIndex;
  340. this.leadIndex = leadIndex;
  341. }
  342. private boolean contains(int a, int b, int i) {
  343. return (i >= a) && (i <= b);
  344. }
  345. private void changeSelection(int clearMin, int clearMax,
  346. int setMin, int setMax, boolean clearFirst) {
  347. for(int i = Math.min(setMin, clearMin); i <= Math.max(setMax, clearMax); i++) {
  348. boolean shouldClear = contains(clearMin, clearMax, i);
  349. boolean shouldSet = contains(setMin, setMax, i);
  350. if (shouldSet && shouldClear) {
  351. if (clearFirst) {
  352. shouldClear = false;
  353. }
  354. else {
  355. shouldSet = false;
  356. }
  357. }
  358. if (shouldSet) {
  359. set(i);
  360. }
  361. if (shouldClear) {
  362. clear(i);
  363. }
  364. }
  365. fireValueChanged();
  366. }
  367. /** Change the selection with the effect of first clearing the values
  368. * in the inclusive range [clearMin, clearMax] then setting the values
  369. * in the inclusive range [setMin, setMax]. Do this in one pass so
  370. * that no values are cleared if they would later be set.
  371. */
  372. private void changeSelection(int clearMin, int clearMax, int setMin, int setMax) {
  373. changeSelection(clearMin, clearMax, setMin, setMax, true);
  374. }
  375. // implements javax.swing.ListSelectionModel
  376. public void clearSelection() {
  377. removeSelectionIntervalImpl(minIndex, maxIndex, false);
  378. }
  379. // implements javax.swing.ListSelectionModel
  380. public void setSelectionInterval(int index0, int index1) {
  381. if (index0 == -1 || index1 == -1) {
  382. return;
  383. }
  384. if (getSelectionMode() == SINGLE_SELECTION) {
  385. index0 = index1;
  386. }
  387. updateLeadAnchorIndices(index0, index1);
  388. int clearMin = minIndex;
  389. int clearMax = maxIndex;
  390. int setMin = Math.min(index0, index1);
  391. int setMax = Math.max(index0, index1);
  392. changeSelection(clearMin, clearMax, setMin, setMax);
  393. }
  394. // implements javax.swing.ListSelectionModel
  395. public void addSelectionInterval(int index0, int index1)
  396. {
  397. if (index0 == -1 || index1 == -1) {
  398. return;
  399. }
  400. // If we only allow a single selection, channel through
  401. // setSelectionInterval() to enforce the rule.
  402. if (getSelectionMode() == SINGLE_SELECTION) {
  403. setSelectionInterval(index0, index1);
  404. return;
  405. }
  406. updateLeadAnchorIndices(index0, index1);
  407. int clearMin = MAX;
  408. int clearMax = MIN;
  409. int setMin = Math.min(index0, index1);
  410. int setMax = Math.max(index0, index1);
  411. // If we only allow a single interval and this would result
  412. // in multiple intervals, then set the selection to be just
  413. // the new range.
  414. if (getSelectionMode() == SINGLE_INTERVAL_SELECTION &&
  415. (setMax < minIndex - 1 || setMin > maxIndex + 1)) {
  416. setSelectionInterval(index0, index1);
  417. return;
  418. }
  419. changeSelection(clearMin, clearMax, setMin, setMax);
  420. }
  421. // implements javax.swing.ListSelectionModel
  422. public void removeSelectionInterval(int index0, int index1)
  423. {
  424. removeSelectionIntervalImpl(index0, index1, true);
  425. }
  426. // private implementation allowing the selection interval
  427. // to be removed without affecting the lead and anchor
  428. private void removeSelectionIntervalImpl(int index0, int index1,
  429. boolean changeLeadAnchor) {
  430. if (index0 == -1 || index1 == -1) {
  431. return;
  432. }
  433. if (changeLeadAnchor) {
  434. updateLeadAnchorIndices(index0, index1);
  435. }
  436. int clearMin = Math.min(index0, index1);
  437. int clearMax = Math.max(index0, index1);
  438. int setMin = MAX;
  439. int setMax = MIN;
  440. // If the removal would produce to two disjoint selections in a mode
  441. // that only allows one, extend the removal to the end of the selection.
  442. if (getSelectionMode() != MULTIPLE_INTERVAL_SELECTION &&
  443. clearMin > minIndex && clearMax < maxIndex) {
  444. clearMax = maxIndex;
  445. }
  446. changeSelection(clearMin, clearMax, setMin, setMax);
  447. }
  448. private void setState(int index, boolean state) {
  449. if (state) {
  450. set(index);
  451. }
  452. else {
  453. clear(index);
  454. }
  455. }
  456. /**
  457. * Insert length indices beginning before/after index. If the value
  458. * at index is itself selected and the selection mode is not
  459. * SINGLE_SELECTION, set all of the newly inserted items as selected.
  460. * Otherwise leave them unselected. This method is typically
  461. * called to sync the selection model with a corresponding change
  462. * in the data model.
  463. */
  464. public void insertIndexInterval(int index, int length, boolean before)
  465. {
  466. /* The first new index will appear at insMinIndex and the last
  467. * one will appear at insMaxIndex
  468. */
  469. int insMinIndex = (before) ? index : index + 1;
  470. int insMaxIndex = (insMinIndex + length) - 1;
  471. /* Right shift the entire bitset by length, beginning with
  472. * index-1 if before is true, index+1 if it's false (i.e. with
  473. * insMinIndex).
  474. */
  475. for(int i = maxIndex; i >= insMinIndex; i--) {
  476. setState(i + length, value.get(i));
  477. }
  478. /* Initialize the newly inserted indices.
  479. */
  480. boolean setInsertedValues = ((getSelectionMode() == SINGLE_SELECTION) ?
  481. false : value.get(index));
  482. for(int i = insMinIndex; i <= insMaxIndex; i++) {
  483. setState(i, setInsertedValues);
  484. }
  485. int leadIndex = this.leadIndex;
  486. if (leadIndex > index || (before && leadIndex == index)) {
  487. leadIndex = this.leadIndex + length;
  488. }
  489. int anchorIndex = this.anchorIndex;
  490. if (anchorIndex > index || (before && anchorIndex == index)) {
  491. anchorIndex = this.anchorIndex + length;
  492. }
  493. if (leadIndex != this.leadIndex || anchorIndex != this.anchorIndex) {
  494. updateLeadAnchorIndices(anchorIndex, leadIndex);
  495. }
  496. fireValueChanged();
  497. }
  498. /**
  499. * Remove the indices in the interval index0,index1 (inclusive) from
  500. * the selection model. This is typically called to sync the selection
  501. * model width a corresponding change in the data model. Note
  502. * that (as always) index0 need not be <= index1.
  503. */
  504. public void removeIndexInterval(int index0, int index1)
  505. {
  506. int rmMinIndex = Math.min(index0, index1);
  507. int rmMaxIndex = Math.max(index0, index1);
  508. int gapLength = (rmMaxIndex - rmMinIndex) + 1;
  509. /* Shift the entire bitset to the left to close the index0, index1
  510. * gap.
  511. */
  512. for(int i = rmMinIndex; i <= maxIndex; i++) {
  513. setState(i, value.get(i + gapLength));
  514. }
  515. int leadIndex = this.leadIndex;
  516. if (leadIndex == 0 && rmMinIndex == 0) {
  517. // do nothing
  518. } else if (leadIndex > rmMaxIndex) {
  519. leadIndex = this.leadIndex - gapLength;
  520. } else if (leadIndex >= rmMinIndex) {
  521. leadIndex = rmMinIndex - 1;
  522. }
  523. int anchorIndex = this.anchorIndex;
  524. if (anchorIndex == 0 && rmMinIndex == 0) {
  525. // do nothing
  526. } else if (anchorIndex > rmMaxIndex) {
  527. anchorIndex = this.anchorIndex - gapLength;
  528. } else if (anchorIndex >= rmMinIndex) {
  529. anchorIndex = rmMinIndex - 1;
  530. }
  531. if (leadIndex != this.leadIndex || anchorIndex != this.anchorIndex) {
  532. updateLeadAnchorIndices(anchorIndex, leadIndex);
  533. }
  534. fireValueChanged();
  535. }
  536. // implements javax.swing.ListSelectionModel
  537. public void setValueIsAdjusting(boolean isAdjusting) {
  538. if (isAdjusting != this.isAdjusting) {
  539. this.isAdjusting = isAdjusting;
  540. this.fireValueChanged(isAdjusting);
  541. }
  542. }
  543. /**
  544. * Returns a string that displays and identifies this
  545. * object's properties.
  546. *
  547. * @return a <code>String</code> representation of this object
  548. */
  549. public String toString() {
  550. String s = ((getValueIsAdjusting()) ? "~" : "=") + value.toString();
  551. return getClass().getName() + " " + Integer.toString(hashCode()) + " " + s;
  552. }
  553. /**
  554. * Returns a clone of this selection model with the same selection.
  555. * <code>listenerLists</code> are not duplicated.
  556. *
  557. * @exception CloneNotSupportedException if the selection model does not
  558. * both (a) implement the Cloneable interface and (b) define a
  559. * <code>clone</code> method.
  560. */
  561. public Object clone() throws CloneNotSupportedException {
  562. DefaultListSelectionModel clone = (DefaultListSelectionModel)super.clone();
  563. clone.value = (BitSet)value.clone();
  564. clone.listenerList = new EventListenerList();
  565. return clone;
  566. }
  567. // implements javax.swing.ListSelectionModel
  568. public int getAnchorSelectionIndex() {
  569. return anchorIndex;
  570. }
  571. // implements javax.swing.ListSelectionModel
  572. public int getLeadSelectionIndex() {
  573. return leadIndex;
  574. }
  575. /**
  576. * Set the anchor selection index, leaving all selection values unchanged.
  577. * If leadAnchorNotificationEnabled is true, send a notification covering
  578. * the old and new anchor cells.
  579. *
  580. * @see #getAnchorSelectionIndex
  581. * @see #setLeadSelectionIndex
  582. */
  583. public void setAnchorSelectionIndex(int anchorIndex) {
  584. updateLeadAnchorIndices(anchorIndex, this.leadIndex);
  585. fireValueChanged();
  586. }
  587. /**
  588. * Set the lead selection index, leaving all selection values unchanged.
  589. * If leadAnchorNotificationEnabled is true, send a notification covering
  590. * the old and new lead cells.
  591. *
  592. * @param leadIndex the new lead selection index
  593. *
  594. * @see #setAnchorSelectionIndex
  595. * @see #setLeadSelectionIndex
  596. * @see #getLeadSelectionIndex
  597. *
  598. * @since 1.5
  599. */
  600. public void moveLeadSelectionIndex(int leadIndex) {
  601. // disallow a -1 lead unless the anchor is already -1
  602. if (leadIndex == -1) {
  603. if (this.anchorIndex != -1) {
  604. return;
  605. }
  606. /* PENDING(shannonh) - The following check is nice, to be consistent with
  607. setLeadSelectionIndex. However, it is not absolutely
  608. necessary: One could work around it by setting the anchor
  609. to something valid, modifying the lead, and then moving
  610. the anchor back to -1. For this reason, there's no sense
  611. in adding it at this time, as that would require
  612. updating the spec and officially committing to it.
  613. // otherwise, don't do anything if the anchor is -1
  614. } else if (this.anchorIndex == -1) {
  615. return;
  616. */
  617. }
  618. updateLeadAnchorIndices(this.anchorIndex, leadIndex);
  619. fireValueChanged();
  620. }
  621. /**
  622. * Sets the lead selection index, ensuring that values between the
  623. * anchor and the new lead are either all selected or all deselected.
  624. * If the value at the anchor index is selected, first clear all the
  625. * values in the range [anchor, oldLeadIndex], then select all the values
  626. * values in the range [anchor, newLeadIndex], where oldLeadIndex is the old
  627. * leadIndex and newLeadIndex is the new one.
  628. * <p>
  629. * If the value at the anchor index is not selected, do the same thing in
  630. * reverse selecting values in the old range and deslecting values in the
  631. * new one.
  632. * <p>
  633. * Generate a single event for this change and notify all listeners.
  634. * For the purposes of generating minimal bounds in this event, do the
  635. * operation in a single pass; that way the first and last index inside the
  636. * ListSelectionEvent that is broadcast will refer to cells that actually
  637. * changed value because of this method. If, instead, this operation were
  638. * done in two steps the effect on the selection state would be the same
  639. * but two events would be generated and the bounds around the changed
  640. * values would be wider, including cells that had been first cleared only
  641. * to later be set.
  642. * <p>
  643. * This method can be used in the <code>mouseDragged</code> method
  644. * of a UI class to extend a selection.
  645. *
  646. * @see #getLeadSelectionIndex
  647. * @see #setAnchorSelectionIndex
  648. */
  649. public void setLeadSelectionIndex(int leadIndex) {
  650. int anchorIndex = this.anchorIndex;
  651. // only allow a -1 lead if the anchor is already -1
  652. if (leadIndex == -1) {
  653. if (anchorIndex == -1) {
  654. updateLeadAnchorIndices(anchorIndex, leadIndex);
  655. fireValueChanged();
  656. }
  657. return;
  658. // otherwise, don't do anything if the anchor is -1
  659. } else if (anchorIndex == -1) {
  660. return;
  661. }
  662. if (this.leadIndex == -1) {
  663. this.leadIndex = leadIndex;
  664. }
  665. boolean shouldSelect = value.get(this.anchorIndex);
  666. if (getSelectionMode() == SINGLE_SELECTION) {
  667. anchorIndex = leadIndex;
  668. shouldSelect = true;
  669. }
  670. int oldMin = Math.min(this.anchorIndex, this.leadIndex);
  671. int oldMax = Math.max(this.anchorIndex, this.leadIndex);
  672. int newMin = Math.min(anchorIndex, leadIndex);
  673. int newMax = Math.max(anchorIndex, leadIndex);
  674. updateLeadAnchorIndices(anchorIndex, leadIndex);
  675. if (shouldSelect) {
  676. changeSelection(oldMin, oldMax, newMin, newMax);
  677. }
  678. else {
  679. changeSelection(newMin, newMax, oldMin, oldMax, false);
  680. }
  681. }
  682. }