defaultlistselectionmodel.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 621 行 · 第 1/2 页
JAVA
621 行
* * @see #getValueIsAdjusting */ public void setValueIsAdjusting(boolean v) { valueIsAdjusting = v; } /** * Determines whether the selection is empty. * * @return <code>true</code> if the selection is empty, otherwise * <code>false</code> */ public boolean isSelectionEmpty() { return sel.isEmpty(); } /** * 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) { return sel.get(a); } /** * If the {@link #selectionMode} property is equal to * <code>SINGLE_SELECTION</code> or * <code>SINGLE_INTERVAL_SELECTION</code>, equivalent to calling * <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) { if (selectionMode == SINGLE_SELECTION || selectionMode == SINGLE_INTERVAL_SELECTION) 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); 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) { int lo = Math.min(index0, index1); int hi = Math.max(index0, index1); sel.clear(lo, hi+1); fireValueChanged(lo, hi, valueIsAdjusting); } /** * Removes all intervals in the selection set. */ public void clearSelection() { int sz = sel.size(); sel.clear(); 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) { 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); 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. * * @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 getListSelectionListener * @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 + =
减小字号Ctrl + -
显示快捷键?