KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > base > FilterPanel


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.specifier.base;
31
32 import java.awt.Component JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.event.KeyEvent JavaDoc;
35 import java.math.BigDecimal JavaDoc;
36 import java.text.SimpleDateFormat JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 import javax.swing.DefaultComboBoxModel JavaDoc;
42 import javax.swing.JComboBox JavaDoc;
43 import javax.swing.JComponent JavaDoc;
44 import javax.swing.JLabel JavaDoc;
45 import javax.swing.JPanel JavaDoc;
46 import javax.swing.JTextField JavaDoc;
47
48 import com.genimen.djeneric.language.Messages;
49 import com.genimen.djeneric.repository.DjDomain;
50 import com.genimen.djeneric.repository.DjDomainValue;
51 import com.genimen.djeneric.repository.DjExtent;
52 import com.genimen.djeneric.repository.DjOql;
53 import com.genimen.djeneric.repository.DjProperty;
54 import com.genimen.djeneric.repository.DjSession;
55 import com.genimen.djeneric.repository.exceptions.DjenericException;
56 import com.genimen.djeneric.tools.specifier.components.DjChooserField;
57 import com.genimen.djeneric.ui.DjFocusMeLater;
58 import com.genimen.djeneric.ui.DjFormLayoutManager;
59 import com.genimen.djeneric.util.DjLogger;
60
61 public class FilterPanel extends JPanel JavaDoc
62 {
63   private static final long serialVersionUID = 1L;
64   DjFormLayoutManager formLayout = new DjFormLayoutManager();
65   JComponent JavaDoc _firstField = null;
66
67   ArrayList JavaDoc _components = new ArrayList JavaDoc();
68   ArrayList JavaDoc _properties = new ArrayList JavaDoc();
69   ArrayList JavaDoc _lovs = new ArrayList JavaDoc();
70   DjSession _session;
71   ArrayList JavaDoc _excludeTheseProperties = new ArrayList JavaDoc();
72   DjDomainValue _nullValue;
73   HashMap JavaDoc _queryProperties = new HashMap JavaDoc();
74   DjExtent _extent;
75   DjOql _restrictingOql;
76   String JavaDoc _additionalFilter = null;
77   HashMap JavaDoc _queryOperators;
78
79   public FilterPanel(DjOql restrictingOql, DjSession session, DjExtent extent, ArrayList JavaDoc excludeTheseProperties)
80   {
81     try
82     {
83       _restrictingOql = restrictingOql;
84       if (excludeTheseProperties != null) _excludeTheseProperties = excludeTheseProperties;
85       _extent = extent;
86       _session = session;
87       _nullValue = new DjDomainValue(null, "", "");
88       addProperties(extent);
89     }
90     catch (Exception JavaDoc ex)
91     {
92       DjLogger.log(ex);
93     }
94   }
95
96   public void requestFocus()
97   {
98     if (_components.size() > 0)
99     {
100       JComponent JavaDoc comp = (JComponent JavaDoc) _components.get(0);
101       comp.requestFocus();
102     }
103   }
104
105   void addProperties(DjExtent extent) throws DjenericException
106   {
107     setLayout(formLayout);
108
109     DjProperty[] props = extent.getProperties();
110
111     for (int i = 0; i < props.length; i++)
112     {
113       DjProperty prop = props[i];
114       if (_excludeTheseProperties.contains(props[i].getName()) || !props[i].isQueryable()) continue;
115
116       JLabel JavaDoc label = new JLabel JavaDoc(prop.getPrompt());
117
118       JComponent JavaDoc comp = null;
119       int compType = DjDomain.COMP_TEXTFIELD;
120       int dspWidth = 200;
121       int dspHeight = 0;
122
123       if (prop.getType() instanceof DjExtent)
124       {
125         DjExtent xt = (DjExtent) prop.getType();
126         if (xt.isLarge()) compType = DjDomain.COMP_CHOOSER;
127         else compType = DjDomain.COMP_COMBOBOX;
128       }
129       else if (prop.getType() instanceof DjDomain)
130       {
131         DjDomain dom = (DjDomain) prop.getType();
132         compType = dom.getComponentType();
133         dspWidth = dom.getDisplayWidth();
134         dspHeight = dom.getDisplayHeight();
135       }
136       else throw new DjenericException(Messages.getString("FilterPanel.UnsupportedPropertyType", prop.getType()
137           .getClass().getName()));
138
139       if (DjDomain.isMemoComponent(compType))
140       {
141         continue;
142         // Skip memos; these will get a JTabbedPane of their own
143
}
144
145       DjDomainValue[] lov = prop.getValidValues(_session);
146       if (lov.length > 0)
147       {
148         DjDomainValue[] nlov = new DjDomainValue[lov.length + 1];
149         nlov[0] = _nullValue;
150         System.arraycopy(lov, 0, nlov, 1, lov.length);
151         lov = nlov;
152       }
153
154       if (compType == DjDomain.COMP_CHECKBOX || compType == DjDomain.COMP_COMBOBOX)
155       {
156         comp = new JComboBox JavaDoc();
157         comp.setPreferredSize(new Dimension JavaDoc(comp.getWidth(), 21));
158         ((JComboBox JavaDoc) comp).setEditable(true);
159         DefaultComboBoxModel JavaDoc model = new DefaultComboBoxModel JavaDoc(lov);
160         ((JComboBox JavaDoc) comp).setModel(model);
161         if (compType == DjDomain.COMP_CHECKBOX) dspWidth = 150;
162       }
163       else if (compType == DjDomain.COMP_CHOOSER)
164       {
165         DjChooserField chooser = new DjChooserField(_session, (DjExtent) prop.getType());
166         chooser.setDialogTitle(Messages.getString("global.Choose", prop.getPrompt()));
167         chooser.setRequired(false);
168         comp = chooser;
169       }
170       else
171       {
172         comp = new JTextField JavaDoc();
173       }
174
175       String JavaDoc tooltip = prop.getDescription();
176       if (tooltip != null && tooltip.trim().length() > 0)
177       {
178         comp.setToolTipText(tooltip);
179         label.setToolTipText(tooltip);
180       }
181
182       DjFocusMeLater.getNeededComponent(comp).addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
183       {
184         public void keyPressed(KeyEvent JavaDoc e)
185         {
186           handleKeyPressed(e);
187         }
188       });
189
190       _components.add(comp);
191       _properties.add(prop);
192       _lovs.add(lov);
193
194       if (_firstField == null)
195       {
196         _firstField = comp;
197       }
198       add(label, DjFormLayoutManager.LABEL);
199       add(comp, DjFormLayoutManager.FIELD);
200
201       int w = dspWidth;
202       if (w <= 1) w = 100;
203
204       int h = dspHeight;
205       if (h <= 1) h = (int) comp.getPreferredSize().getHeight();
206       if (h <= 1) h = 21;
207
208       comp.setPreferredSize(new Dimension JavaDoc(w, h));
209     }
210   }
211
212   public void validateValues() throws DjenericException
213   {
214     _queryProperties = new HashMap JavaDoc();
215     _queryOperators = new HashMap JavaDoc();
216
217     for (int i = 0; i < _components.size(); i++)
218     {
219       JComponent JavaDoc comp = (JComponent JavaDoc) _components.get(i);
220       DjProperty prop = (DjProperty) _properties.get(i);
221       DjDomainValue[] lov = (DjDomainValue[]) _lovs.get(i);
222
223       boolean enforce;
224
225       if (prop.getType() instanceof DjDomain && lov.length > 0)
226       {
227         enforce = ((DjDomain) prop.getType()).isEnforced();
228       }
229       else enforce = (prop.getType() instanceof DjExtent);
230
231       String JavaDoc value = null;
232       if (comp instanceof JTextField JavaDoc) value = ((JTextField JavaDoc) comp).getText();
233       else if (comp instanceof JComboBox JavaDoc)
234       {
235         Component JavaDoc c = ((JComboBox JavaDoc) comp).getEditor().getEditorComponent();
236         if (c instanceof JTextField JavaDoc) value = ((JTextField JavaDoc) c).getText();
237       }
238       else if (comp instanceof DjChooserField)
239       {
240         DjChooserField chooser = (DjChooserField) comp;
241         Object JavaDoc v = chooser.getDisplayedValue();
242         if (v != null) _queryProperties.put(prop.getName(), chooser.getDisplayedValue());
243
244         continue;
245       }
246
247       if (value == null) throw new DjenericException(Messages.getString("FilterPanel.UnsupportedComponent", comp
248           .getClass().getName()));
249
250       if (value != null && value.equals("")) value = null;
251       else
252       {
253         String JavaDoc operator = fetchOperator(value);
254         if (operator != null)
255         {
256           value = value.substring(operator.length()).trim();
257           operator = operator.trim();
258         }
259         _queryOperators.put(prop.getName(), operator);
260
261       }
262
263       if (value != null)
264       {
265         if (enforce)
266         {
267           DjDomainValue matched = DjDomain.looselyMatchDescription(lov, value);
268           if (matched == null)
269           {
270             comp.requestFocus();
271             throw new DjenericException(Messages.getString("FilterPanel.InvalidValueFor", prop.getPrompt()));
272           }
273           Object JavaDoc translatedValue = matched.getValue();
274
275           _queryProperties.put(prop.getName(), translatedValue);
276         }
277         else
278         {
279           if (prop.getType().getTypeCode() == DjDomain.DATE_TYPE)
280           {
281             DjDomain dom = (DjDomain) prop.getType();
282             String JavaDoc mask = dom.getFormatMask();
283             if (mask == null || mask.trim().length() == 0) mask = Messages.getString("global.DateFormatMask");
284             try
285             {
286               SimpleDateFormat JavaDoc sf = new SimpleDateFormat JavaDoc(mask);
287               _queryProperties.put(prop.getName(), sf.parse(value));
288             }
289             catch (Exception JavaDoc x)
290             {
291               comp.requestFocus();
292               throw new DjenericException(Messages.getString("FilterPanel.InvalidValueForMask", prop.getPrompt(), mask));
293             }
294           }
295           else if (prop.getType().getTypeCode() == DjDomain.BIGDECIMAL_TYPE)
296           {
297             try
298             {
299               _queryProperties.put(prop.getName(), new BigDecimal JavaDoc(value));
300             }
301             catch (Exception JavaDoc x)
302             {
303               comp.requestFocus();
304               throw new DjenericException(Messages.getString("FilterPanel.InvalidValueFor", prop.getPrompt()));
305             }
306           }
307           else if (prop.getType().getTypeCode() == DjDomain.LONG_TYPE
308                    || prop.getType().getTypeCode() == DjDomain.INT_TYPE)
309           {
310             try
311             {
312               _queryProperties.put(prop.getName(), new Long JavaDoc(value));
313             }
314             catch (Exception JavaDoc x)
315             {
316               comp.requestFocus();
317               throw new DjenericException(Messages.getString("FilterPanel.InvalidValueFor", prop.getPrompt()));
318             }
319           }
320           else _queryProperties.put(prop.getName(), value);
321
322         }
323       }
324     }
325   }
326
327   private String JavaDoc fetchOperator(String JavaDoc value)
328   {
329     if (value.startsWith(">=")) return ">=";
330     if (value.startsWith("<=")) return "<=";
331     if (value.startsWith("!=")) return "!=";
332     if (value.startsWith("<>")) return "!=";
333     if (value.startsWith("<")) return "<";
334     if (value.startsWith(">")) return ">";
335     return null;
336   }
337
338   private HashMap JavaDoc getQueryProperties()
339   {
340     return _queryProperties;
341   }
342
343   void handleKeyPressed(KeyEvent JavaDoc e)
344   {
345     if (e.getSource() instanceof JTextField JavaDoc)
346     {
347       JTextField JavaDoc tf = (JTextField JavaDoc) e.getSource();
348       if (tf.getParent() instanceof JComboBox JavaDoc)
349       {
350         JComboBox JavaDoc cbb = (JComboBox JavaDoc) tf.getParent();
351         if (cbb.isPopupVisible()) return;
352       }
353     }
354
355     if (e.getKeyCode() == KeyEvent.VK_ENTER && e.getModifiers() == 0) enterHit();
356     if (e.getKeyCode() == KeyEvent.VK_ENTER && (e.getModifiers() & KeyEvent.CTRL_MASK) != 0) acceptHit();
357     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) escapeHit();
358   }
359
360   protected void enterHit()
361   {
362   }
363
364   protected void acceptHit()
365   {
366   }
367
368   protected void escapeHit()
369   {
370   }
371
372   public DjProperty[] getQueryableProperties()
373   {
374     return (DjProperty[]) _properties.toArray(new DjProperty[0]);
375   }
376
377   public DjOql getOql() throws DjenericException
378   {
379     DjOql oql;
380
381     if (_restrictingOql == null) oql = new DjOql(_extent);
382     else oql = _restrictingOql;
383
384     StringBuffer JavaDoc result = new StringBuffer JavaDoc(1000);
385     validateValues();
386
387     HashMap JavaDoc qryProps = getQueryProperties();
388
389     Iterator JavaDoc keys = qryProps.keySet().iterator();
390     while (keys.hasNext())
391     {
392       String JavaDoc key = keys.next().toString();
393       result.append(key);
394
395       Object JavaDoc value = qryProps.get(key);
396
397       boolean like = false;
398       if (_extent.getProperty(key).getTypeCode() == DjDomain.STRING_TYPE && value != null)
399       {
400         String JavaDoc v = value.toString();
401         like = (v.indexOf("%") != -1);
402       }
403
404       if (like) result.append(" like ");
405       else
406       {
407         String JavaDoc oper = (String JavaDoc) _queryOperators.get(key);
408         if (oper == null) oper = "==";
409         result.append(" ");
410         result.append(oper);
411         result.append(" ");
412       }
413
414       result.append(" :" + key);
415       oql.setParameter(key, value);
416       if (keys.hasNext()) result.append(" and ");
417     }
418     String JavaDoc expr = result.toString();
419     if (_additionalFilter != null)
420     {
421       if (expr.length() > 0) expr = "(" + expr + ") and (" + _additionalFilter + ")";
422       else expr = _additionalFilter;
423     }
424     oql.setWhereExpression(expr);
425     return oql;
426   }
427
428   public void setAdditionalFilter(String JavaDoc oqlExpression)
429   {
430     _additionalFilter = oqlExpression;
431   }
432
433 }
Popular Tags