1 /***
2 * Copyright (c) 2003 held jointly by the individual authors.
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; with out even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * > http://www.gnu.org/copyleft/lesser.html
19 * > http://www.opensource.org/licenses/lgpl-license.php
20 */
21 package net.mlw.vlh.swing;
22
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import javax.swing.SwingUtilities;
30
31 import net.mlw.vlh.Errors;
32 import net.mlw.vlh.ErrorsException;
33 import net.mlw.vlh.ValueList;
34 import net.mlw.vlh.ValueListHandler;
35 import net.mlw.vlh.ValueListInfo;
36 import net.mlw.vlh.swing.support.TableSorter;
37
38 /***
39 * @author Matthew L. Wilson
40 * @version $Revision: 1.8 $ $Date: 2005/08/30 18:54:25 $
41 */
42 public class ValueListHelper implements ActionListener
43 {
44 public static final String ACTION_COMMAND_RESET = "reset";
45
46 public static final String ACTION_COMMAND_FIRST = "first";
47
48 public static final String ACTION_COMMAND_PREVIOUS = "previous";
49
50 public static final String ACTION_COMMAND_NEXT = "next";
51
52 public static final String ACTION_COMMAND_LAST = "last";
53
54 public static final String ACTION_COMMAND_SORT = "sort";
55
56 private boolean useInvokeLatter = false;
57
58 private int pagingPage = 1;
59
60 private ValueListHandler vlh;
61
62 private String name;
63
64 private ValueListTableModel tableModel;
65
66 private PagingComponent pagingComponent;
67
68 private TableSorter tableSorter;
69
70 private ValueListInfo info = new ValueListInfo();
71
72 private List filterRetrievers = new ArrayList();
73
74 private int pagingNumberPer = Integer.MAX_VALUE;
75
76 private ActionListener errorListener;
77
78 public ValueListHelper(ValueListHandler vlh, String name)
79 {
80 this(vlh, name, false);
81 }
82
83 public ValueListHelper(ValueListHandler vlh, String name, boolean useInvokeLatter)
84 {
85 this.vlh = vlh;
86 this.name = name;
87 this.useInvokeLatter = useInvokeLatter;
88 }
89
90 public void setPagingComponent(PagingComponent pagingComponent)
91 {
92 this.pagingComponent = pagingComponent;
93 pagingComponent.addActionListener(this);
94 }
95
96 public void setValueListTableModel(ValueListTableModel tableModel)
97 {
98 this.tableModel = tableModel;
99 }
100
101 protected void getNewValueList()
102 {
103 if (useInvokeLatter)
104 {
105 SwingUtilities.invokeLater(valueListRunnable);
106 }
107 else
108 {
109 valueListRunnable.run();
110 }
111 }
112
113 /***
114 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
115 */
116 public void actionPerformed(ActionEvent event)
117 {
118 if (event != null)
119 {
120 String command = event.getActionCommand();
121 if (command != null)
122 {
123 if (ACTION_COMMAND_RESET.equals(command))
124 {
125 for (Iterator iter = filterRetrievers.iterator(); iter.hasNext();)
126 {
127 FilterRetriever element = (FilterRetriever) iter.next();
128 element.reset();
129 }
130 return;
131 }
132 else if (ACTION_COMMAND_FIRST.equals(command))
133 {
134 info.setPagingPage(1);
135 }
136 else if (ACTION_COMMAND_PREVIOUS.equals(command))
137 {
138 info.setPagingPage(info.getPagingPage() - 1);
139 }
140 else if (ACTION_COMMAND_NEXT.equals(command))
141 {
142 info.setPagingPage(info.getPagingPage() + 1);
143 }
144 else if (ACTION_COMMAND_LAST.equals(command))
145 {
146 info.setPagingPage(info.getTotalNumberOfPages());
147 }
148 else if (ACTION_COMMAND_SORT.equals(command))
149 {
150 if (tableSorter != null)
151 {
152 info.resetSorting();
153
154 Iterator iter = tableSorter.getSortingColumns().iterator();
155 if (iter.hasNext())
156 {
157 TableSorter.Directive directive = (TableSorter.Directive) iter.next();
158 info.setPrimarySortColumn(tableModel.getSortPropertyName(directive.getColumn()));
159 info.setPrimarySortDirection(directive.getDirection() == 1 ? ValueListInfo.ASCENDING : ValueListInfo.DESCENDING);
160
161 info.getFilters().put(ValueListInfo.SORT_COLUMN + "1", tableModel.getSortPropertyName(directive.getColumn()));
162 info.getFilters().put(ValueListInfo.SORT_DIRECTION + "1", directive.getDirection() == 1 ? "asc" : "desc");
163 }
164
165 for (int i = 2; iter.hasNext(); i++)
166 {
167 TableSorter.Directive directive = (TableSorter.Directive) iter.next();
168 info.getFilters().put(ValueListInfo.SORT_COLUMN + i, tableModel.getSortPropertyName(directive.getColumn()));
169 info.getFilters().put(ValueListInfo.SORT_DIRECTION + i, directive.getDirection() == 1 ? "asc" : "desc");
170 }
171 }
172
173 }
174 else
175 {
176 Errors errors = new Errors();
177 for (Iterator iter = filterRetrievers.iterator(); iter.hasNext();)
178 {
179 FilterRetriever element = (FilterRetriever) iter.next();
180 String key = element.getFilterKey();
181 Object value = element.getFilterValue(errors);
182 info.getFilters().put(key, value);
183 }
184
185 if (errors.hasErrors())
186 {
187 if (errorListener != null)
188 {
189 errorListener.actionPerformed(new ActionEvent(errors, 0, "errors"));
190 return;
191 }
192 else
193 {
194 throw new ErrorsException(errors);
195 }
196 }
197
198 info.setPagingPage(1);
199 }
200
201 info.getFilters().put("command", command);
202 getNewValueList();
203 info.getFilters().remove("command");
204 }
205 }
206
207 }
208
209 public void addFilterRetriever(FilterRetriever filterRetriever)
210 {
211 filterRetrievers.add(filterRetriever);
212 }
213
214 /***
215 * @param tableSorter
216 * The tableSorter to set.
217 */
218 public void setTableSorter(TableSorter tableSorter)
219 {
220 tableSorter.addActionListener(this);
221 this.tableSorter = tableSorter;
222 }
223
224 /***
225 * @param pagingNumberPer The pagingNumberPer to set.
226 */
227 public void setPagingNumberPer(int pagingNumberPer)
228 {
229 info.setPagingNumberPer(pagingNumberPer);
230 }
231
232 protected Runnable valueListRunnable = new Runnable()
233 {
234 public synchronized void run()
235 {
236 try
237 {
238 ValueList valueList = vlh.getValueList(name, info);
239
240 if (pagingComponent != null)
241 {
242 pagingComponent.setPagingInfo(valueList.getValueListInfo());
243 }
244
245 tableModel.setValueList(valueList);
246 }
247 catch (Throwable e)
248 {
249 if (errorListener != null)
250 {
251 errorListener.actionPerformed(new ActionEvent(e, 0, "error"));
252 }
253 else
254 {
255 e.printStackTrace();
256 }
257 }
258 }
259 };
260
261 /***
262 * @param errorListener The errorListener to set.
263 */
264 public void setErrorListener(ActionListener errorListener)
265 {
266 this.errorListener = errorListener;
267 }
268 }