1 /***
2 * Copyright (C) 2006 Philipp Mpalampanis
3 *
4 * License: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Alternatively, the contents of this file may be used under the terms of
17 * either the GNU General Public License Version 2 or later (the "GPL"), or
18 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19 * in which case the provisions of the GPL or the LGPL are applicable instead
20 * of those above. If you wish to allow use of your version of this file only
21 * under the terms of either the GPL or the LGPL, and not to allow others to
22 * use your version of this file under the terms of the MPL, indicate your
23 * decision by deleting the provisions above and replace them with the notice
24 * and other provisions required by the GPL or the LGPL. If you do not delete
25 * the provisions above, a recipient may use your version of this file under
26 * the terms of any one of the MPL, the GPL or the LGPL.
27 */
28
29 package net.sf.echobinding.table;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import net.sf.echobinding.binding.BindingContext;
35 import net.sf.echobinding.event.SelectionChangeListener;
36 import nextapp.echo2.app.Color;
37 import nextapp.echo2.app.list.ListSelectionModel;
38
39 /***
40 * A data bound table with selectable rows. On selection a
41 * <code>SelectionChangeEvent</code> is send to all registered
42 * <code>SelectionChangeListener</code>. The <code>BindingContext</code>
43 * for the selected row can be retrieved through this event.
44 *
45 */
46 public class SelectableTable<T> extends BoundTable<T> implements
47 TableSelectionListener<T> {
48
49 private static final long serialVersionUID = 887162060539185694L;
50
51 private List<RowSelectionListener> _selectionListener = new ArrayList<RowSelectionListener>();
52
53 private T _selectedItem;
54
55 /***
56 * Creates a new <code>SelectableTable</code> from a list and a binding
57 * context.
58 *
59 * @param list
60 * @param ctx
61 */
62 public SelectableTable(List<T> list, BindingContext ctx) {
63 super(list, ctx);
64 }
65
66 /***
67 * Creates a new <code>SelectableTable</code> from a data bound list and a
68 * binding context.
69 *
70 * @param listBindingId
71 * @param ctx
72 */
73 public SelectableTable(String listBindingId, BindingContext ctx) {
74 super(listBindingId, ctx);
75
76 }
77
78 @Override
79 protected void initialize() {
80 super.initialize();
81
82 ListSelectionModel selectionModel = getSelectionModel();
83 selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
84 selectionModel.addChangeListener(new SelectionChangeListener(this));
85 setSelectionModel(selectionModel);
86 setSelectionBackground(Color.CYAN);
87 setSelectionEnabled(true);
88
89 setRolloverBackground(Color.GREEN);
90 setRolloverEnabled(true);
91
92
93
94 addActionListener(this);
95 }
96
97
98
99
100
101
102 public void rowIndexChanged(int rowIndex) {
103 setSelectedItem(getItem(rowIndex));
104 fireRowSelectionChanged(rowIndex);
105 }
106
107 /***
108 * Returns the currently selected item.
109 *
110 * @return the selected item
111 */
112 public T getSelectedItem() {
113 return _selectedItem;
114 }
115
116 /***
117 * Sets the selected item.
118 *
119 */
120 protected void setSelectedItem(T selectedItem) {
121 _selectedItem = selectedItem;
122 }
123
124 public void addRowSelectionListener(RowSelectionListener listener) {
125 _selectionListener.add(listener);
126 }
127
128 private void fireRowSelectionChanged(int row) {
129 for (RowSelectionListener listener : _selectionListener) {
130 listener.rowSelectionChanged(getBindingContext(row), row);
131 }
132 }
133
134 }