1 30 package com.genimen.djeneric.tools.specifier.base; 31 32 import java.awt.Component ; 33 import java.awt.Dimension ; 34 import java.awt.event.KeyEvent ; 35 import java.math.BigDecimal ; 36 import java.text.SimpleDateFormat ; 37 import java.util.ArrayList ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 41 import javax.swing.DefaultComboBoxModel ; 42 import javax.swing.JComboBox ; 43 import javax.swing.JComponent ; 44 import javax.swing.JLabel ; 45 import javax.swing.JPanel ; 46 import javax.swing.JTextField ; 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 62 { 63 private static final long serialVersionUID = 1L; 64 DjFormLayoutManager formLayout = new DjFormLayoutManager(); 65 JComponent _firstField = null; 66 67 ArrayList _components = new ArrayList (); 68 ArrayList _properties = new ArrayList (); 69 ArrayList _lovs = new ArrayList (); 70 DjSession _session; 71 ArrayList _excludeTheseProperties = new ArrayList (); 72 DjDomainValue _nullValue; 73 HashMap _queryProperties = new HashMap (); 74 DjExtent _extent; 75 DjOql _restrictingOql; 76 String _additionalFilter = null; 77 HashMap _queryOperators; 78 79 public FilterPanel(DjOql restrictingOql, DjSession session, DjExtent extent, ArrayList 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 ex) 91 { 92 DjLogger.log(ex); 93 } 94 } 95 96 public void requestFocus() 97 { 98 if (_components.size() > 0) 99 { 100 JComponent comp = (JComponent ) _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 label = new JLabel (prop.getPrompt()); 117 118 JComponent 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 } 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 (); 157 comp.setPreferredSize(new Dimension (comp.getWidth(), 21)); 158 ((JComboBox ) comp).setEditable(true); 159 DefaultComboBoxModel model = new DefaultComboBoxModel (lov); 160 ((JComboBox ) 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 (); 173 } 174 175 String 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 () 183 { 184 public void keyPressed(KeyEvent 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 (w, h)); 209 } 210 } 211 212 public void validateValues() throws DjenericException 213 { 214 _queryProperties = new HashMap (); 215 _queryOperators = new HashMap (); 216 217 for (int i = 0; i < _components.size(); i++) 218 { 219 JComponent comp = (JComponent ) _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 value = null; 232 if (comp instanceof JTextField ) value = ((JTextField ) comp).getText(); 233 else if (comp instanceof JComboBox ) 234 { 235 Component c = ((JComboBox ) comp).getEditor().getEditorComponent(); 236 if (c instanceof JTextField ) value = ((JTextField ) c).getText(); 237 } 238 else if (comp instanceof DjChooserField) 239 { 240 DjChooserField chooser = (DjChooserField) comp; 241 Object 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 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 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 mask = dom.getFormatMask(); 283 if (mask == null || mask.trim().length() == 0) mask = Messages.getString("global.DateFormatMask"); 284 try 285 { 286 SimpleDateFormat sf = new SimpleDateFormat (mask); 287 _queryProperties.put(prop.getName(), sf.parse(value)); 288 } 289 catch (Exception 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 (value)); 300 } 301 catch (Exception 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 (value)); 313 } 314 catch (Exception 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 fetchOperator(String 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 getQueryProperties() 339 { 340 return _queryProperties; 341 } 342 343 void handleKeyPressed(KeyEvent e) 344 { 345 if (e.getSource() instanceof JTextField ) 346 { 347 JTextField tf = (JTextField ) e.getSource(); 348 if (tf.getParent() instanceof JComboBox ) 349 { 350 JComboBox cbb = (JComboBox ) 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 result = new StringBuffer (1000); 385 validateValues(); 386 387 HashMap qryProps = getQueryProperties(); 388 389 Iterator keys = qryProps.keySet().iterator(); 390 while (keys.hasNext()) 391 { 392 String key = keys.next().toString(); 393 result.append(key); 394 395 Object value = qryProps.get(key); 396 397 boolean like = false; 398 if (_extent.getProperty(key).getTypeCode() == DjDomain.STRING_TYPE && value != null) 399 { 400 String v = value.toString(); 401 like = (v.indexOf("%") != -1); 402 } 403 404 if (like) result.append(" like "); 405 else 406 { 407 String oper = (String ) _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 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 oqlExpression) 429 { 430 _additionalFilter = oqlExpression; 431 } 432 433 } | Popular Tags |