KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
20
21 import org.compiere.apps.*;
22 import org.compiere.util.*;
23 import org.compiere.model.*;
24 import org.compiere.plaf.*;
25 import org.compiere.swing.*;
26
27 /**
28  * Dialog to enter Location Info (Address)
29  *
30  * @author Jorg Janke
31  * @version $Id: VLocationDialog.java,v 1.11 2003/10/09 06:13:01 jjanke Exp $
32  */

33 public class VLocationDialog extends JDialog implements ActionListener
34 {
35     /**
36      * Constructor
37      *
38      * @param frame parent
39      * @param title title (field name)
40      * @param mLocation Model Location
41      */

42     public VLocationDialog (Frame frame, String JavaDoc title, MLocation mLocation)
43     {
44         super(frame, title, true);
45         try
46         {
47             jbInit();
48             setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
49         }
50         catch(Exception JavaDoc ex)
51         {
52             Log.error("VLocationDialog - " + ex.getMessage());
53         }
54         //
55
m_mLocation = mLocation;
56         if (mLocation.getC_Location_ID() == 0)
57             setTitle(Msg.getMsg(Env.getCtx(), "LocationNew"));
58         else
59             setTitle(Msg.getMsg(Env.getCtx(), "LocationUpdate"));
60
61         // Current Country
62
fCountry = new CComboBox(MCountry.getCountries(Env.getCtx()));
63         fCountry.setSelectedItem(m_mLocation.getCountry());
64         m_origCountry_ID = m_mLocation.getC_Country_ID();
65         // Current Region
66
fRegion = new CComboBox(MRegion.getRegions(Env.getCtx(), m_origCountry_ID));
67         if (m_mLocation.getCountry().isHasRegion())
68             lRegion.setText(m_mLocation.getCountry().getRegionName()); // name for region
69
fRegion.setSelectedItem(m_mLocation.getRegion());
70         //
71
initLocation();
72         fCountry.addActionListener(this);
73         AEnv.positionCenterWindow(frame, this);
74     } // VLocationDialog
75

76     private boolean m_change = false;
77     private MLocation m_mLocation;
78     private int m_origCountry_ID;
79     private int s_oldCountry_ID = 0;
80
81     private CPanel panel = new CPanel();
82     private CPanel mainPanel = new CPanel();
83     private CPanel southPanel = new CPanel();
84     private BorderLayout panelLayout = new BorderLayout();
85     private GridBagLayout gridBagLayout = new GridBagLayout();
86     private ConfirmPanel confirmPanel = new ConfirmPanel(true);
87     private BorderLayout southLayout = new BorderLayout();
88     //
89
private JLabel lAddress1 = new JLabel(Msg.getMsg(Env.getCtx(), "Address")+ " 1");
90     private JLabel lAddress2 = new JLabel(Msg.getMsg(Env.getCtx(), "Address")+ " 2");
91     private JLabel lCity = new JLabel(Msg.getMsg(Env.getCtx(), "City"));
92     private JLabel lCountry = new JLabel(Msg.getMsg(Env.getCtx(), "Country"));
93     private JLabel lRegion = new JLabel(Msg.getMsg(Env.getCtx(), "Region"));
94     private JLabel lPostal = new JLabel(Msg.getMsg(Env.getCtx(), "Postal"));
95     private JLabel lPostalAdd = new JLabel(Msg.getMsg(Env.getCtx(), "PostalAdd"));
96     private CTextField fAddress1 = new CTextField(15); // length=60
97
private CTextField fAddress2 = new CTextField(15); // length=60
98
private CTextField fCity = new CTextField(10); // length=60
99
private CComboBox fCountry;
100     private CComboBox fRegion;
101     private CTextField fPostal = new CTextField(5); // length=10
102
private CTextField fPostalAdd = new CTextField(5); // length=10
103
//
104
private GridBagConstraints gbc = new GridBagConstraints();
105     private Insets labelInsets = new Insets(2,15,2,0); // top,left,bottom,right
106
private Insets fieldInsets = new Insets(2,5,2,10);
107
108     /**
109      * Static component init
110      * @throws Exception
111      */

112     void jbInit() throws Exception JavaDoc
113     {
114         CompiereColor.setBackground(this);
115         panel.setLayout(panelLayout);
116         southPanel.setLayout(southLayout);
117         mainPanel.setLayout(gridBagLayout);
118         panelLayout.setHgap(5);
119         panelLayout.setVgap(10);
120         getContentPane().add(panel);
121         panel.add(mainPanel, BorderLayout.CENTER);
122         panel.add(southPanel, BorderLayout.SOUTH);
123         southPanel.add(confirmPanel, BorderLayout.NORTH);
124         //
125
confirmPanel.addActionListener(this);
126     } // jbInit
127

128     /**
129      * Dynanmic Init & fill fields - Called when Country changes!
130      */

131     private void initLocation()
132     {
133         MCountry country = m_mLocation.getCountry();
134         Log.trace(Log.l5_DData, "VLocationDialog.initLocation",
135             country.getName() + ", Region=" + country.isHasRegion() + " " + country.getDisplaySequence()
136             + ", C_Location_ID=" + m_mLocation.getC_Location_ID());
137         // new Region
138
if (m_mLocation.getC_Country_ID() != s_oldCountry_ID && country.isHasRegion())
139         {
140             fRegion = new CComboBox(MRegion.getRegions(Env.getCtx(), country.getC_Country_ID()));
141             if (m_mLocation.getRegion() != null)
142                 fRegion.setSelectedItem(m_mLocation.getRegion());
143             lRegion.setText(country.getRegionName());
144             s_oldCountry_ID = m_mLocation.getC_Country_ID();
145         }
146
147         gbc.anchor = GridBagConstraints.NORTHWEST;
148         gbc.gridy = 0; // line
149
gbc.gridx = 0;
150         gbc.gridwidth = 1;
151         gbc.insets = fieldInsets;
152         gbc.fill = GridBagConstraints.HORIZONTAL;
153         gbc.weightx = 0;
154         gbc.weighty = 0;
155
156         mainPanel.add(Box.createVerticalStrut(5), gbc); // top gap
157

158         int line = 1;
159         addLine(line++, lAddress1, fAddress1);
160         addLine(line++, lAddress2, fAddress2);
161
162         // sequence of City Postal Region - @P@ @C@ - @C@, @R@ @P@
163
StringTokenizer st = new StringTokenizer(country.getDisplaySequence(), "@", false);
164         while (st.hasMoreTokens())
165         {
166             String JavaDoc s = st.nextToken();
167             if (s.startsWith("C"))
168                 addLine(line++, lCity, fCity);
169             else if (s.startsWith("P"))
170                 addLine(line++, lPostal, fPostal);
171             else if (s.startsWith("A"))
172                 addLine(line++, lPostalAdd, fPostalAdd);
173             else if (s.startsWith("R") && m_mLocation.getCountry().isHasRegion())
174                 addLine(line++, lRegion, fRegion);
175         }
176         // Country Last
177
addLine(line++, lCountry, fCountry);
178
179         // Fill it
180
if (m_mLocation.getC_Location_ID() != 0)
181         {
182             fAddress1.setText(m_mLocation.getAddress1());
183             fAddress2.setText(m_mLocation.getAddress2());
184             fCity.setText(m_mLocation.getCity());
185             fPostal.setText(m_mLocation.getPostal());
186             fPostalAdd.setText(m_mLocation.getPostal_Add());
187             if (m_mLocation.getCountry().isHasRegion())
188             {
189                 lRegion.setText(m_mLocation.getCountry().getRegionName());
190                 fRegion.setSelectedItem(m_mLocation.getRegion());
191             }
192             fCountry.setSelectedItem(country);
193         }
194         // Update UI
195
pack();
196     } // initLocation
197

198     /**
199      * Add Line to screen
200      *
201      * @param line line number (zero based)
202      * @param label label
203      * @param field field
204      */

205     private void addLine(int line, JLabel label, JComponent field)
206     {
207         gbc.gridy = line;
208         // label
209
gbc.insets = labelInsets;
210         gbc.gridx = 0;
211         gbc.fill = GridBagConstraints.HORIZONTAL;
212         label.setHorizontalAlignment(SwingConstants.RIGHT);
213         mainPanel.add(label, gbc);
214         // Field
215
gbc.insets = fieldInsets;
216         gbc.gridx = 1;
217         gbc.fill = GridBagConstraints.NONE;
218         mainPanel.add(field, gbc);
219     } // addLine
220

221
222     /**
223      * ActionListener
224      * @param e ActionEvent
225      */

226     public void actionPerformed(ActionEvent e)
227     {
228         if (e.getActionCommand().equals(ConfirmPanel.A_OK))
229         {
230             action_OK();
231             m_change = true;
232             dispose();
233         }
234         else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
235         {
236             m_change = false;
237             dispose();
238         }
239
240         // Country Changed - display in new Format
241
else if (e.getSource() == fCountry)
242         {
243             // Modifier for Mouse selection is 16 - for any key selection 0
244
MCountry c = (MCountry)fCountry.getSelectedItem();
245             m_mLocation.setCountry(c);
246             // refrseh
247
mainPanel.removeAll();
248             initLocation();
249             fCountry.requestFocus(); // allows to use Keybord selection
250
}
251     } // actionPerformed
252

253     /**
254      * OK - check for changes (save them) & Exit
255      */

256     private void action_OK()
257     {
258         m_mLocation.setAddress1(fAddress1.getText());
259         m_mLocation.setAddress2(fAddress2.getText());
260         m_mLocation.setCity(fCity.getText());
261         m_mLocation.setPostal(fPostal.getText());
262         m_mLocation.setPostal_Add(fPostalAdd.getText());
263         // Country/Region
264
MCountry c = (MCountry)fCountry.getSelectedItem();
265         m_mLocation.setCountry(c);
266         if (m_mLocation.getCountry().isHasRegion())
267         {
268             MRegion r = (MRegion)fRegion.getSelectedItem();
269             m_mLocation.setRegion(r);
270         }
271         else
272             m_mLocation.setC_Region_ID(0);
273         // Save chnages
274
m_mLocation.save();
275     } // actionOK
276

277     /**
278      * Get result
279      * @return true, if changed
280      */

281     public boolean isChanged()
282     {
283         return m_change;
284     } // getChange
285

286 } // VLocationDialog
287
Popular Tags