KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > list > DefaultListSelectionModel


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.list;
31
32 import java.io.Serializable JavaDoc;
33 import java.util.BitSet JavaDoc;
34 import java.util.EventListener JavaDoc;
35
36 import nextapp.echo2.app.event.EventListenerList;
37 import nextapp.echo2.app.event.ChangeEvent;
38 import nextapp.echo2.app.event.ChangeListener;
39
40 /**
41  * Default <code>ListSelectionModel</code> implementation.
42  */

43 public class DefaultListSelectionModel
44 implements ListSelectionModel, Serializable JavaDoc {
45
46     private EventListenerList listenerList = new EventListenerList();
47     private int selectionMode = SINGLE_SELECTION;
48     private BitSet JavaDoc selection = new BitSet JavaDoc();
49     private int minSelectedIndex = -1;
50     
51     /**
52      * @see nextapp.echo2.app.list.ListSelectionModel#addChangeListener(
53      * nextapp.echo2.app.event.ChangeListener)
54      */

55     public void addChangeListener(ChangeListener l) {
56         listenerList.addListener(ChangeListener.class, l);
57     }
58     
59     /**
60      * @see nextapp.echo2.app.list.ListSelectionModel#clearSelection()
61      */

62     public void clearSelection() {
63         selection = new BitSet JavaDoc();
64         minSelectedIndex = -1;
65         fireValueChanged();
66     }
67     
68     /**
69      * Notifies <code>ChangeListener</code>s that the selection has
70      * changed.
71      */

72     protected void fireValueChanged() {
73         ChangeEvent e = new ChangeEvent(this);
74         EventListener JavaDoc[] listeners = listenerList.getListeners(ChangeListener.class);
75         for (int index = 0; index < listeners.length; ++index) {
76             ((ChangeListener) listeners[index]).stateChanged(e);
77         }
78     }
79         
80     /**
81      * @see nextapp.echo2.app.list.ListSelectionModel#getMaxSelectedIndex()
82      */

83     public int getMaxSelectedIndex() {
84         return selection.length() - 1;
85     }
86     
87     /**
88      * @see nextapp.echo2.app.list.ListSelectionModel#getMinSelectedIndex()
89      */

90     public int getMinSelectedIndex() {
91         return minSelectedIndex;
92     }
93     
94     /**
95      * @see nextapp.echo2.app.list.ListSelectionModel#getSelectionMode()
96      */

97     public int getSelectionMode() {
98         return selectionMode;
99     }
100     
101     /**
102      * @see nextapp.echo2.app.list.ListSelectionModel#isSelectedIndex(int)
103      */

104     public boolean isSelectedIndex(int index) {
105         return selection.get(index);
106     }
107     
108     /**
109      * @see nextapp.echo2.app.list.ListSelectionModel#isSelectionEmpty()
110      */

111     public boolean isSelectionEmpty() {
112         return selection.length() == 0;
113     }
114     
115     /**
116      * @see nextapp.echo2.app.list.ListSelectionModel#removeChangeListener(nextapp.echo2.app.event.ChangeListener)
117      */

118     public void removeChangeListener(ChangeListener l) {
119         listenerList.removeListener(ChangeListener.class, l);
120     }
121
122     /**
123      * @see nextapp.echo2.app.list.ListSelectionModel#setSelectedIndex(int, boolean)
124      */

125     public void setSelectedIndex(int index, boolean newValue) {
126         boolean oldValue = isSelectedIndex(index);
127     
128         if (newValue ^ oldValue) {
129             if (newValue) {
130                 if (selectionMode == SINGLE_SELECTION && getMinSelectedIndex() != -1) {
131                     setSelectedIndex(getMinSelectedIndex(), false);
132                 }
133                 selection.set(index);
134                 if (index < minSelectedIndex || minSelectedIndex == -1) {
135                     minSelectedIndex = index;
136                 }
137             } else {
138                 selection.clear(index);
139                 if (index == minSelectedIndex) {
140                     // Minimum selected index has been deselected, find new minimum selected index.
141
int max = getMaxSelectedIndex();
142                     minSelectedIndex = -1;
143                     for (int i = 0; i <= max; ++i) {
144                         if (selection.get(i)) {
145                             minSelectedIndex = i;
146                             break;
147                         }
148                     }
149                 }
150             }
151             fireValueChanged();
152         }
153     }
154
155     /**
156      * @see nextapp.echo2.app.list.ListSelectionModel#setSelectionMode(int)
157      */

158     public void setSelectionMode(int selectionMode) {
159         if (selectionMode != MULTIPLE_SELECTION && this.selectionMode == MULTIPLE_SELECTION) {
160             // deselect all but first selected element.
161
int maxSelectedIndex = getMaxSelectedIndex();
162             for (int i = minSelectedIndex + 1; i <= maxSelectedIndex; ++i) {
163                 setSelectedIndex(i, false);
164             }
165         }
166         this.selectionMode = selectionMode;
167         fireValueChanged();
168     }
169 }
170
Popular Tags