KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > search > Find


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps.search;
15
16 import java.awt.BorderLayout JavaDoc;
17 import java.awt.Component JavaDoc;
18 import java.awt.Dimension JavaDoc;
19 import java.awt.Frame JavaDoc;
20 import java.awt.GridBagConstraints JavaDoc;
21 import java.awt.GridBagLayout JavaDoc;
22 import java.awt.GridLayout JavaDoc;
23 import java.awt.Insets JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.math.BigDecimal JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Statement JavaDoc;
31 import java.sql.Timestamp JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import javax.swing.Box JavaDoc;
37 import javax.swing.DefaultCellEditor JavaDoc;
38 import javax.swing.ImageIcon JavaDoc;
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JDialog JavaDoc;
41 import javax.swing.JLabel JavaDoc;
42 import javax.swing.JScrollPane JavaDoc;
43 import javax.swing.JTable JavaDoc;
44 import javax.swing.JTextField JavaDoc;
45 import javax.swing.JToolBar JavaDoc;
46 import javax.swing.event.ChangeEvent JavaDoc;
47 import javax.swing.event.ChangeListener JavaDoc;
48 import javax.swing.table.DefaultTableModel JavaDoc;
49 import javax.swing.table.TableColumn JavaDoc;
50
51 import org.compiere.apps.ADialog;
52 import org.compiere.apps.AEnv;
53 import org.compiere.apps.ConfirmPanel;
54 import org.compiere.apps.StatusBar;
55 import org.compiere.grid.ed.VEditor;
56 import org.compiere.grid.ed.VEditorFactory;
57 import org.compiere.model.DataStatusEvent;
58 import org.compiere.model.DataStatusListener;
59 import org.compiere.model.MField;
60 import org.compiere.model.MQuery;
61 import org.compiere.model.MTab;
62 import org.compiere.model.MRole;
63 import org.compiere.plaf.CompiereColor;
64 import org.compiere.swing.CComboBox;
65 import org.compiere.swing.CLabel;
66 import org.compiere.swing.CPanel;
67 import org.compiere.swing.CTabbedPane;
68 import org.compiere.swing.CTable;
69 import org.compiere.util.Access;
70 import org.compiere.util.DB;
71 import org.compiere.util.DisplayType;
72 import org.compiere.util.Env;
73 import org.compiere.util.Log;
74 import org.compiere.util.Msg;
75 import org.compiere.util.ValueNamePair;
76
77 /**
78  * Find Records.
79  * Based on AD_Find for persistency, query is build to restrict info
80  *
81  * @author Jorg Janke
82  * @version $Id: Find.java,v 1.16 2003/10/22 05:48:17 jjanke Exp $
83  */

84 public final class Find extends JDialog JavaDoc
85         implements ActionListener JavaDoc, ChangeListener JavaDoc, DataStatusListener
86 {
87     /**
88      * Constructor
89      * @param owner Frame Dialog Onwer
90      * @param targetWindowNo WindowNo of target window
91      * @param targetTab Tab Model
92      */

93     public Find (Frame JavaDoc owner, int targetWindowNo, MTab targetTab)
94     {
95         super(owner, Msg.getMsg(Env.getCtx(), "Find") + ": " + targetTab.getName(), true);
96         Log.trace(Log.l3_Util, "Find", targetTab.getName());
97         //
98
m_targetWindowNo = targetWindowNo;
99         m_query = targetTab.getQuery();
100         m_targetTab = targetTab;
101         setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
102         // Required for Column Validation
103
Env.setContext(Env.getCtx(), m_targetWindowNo, "Find_Table_ID", targetTab.getAD_Table_ID());
104         // Context for Advanced Search Grid is WINDOW_FIND
105
Env.setContext(Env.getCtx(), Env.WINDOW_FIND, "Find_Table_ID", targetTab.getAD_Table_ID());
106         //
107
try
108         {
109             CompiereColor.setBackground(this);
110             jbInit();
111             initFind();
112         }
113         catch(Exception JavaDoc e)
114         {
115             Log.error("Find", e);
116         }
117         //
118
this.getRootPane().setDefaultButton(confirmPanelS.getOKButton());
119         AEnv.showCenterWindow(owner, this);
120     } // Find
121

122     /** Target Window No */
123     private int m_targetWindowNo;
124     /** Target Window MTab */
125     private MTab m_targetTab;
126     /** Resulting query */
127     private MQuery m_query = null;
128
129
130     private int m_total;
131     private PreparedStatement JavaDoc m_pstmt;
132     //
133
private boolean hasValue = false;
134     private boolean hasDocNo = false;
135     private boolean hasName = false;
136     private boolean hasDescription = false;
137     /** Line in Simple Content */
138     private int m_sLine = 6;
139     /** List of VEditors */
140     private ArrayList JavaDoc m_sEditors = new ArrayList JavaDoc();
141
142     /** Target Fields with AD_Column_ID as key */
143     private Hashtable JavaDoc m_targetFields = new Hashtable JavaDoc();
144
145     //
146
public static final int TABNO = 99; // for GridController
147
//
148
private CPanel southPanel = new CPanel();
149     private BorderLayout JavaDoc southLayout = new BorderLayout JavaDoc();
150     private StatusBar statusBar = new StatusBar();
151     private CTabbedPane tabbedPane = new CTabbedPane();
152     private CPanel advancedPanel = new CPanel();
153     private BorderLayout JavaDoc advancedLayout = new BorderLayout JavaDoc();
154     private ConfirmPanel confirmPanelA = new ConfirmPanel(true, true, false, false, false, false, true);
155     private JButton JavaDoc bIgnore = new JButton JavaDoc();
156     private JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
157     private JButton JavaDoc bSave = new JButton JavaDoc();
158     private JButton JavaDoc bNew = new JButton JavaDoc();
159     private JButton JavaDoc bDelete = new JButton JavaDoc();
160     private GridLayout JavaDoc gridLayout1 = new GridLayout JavaDoc();
161     private ConfirmPanel confirmPanelS = new ConfirmPanel(true);
162     private BorderLayout JavaDoc simpleLayout = new BorderLayout JavaDoc();
163     private CPanel scontentPanel = new CPanel();
164     private GridBagLayout JavaDoc scontentLayout = new GridBagLayout JavaDoc();
165     private CPanel simplePanel = new CPanel();
166     private JLabel JavaDoc valueLabel = new JLabel JavaDoc();
167     private JLabel JavaDoc nameLabel = new JLabel JavaDoc();
168     private JLabel JavaDoc descriptionLabel = new JLabel JavaDoc();
169     private JTextField JavaDoc valueField = new JTextField JavaDoc();
170     private JTextField JavaDoc nameField = new JTextField JavaDoc();
171     private JTextField JavaDoc descriptionField = new JTextField JavaDoc();
172     private JLabel JavaDoc docNoLabel = new JLabel JavaDoc();
173     private JTextField JavaDoc docNoField = new JTextField JavaDoc();
174     private Component JavaDoc spaceE;
175     private Component JavaDoc spaceN;
176     private Component JavaDoc spaceW;
177     private Component JavaDoc spaceS;
178     private JScrollPane JavaDoc advancedScrollPane = new JScrollPane JavaDoc();
179     private CTable advancedTable = new CTable();
180
181
182     public static final int INDEX_COLUMNNAME = 0;
183     public static final int INDEX_OPERATOR = 1;
184     public static final int INDEX_VALUE = 2;
185     public static final int INDEX_VALUE2 = 3;
186
187
188     /**
189      * Static Init.
190      * <pre>
191      * tabbedPane
192      * simplePanel
193      * scontentPanel
194      * confirmPanelS
195      * advancedPanel
196      * toolBar
197      * GC
198      * confirmPanelA
199      * southPanel
200      * statusBar
201      * </pre>
202      * @throws Exception
203      */

204     private void jbInit() throws Exception JavaDoc
205     {
206         spaceE = Box.createHorizontalStrut(8);
207         spaceN = Box.createVerticalStrut(8);
208         spaceW = Box.createHorizontalStrut(8);
209         spaceS = Box.createVerticalStrut(8);
210         bIgnore.setIcon(new ImageIcon JavaDoc(org.compiere.Compiere.class.getResource("images/Ignore24.gif")));
211         bIgnore.setMargin(new Insets JavaDoc(2, 2, 2, 2));
212         bIgnore.setToolTipText(Msg.getMsg(Env.getCtx(),"New"));
213         bIgnore.addActionListener(this);
214         bSave.setIcon(new ImageIcon JavaDoc(org.compiere.Compiere.class.getResource("images/Save24.gif")));
215         bSave.setMargin(new Insets JavaDoc(2, 2, 2, 2));
216         bSave.setToolTipText(Msg.getMsg(Env.getCtx(),"Save"));
217         bSave.addActionListener(this);
218         bNew.setIcon(new ImageIcon JavaDoc(org.compiere.Compiere.class.getResource("images/New24.gif")));
219         bNew.setMargin(new Insets JavaDoc(2, 2, 2, 2));
220         bNew.setToolTipText(Msg.getMsg(Env.getCtx(),"New"));
221         bNew.addActionListener(this);
222         bDelete.setIcon(new ImageIcon JavaDoc(org.compiere.Compiere.class.getResource("images/Delete24.gif")));
223         bDelete.setMargin(new Insets JavaDoc(2, 2, 2, 2));
224         bDelete.setToolTipText(Msg.getMsg(Env.getCtx(),"Delete"));
225         bDelete.addActionListener(this);
226         //
227
southPanel.setLayout(southLayout);
228         valueLabel.setLabelFor(valueField);
229         valueLabel.setText(Msg.translate(Env.getCtx(),"Value"));
230         nameLabel.setLabelFor(nameField);
231         nameLabel.setText(Msg.translate(Env.getCtx(),"Name"));
232         descriptionLabel.setLabelFor(descriptionField);
233         descriptionLabel.setText(Msg.translate(Env.getCtx(),"Description"));
234         valueField.setText("%");
235         valueField.setColumns(20);
236         nameField.setText("%");
237         nameField.setColumns(20);
238         descriptionField.setText("%");
239         descriptionField.setColumns(20);
240         scontentPanel.setToolTipText(Msg.getMsg(Env.getCtx(),"FindTip"));
241         docNoLabel.setLabelFor(docNoField);
242         docNoLabel.setText(Msg.translate(Env.getCtx(),"DocumentNo"));
243         docNoField.setText("%");
244         docNoField.setColumns(20);
245         advancedScrollPane.setPreferredSize(new Dimension JavaDoc(450, 150));
246         southPanel.add(statusBar, BorderLayout.SOUTH);
247         this.getContentPane().add(southPanel, BorderLayout.SOUTH);
248         //
249
scontentPanel.setLayout(scontentLayout);
250         simplePanel.setLayout(simpleLayout);
251         simplePanel.add(confirmPanelS, BorderLayout.SOUTH);
252         simplePanel.add(scontentPanel, BorderLayout.CENTER);
253         scontentPanel.add(valueLabel, new GridBagConstraints JavaDoc(1, 1, 1, 1, 0.0, 0.0
254             ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets JavaDoc(7, 5, 0, 5), 0, 0));
255         scontentPanel.add(nameLabel, new GridBagConstraints JavaDoc(1, 3, 1, 1, 0.0, 0.0
256             ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets JavaDoc(7, 5, 0, 5), 0, 0));
257         scontentPanel.add(descriptionLabel, new GridBagConstraints JavaDoc(1, 4, 1, 1, 0.0, 0.0
258             ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets JavaDoc(7, 5, 5, 5), 0, 0));
259         scontentPanel.add(valueField, new GridBagConstraints JavaDoc(2, 1, 1, 1, 0.0, 0.0
260             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets JavaDoc(5, 0, 0, 5), 0, 0));
261         scontentPanel.add(descriptionField, new GridBagConstraints JavaDoc(2, 4, 1, 1, 0.0, 0.0
262             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets JavaDoc(5, 0, 5, 5), 0, 0));
263         scontentPanel.add(docNoLabel, new GridBagConstraints JavaDoc(1, 2, 1, 1, 0.0, 0.0
264             ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets JavaDoc(7, 5, 0, 5), 0, 0));
265         scontentPanel.add(nameField, new GridBagConstraints JavaDoc(2, 3, 1, 1, 0.0, 0.0
266             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets JavaDoc(5, 0, 0, 5), 0, 0));
267         scontentPanel.add(docNoField, new GridBagConstraints JavaDoc(2, 2, 1, 1, 0.0, 0.0
268             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets JavaDoc(5, 0, 0, 5), 0, 0));
269         //
270
scontentPanel.add(spaceE, new GridBagConstraints JavaDoc(3, 3, 1, 1, 0.0, 0.0
271             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets JavaDoc(10, 10, 10, 10), 0, 0));
272         scontentPanel.add(spaceN, new GridBagConstraints JavaDoc(2, 0, 1, 1, 0.0, 0.0
273             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets JavaDoc(10, 10, 10, 10), 0, 0));
274         scontentPanel.add(spaceW, new GridBagConstraints JavaDoc(0, 3, 1, 1, 0.0, 0.0
275             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets JavaDoc(10, 10, 10, 10), 0, 0));
276         scontentPanel.add(spaceS, new GridBagConstraints JavaDoc(2, 15, 1, 1, 0.0, 0.0
277             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets JavaDoc(10, 10, 10, 10), 0, 0));
278         //
279
// tabbedPane.add(simplePanel, Msg.getMsg(Env.getCtx(),"Find"));
280
tabbedPane.add(simplePanel, Msg.getMsg(Env.getCtx(),"Find"));
281         //
282
toolBar.add(bIgnore, null);
283         toolBar.addSeparator();
284         toolBar.add(bNew, null);
285         toolBar.add(bSave, null);
286         toolBar.add(bDelete, null);
287         advancedPanel.setLayout(advancedLayout);
288         advancedPanel.add(toolBar, BorderLayout.NORTH);
289         advancedPanel.add(confirmPanelA, BorderLayout.SOUTH);
290         advancedPanel.add(advancedScrollPane, BorderLayout.CENTER);
291         advancedScrollPane.getViewport().add(advancedTable, null);
292     // tabbedPane.add(advancedPanel, Msg.getMsg(Env.getCtx(),"Advanced"));
293
tabbedPane.add(advancedPanel, Msg.getMsg(Env.getCtx(),"Advanced"));
294         //
295
this.getContentPane().add(tabbedPane, BorderLayout.CENTER);
296         //
297
confirmPanelA.addActionListener(this);
298         confirmPanelS.addActionListener(this);
299     } // jbInit
300

301     /**
302      * Dynamic Init.
303      * set up GridController
304      */

305     private void initFind()
306     {
307         Log.trace(Log.l3_Util, "Find.initFind");
308
309         // Get Info from target Tab
310
int size = m_targetTab.getFieldCount();
311         for (int i = 0; i < size; i++)
312         {
313             MField mField = m_targetTab.getField(i);
314             String JavaDoc columnName = mField.getColumnName();
315
316             if (columnName.equals("Value"))
317                 hasValue = true;
318             else if (columnName.equals("Name"))
319                 hasName = true;
320             else if (columnName.equals("DocumentNo"))
321                 hasDocNo = true;
322             else if (columnName.equals("Description"))
323                 hasDescription = true;
324             else if (mField.isSelectionColumn())
325                 addSelectionColumn (mField);
326
327             // TargetFields
328
m_targetFields.put (new Integer JavaDoc(mField.getAD_Column_ID()), mField);
329         } // for all target tab fields
330

331         // Disable simple query fields
332
valueLabel.setVisible(hasValue);
333         valueField.setVisible(hasValue);
334         if (hasValue)
335             valueField.addActionListener(this);
336         docNoLabel.setVisible(hasDocNo);
337         docNoField.setVisible(hasDocNo);
338         if (hasDocNo)
339             docNoField.addActionListener(this);
340         nameLabel.setVisible(hasName);
341         nameField.setVisible(hasName);
342         if (hasName)
343             nameField.addActionListener(this);
344         descriptionLabel.setVisible(hasDescription);
345         descriptionField.setVisible(hasDescription);
346         if (hasDescription)
347             descriptionField.addActionListener(this);
348
349         // Get Total
350
m_total = getNoOfRecords(null);
351         setStatusDB (m_total);
352         statusBar.setStatusLine("");
353
354         tabbedPane.addChangeListener(this);
355
356         // Better Labels for OK/Cancel
357
confirmPanelA.getOKButton().setToolTipText(Msg.getMsg(Env.getCtx(),"QueryEnter"));
358         confirmPanelA.getCancelButton().setToolTipText(Msg.getMsg(Env.getCtx(),"QueryCancel"));
359         confirmPanelS.getOKButton().setToolTipText(Msg.getMsg(Env.getCtx(),"QueryEnter"));
360         confirmPanelS.getCancelButton().setToolTipText(Msg.getMsg(Env.getCtx(),"QueryCancel"));
361     } // initFind
362

363     /**
364      * Add Selection Column to first Tab
365      * @param mField field
366      */

367     private void addSelectionColumn (MField mField)
368     {
369         Log.trace(Log.l4_Data, "Find.addSelectionColumn", mField.getHeader());
370         VEditor editor = VEditorFactory.getEditor(mField, false);
371         editor.setMandatory(false);
372         editor.setReadWrite(true);
373         CLabel label = VEditorFactory.getLabel(mField);
374         //
375
m_sLine++;
376         scontentPanel.add(label, new GridBagConstraints JavaDoc(1, m_sLine, 1, 1, 0.0, 0.0
377             ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets JavaDoc(7, 5, 5, 5), 0, 0));
378         scontentPanel.add((Component JavaDoc)editor, new GridBagConstraints JavaDoc(2, m_sLine, 1, 1, 0.0, 0.0
379             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets JavaDoc(5, 0, 5, 5), 0, 0));
380         m_sEditors.add(editor);
381     } // addSelectionColumn
382

383
384     /**
385      * Init Find GridController
386      */

387     private void initFindAdvanced()
388     {
389         Log.trace(Log.l3_Util, "Find.initFindAdvanced");
390         advancedTable.setModel(new DefaultTableModel JavaDoc(0, 4));
391         advancedTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
392
393         // 0 = Columns
394
Vector JavaDoc items = new Vector JavaDoc();
395         for (int c = 0; c < m_targetTab.getFieldCount(); c++)
396         {
397             MField field = m_targetTab.getField(c);
398             if (field.isKey())
399                 ;
400             else
401                 items.add(new ValueNamePair(field.getColumnName(), field.getHeader()));
402         }
403         CComboBox columns = new CComboBox(items);
404         TableColumn JavaDoc tc = advancedTable.getColumnModel().getColumn(INDEX_COLUMNNAME);
405         tc.setPreferredWidth(150);
406         tc.setCellEditor(new DefaultCellEditor JavaDoc(columns));
407         tc.setHeaderValue(Msg.translate(Env.getCtx(), "AD_Column_ID"));
408
409         // 1 = Operators
410
CComboBox compare = new CComboBox(MQuery.OPERATORS);
411         tc = advancedTable.getColumnModel().getColumn(INDEX_OPERATOR);
412         tc.setPreferredWidth(40);
413         tc.setCellEditor(new DefaultCellEditor JavaDoc(compare));
414         tc.setHeaderValue(Msg.getMsg(Env.getCtx(), "Operator"));
415
416         // 2 = QueryValue
417
tc = advancedTable.getColumnModel().getColumn(INDEX_VALUE);
418         tc.setCellEditor(new FindValueEditor(this, false));
419         tc.setCellRenderer(new FindValueRenderer(this, false));
420         tc.setHeaderValue(Msg.getMsg(Env.getCtx(), "QueryValue"));
421
422         // 3 = QueryValue2
423
tc = advancedTable.getColumnModel().getColumn(INDEX_VALUE2);
424         tc.setPreferredWidth(50);
425         tc.setCellEditor(new FindValueEditor(this, true));
426         tc.setCellRenderer(new FindValueRenderer(this, true));
427         tc.setHeaderValue(Msg.getMsg(Env.getCtx(), "QueryValue2"));
428
429         // No Row - Create one
430
cmd_new();
431     } // initFindAdvanced
432

433     /**
434      * Dispose window
435      */

436     public void dispose()
437     {
438         Log.trace(Log.l3_Util, "Find.dispose");
439
440         // Find SQL
441
if (m_pstmt != null)
442         {
443             try {
444                 m_pstmt.close();
445             } catch (SQLException JavaDoc e) {}
446         }
447         m_pstmt = null;
448
449         // TargetFields
450
if (m_targetFields != null)
451             m_targetFields.clear();
452         m_targetFields = null;
453         //
454
removeAll();
455         super.dispose();
456     } // dispose
457

458     /*************************************************************************/
459
460     /**
461      * Action Listener
462      * @param e ActionEvent
463      */

464     public void actionPerformed (ActionEvent JavaDoc e)
465     {
466         Log.trace(Log.l1_User, "Find.actionPerformed", e.getActionCommand());
467         //
468
if (e.getActionCommand() == ConfirmPanel.A_CANCEL)
469             cmd_cancel();
470         else if (e.getActionCommand() == ConfirmPanel.A_REFRESH)
471             cmd_refresh();
472         //
473
else if (e.getSource() == bIgnore)
474             cmd_ignore();
475         else if (e.getSource() == bNew)
476             cmd_new();
477         else if (e.getSource() == bSave)
478             cmd_save();
479         else if (e.getSource() == bDelete)
480             cmd_delete();
481         //
482
else // ConfirmPanel.A_OK and enter in fields
483
{
484             if (e.getSource() == confirmPanelA.getOKButton())
485                 cmd_ok_Advanced();
486             else
487                 cmd_ok_Simple();
488         }
489     } // actionPerformed
490

491     /**
492      * Change Listener (tab change)
493      * @param e ChangeEbent
494      */

495     public void stateChanged(ChangeEvent JavaDoc e)
496     {
497         Log.trace(Log.l1_User, "Find.stateChanged");
498         if (tabbedPane.getSelectedIndex() == 0)
499             this.getRootPane().setDefaultButton(confirmPanelS.getOKButton());
500         else
501         {
502             initFindAdvanced();
503             this.getRootPane().setDefaultButton(confirmPanelA.getOKButton());
504         }
505     } // stateChanged
506

507     /**
508      * Simple OK Button pressed
509      */

510     private void cmd_ok_Simple()
511     {
512         // Create Query String
513
m_query = new MQuery(m_targetTab.getTableName());
514         if (hasValue && !valueField.getText().equals("%") && valueField.getText().length() != 0)
515         {
516             String JavaDoc value = valueField.getText().toUpperCase();
517             if (!value.endsWith("%"))
518                 value += "%";
519             m_query.addRestriction("UPPER(Value)", MQuery.LIKE, value, valueLabel.getText(), value);
520         }
521         //
522
if (hasDocNo && !docNoField.getText().equals("%") && docNoField.getText().length() != 0)
523         {
524             String JavaDoc value = docNoField.getText().toUpperCase();
525             if (!value.endsWith("%"))
526                 value += "%";
527             m_query.addRestriction("UPPER(DocumentNo)", MQuery.LIKE, value, docNoLabel.getText(), value);
528         }
529         //
530
if ((hasName) && !nameField.getText().equals("%") && nameField.getText().length() != 0)
531         {
532             String JavaDoc value = nameField.getText().toUpperCase();
533             if (!value.endsWith("%"))
534                 value += "%";
535             m_query.addRestriction("UPPER(Name)", MQuery.LIKE, value, nameLabel.getText(), value);
536         }
537         //
538
if (hasDescription && !descriptionField.getText().equals("%") && descriptionField.getText().length() != 0)
539         {
540             String JavaDoc value = descriptionField.getText().toUpperCase();
541             if (!value.endsWith("%"))
542                 value += "%";
543             m_query.addRestriction("UPPER(Description)", MQuery.LIKE, value, descriptionLabel.getText(), value);
544         }
545         // Special Editors
546
for (int i = 0; i < m_sEditors.size(); i++)
547         {
548             VEditor ved = (VEditor)m_sEditors.get(i);
549             Object JavaDoc value = ved.getValue();
550             if (value != null && value.toString().length() > 0)
551             {
552                 String JavaDoc ColumnName = ((Component JavaDoc)ved).getName ();
553                 Log.trace (Log.l5_DData, "Find.cmd_ok_Simple - " + ColumnName, value);
554                 if (value.toString().indexOf("%") != -1)
555                     m_query.addRestriction(ColumnName, MQuery.LIKE, value, ColumnName, ved.getDisplay());
556                 else
557                     m_query.addRestriction(ColumnName, MQuery.EQUAL, value, ColumnName, ved.getDisplay());
558             }
559         } // editors
560

561
562         // Test for no records
563
if (getNoOfRecords(m_query) != 0)
564             dispose();
565     } // cmd_ok_Simple
566

567     /**
568      * Advanced OK Button pressed
569      */

570     private void cmd_ok_Advanced()
571     {
572         // save pending
573
if (bSave.isEnabled())
574             cmd_save();
575         if (getNoOfRecords(m_query) != 0)
576             dispose();
577     } // cmd_ok_Advanced
578

579     /**
580      * Cancel Button pressed
581      */

582     private void cmd_cancel()
583     {
584         Log.trace(Log.l1_User, "Find.cmd_cancel");
585         m_query = null;
586         dispose();
587     } // cmd_ok
588

589     /**
590      * Ignore
591      */

592     private void cmd_ignore()
593     {
594         Log.trace(Log.l1_User, "Find.cmd_ignore");
595     } // cmd_ignore
596

597     /**
598      * New record
599      */

600     private void cmd_new()
601     {
602         Log.trace(Log.l1_User, "Find.cmd_new");
603         DefaultTableModel JavaDoc model = (DefaultTableModel JavaDoc)advancedTable.getModel();
604         model.addRow(new Object JavaDoc[] {null, MQuery.OPERATORS[MQuery.EQUAL_INDEX], null, null});
605     } // cmd_new
606

607     /**
608      * Save
609      */

610     private void cmd_save()
611     {
612         Log.trace(Log.l1_User, "Find.cmd_save");
613         advancedTable.stopEditor(true);
614         //
615
m_query = new MQuery();
616         for (int row = 0; row < advancedTable.getRowCount(); row++)
617         {
618             // Column
619
Object JavaDoc column = advancedTable.getValueAt(row, INDEX_COLUMNNAME);
620             if (column == null)
621                 continue;
622             String JavaDoc ColumnName = ((ValueNamePair)column).getValue();
623             String JavaDoc infoName = column.toString();
624             MField field = getTargetMField(ColumnName);
625             // Op
626
Object JavaDoc op = advancedTable.getValueAt(row, INDEX_OPERATOR);
627             if (op == null)
628                 continue;
629             String JavaDoc Operator = ((ValueNamePair)op).getValue();
630             // Value
631
Object JavaDoc value = advancedTable.getValueAt(row, INDEX_VALUE);
632             if (value == null)
633                 continue;
634             Object JavaDoc parsedValue = parseValue(field, value);
635             if (parsedValue == null)
636                 continue;
637             String JavaDoc infoDisplay = value.toString();
638             if (field.isLookup())
639                 infoDisplay = field.getLookup().getDisplay(value);
640             else if (field.getDisplayType() == DisplayType.YesNo)
641                 infoDisplay = Msg.getMsg(Env.getCtx(), infoDisplay.equals("Y") ? "Yes" : "No");
642             //
643
if (MQuery.OPERATORS[MQuery.BETWEEN_INDEX].equals(op))
644             {
645                 Object JavaDoc value2 = advancedTable.getValueAt(row, INDEX_VALUE2);
646                 if (value2 == null)
647                     continue;
648                 Object JavaDoc parsedValue2 = parseValue(field, value2);
649                 String JavaDoc infoDisplay_to = value2.toString();
650                 if (parsedValue2 == null)
651                     continue;
652                 m_query.addRangeRestriction(ColumnName, parsedValue, parsedValue2,
653                     infoName, infoDisplay, infoDisplay_to);
654             }
655             else
656                 m_query.addRestriction(ColumnName, Operator, parsedValue,
657                     infoName, infoDisplay);
658         }
659     } // cmd_save
660

661     /**
662      * Parse Value
663      * @param field column
664      * @param in value
665      * @return data type corected value
666      */

667     private Object JavaDoc parseValue (MField field, Object JavaDoc in)
668     {
669         if (in == null)
670             return null;
671         int dt = field.getDisplayType();
672         try
673         {
674             // Return Integer
675
if (DisplayType.isID(dt) && field.getColumnName().endsWith("_ID"))
676             {
677                 if (in instanceof Integer JavaDoc)
678                     return in;
679                 int i = Integer.parseInt(in.toString());
680                 return new Integer JavaDoc(i);
681             }
682             // Return BigDecimal
683
else if (DisplayType.isNumeric(dt))
684             {
685                 if (in instanceof BigDecimal JavaDoc)
686                     return in;
687                 return DisplayType.getNumberFormat(dt).parse(in.toString());
688             }
689             // Return Timestamp
690
else if (DisplayType.isDate(dt))
691             {
692                 if (in instanceof Timestamp JavaDoc)
693                     return in;
694                 long time = 0;
695                 try
696                 {
697                     time = DisplayType.getDateFormat_JDBC().parse(in.toString()).getTime();
698                     return new Timestamp JavaDoc(time);
699                 }
700                 catch (Exception JavaDoc e)
701                 {
702                     Log.error("Find.parseValue " + in + "(" + in.getClass() + ")" + e);
703                     time = DisplayType.getDateFormat(dt).parse(in.toString()).getTime();
704                 }
705                 return new Timestamp JavaDoc(time);
706             }
707         }
708         catch (Exception JavaDoc ex)
709         {
710             Log.error("Find.parseValue", ex);
711             String JavaDoc error = ex.getLocalizedMessage();
712             if (error == null || error.length() == 0)
713                 error = ex.toString();
714             StringBuffer JavaDoc errMsg = new StringBuffer JavaDoc();
715             errMsg.append(field.getColumnName()).append(" = ").append(in).append(" - ").append(error);
716             //
717
ADialog.error(0, this, "ValidationError", errMsg.toString());
718             return null;
719         }
720
721         return in;
722     } // parseValue
723

724     /**
725      * Delete
726      */

727     private void cmd_delete()
728     {
729         Log.trace(Log.l1_User, "Find.cmd_delete");
730         DefaultTableModel JavaDoc model = (DefaultTableModel JavaDoc)advancedTable.getModel();
731         int row = advancedTable.getSelectedRow();
732         if (row >= 0)
733             model.removeRow(row);
734         cmd_refresh();
735     } // cmd_delete
736

737     /**
738      * Refresh
739      */

740     private void cmd_refresh()
741     {
742         Log.trace(Log.l1_User, "Find.cmd_refresh");
743         int records = getNoOfRecords(m_query);
744         setStatusDB (records);
745         statusBar.setStatusLine("");
746     } // cmd_refresh
747

748     /*************************************************************************/
749
750     /**
751      * Get Query - Retrieve result
752      * @return String representation of query
753      */

754     public MQuery getQuery()
755     {
756         Log.trace(Log.l1_User, "Find.getQuery=" + m_query);
757         return m_query;
758     } // getQuery
759

760     /**
761      * Get the number of records of target tab
762      * @param query where clause for target tab
763      * @return number of selected records
764      */

765     private int getNoOfRecords(MQuery query)
766     {
767         Log.trace(Log.l4_Data, "Find.getNoOfRecords - " + query);
768         StringBuffer JavaDoc sql = new StringBuffer JavaDoc("SELECT COUNT(*) FROM ");
769         sql.append(m_targetTab.getTableName());
770         boolean hasWhere = false;
771         if (m_targetTab.getWhereExtended().length() > 0)
772         {
773             sql.append(" WHERE ").append(m_targetTab.getWhereExtended());
774             hasWhere = true;
775         }
776         if (query != null && query.isActive())
777         {
778             if (hasWhere)
779                 sql.append(" AND ");
780             else
781                 sql.append(" WHERE ");
782             sql.append(query.getWhereClause());
783         }
784         // Add Access
785
String JavaDoc finalSQL = MRole.getDefault().addAccessSQL(sql.toString(),
786             m_targetTab.getTableName(), MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
787         finalSQL = Env.parseContext(Env.getCtx(), m_targetWindowNo, finalSQL, false);
788         Env.setContext(Env.getCtx(), m_targetWindowNo, TABNO, "FindSQL", finalSQL);
789
790         int count = 0;
791
792         // Execute Qusery
793
try
794         {
795             Statement JavaDoc stmt = DB.createStatement();
796             ResultSet JavaDoc rs = stmt.executeQuery(finalSQL);
797             if (rs.next())
798                 count = rs.getInt(1);
799             rs.close();
800             stmt.close();
801         }
802         catch (SQLException JavaDoc e)
803         {
804             Log.error("Find.getNoOfRecords SQL=" + finalSQL, e);
805         }
806
807         if (count == 0)
808             ADialog.info(m_targetWindowNo, this, "FindZeroRecords");
809         if (query != null)
810             statusBar.setStatusToolTip (query.getWhereClause());
811         return count;
812     } // getNoOfRecords
813

814     /**
815      * Display current count
816      * @param currentCount String representation of current/total
817      */

818     private void setStatusDB (int currentCount)
819     {
820         String JavaDoc text = " " + currentCount + " / " + m_total + " ";
821         statusBar.setStatusDB(text);
822     } // setDtatusDB
823

824     /*************************************************************************/
825
826     /**
827      * Grid Status Changed.
828      * @param e DataStatueEvent
829      */

830     public void dataStatusChanged (DataStatusEvent e)
831     {
832         Log.trace(Log.l4_Data, "Find.dataStatusChanged", e.getMessage());
833         // Action control
834
boolean changed = e.isChanged();
835         bIgnore.setEnabled(changed);
836         bNew.setEnabled(!changed);
837         bSave.setEnabled(changed);
838         bDelete.setEnabled(!changed);
839     } // statusChanged
840

841     /**
842      * Get Target MField
843      * @param columnName column name
844      * @return MField
845      */

846     public MField getTargetMField (String JavaDoc columnName)
847     {
848         if (columnName == null)
849             return null;
850         for (int c = 0; c < m_targetTab.getFieldCount(); c++)
851         {
852             MField field = m_targetTab.getField(c);
853             if (columnName.equals(field.getColumnName()))
854                 return field;
855         }
856         return null;
857     } // getTargetMField
858

859 } // Find
860
Popular Tags