📄 defaultlistselectionmodel.java
字号:
/** * Gets the smallest index which is currently a member of a selection * interval. * * @return The least integer <code>i</code> such that <code>i >= * 0</code> and <code>i</code> is a member of a selected interval, or * <code>-1</code> if there are no selected intervals * * @see #getMaxSelectionIndex */ public int getMinSelectionIndex() { if (isSelectionEmpty()) return -1; return sel.nextSetBit(0); } /** * Gets the largest index which is currently a member of a selection * interval. * * @return The greatest integer <code>i</code> such that <code>i >= * 0</code> and <code>i</code> is a member of a selected interval, or * <code>-1</code> if there are no selected intervals * * @see #getMinSelectionIndex */ public int getMaxSelectionIndex() { if (isSelectionEmpty()) return -1; int mx = -1; for(int i=sel.nextSetBit(0); i >= 0; i=sel.nextSetBit(i+1)) { mx = i; } return mx; } /** * Determines whether a particular index is a member of a selection * interval. * * @param a The index to search for * * @return <code>true</code> if the index is a member of a selection interval, * otherwise <code>false</code> */ public boolean isSelectedIndex(int a) { // TODO: Probably throw an exception here? if (a >= sel.length() || a < 0) return false; return sel.get(a); } /** * If the {@link #selectionMode} property is equal to * <code>SINGLE_SELECTION</code> equivalent to calling * <code>setSelectionInterval(index1, index2)</code>; * If the {@link #selectionMode} property is equal to * <code>SINGLE_INTERVAL_SELECTION</code> and the interval being * added is not adjacent to an already selected interval, * equivalent to <code>setSelectionInterval(index1, index2)</code>. * Otherwise adds the range <code>[index0, index1]</code> * to the selection interval set. * * @param index0 The beginning of the range of indices to select * @param index1 The end of the range of indices to select * * @see #setSelectionInterval * @see #removeSelectionInterval */ public void addSelectionInterval(int index0, int index1) { int lo = Math.min(index0, index1); int hi = Math.max(index0, index1); oldSel = sel.clone(); if (selectionMode == SINGLE_SELECTION) setSelectionInterval(index0, index1); // COMPAT: Like Sun (but not like IBM), we allow calls to // addSelectionInterval when selectionMode is // SINGLE_SELECTION_INTERVAL iff the interval being added // is adjacent to an already selected interval if (selectionMode == SINGLE_INTERVAL_SELECTION) if (!(isSelectedIndex(index0) || isSelectedIndex(index1) || isSelectedIndex(Math.max(lo-1,0)) || isSelectedIndex(Math.min(hi+1,sel.size())))) sel.clear(); // We have to update the anchorSelectionIndex and leadSelectionIndex // variables // The next if statements breaks down to "if this selection is adjacent // to the previous selection and going in the same direction" if ((isSelectedIndex(leadSelectionIndex)) && ((index0 - 1 == leadSelectionIndex && (index1 >= index0) && (leadSelectionIndex >= anchorSelectionIndex)) || (index0 + 1 == leadSelectionIndex && (index1 <= index0) && (leadSelectionIndex <= anchorSelectionIndex))) && (anchorSelectionIndex != -1 || leadSelectionIndex != -1)) { // setting setLeadCalledFromAdd to true tells setLeadSelectionIndex // not to update oldSel setLeadCalledFromAdd = true; setLeadSelectionIndex(index1); setLeadCalledFromAdd = false; } else { leadSelectionIndex = index1; anchorSelectionIndex = index0; sel.set(lo, hi+1); if (sel.equals(oldSel) == false) fireValueChanged(lo, hi, valueIsAdjusting); } } /** * Deselects all indices in the inclusive range * <code>[index0,index1]</code>. * * @param index0 The beginning of the range of indices to deselect * @param index1 The end of the range of indices to deselect * * @see #addSelectionInterval * @see #setSelectionInterval */ public void removeSelectionInterval(int index0, int index1) { oldSel = sel.clone(); int lo = Math.min(index0, index1); int hi = Math.max(index0, index1); // if selectionMode is SINGLE_INTERVAL_SELECTION and removing the interval // (index0,index1) would leave two disjoint selection intervals, remove all // selected indices from lo to the last selected index if (getMinSelectionIndex() > 0 && getMinSelectionIndex() < lo && selectionMode == SINGLE_INTERVAL_SELECTION) hi = sel.size() - 1; sel.clear(lo, hi+1); //update anchorSelectionIndex and leadSelectionIndex variables //TODO: will probably need MouseDragged to test properly and know if this works setAnchorSelectionIndex(index0); leadSelectionIndex = index1; if (sel.equals(oldSel) == false) fireValueChanged(lo, hi, valueIsAdjusting); } /** * Removes all intervals in the selection set. */ public void clearSelection() { oldSel = sel.clone(); int sz = sel.size(); sel.clear(); if (sel.equals(oldSel) == false) fireValueChanged(0, sz, valueIsAdjusting); } /** * Clears the current selection and marks a given interval as * "selected". If the current selection mode is * <code>SINGLE_SELECTION</code> only the index <code>index2</code> is * selected. * * @param index0 The low end of the new selection * @param index1 The high end of the new selection */ public void setSelectionInterval(int index0, int index1) { oldSel = sel.clone(); sel.clear(); if (selectionMode == SINGLE_SELECTION) index0 = index1; int lo = Math.min(index0, index1); int hi = Math.max(index0, index1); sel.set(lo, hi+1); // update the anchorSelectionIndex and leadSelectionIndex variables setAnchorSelectionIndex(index0); leadSelectionIndex=index1; if (sel.equals(oldSel) == false) fireValueChanged(lo, hi, valueIsAdjusting); } /** * Inserts a number of indices either before or after a particular * position in the set of indices. Renumbers all indices after the * inserted range. The new indices in the inserted range are not * selected. This method is typically called to synchronize the selection * model with an inserted range of elements in a {@link ListModel}. * * @param index The position to insert indices at * @param length The number of indices to insert * @param before Indicates whether to insert the indices before the index * or after it */ public void insertIndexInterval(int index, int length, boolean before) { if (!before) { index++; length--; } BitSet tmp = sel.get(index, sel.size()); sel.clear(index, sel.size()); int n = tmp.size(); for (int i = 0; i < n; ++i) sel.set(index + length + i, tmp.get(i)); } /** * Removes a range from the set of indices. Renumbers all indices after * the removed range. This method is typically called to synchronize the * selection model with a deleted range of elements in a {@link * ListModel}. * * @param index0 The first index to remove (inclusive) * @param index1 The last index to remove (inclusive) */ public void removeIndexInterval(int index0, int index1) { int lo = Math.min(index0, index1); int hi = Math.max(index0, index1); BitSet tmp = sel.get(hi, sel.size()); sel.clear(lo, sel.size()); int n = tmp.size(); for (int i = 0; i < n; ++i) sel.set(lo + i, tmp.get(i)); } /** * Fires a {@link ListSelectionEvent} to all the listeners of type {@link * ListSelectionListener} registered with this selection model to * indicate that a series of adjustment has just ended. * * The values of {@link #getMinSelectionIndex} and * {@link #getMaxSelectionIndex} are used in the {@link ListSelectionEvent} * that gets fired. * * @param isAdjusting <code>true</code> if this is the final change * in a series of adjustments, <code>false/code> otherwise */ protected void fireValueChanged(boolean isAdjusting) { fireValueChanged(getMinSelectionIndex(), getMaxSelectionIndex(), isAdjusting); } /** * Fires a {@link ListSelectionEvent} to all the listeners of type {@link * ListSelectionListener} registered with this selection model. * * @param firstIndex The low index of the changed range * @param lastIndex The high index of the changed range */ protected void fireValueChanged(int firstIndex, int lastIndex) { fireValueChanged(firstIndex, lastIndex, getValueIsAdjusting()); } /** * Fires a {@link ListSelectionEvent} to all the listeners of type {@link * ListSelectionListener} registered with this selection model. * * @param firstIndex The low index of the changed range * @param lastIndex The high index of the changed range * @param isAdjusting Whether this change is part of a seqence of adjustments * made to the selection, such as during interactive scrolling */ protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting) { ListSelectionEvent evt = new ListSelectionEvent(this, firstIndex, lastIndex, isAdjusting); ListSelectionListener[] listeners = getListSelectionListeners(); for (int i = 0; i < listeners.length; ++i) listeners[i].valueChanged(evt); } /** * Adds a listener. * * @param listener The listener to add * * @see #removeListSelectionListener * @see #getListSelectionListeners */ public void addListSelectionListener(ListSelectionListener listener) { listenerList.add(ListSelectionListener.class, listener); } /** * Removes a registered listener. * * @param listener The listener to remove * * @see #addListSelectionListener * @see #getListSelectionListeners */ public void removeListSelectionListener(ListSelectionListener listener) { listenerList.remove(ListSelectionListener.class, listener); } /** * Returns an array of all registerers listeners. * * @param listenerType The type of listener to retrieve * * @return The array * * @see #getListSelectionListeners * @since 1.3 */ public EventListener[] getListeners(Class listenerType) { return listenerList.getListeners(listenerType); } /** * Returns an array of all registerd list selection listeners. * * @return the array * * @see #addListSelectionListener * @see #removeListSelectionListener * @see #getListeners * @since 1.4 */ public ListSelectionListener[] getListSelectionListeners() { return (ListSelectionListener[]) getListeners(ListSelectionListener.class); } /** * Returns a clone of this object. * <code>listenerList</code> don't gets duplicated. * * @return the cloned object * * @throws CloneNotSupportedException if an error occurs */ public Object clone() throws CloneNotSupportedException { DefaultListSelectionModel model = (DefaultListSelectionModel) super.clone(); model.sel = (BitSet) sel.clone(); return model; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -