001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import javax.swing.*;
016: import javax.swing.event.ListSelectionEvent;
017: import javax.swing.event.ListSelectionListener;
018: import java.util.ArrayList;
019: import java.util.Iterator;
020:
021: /**
022: * Default implementation of a list selection model.
023: *
024: * @author <a href="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
025: * @see javax.swing.ListSelectionModel
026: * @see SList
027: * @see STable
028: */
029: public class SDefaultListSelectionModel extends
030: DefaultListSelectionModel implements SListSelectionModel {
031:
032: /**
033: * indicates the the selection model is in {@link #NO_SELECTION} mode. This
034: * is necessary, because we cannot set the selection mode of the swing
035: * DefaultSelectionModel to a value we want (it does not provide
036: * for NO_SELEFCTION), so we have to wrap it...
037: */
038: private boolean noSelection = false;
039:
040: /**
041: * indicates if we should fire event immediately when they arise, or if we
042: * should collect them for a later delivery
043: */
044: private boolean delayEvents = false;
045:
046: /**
047: * should we fire adjusting events. This is an optimization feature. In most
048: * scenarios we don't need that events. In fact I cannot imagine a scenario
049: * where we need this events...
050: */
051: private boolean fireAdjustingEvents = false;
052:
053: /**
054: * all delayed events are stored here.
055: */
056: protected final ArrayList delayedEvents = new ArrayList(5);
057:
058: public int getMinSelectionIndex() {
059: if (noSelection) {
060: return -1;
061: } else {
062: return super .getMinSelectionIndex();
063: }
064: }
065:
066: public int getMaxSelectionIndex() {
067: if (noSelection) {
068: return -1;
069: } else {
070: return super .getMaxSelectionIndex();
071: }
072: }
073:
074: public boolean isSelectedIndex(int index) {
075: if (noSelection) {
076: return false;
077: } else {
078: return super .isSelectedIndex(index);
079: }
080: }
081:
082: public int getAnchorSelectionIndex() {
083: if (noSelection) {
084: return -1;
085: } else {
086: return super .getAnchorSelectionIndex();
087: }
088: }
089:
090: public int getLeadSelectionIndex() {
091: if (noSelection) {
092: return -1;
093: } else {
094: return super .getLeadSelectionIndex();
095: }
096: }
097:
098: public boolean isSelectionEmpty() {
099: if (noSelection) {
100: return true;
101: } else {
102: return super .isSelectionEmpty();
103: }
104: }
105:
106: public int getSelectionMode() {
107: if (noSelection) {
108: return NO_SELECTION;
109: } else {
110: return super .getSelectionMode();
111: }
112: }
113:
114: public void setSelectionMode(int selectionMode) {
115: if (selectionMode == NO_SELECTION) {
116: noSelection = true;
117: } else {
118: noSelection = false;
119: super .setSelectionMode(selectionMode);
120: }
121: }
122:
123: protected void fireDelayedEvents(boolean onlyAdjusting) {
124: for (Iterator iter = delayedEvents.iterator(); iter.hasNext();) {
125: ListSelectionEvent e = (ListSelectionEvent) iter.next();
126:
127: if (!onlyAdjusting || e.getValueIsAdjusting()) {
128: fireValueChanged(e.getFirstIndex(), e.getLastIndex(), e
129: .getValueIsAdjusting());
130: iter.remove();
131: }
132: }
133: }
134:
135: public boolean getDelayEvents() {
136: return delayEvents;
137: }
138:
139: public void setDelayEvents(boolean b) {
140: delayEvents = b;
141: }
142:
143: public boolean getFireAdjustingEvents() {
144: return fireAdjustingEvents;
145: }
146:
147: public void setFireAdjustingEvents(boolean b) {
148: fireAdjustingEvents = b;
149: }
150:
151: /**
152: * fire event with isValueIsAdjusting true
153: */
154: public void fireDelayedIntermediateEvents() {
155: if (fireAdjustingEvents) {
156: fireDelayedEvents(true);
157: }
158: }
159:
160: public void fireDelayedFinalEvents() {
161: if (!delayEvents) {
162: fireDelayedEvents(false);
163: delayedEvents.clear();
164: }
165: }
166:
167: protected void fireValueChanged(int firstIndex, int lastIndex,
168: boolean isAdjusting) {
169: if (!noSelection) {
170:
171: if (delayEvents) {
172: if (!isAdjusting || fireAdjustingEvents) {
173: delayedEvents.add(new ListSelectionEvent(this ,
174: firstIndex, lastIndex, isAdjusting));
175: }
176: } else {
177: super .fireValueChanged(firstIndex, lastIndex,
178: isAdjusting);
179: }
180: }
181: }
182:
183: /**
184: * use this with care. It is only a way to speed up your application if you
185: * don't need selection in your list. If you set this selection model, the
186: * only way to switch between selection modes is to set a new selection
187: * model, which supports the wished selection mode
188: */
189: public static final ListSelectionModel NO_SELECTION_LIST_SELECTION_MODEL = new ListSelectionModel() {
190:
191: public void setSelectionInterval(int index0, int index1) {
192: }
193:
194: public void addSelectionInterval(int index0, int index1) {
195: }
196:
197: public void removeSelectionInterval(int index0, int index1) {
198: }
199:
200: public int getMinSelectionIndex() {
201: return -1;
202: }
203:
204: public int getMaxSelectionIndex() {
205: return -1;
206: }
207:
208: public boolean isSelectedIndex(int index) {
209: return false;
210: }
211:
212: public int getAnchorSelectionIndex() {
213: return -1;
214: }
215:
216: public void setAnchorSelectionIndex(int index) {
217: }
218:
219: public int getLeadSelectionIndex() {
220: return -1;
221: }
222:
223: public void setLeadSelectionIndex(int index) {
224: }
225:
226: public void clearSelection() {
227: }
228:
229: public boolean isSelectionEmpty() {
230: return true;
231: }
232:
233: public void insertIndexInterval(int index, int length,
234: boolean before) {
235: }
236:
237: public void removeIndexInterval(int index0, int index1) {
238: }
239:
240: public void setValueIsAdjusting(boolean valueIsAdjusting) {
241: }
242:
243: public boolean getValueIsAdjusting() {
244: return false;
245: }
246:
247: public void setSelectionMode(int selectionMode) {
248: }
249:
250: public int getSelectionMode() {
251: return NO_SELECTION;
252: }
253:
254: public void addListSelectionListener(ListSelectionListener x) {
255: }
256:
257: public void removeListSelectionListener(ListSelectionListener x) {
258: }
259:
260: public boolean getDelayEvents() {
261: return false;
262: }
263:
264: public void setDelayEvents(boolean b) {
265: }
266:
267: public void fireDelayedIntermediateEvents() {
268: }
269:
270: public void fireDelayedFinalEvents() {
271: }
272: };
273:
274: }
|