001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.list;
031:
032: import java.io.Serializable;
033: import java.util.BitSet;
034: import java.util.EventListener;
035:
036: import nextapp.echo2.app.event.EventListenerList;
037: import nextapp.echo2.app.event.ChangeEvent;
038: import nextapp.echo2.app.event.ChangeListener;
039:
040: /**
041: * Default <code>ListSelectionModel</code> implementation.
042: */
043: public class DefaultListSelectionModel implements ListSelectionModel,
044: Serializable {
045:
046: private EventListenerList listenerList = new EventListenerList();
047: private int selectionMode = SINGLE_SELECTION;
048: private BitSet selection = new BitSet();
049: private int minSelectedIndex = -1;
050:
051: /**
052: * @see nextapp.echo2.app.list.ListSelectionModel#addChangeListener(
053: * nextapp.echo2.app.event.ChangeListener)
054: */
055: public void addChangeListener(ChangeListener l) {
056: listenerList.addListener(ChangeListener.class, l);
057: }
058:
059: /**
060: * @see nextapp.echo2.app.list.ListSelectionModel#clearSelection()
061: */
062: public void clearSelection() {
063: selection = new BitSet();
064: minSelectedIndex = -1;
065: fireValueChanged();
066: }
067:
068: /**
069: * Notifies <code>ChangeListener</code>s that the selection has
070: * changed.
071: */
072: protected void fireValueChanged() {
073: ChangeEvent e = new ChangeEvent(this );
074: EventListener[] listeners = listenerList
075: .getListeners(ChangeListener.class);
076: for (int index = 0; index < listeners.length; ++index) {
077: ((ChangeListener) listeners[index]).stateChanged(e);
078: }
079: }
080:
081: /**
082: * @see nextapp.echo2.app.list.ListSelectionModel#getMaxSelectedIndex()
083: */
084: public int getMaxSelectedIndex() {
085: return selection.length() - 1;
086: }
087:
088: /**
089: * @see nextapp.echo2.app.list.ListSelectionModel#getMinSelectedIndex()
090: */
091: public int getMinSelectedIndex() {
092: return minSelectedIndex;
093: }
094:
095: /**
096: * @see nextapp.echo2.app.list.ListSelectionModel#getSelectionMode()
097: */
098: public int getSelectionMode() {
099: return selectionMode;
100: }
101:
102: /**
103: * @see nextapp.echo2.app.list.ListSelectionModel#isSelectedIndex(int)
104: */
105: public boolean isSelectedIndex(int index) {
106: return selection.get(index);
107: }
108:
109: /**
110: * @see nextapp.echo2.app.list.ListSelectionModel#isSelectionEmpty()
111: */
112: public boolean isSelectionEmpty() {
113: return selection.length() == 0;
114: }
115:
116: /**
117: * @see nextapp.echo2.app.list.ListSelectionModel#removeChangeListener(nextapp.echo2.app.event.ChangeListener)
118: */
119: public void removeChangeListener(ChangeListener l) {
120: listenerList.removeListener(ChangeListener.class, l);
121: }
122:
123: /**
124: * @see nextapp.echo2.app.list.ListSelectionModel#setSelectedIndex(int, boolean)
125: */
126: public void setSelectedIndex(int index, boolean newValue) {
127: boolean oldValue = isSelectedIndex(index);
128:
129: if (newValue ^ oldValue) {
130: if (newValue) {
131: if (selectionMode == SINGLE_SELECTION
132: && getMinSelectedIndex() != -1) {
133: setSelectedIndex(getMinSelectedIndex(), false);
134: }
135: selection.set(index);
136: if (index < minSelectedIndex || minSelectedIndex == -1) {
137: minSelectedIndex = index;
138: }
139: } else {
140: selection.clear(index);
141: if (index == minSelectedIndex) {
142: // Minimum selected index has been deselected, find new minimum selected index.
143: int max = getMaxSelectedIndex();
144: minSelectedIndex = -1;
145: for (int i = 0; i <= max; ++i) {
146: if (selection.get(i)) {
147: minSelectedIndex = i;
148: break;
149: }
150: }
151: }
152: }
153: fireValueChanged();
154: }
155: }
156:
157: /**
158: * @see nextapp.echo2.app.list.ListSelectionModel#setSelectionMode(int)
159: */
160: public void setSelectionMode(int selectionMode) {
161: if (selectionMode != MULTIPLE_SELECTION
162: && this .selectionMode == MULTIPLE_SELECTION) {
163: // deselect all but first selected element.
164: int maxSelectedIndex = getMaxSelectedIndex();
165: for (int i = minSelectedIndex + 1; i <= maxSelectedIndex; ++i) {
166: setSelectedIndex(i, false);
167: }
168: }
169: this.selectionMode = selectionMode;
170: fireValueChanged();
171: }
172: }
|