KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > ed > VLocator


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.grid.ed;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import java.text.*;
19 import javax.swing.*;
20 import java.math.*;
21 import java.beans.*;
22 import java.sql.*;
23
24 import org.compiere.apps.*;
25 import org.compiere.util.*;
26 import org.compiere.model.*;
27 import org.compiere.plaf.*;
28 import org.compiere.swing.*;
29
30 /**
31  * Warehouse Locator Control
32  *
33  * @author Jorg Janke
34  * @version $Id: VLocator.java,v 1.16 2003/09/05 04:58:59 jjanke Exp $
35  */

36 public class VLocator extends JComponent
37     implements VEditor, ActionListener
38 {
39     /**
40      * IDE Constructor
41      */

42     public VLocator ()
43     {
44         this("M_Locator_ID", false, false, true, null, 0);
45     } // VLocator
46

47     /**
48      * Constructor
49      *
50      * @param columnName ColumnName
51      * @param mandatory mandatory
52      * @param isReadOnly read only
53      * @param isUpdateable updateable
54      * @param mLocator locator (lookup) model
55      * @param WindowNo window no
56      */

57     public VLocator(String JavaDoc columnName, boolean mandatory, boolean isReadOnly, boolean isUpdateable,
58         MLocatorLookup mLocator, int WindowNo)
59     {
60         super();
61         super.setName(columnName);
62         m_columnName = columnName;
63         m_mLocator = mLocator;
64         m_WindowNo = WindowNo;
65         //
66
LookAndFeel.installBorder(this, "TextField.border");
67         this.setLayout(new BorderLayout());
68         // Size
69
this.setPreferredSize(m_text.getPreferredSize()); // causes r/o to be the same length
70
int height = m_text.getPreferredSize().height;
71
72         // *** Button & Text ***
73
m_text.setBorder(null);
74         m_text.setEditable(true);
75         m_text.setFocusable(true);
76         m_text.addMouseListener(new VLocator_mouseAdapter(this)); // popup
77
m_text.setFont(CompierePLAF.getFont_Field());
78         m_text.setForeground(CompierePLAF.getTextColor_Normal());
79         m_text.addActionListener(this);
80         this.add(m_text, BorderLayout.CENTER);
81
82         m_button.setIcon(new ImageIcon(org.compiere.Compiere.class.getResource("images/Locator10.gif")));
83         m_button.setMargin(new Insets(0, 0, 0, 0));
84         m_button.setPreferredSize(new Dimension(height, height));
85         m_button.addActionListener(this);
86         this.add(m_button, BorderLayout.EAST);
87
88         // Prefereed Size
89
this.setPreferredSize(this.getPreferredSize()); // causes r/o to be the same length
90

91         // ReadWrite
92
if (isReadOnly || !isUpdateable)
93             setReadWrite (false);
94         else
95             setReadWrite (true);
96         setMandatory (mandatory);
97         //
98
mZoom = new JMenuItem(Msg.getMsg(Env.getCtx(), "Zoom"), Env.getImageIcon("Zoom16.gif"));
99         mZoom.addActionListener(this);
100         popupMenu.add(mZoom);
101         mRefresh = new JMenuItem(Msg.getMsg(Env.getCtx(), "Refresh"), Env.getImageIcon("Refresh16.gif"));
102         mRefresh.addActionListener(this);
103         popupMenu.add(mRefresh);
104     } // VLocator
105

106     /**
107      * Dispose
108      */

109     public void dispose()
110     {
111         m_text = null;
112         m_button = null;
113         m_mLocator = null;
114     } // dispose
115

116     private JTextField m_text = new JTextField (VLookup.DISPLAY_LENGTH);
117     private CButton m_button = new CButton();
118     private MLocatorLookup m_mLocator;
119     private Object JavaDoc m_value;
120     //
121
private String JavaDoc m_columnName;
122     private int m_WindowNo;
123     // Popup
124
JPopupMenu popupMenu = new JPopupMenu();
125     private JMenuItem mZoom;
126     private JMenuItem mRefresh;
127
128     /**
129      * Enable/disable
130      * @param value r/w
131      */

132     public void setReadWrite (boolean value)
133     {
134         m_button.setReadWrite(value);
135         if (m_button.isVisible() != value)
136             m_button.setVisible(value);
137         setBackground(false);
138     } // setReadWrite
139

140     /**
141      * IsReadWrite
142      * @return true if ReadWrite
143      */

144     public boolean isReadWrite()
145     {
146         return m_button.isReadWrite();
147     } // isReadWrite
148

149     /**
150      * Set Mandatory (and back bolor)
151      * @param mandatory true if mandatory
152      */

153     public void setMandatory (boolean mandatory)
154     {
155         m_button.setMandatory(mandatory);
156         setBackground(false);
157     } // setMandatory
158

159     /**
160      * Is it mandatory
161      * @return true if mandatory
162      */

163     public boolean isMandatory()
164     {
165         return m_button.isMandatory();
166     } // isMandatory
167

168     /**
169      * Set Background
170      * @param color color
171      */

172     public void setBackground (Color color)
173     {
174         if (!color.equals(m_text.getBackground()))
175             m_text.setBackground(color);
176     } // setBackground
177

178     /**
179      * Set Background based on editable / mandatory / error
180      * @param error if true, set background to error color, otherwise mandatory/editable
181      */

182     public void setBackground (boolean error)
183     {
184         if (error)
185             setBackground(CompierePLAF.getFieldBackground_Error());
186         else if (!isReadWrite())
187             setBackground(CompierePLAF.getFieldBackground_Inactive());
188         else if (isMandatory())
189             setBackground(CompierePLAF.getFieldBackground_Mandatory());
190         else
191             setBackground(CompierePLAF.getFieldBackground_Normal());
192     } // setBackground
193

194     /**
195      * Set Foreground
196      * @param fg color
197      */

198     public void setForeground(Color fg)
199     {
200         m_text.setForeground(fg);
201     } // setForeground
202

203     /**
204      * Set Editor to value
205      * @param value Integer
206      */

207     public void setValue(Object JavaDoc value)
208     {
209         setValue (value, false);
210     } // setValue
211

212     /**
213      * Set Value
214      * @param value value
215      * @param fire data binding
216      */

217     private void setValue (Object JavaDoc value, boolean fire)
218     {
219         if (value != null)
220         {
221             m_mLocator.setOnly_Warehouse_ID (getOnly_Warehouse_ID ());
222             if (!m_mLocator.isValid(value))
223                 value = null;
224         }
225         //
226
m_value = value;
227         m_text.setText(m_mLocator.getDisplay(value)); // loads value
228

229         // Data Binding
230
try
231         {
232             fireVetoableChange(m_columnName, null, value);
233         }
234         catch (PropertyVetoException pve)
235         {
236         }
237     } // setValue
238

239
240     /**
241      * Property Change Listener
242      * @param evt PropertyChangeEvent
243      */

244     public void propertyChange (PropertyChangeEvent evt)
245     {
246         if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY))
247             setValue(evt.getNewValue());
248     } // propertyChange
249

250     /**
251      * Return Editor value
252      * @return value
253      */

254     public Object JavaDoc getValue()
255     {
256         return m_value;
257     } // getValue
258

259     /**
260      * Return Display Value
261      * @return display value
262      */

263     public String JavaDoc getDisplay()
264     {
265         return m_text.getText();
266     } // getDisplay
267

268     /**
269      * ActionListener
270      * @param e ActionEvent
271      */

272     public void actionPerformed(ActionEvent e)
273     {
274         // Refresh
275
if (e.getSource() == mRefresh)
276         {
277             m_mLocator.refresh();
278             return;
279         }
280
281         // Zoom to M_Warehouse
282
if (e.getSource() == mZoom)
283         {
284             actionZoom();
285             return;
286         }
287
288         // Warehouse
289
int only_Warehouse_ID = getOnly_Warehouse_ID();
290         Log.trace(Log.l3_Util, "VLocator.actionPerformed - Only Wharehouse_ID=" + only_Warehouse_ID);
291
292         // Text Entry ok
293
if (e.getSource() == m_text && actionText(only_Warehouse_ID))
294                 return;
295
296         // Button - Start Dialog
297
int M_Locator_ID = 0;
298         if (m_value instanceof Integer JavaDoc)
299             M_Locator_ID = ((Integer JavaDoc)m_value).intValue();
300         //
301
m_mLocator.setOnly_Warehouse_ID(only_Warehouse_ID);
302         VLocatorDialog ld = new VLocatorDialog(Env.getFrame(this),
303             Msg.translate(Env.getCtx(), m_columnName),
304             m_mLocator, M_Locator_ID, isMandatory(), only_Warehouse_ID);
305         // display
306
ld.setVisible(true);
307         m_mLocator.setOnly_Warehouse_ID(0);
308
309         // redisplay
310
if (!ld.isChanged())
311             return;
312         setValue (ld.getValue(), true);
313     } // actionPerformed
314

315     /**
316      * Hit Enter in Text Field
317      * @param only_Warehouse_ID if not 0 restrict warehouse
318      * @return true if found
319      */

320     private boolean actionText(int only_Warehouse_ID)
321     {
322         String JavaDoc text = m_text.getText();
323         Log.trace(Log.l5_DData, "VLocator.actionText", text);
324         if (text == null || text.length() == 0)
325         {
326             if (isMandatory())
327                 return false;
328             else
329             {
330                 setValue (null, true);
331                 return true;
332             }
333         }
334         if (text.endsWith("%"))
335             text = text.toUpperCase();
336         else
337             text = text.toUpperCase() + "%";
338         // Look up
339
int M_Locator_ID = 0;
340         String JavaDoc sql = "SELECT M_Locator_ID FROM M_Locator WHERE UPPER(Value) LIKE "
341             + DB.TO_STRING(text);
342         if (only_Warehouse_ID != 0)
343             sql += " AND M_Warehouse_ID=?";
344         PreparedStatement pstmt = null;
345         try
346         {
347             pstmt = DB.prepareStatement(sql);
348             if (only_Warehouse_ID != 0)
349                 pstmt.setInt(1, only_Warehouse_ID);
350             ResultSet rs = pstmt.executeQuery();
351             if (rs.next())
352             {
353                 M_Locator_ID = rs.getInt(1);
354                 if (rs.next())
355                     M_Locator_ID = 0; // more than one
356
}
357             rs.close();
358             pstmt.close();
359             pstmt = null;
360         }
361         catch (SQLException ex)
362         {
363             Log.error("VLocator.actionText", ex);
364         }
365         try
366         {
367             if (pstmt != null)
368                 pstmt.close();
369         }
370         catch (SQLException ex1)
371         {
372         }
373         pstmt = null;
374         if (M_Locator_ID == 0)
375             return false;
376
377         setValue (new Integer JavaDoc(M_Locator_ID), true);
378         return true;
379     } // actionText
380

381     /**
382      * Action Listener Interface
383      * @param listener listener
384      */

385     public void addActionListener(ActionListener listener)
386     {
387         m_text.addActionListener(listener);
388     } // addActionListener
389

390
391     /**
392      * Action - Zoom
393      */

394     private void actionZoom()
395     {
396         int AD_Window_ID = 139; // hardcoded
397
Log.trace(Log.l1_User, "VLocator.actionZoom");
398         //
399
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
400         AWindow frame = new AWindow();
401         if (!frame.initWindow(AD_Window_ID, null, false)) // !IsSOTrx
402
return;
403         AEnv.showCenterScreen(frame);
404         frame = null;
405         setCursor(Cursor.getDefaultCursor());
406     } // actionZoom
407

408     /**
409      * Set Field/WindowNo for ValuePreference (NOP)
410      * @param mField Model Field
411      */

412     public void setField (org.compiere.model.MField mField)
413     {
414     } // setField
415

416
417     /**
418      * Get Warehouse restriction if any.
419      * @return M_Warehouse_ID or 0
420      */

421     private int getOnly_Warehouse_ID()
422     {
423         String JavaDoc only_Warehouse = Env.getContext(Env.getCtx(), m_WindowNo, "M_Warehouse_ID", true);
424         int only_Warehouse_ID = 0;
425         try
426         {
427             if (only_Warehouse != null && only_Warehouse.length () > 0)
428                 only_Warehouse_ID = Integer.parseInt (only_Warehouse);
429         }
430         catch (Exception JavaDoc ex)
431         {
432         }
433         return only_Warehouse_ID;
434     } // getOnly_Warehouse_ID
435

436 } // VLocator
437

438 /*****************************************************************************/
439
440 /**
441  * Mouse Listener for Popup Menu
442  */

443 final class VLocator_mouseAdapter extends java.awt.event.MouseAdapter JavaDoc
444 {
445     /**
446      * Constructor
447      * @param adaptee VLocator
448      */

449     VLocator_mouseAdapter(VLocator adaptee)
450     {
451         this.adaptee = adaptee;
452     } // VLookup_mouseAdapter
453

454     private VLocator adaptee;
455
456     /**
457      * Mouse Listener
458      * @param e MouseEvent
459      */

460     public void mouseClicked(MouseEvent e)
461     {
462         // popup menu
463
if (SwingUtilities.isRightMouseButton(e))
464             adaptee.popupMenu.show((Component)e.getSource(), e.getX(), e.getY());
465     } // mouse Clicked
466

467 } // VLocator_mouseAdapter
468
Popular Tags