KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > form > VSetup


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.form;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import javax.swing.*;
19 import java.sql.*;
20 import java.io.*;
21
22 import org.compiere.util.*;
23 import org.compiere.apps.*;
24 import org.compiere.apps.*;
25 import org.compiere.model.*;
26 import org.compiere.plaf.*;
27 import org.compiere.print.*;
28 import org.compiere.swing.*;
29
30 /**
31  * Setup System
32  *
33  * @author Jorg Janke
34  * @version $Id: VSetup.java,v 1.17 2003/11/06 07:08:28 jjanke Exp $
35  */

36 public class VSetup extends CPanel
37     implements FormPanel, ActionListener
38 {
39     /**
40      * Constructor
41      */

42     public VSetup()
43     {
44     } // VSetup
45

46     /**
47      * Initialize Panel
48      * @param WindowNo window
49      * @param frame frame
50      */

51     public void init (int WindowNo, FormFrame frame)
52     {
53         Log.trace(Log.l1_User, "VSetup.init");
54         m_WindowNo = WindowNo;
55         m_frame = frame;
56         try
57         {
58             jbInit();
59             dynInit();
60             frame.getContentPane().add(centerPane, BorderLayout.CENTER);
61             frame.getContentPane().add(confirmPanel, BorderLayout.SOUTH);
62         }
63         catch(Exception JavaDoc e)
64         {
65             Log.error("VSetup.init", e);
66         }
67     } // init
68

69     /** Window No */
70     private int m_WindowNo = 0;
71     /** FormFrame */
72     private FormFrame m_frame;
73
74     /* Natural Account file */
75     private File m_file = null;
76
77     //
78
private JScrollPane centerPane = new JScrollPane();
79     private ConfirmPanel confirmPanel = new ConfirmPanel(true);
80     private CPanel centerPanel = new CPanel();
81     private GridBagLayout centerLayout = new GridBagLayout();
82     private JLabel lClientName = new JLabel();
83     private JTextField fClientName = new JTextField();
84     private JLabel lOrgName = new JLabel();
85     private JTextField fOrgName = new JTextField();
86     private JLabel lCurrency = new JLabel();
87     private JComboBox fCurrency = new JComboBox();
88     private JLabel lUserClient = new JLabel();
89     private JTextField fUserClient = new JTextField();
90     private JLabel lUserOrg = new JLabel();
91     private JTextField fUserOrg = new JTextField();
92     private JCheckBox fProject = new JCheckBox();
93     private JCheckBox fProduct = new JCheckBox();
94     private JCheckBox fBPartner = new JCheckBox();
95     private JLabel lAccountSeg = new JLabel();
96     private JCheckBox fMCampaign = new JCheckBox();
97     private JCheckBox fSRegion = new JCheckBox();
98     private JButton buttonLoadAcct = new JButton();
99     private JLabel lCountry = new JLabel();
100     private JLabel lCity = new JLabel();
101     private JComboBox fCountry = new JComboBox();
102     private JTextField fCity = new JTextField();
103     private JLabel lRegion = new JLabel();
104     private JComboBox fRegion = new JComboBox();
105
106     /**
107      * Static Init
108      * @throws Exception
109      */

110     private void jbInit() throws Exception JavaDoc
111     {
112         CompiereColor.setBackground(this);
113         centerPanel.setLayout(centerLayout);
114         String JavaDoc optional = Msg.translate(Env.getCtx(), "Optional");
115         //
116
lClientName.setLabelFor(fClientName);
117         lClientName.setText(Msg.translate(Env.getCtx(), "AD_Client_ID"));
118     // lClientName.setToolTipText("");
119
fClientName.setText("client");
120         fClientName.setColumns(20);
121         //
122
lOrgName.setLabelFor(fOrgName);
123         lOrgName.setText(Msg.translate(Env.getCtx(), "AD_Org_ID"));
124         fOrgName.setText("org");
125         fOrgName.setColumns(20);
126         //
127
lCurrency.setLabelFor(fCurrency);
128         lCurrency.setText(Msg.translate(Env.getCtx(), "C_Currency_ID"));
129     // lCurrency.setToolTipText("");
130
//
131
lUserClient.setLabelFor(fUserClient);
132         lUserClient.setText(Msg.parseTranslation(Env.getCtx(), "@AD_User_ID@ @AD_Client_ID@"));
133     // lUserClient.setToolTipText("User name for client level access");
134
fUserClient.setText("clientAdmin");
135         fUserClient.setColumns(20);
136         //
137
lUserOrg.setLabelFor(fUserOrg);
138         lUserOrg.setText(Msg.parseTranslation(Env.getCtx(), "@AD_User_ID@ @AD_Org_ID@"));
139     // lUserOrg.setToolTipText("");
140
fUserOrg.setText("clientUser");
141         fUserOrg.setColumns(20);
142         //
143
lCountry.setLabelFor(fCountry);
144         lCountry.setText(Msg.translate(Env.getCtx(), "C_Country_ID"));
145         lCity.setLabelFor(fCity);
146         lCity.setText(Msg.translate(Env.getCtx(), "C_City_ID"));
147         fCity.setText("city");
148         fCity.setColumns(20);
149         lRegion.setLabelFor(fRegion);
150         lRegion.setText(Msg.translate(Env.getCtx(), "C_Region_ID"));
151         lRegion.setToolTipText(optional);
152         //
153
lAccountSeg.setText(optional);
154         fBPartner.setSelected(true);
155         fBPartner.setText(Msg.translate(Env.getCtx(), "C_BPartner_ID"));
156         fProduct.setSelected(true);
157         fProduct.setText(Msg.translate(Env.getCtx(), "M_Product_ID"));
158         fProject.setText(Msg.translate(Env.getCtx(), "C_Project_ID"));
159         fMCampaign.setText(Msg.translate(Env.getCtx(), "C_Campaign_ID"));
160         fSRegion.setText(Msg.translate(Env.getCtx(), "C_SalesRegion_ID"));
161         //
162
buttonLoadAcct.setText(Msg.getMsg(Env.getCtx(), "LoadAccountingValues"));
163
164         centerPane.getViewport().add(centerPanel, null);
165         centerPanel.add(lClientName, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
166             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
167         centerPanel.add(fClientName, new GridBagConstraints(1, 0, 4, 1, 0.0, 0.0
168             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 0, 5, 5), 0, 0));
169         centerPanel.add(lOrgName, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
170             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
171         centerPanel.add(fOrgName, new GridBagConstraints(1, 1, 4, 1, 0.0, 0.0
172             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
173         centerPanel.add(lUserClient, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
174             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
175         centerPanel.add(fUserClient, new GridBagConstraints(1, 2, 4, 1, 0.0, 0.0
176             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
177         centerPanel.add(lUserOrg, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
178             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
179         centerPanel.add(fUserOrg, new GridBagConstraints(1, 3, 2, 1, 0.0, 0.0
180             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
181         centerPanel.add(lAccountSeg, new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0
182             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
183         centerPanel.add(fProject, new GridBagConstraints(1, 9, 3, 1, 0.0, 0.0
184             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
185         centerPanel.add(fBPartner, new GridBagConstraints(1, 8, 1, 1, 0.0, 0.0
186             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
187         centerPanel.add(fMCampaign, new GridBagConstraints(1, 10, 1, 1, 0.0, 0.0
188             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
189         centerPanel.add(fCurrency, new GridBagConstraints(1, 4, 2, 1, 0.0, 0.0
190             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
191         centerPanel.add(lCurrency, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0
192             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
193         centerPanel.add(buttonLoadAcct, new GridBagConstraints(1, 11, 2, 1, 0.0, 0.0
194             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 0, 5, 5), 0, 0));
195         centerPanel.add(lCountry, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0
196             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
197         centerPanel.add(lCity, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0
198             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
199         centerPanel.add(fCountry, new GridBagConstraints(1, 5, 3, 1, 0.0, 0.0
200             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
201         centerPanel.add(fCity, new GridBagConstraints(1, 6, 3, 1, 0.0, 0.0
202             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
203         centerPanel.add(fProduct, new GridBagConstraints(2, 8, 1, 1, 0.0, 0.0
204             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 0), 0, 0));
205         centerPanel.add(fSRegion, new GridBagConstraints(2, 10, 1, 1, 0.0, 0.0
206             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
207         centerPanel.add(lRegion, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0
208             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
209         centerPanel.add(fRegion, new GridBagConstraints(1, 7, 2, 1, 0.0, 0.0
210             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
211     } // jbInit
212

213     /**
214      * Dynamic Init
215      */

216     private void dynInit()
217     {
218         // Currency
219
String JavaDoc sql = "SELECT C_Currency_ID, Description FROM C_Currency ORDER BY 1"; // USD first
220
try
221         {
222             Statement stmt = DB.createStatement();
223             ResultSet rs = stmt.executeQuery(sql);
224             while (rs.next())
225                 fCurrency.addItem(new KeyNamePair(rs.getInt(1) , rs.getString(2)));
226             rs.close();
227             stmt.close();
228         }
229         catch (SQLException e1)
230         {
231             Log.error ("VSetup.dynInit -currency", e1);
232         }
233         fCurrency.setSelectedIndex(0);
234
235         // Country
236
sql = "SELECT C_Country_ID, Name FROM C_Country ORDER BY 1"; // US first
237
try
238         {
239             Statement stmt = DB.createStatement();
240             ResultSet rs = stmt.executeQuery(sql);
241             while (rs.next())
242                 fCountry.addItem(new KeyNamePair(rs.getInt(1) , rs.getString(2)));
243             rs.close();
244             stmt.close();
245         }
246         catch (SQLException e1)
247         {
248             Log.error ("VSetup.dynInit -country", e1);
249         }
250         fCountry.setSelectedIndex(0);
251
252         // Region (optional)
253
sql = "SELECT C_Region_ID, Name FROM C_Region ORDER BY C_Country_ID, Name";
254         try
255         {
256             fRegion.addItem(new KeyNamePair(0, " "));
257             Statement stmt = DB.createStatement();
258             ResultSet rs = stmt.executeQuery(sql);
259             while (rs.next())
260                 fRegion.addItem(new KeyNamePair(rs.getInt(1) , rs.getString(2)));
261             rs.close();
262             stmt.close();
263         }
264         catch (SQLException e1)
265         {
266             Log.error ("VSetup.dynInit -region", e1);
267         }
268         fRegion.setSelectedIndex(0);
269
270         // General Listeners
271
confirmPanel.addActionListener(this);
272         buttonLoadAcct.addActionListener(this);
273         confirmPanel.getOKButton().setEnabled(false);
274     } // dynInit
275

276     /**
277      * Dispose
278      */

279     public void dispose()
280     {
281         m_frame.dispose();
282     } // dispose
283

284     /**
285      * ActionListener
286      * @param e event
287      */

288     public void actionPerformed (ActionEvent e)
289     {
290         // load file
291
if (e.getSource().equals(buttonLoadAcct))
292             m_file = getFile();
293         // OK
294
else if (e.getActionCommand().equals(ConfirmPanel.A_OK) && m_file != null)
295         {
296             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
297             confirmPanel.getCancelButton().setEnabled(false);
298             confirmPanel.getOKButton().setEnabled(false);
299             if (createSetup())
300                 dispose();
301             setCursor(Cursor.getDefaultCursor());
302             confirmPanel.getCancelButton().setEnabled(true);
303             confirmPanel.getOKButton().setEnabled(true);
304         }
305         // Cancel
306
else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
307             dispose();
308     } // actionPerformed
309

310     /**
311      * Get File for CoA
312      * @return File
313      */

314     private File getFile()
315     {
316         File file = null;
317         String JavaDoc dirName = org.compiere.Compiere.getCompiereHome() + File.separator + "data" + File.separator + "import";
318         Log.trace(Log.l4_Data, "VSetup.getFile", dirName);
319         JFileChooser chooser = new JFileChooser(dirName);
320         chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
321         chooser.setMultiSelectionEnabled(false);
322         chooser.setDialogTitle(Msg.translate(Env.getCtx(), "LoadAccountingValues"));
323         chooser.addChoosableFileFilter(new ExtensionFileFilter("csv", "Comma Separated Values (*.csv)"));
324         // Try selecting file
325
file = new File(dirName + File.pathSeparator + "AccountingUS.csv");
326         if (file.exists())
327             chooser.setSelectedFile(file);
328
329         // Show it
330
if (chooser.showOpenDialog(this.getParent()) == JFileChooser.APPROVE_OPTION)
331             file = chooser.getSelectedFile();
332         else
333             file = null;
334         chooser = null;
335
336         if (file == null)
337             buttonLoadAcct.setText(Msg.translate(Env.getCtx(), "LoadAccountingValues"));
338         else
339             buttonLoadAcct.setText(file.getAbsolutePath());
340         confirmPanel.getOKButton().setEnabled(file != null);
341         m_frame.pack();
342         return file;
343     } // getFile
344

345     /*************************************************************************/
346
347     /**
348      * Create Setup
349      * @returns true if created
350      */

351     private boolean createSetup()
352     {
353         // Change critical characters ' => " \ => /
354
fClientName.setText(fClientName.getText().replace('\'','"'));
355         fClientName.setText(fClientName.getText().replace('\\','/'));
356         fOrgName.setText(fOrgName.getText().replace('\'','"'));
357         fOrgName.setText(fOrgName.getText().replace('\\','/'));
358         fUserClient.setText(fUserClient.getText().replace('\'','"'));
359         fUserClient.setText(fUserClient.getText().replace('\\','/'));
360         fUserOrg.setText(fUserOrg.getText().replace('\'','"'));
361         fUserOrg.setText(fUserOrg.getText().replace('\\','/'));
362
363         // Unique Client Name
364
String JavaDoc SQL = "UPDATE AD_CLient SET CreatedBy=0 WHERE Name='" + fClientName.getText() + "'";
365         if (DB.executeUpdate(SQL) != 0)
366         {
367             fClientName.setBackground(CompierePLAF.getFieldBackground_Error());
368             ADialog.error(m_WindowNo, this, "NotUnique", lClientName.getText());
369             return false;
370         }
371         fClientName.setBackground(CompierePLAF.getFieldBackground_Normal());
372
373 /**
374         // Unique Org Name
375         SQL = "UPDATE AD_Org SET CreatedBy=0 WHERE Value='" + fOrgName.getText() + "'";
376         if (DB.executeUpdate(SQL) != 0)
377         {
378             fOrgName.setBackground(CompierePLAF.getFieldBackground_Error());
379             ADialog.error(m_WindowNo, this, "NotUnique", lOrgName.getText());
380             return false;
381         }
382         fOrgName.setBackground(CompierePLAF.getFieldBackground_Normal());
383 **/

384
385         // Unique User Name
386
SQL = "UPDATE AD_User SET CreatedBy=0 WHERE Name='" + fUserClient.getText() + "'";
387         if (DB.executeUpdate(SQL) != 0)
388         {
389             fUserClient.setBackground(CompierePLAF.getFieldBackground_Error());
390             ADialog.error(m_WindowNo, this, "NotUnique", lUserClient.getText());
391             return false;
392         }
393         fUserClient.setBackground(CompierePLAF.getFieldBackground_Normal());
394         SQL = "UPDATE AD_User SET CreatedBy=0 WHERE Name='" + fUserOrg.getText() + "'";
395         if (DB.executeUpdate(SQL) != 0 || fUserClient.getText().equals(fUserOrg.getText()))
396         {
397             fUserOrg.setBackground(CompierePLAF.getFieldBackground_Error());
398             ADialog.error(m_WindowNo, this, "NotUnique", lUserOrg.getText());
399             return false;
400         }
401         fUserOrg.setBackground(CompierePLAF.getFieldBackground_Normal());
402
403         /**
404          * Start Creation
405          */

406         MSetup ms = new MSetup(Env.getCtx(), m_WindowNo);
407         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
408         // Step 1
409
boolean ok = ms.createClient(fClientName.getText(), fOrgName.getText(),
410             fUserClient.getText(), fUserOrg.getText());
411         String JavaDoc info = ms.getInfo();
412
413         if (ok)
414         {
415             // Generate Accounting
416
KeyNamePair p = (KeyNamePair)fCurrency.getSelectedItem();
417             ms.createAccounting(p.getKey(), p.getName(),
418                 fProduct.isSelected(), fBPartner.isSelected(), fProject.isSelected(),
419                 fMCampaign.isSelected(), fSRegion.isSelected(),
420                 m_file);
421             // Generate Entities
422
p = (KeyNamePair)fCountry.getSelectedItem();
423             int C_Country_ID = p.getKey();
424             p = (KeyNamePair)fRegion.getSelectedItem();
425             int C_Region_ID = p.getKey();
426             ms.createEntities(C_Country_ID, fCity.getText(), C_Region_ID);
427             info += ms.getInfo();
428             // Create Print Documents
429
PrintUtil.setupPrintForm(ms.getAD_Client_ID());
430         }
431
432         setCursor(Cursor.getDefaultCursor());
433         ADialog.info(m_WindowNo, this, "VSetup", info.toString());
434         return ok;
435     } // createSetup
436

437 } // VSetup
438
Popular Tags