KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.*;
19 import java.sql.*;
20
21 import org.compiere.util.*;
22 import org.compiere.model.*;
23 import org.compiere.apps.*;
24 import org.compiere.plaf.*;
25 import org.compiere.swing.*;
26
27 /**
28  * Dialog to enter Warehouse Locator Info
29  *
30  * @author Jorg Janke
31  * @version $Id: VLocatorDialog.java,v 1.12 2003/11/06 07:08:28 jjanke Exp $
32  */

33 public class VLocatorDialog extends JDialog implements ActionListener, KeyListener
34 {
35     /**
36      * Constructor
37      * @param frame frame
38      * @param title title
39      * @param mLocator locator
40      * @param M_Locator_ID locator id
41      * @param mandatory mandatory
42      * @param only_Warehouse_ID of not 0 restrict warehouse
43      */

44     public VLocatorDialog (Frame frame, String JavaDoc title, MLocatorLookup mLocator,
45         int M_Locator_ID, boolean mandatory, int only_Warehouse_ID)
46     {
47         super (frame, title, true);
48         m_WindowNo = Env.getWindowNo(frame);
49         try
50         {
51             jbInit();
52             setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
53         }
54         catch(Exception JavaDoc ex)
55         {
56             Log.error("VLocatorDialog", ex);
57         }
58         //
59
m_mLocator = mLocator;
60         m_M_Locator_ID = M_Locator_ID;
61         m_mandatory = mandatory;
62         m_only_Warehouse_ID = only_Warehouse_ID;
63         //
64
initLocator();
65         AEnv.positionCenterWindow(frame, this);
66     } // VLocatorDialog
67

68     private int m_WindowNo;
69     private boolean m_change = false;
70     private MLocatorLookup m_mLocator;
71     private int m_M_Locator_ID;
72     private boolean m_mandatory = false;
73     private int m_only_Warehouse_ID = 0;
74     //
75
private int m_M_Warehouse_ID;
76     private String JavaDoc m_M_WarehouseName;
77     private String JavaDoc m_M_WarehouseValue;
78     private String JavaDoc m_Separator;
79     private int m_AD_Client_ID;
80     private int m_AD_Org_ID;
81     //
82
private CPanel panel = new CPanel();
83     private CPanel mainPanel = new CPanel();
84     private CPanel southPanel = new CPanel();
85     private BorderLayout panelLayout = new BorderLayout();
86     private GridBagLayout gridBagLayout = new GridBagLayout();
87     private ConfirmPanel confirmPanel = new ConfirmPanel(true);
88     private BorderLayout southLayout = new BorderLayout();
89     //
90
private VComboBox fLocator = new VComboBox();
91     private CComboBox fWarehouse = new CComboBox();
92     private JCheckBox fCreateNew = new JCheckBox();
93     private CTextField fX = new CTextField();
94     private CTextField fY = new CTextField();
95     private CTextField fZ = new CTextField();
96     private JLabel lLocator = new JLabel();
97     private CTextField fWarehouseInfo = new CTextField();
98     private CTextField fValue = new CTextField();
99     private JLabel lWarehouseInfo = new JLabel();
100     private JLabel lWarehouse = new JLabel();
101     private JLabel lX = new JLabel();
102     private JLabel lY = new JLabel();
103     private JLabel lZ = new JLabel();
104     private JLabel lValue = new JLabel();
105
106     /**
107      * Static component init
108      * @throws Exception
109      */

110     private void jbInit() throws Exception JavaDoc
111     {
112         CompiereColor.setBackground(this);
113         panel.setLayout(panelLayout);
114         southPanel.setLayout(southLayout);
115         mainPanel.setLayout(gridBagLayout);
116         panelLayout.setHgap(5);
117         panelLayout.setVgap(10);
118         fCreateNew.setText(Msg.getMsg(Env.getCtx(), "CreateNew"));
119         fX.setColumns(15);
120         fY.setColumns(15);
121         fZ.setColumns(15);
122         lLocator.setLabelFor(fLocator);
123         lLocator.setText(Msg.translate(Env.getCtx(), "M_Locator_ID"));
124         fWarehouseInfo.setBackground(CompierePLAF.getFieldBackground_Inactive());
125         fWarehouseInfo.setReadWrite(false);
126         fWarehouseInfo.setColumns(15);
127         fValue.setColumns(15);
128         lWarehouseInfo.setLabelFor(fWarehouseInfo);
129         lWarehouseInfo.setText(Msg.translate(Env.getCtx(), "M_Warehouse_ID"));
130         lWarehouse.setLabelFor(fWarehouse);
131         lWarehouse.setText(Msg.translate(Env.getCtx(), "M_Warehouse_ID"));
132         lX.setLabelFor(fX);
133         lX.setText(Msg.getElement(Env.getCtx(), "X"));
134         lY.setLabelFor(fY);
135         lY.setText(Msg.getElement(Env.getCtx(), "Y"));
136         lZ.setLabelFor(fZ);
137         lZ.setText(Msg.getElement(Env.getCtx(), "Z"));
138         lValue.setLabelFor(fValue);
139         lValue.setText(Msg.translate(Env.getCtx(), "Value"));
140         getContentPane().add(panel);
141         panel.add(mainPanel, BorderLayout.CENTER);
142         //
143
mainPanel.add(lLocator, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
144             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
145         mainPanel.add(fLocator, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
146             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0));
147         mainPanel.add(fCreateNew, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
148             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 0, 0, 5), 0, 0));
149         mainPanel.add(lWarehouseInfo, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
150             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
151         mainPanel.add(fWarehouseInfo, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
152             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0));
153         mainPanel.add(lWarehouse, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
154             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
155         mainPanel.add(fWarehouse, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0
156             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0));
157         mainPanel.add(lX, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0
158             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
159         mainPanel.add(fX, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0
160             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0));
161         mainPanel.add(lY, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0
162             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
163         mainPanel.add(fY, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0
164             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0));
165         mainPanel.add(lZ, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0
166             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
167         mainPanel.add(fZ, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0
168             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0));
169         mainPanel.add(lValue, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0
170             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
171         mainPanel.add(fValue, new GridBagConstraints(1, 7, 1, 1, 0.0, 0.0
172             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0));
173         //
174
panel.add(southPanel, BorderLayout.SOUTH);
175         southPanel.add(confirmPanel, BorderLayout.NORTH);
176         confirmPanel.addActionListener(this);
177     } // jbInit
178

179     /**
180      * Dynanmic Init & fill fields
181      */

182     private void initLocator()
183     {
184         Log.trace(Log.l5_DData, "VLocatorDialog.initLocator");
185
186         // Load Warehouse
187
String JavaDoc sql = "SELECT M_Warehouse_ID, Name FROM M_Warehouse";
188         if (m_only_Warehouse_ID != 0)
189             sql += " WHERE M_Warehouse_ID=" + m_only_Warehouse_ID;
190         String JavaDoc SQL = MRole.getDefault().addAccessSQL(
191             sql, "M_Warehouse", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO)
192             + " ORDER BY 2";
193         try
194         {
195             PreparedStatement pstmt = DB.prepareStatement(SQL);
196             ResultSet rs = pstmt.executeQuery();
197             while (rs.next())
198                 fWarehouse.addItem(new KeyNamePair(rs.getInt(1), rs.getString(2)));
199             rs.close();
200             pstmt.close();
201         }
202         catch (SQLException e)
203         {
204             Log.error ("VLocatorDialog.initLoactor", e);
205         }
206         Log.trace(Log.l6_Database, "Warehouses=" + fWarehouse.getItemCount());
207
208         // Load existing Locators
209
m_mLocator.fillComboBox(m_mandatory, true, true, false);
210         Log.trace(Log.l6_Database, "initLocator", m_mLocator.toString());
211         fLocator.setModel(m_mLocator);
212         fLocator.setValue(m_M_Locator_ID);
213         fLocator.addActionListener(this);
214         displayLocator();
215         //
216
fCreateNew.setSelected(false);
217         fCreateNew.addActionListener(this);
218         enableNew();
219         //
220
fWarehouse.addActionListener(this);
221         fX.addKeyListener(this);
222         fY.addKeyListener(this);
223         fZ.addKeyListener(this);
224
225         // Update UI
226
pack();
227     } // initLocator
228

229
230     /*************************************************************************/
231
232     /**
233      * ActionListener
234      * @param e event
235      */

236     public void actionPerformed(ActionEvent e)
237     {
238         Object JavaDoc source = e.getSource();
239         //
240
if (e.getActionCommand().equals(ConfirmPanel.A_OK))
241         {
242             actionOK();
243             m_change = true;
244             dispose();
245         }
246         else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
247         {
248             m_change = false;
249             dispose();
250         }
251         // Locator Change
252
else if (e.getSource() == fLocator)
253             displayLocator();
254
255         // New Value Change
256
else if (source == fCreateNew)
257             enableNew();
258
259         // Entered/Changed data for Value
260
else if (fCreateNew.isSelected() && source == fWarehouse)
261             createValue();
262
263     } // actionPerformed
264

265     /**
266      * KeyListener - nop
267      * @param e event
268      */

269     public void keyPressed(KeyEvent e)
270     {}
271     /**
272      * KeyListener
273      * @param e event
274      */

275     public void keyReleased(KeyEvent e)
276     {
277         if (fCreateNew.isSelected())
278             createValue();
279     }
280     /**
281      * KeyListener - nop
282      * @param e event
283      */

284     public void keyTyped(KeyEvent e)
285     {}
286
287     /**
288      * Display value of current locator
289      */

290     private void displayLocator()
291     {
292         MLocator l = (MLocator) fLocator.getSelectedItem();
293         if (l == null)
294             return;
295         //
296
m_M_Locator_ID = l.getM_Locator_ID();
297         fWarehouseInfo.setText(l.getWarehouseName());
298         fX.setText(l.getX());
299         fY.setText(l.getY());
300         fZ.setText(l.getZ());
301         fValue.setText(l.getValue());
302         getWarehouseInfo(l.getM_Warehouse_ID());
303         // Set Warehouse
304
int size = fWarehouse.getItemCount();
305         for (int i = 0; i < size; i++)
306         {
307             KeyNamePair pp = (KeyNamePair)fWarehouse.getItemAt(i);
308             if (pp.getKey() == l.getM_Warehouse_ID())
309             {
310                 fWarehouse.setSelectedIndex(i);
311                 continue;
312             }
313         }
314     } // displayLocator
315

316     /**
317      * Enable/disable New data entry
318      */

319     private void enableNew()
320     {
321         boolean sel = fCreateNew.isSelected();
322         lWarehouse.setVisible(sel);
323         fWarehouse.setVisible(sel);
324         lWarehouseInfo.setVisible(!sel);
325         fWarehouseInfo.setVisible(!sel);
326         fX.setReadWrite(sel);
327         fY.setReadWrite(sel);
328         fZ.setReadWrite(sel);
329         fValue.setReadWrite(sel);
330         pack();
331     } // enableNew
332

333     /**
334      * Get Warehouse Info
335      * @param M_Warehouse_ID warehouse
336      */

337     private void getWarehouseInfo (int M_Warehouse_ID)
338     {
339         if (M_Warehouse_ID == m_M_Warehouse_ID)
340             return;
341         // Defaults
342
m_M_Warehouse_ID = 0;
343         m_M_WarehouseName = "";
344         m_M_WarehouseValue = "";
345         m_Separator = ".";
346         m_AD_Client_ID = 0;
347         m_AD_Org_ID = 0;
348         //
349
String JavaDoc SQL = "SELECT M_Warehouse_ID, Value, Name, Separator, AD_Client_ID, AD_Org_ID "
350             + "FROM M_Warehouse WHERE M_Warehouse_ID=?";
351         try
352         {
353             PreparedStatement pstmt = DB.prepareStatement(SQL);
354             pstmt.setInt(1, M_Warehouse_ID);
355             ResultSet rs = pstmt.executeQuery();
356             if (rs.next())
357             {
358                 m_M_Warehouse_ID = rs.getInt(1);
359                 m_M_WarehouseValue = rs.getString(2);
360                 m_M_WarehouseName = rs.getString(3);
361                 m_Separator = rs.getString(4);
362                 m_AD_Client_ID = rs.getInt(5);
363                 m_AD_Org_ID = rs.getInt(6);
364             }
365             rs.close();
366             pstmt.close();
367         }
368         catch (SQLException e)
369         {
370             Log.error ("VLocatorDialog.getWarehouseInfo", e);
371         }
372     } // getWarehouseInfo
373

374     /**
375      * Create Locator-Value
376      */

377     private void createValue()
378     {
379         // Get Warehouse Info
380
KeyNamePair pp = (KeyNamePair)fWarehouse.getSelectedItem();
381         if (pp == null)
382             return;
383         getWarehouseInfo(pp.getKey());
384         //
385
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(m_M_WarehouseValue);
386         buf.append(m_Separator).append(fX.getText());
387         buf.append(m_Separator).append(fY.getText());
388         buf.append(m_Separator).append(fZ.getText());
389         fValue.setText(buf.toString());
390     } // createValue
391

392     /**
393      * OK - check for changes (save them) & Exit
394      */

395     private void actionOK()
396     {
397         if (fCreateNew.isSelected())
398         {
399             // Get Warehouse Info
400
KeyNamePair pp = (KeyNamePair)fWarehouse.getSelectedItem();
401             if (pp != null)
402                 getWarehouseInfo(pp.getKey());
403
404             // Check mandatory values
405
String JavaDoc mandatoryFields = "";
406             if (m_M_Warehouse_ID == 0)
407                 mandatoryFields += lWarehouse.getText() + " - ";
408             if (fValue.getText().length()==0)
409                 mandatoryFields += lValue.getText() + " - ";
410             if (fX.getText().length()==0)
411                 mandatoryFields += lX.getText() + " - ";
412             if (fY.getText().length()==0)
413                 mandatoryFields += lY.getText() + " - ";
414             if (fZ.getText().length()==0)
415                 mandatoryFields += lZ.getText() + " - ";
416             if (mandatoryFields.length() != 0)
417             {
418                 ADialog.error(m_WindowNo, this, "FillMandatory", mandatoryFields.substring(0, mandatoryFields.length()-3));
419                 return;
420             }
421
422             MLocator loc = MLocator.get(Env.getCtx(), m_M_Warehouse_ID, fValue.getText(),
423                 fX.getText(), fY.getText(), fZ.getText());
424             m_M_Locator_ID = loc.getM_Locator_ID();
425             fLocator.addItem(loc);
426             fLocator.setSelectedItem(loc);
427         } // createNew
428
//
429
Log.trace(Log.l4_Data, "VLocationDialog.actionOK - M_Locator_ID=" + m_M_Locator_ID);
430     } // actionOK
431

432     /**
433      * Get Selected value
434      * @return value as Integer
435      */

436     public Integer JavaDoc getValue()
437     {
438         MLocator l = (MLocator) fLocator.getSelectedItem();
439         if (l != null && l.getM_Locator_ID() != 0)
440             return new Integer JavaDoc (l.getM_Locator_ID());
441         return null;
442     } // getValue
443

444     /**
445      * Get result
446      * @return true if changed
447      */

448     public boolean isChanged()
449     {
450         if (m_change)
451         {
452             MLocator l = (MLocator) fLocator.getSelectedItem();
453             if (l != null)
454                 return l.getM_Locator_ID() == m_M_Locator_ID;
455         }
456         return m_change;
457     } // getChange
458

459 } // VLocatorDialog
460
Popular Tags