KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SDefaultListSelectionModel


1 /*
2  * $Id: SDefaultListSelectionModel.java,v 1.4 2005/04/08 17:41:34 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import javax.swing.*;
17 import javax.swing.event.ListSelectionEvent JavaDoc;
18 import javax.swing.event.ListSelectionListener JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22
23 /**
24  * @author <a HREF="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
25  * @version $Revision: 1.4 $
26  * @see javax.swing.ListSelectionModel
27  * @see SList
28  * @see STable
29  */

30 public class SDefaultListSelectionModel
31         extends DefaultListSelectionModel
32         implements SListSelectionModel {
33
34     /**
35      * indicates the the selection model is in {@link #NO_SELECTION} mode. This
36      * is necessary, because we cannot set the selection mode of the swing
37      * DefaultSelectionModel to a value we want (it does not provide
38      * for NO_SELEFCTION), so we have to wrap it...
39      */

40     private boolean noSelection = false;
41
42     /**
43      * indicates if we should fire event immediately when they arise, or if we
44      * should collect them for a later delivery
45      */

46     private boolean delayEvents = false;
47
48     /**
49      * should we fire adjusting events. This is an optimization feature. In most
50      * scenarios we don't need that events. In fact I cannot imagine a scenario
51      * where we need this events...
52      */

53     private boolean fireAdjustingEvents = false;
54
55     /**
56      * all delayed events are stored here.
57      */

58     protected final ArrayList JavaDoc delayedEvents = new ArrayList JavaDoc(5);
59
60     public int getMinSelectionIndex() {
61         if (noSelection) {
62             return -1;
63         } else {
64             return super.getMinSelectionIndex();
65         }
66     }
67
68     public int getMaxSelectionIndex() {
69         if (noSelection) {
70             return -1;
71         } else {
72             return super.getMaxSelectionIndex();
73         }
74     }
75
76     public boolean isSelectedIndex(int index) {
77         if (noSelection) {
78             return false;
79         } else {
80             return super.isSelectedIndex(index);
81         }
82     }
83
84     public int getAnchorSelectionIndex() {
85         if (noSelection) {
86             return -1;
87         } else {
88             return super.getAnchorSelectionIndex();
89         }
90     }
91
92     public int getLeadSelectionIndex() {
93         if (noSelection) {
94             return -1;
95         } else {
96             return super.getLeadSelectionIndex();
97         }
98     }
99
100     public boolean isSelectionEmpty() {
101         if (noSelection) {
102             return true;
103         } else {
104             return super.isSelectionEmpty();
105         }
106     }
107
108     public int getSelectionMode() {
109         if (noSelection) {
110             return NO_SELECTION;
111         } else {
112             return super.getSelectionMode();
113         }
114     }
115
116     public void setSelectionMode(int selectionMode) {
117         if (selectionMode == NO_SELECTION) {
118             noSelection = true;
119         } else {
120             noSelection = false;
121             super.setSelectionMode(selectionMode);
122         }
123     }
124
125     protected void fireDelayedEvents(boolean onlyAdjusting) {
126         for (Iterator JavaDoc iter = delayedEvents.iterator(); iter.hasNext();) {
127             ListSelectionEvent JavaDoc e = (ListSelectionEvent JavaDoc) iter.next();
128
129             if (!onlyAdjusting || e.getValueIsAdjusting()) {
130                 fireValueChanged(e.getFirstIndex(), e.getLastIndex(),
131                         e.getValueIsAdjusting());
132             }
133             iter.remove();
134         }
135     }
136
137     public boolean getDelayEvents() {
138         return delayEvents;
139     }
140
141     public void setDelayEvents(boolean b) {
142         delayEvents = b;
143     }
144
145     public boolean getFireAdjustingEvents() {
146         return fireAdjustingEvents;
147     }
148
149     public void setFireAdjustingEvents(boolean b) {
150         fireAdjustingEvents = b;
151     }
152
153     /**
154      * fire event with isValueIsAdjusting true
155      */

156     public void fireDelayedIntermediateEvents() {
157         if (fireAdjustingEvents) {
158             fireDelayedEvents(true);
159         }
160     }
161
162     public void fireDelayedFinalEvents() {
163         if (!delayEvents) {
164             fireDelayedEvents(false);
165             delayedEvents.clear();
166         }
167     }
168
169     protected void fireValueChanged(int firstIndex, int lastIndex,
170                                     boolean isAdjusting) {
171         if (!noSelection) {
172
173             if (delayEvents) {
174                 if (!isAdjusting || fireAdjustingEvents) {
175                     delayedEvents.add(new ListSelectionEvent JavaDoc(this,
176                             firstIndex, lastIndex,
177                             isAdjusting));
178                 }
179             } else {
180                 super.fireValueChanged(firstIndex, lastIndex, isAdjusting);
181             }
182         }
183     }
184
185     /**
186      * use this with care. It is only a way to speed up your application if you
187      * don't need selection in your list. If you set this selection model, the
188      * only way to switch between selection modes is to set a new selection
189      * model, which supports the wished selection mode
190      */

191     public static final ListSelectionModel NO_SELECTION_LIST_SELECTION_MODEL =
192             new ListSelectionModel() {
193
194                 public void setSelectionInterval(int index0, int index1) {}
195
196                 public void addSelectionInterval(int index0, int index1) {}
197
198                 public void removeSelectionInterval(int index0, int index1) {}
199
200                 public int getMinSelectionIndex() { return -1;}
201
202                 public int getMaxSelectionIndex() { return -1;}
203
204                 public boolean isSelectedIndex(int index) { return false;}
205
206                 public int getAnchorSelectionIndex() { return -1;}
207
208                 public void setAnchorSelectionIndex(int index) {}
209
210                 public int getLeadSelectionIndex() { return -1;}
211
212                 public void setLeadSelectionIndex(int index) {}
213
214                 public void clearSelection() {}
215
216                 public boolean isSelectionEmpty() { return true;}
217
218                 public void insertIndexInterval(int index, int length,
219                                                 boolean before) {}
220
221                 public void removeIndexInterval(int index0, int index1) {}
222
223                 public void setValueIsAdjusting(boolean valueIsAdjusting) {}
224
225                 public boolean getValueIsAdjusting() { return false;}
226
227                 public void setSelectionMode(int selectionMode) {}
228
229                 public int getSelectionMode() { return NO_SELECTION; }
230
231                 public void addListSelectionListener(ListSelectionListener JavaDoc x) {}
232
233                 public void removeListSelectionListener(ListSelectionListener JavaDoc x) {}
234
235                 public boolean getDelayEvents() { return false;}
236
237                 public void setDelayEvents(boolean b) {}
238
239                 public void fireDelayedIntermediateEvents() {}
240
241                 public void fireDelayedFinalEvents() {}
242             };
243
244 }
245
246
247
Popular Tags