1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivegui.table; |
16 | |
17 | import java.util.Arrays; |
18 | |
19 | import javax.swing.event.ListSelectionEvent; |
20 | import javax.swing.event.ListSelectionListener; |
21 | |
22 | import net.sourceforge.hiveevents.Channel; |
23 | import net.sourceforge.hiveevents.ChannelImpl; |
24 | import net.sourceforge.hiveevents.Consumer; |
25 | |
26 | /** |
27 | * @author Jean-Francois Poilpret |
28 | */ |
29 | public class TableSelectionHandler<T> |
30 | { |
31 | public TableSelectionHandler(BeanTable<T> table, TableContribution contrib) |
32 | { |
33 | _table = table; |
34 | _beanClass = _table.getDataListModel().getRowClass(); |
35 | _selectionMode = contrib.getSelectionMode(); |
36 | _table.setSelectionMode(_selectionMode.value()); |
37 | _selection = contrib.getSelectionEvent(); |
38 | if (_selection != null) |
39 | { |
40 | initSelectionChannel(); |
41 | } |
42 | } |
43 | |
44 | @SuppressWarnings("unchecked") |
45 | public void addSelectionConsumer(int priority, Consumer consumer) |
46 | { |
47 | createSelectionChannel(); |
48 | _selection.registerPushConsumer(priority, consumer); |
49 | } |
50 | |
51 | @SuppressWarnings("unchecked") |
52 | private void createSelectionChannel() |
53 | { |
54 | if (_selection == null) |
55 | { |
56 | _selection = new ChannelImpl( "BeanTable.SelectionChannel", |
57 | Integer.MAX_VALUE, |
58 | _beanClass, |
59 | false); |
60 | initSelectionChannel(); |
61 | } |
62 | } |
63 | |
64 | private void initSelectionChannel() |
65 | { |
66 | if (_selection != null) |
67 | { |
68 | ListSelectionListener listener = null; |
69 | switch (_selectionMode) |
70 | { |
71 | case Single: |
72 | listener = new SingleSelectionListener(); |
73 | break; |
74 | |
75 | case Interval: |
76 | case Multiple: |
77 | listener = new MultipleSelectionListener(); |
78 | break; |
79 | |
80 | default: |
81 | // No other possible value |
82 | break; |
83 | } |
84 | _table.getSelectionModel().addListSelectionListener(listener); |
85 | } |
86 | } |
87 | |
88 | private class SingleSelectionListener implements ListSelectionListener |
89 | { |
90 | @SuppressWarnings("unchecked") |
91 | public void valueChanged(ListSelectionEvent e) |
92 | { |
93 | int selected = _table.getSelectedRow(); |
94 | if (selected != _lastSelection) |
95 | { |
96 | Object row = null; |
97 | if (selected > -1) |
98 | { |
99 | row = _table.getDataListModel().getRow(selected); |
100 | } |
101 | _lastSelection = selected; |
102 | _selection.push(row); |
103 | } |
104 | } |
105 | |
106 | private int _lastSelection = -1; |
107 | } |
108 | |
109 | private class MultipleSelectionListener implements ListSelectionListener |
110 | { |
111 | @SuppressWarnings("unchecked") |
112 | public void valueChanged(ListSelectionEvent e) |
113 | { |
114 | int[] selected = _table.getSelectedRows(); |
115 | if (!Arrays.equals(selected, _lastSelection)) |
116 | { |
117 | Object[] rows = EMPTY; |
118 | if (selected.length > 0) |
119 | { |
120 | DataListModel<T> model = _table.getDataListModel(); |
121 | rows = new Object[selected.length]; |
122 | for (int i = 0; i < selected.length; i++) |
123 | { |
124 | rows[i] = model.getRow(selected[i]); |
125 | } |
126 | } |
127 | _lastSelection = selected; |
128 | _selection.push(rows); |
129 | } |
130 | } |
131 | |
132 | private int[] _lastSelection = new int[0]; |
133 | } |
134 | |
135 | private Channel _selection; |
136 | private final Class<T> _beanClass; |
137 | private final SelectionMode _selectionMode; |
138 | private final BeanTable<T> _table; |
139 | |
140 | static final private Object[] EMPTY = new Object[0]; |
141 | } |