KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > DefaultListSelectionModel


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9  
10    $Log: DefaultListSelectionModel.java,v $
11    Revision 1.4 2004/05/05 13:24:32 bobintetley
12    Bugfixes and Laurent's patch for binary compatibility on Container.add()
13
14    Revision 1.3 2004/04/30 23:18:26 dannaab
15    List selection support, misc bug fixes
16
17    Revision 1.2 2004/04/15 11:24:33 bobintetley
18    (Dan Naab) ComponentUI, UIDefaults/UIManager and Accessibility support.
19    (Antonio Weber) TableColumnModelListener implementation and support
20
21    Revision 1.1 2004/04/06 12:30:53 bobintetley
22    JTable thread safety, ListSelectionModel implementation for JList/JTable
23
24  
25 */

26
27 package swingwtx.swing;
28
29 import swingwtx.swing.event.*;
30
31 import java.util.*;
32
33 /**
34  * Handles tying to components and how selections are
35  * performed/reported.
36  *
37  * Note: This class is very important for JTable/JList and if you
38  * want to implement your own, you are better off extending this
39  * class than starting with something that implements ListSelectionModel.
40  *
41  * @author Robin Rawson-Tetley
42  */

43 public class DefaultListSelectionModel implements ListSelectionModel {
44
45     /**
46      * We hold a reference to the component we are being a selection
47      * model for. This allows us to manipulate it's selection
48      * much easier and retrieve info about the
49      * selection from the component (and the component
50      * can update us when the selection changes).
51      */

52     protected JComponent component = null;
53     protected JTable table = null;
54     protected JList list = null;
55     protected boolean isTable = true;
56     protected Vector listeners = new Vector();
57     protected int index0 = -1;
58     protected int index1 = -1;
59     protected int mode = ListSelectionModel.SINGLE_SELECTION;
60     protected boolean isAdjusting = false;
61     
62     public DefaultListSelectionModel() {}
63     protected DefaultListSelectionModel(JComponent component) {
64         this.component = component;
65         isTable = component instanceof JTable;
66         if (isTable)
67             table = (JTable) component;
68         else
69             list = (JList) component;
70     }
71     
72     public void addListSelectionListener(ListSelectionListener x) {
73         listeners.add(x);
74     }
75     
76     public void addSelectionInterval(int index0, int index1) {
77         setSelectionInterval(index0, index1);
78     }
79     
80     public void clearSelection() {
81         if (isTable)
82             table.clearSelection();
83         else
84             list.clearSelection();
85     }
86     
87     public int getAnchorSelectionIndex() {
88         return index0;
89     }
90     
91     public int getLeadSelectionIndex() {
92         return index1;
93     }
94     
95     public int getMaxSelectionIndex() {
96         return index1;
97     }
98     
99     public int getMinSelectionIndex() {
100         return index0;
101     }
102     
103     public int getSelectionMode() {
104         return mode;
105     }
106     
107     public boolean getValueIsAdjusting() {
108         return isAdjusting;
109     }
110     
111     public void insertIndexInterval(int index, int length, boolean before) {
112     }
113     
114     public boolean isSelectedIndex(int index) {
115         return index >= index0 && index <= index1;
116     }
117     
118     public boolean isSelectionEmpty() {
119         return ((index0 == -1) && (index1 == -1));
120     }
121     
122     public void removeIndexInterval(int index0, int index1) {
123         index0 = -1; index1 = -1;
124     }
125     
126     public void removeListSelectionListener(swingwtx.swing.event.ListSelectionListener x) {
127         listeners.remove(x);
128     }
129     
130     public void removeSelectionInterval(int index0, int index1) {
131         index0 = -1; index1 = -1;
132     }
133     
134     public void setAnchorSelectionIndex(int index) {
135         index0 = index;
136         updateComponentForSelection();
137     }
138     
139     public void setLeadSelectionIndex(int index) {
140         index1 = index;
141         updateComponentForSelection();
142     }
143     
144     public void setSelectionInterval(int index0, int index1) {
145         this.index0 = index0; this.index1 = index1;
146         updateComponentForSelection();
147     }
148     
149     public void setSelectionMode(int selectionMode) {
150         mode = selectionMode;
151         if (isTable) {
152             table.setModelDirty(true);
153             table.refreshTable();
154         }
155     }
156     
157     public void setValueIsAdjusting(boolean valueIsAdjusting) {
158         isAdjusting = valueIsAdjusting;
159     }
160     
161     /**
162      * Sets the component to have the selection specified
163      * in this model.
164      */

165     protected void updateComponentForSelection() {
166         if (isTable) {
167             if (mode == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
168                 for (int i = index0; i <= index1; i++)
169                     table.changeSelection(i, 0, true, false);
170         }
171         else {
172             if (mode == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) {
173                 int s[] = new int[index1 - index0 + 1];
174                 for (int i = 0; i < s.length; i++)
175                     s[i] = index0 + 1;
176                 list.setSelectedIndices(s);
177             }
178         }
179     }
180     
181     /**
182      * Callback routine for components so they can update
183      * this model with their current selection as user
184      * interaction changes them.
185      *
186      * This does mean that if you make your own ListSelectionModel
187      * and don't extend this class, you're pretty much screwed :-)
188      */

189     void fireListSelectionEvent(Object JavaDoc source, int index0, int index1) {
190         
191         this.index0 = index0;
192         this.index1 = index1;
193         
194         // Drop out if no listeners
195
if (listeners.size() == 0) return;
196         
197         // Fire events
198
ListSelectionEvent e = new ListSelectionEvent(this, index0, index1, false);
199         for (int i = 0; i < listeners.size(); i++) {
200             ((ListSelectionListener) listeners.get(i)).valueChanged(e);
201         }
202     }
203     
204 }
205
Popular Tags