KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
20 import java.util.*;
21 import java.sql.*;
22
23 import org.compiere.util.*;
24 import org.compiere.apps.*;
25 import org.compiere.impexp.*;
26 import org.compiere.plaf.*;
27 import org.compiere.swing.*;
28
29
30 /**
31  * Fixed length file import
32  *
33  * @author Jorg Janke
34  * @version $Id: VFileImport.java,v 1.14 2003/01/21 05:24:47 jjanke Exp $
35  */

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

42     public VFileImport()
43     {
44     } // FileImport
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, "VFileImport.init");
54         m_WindowNo = WindowNo;
55         m_frame = frame;
56         try
57         {
58             jbInit();
59             dynInit();
60             frame.getContentPane().add(northPanel, BorderLayout.NORTH);
61             frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
62             frame.getContentPane().add(confirmPanel, BorderLayout.SOUTH);
63         }
64         catch(Exception JavaDoc e)
65         {
66             Log.error("VFileImport.init", e);
67         }
68     } // init
69

70     /** Window No */
71     private int m_WindowNo = 0;
72     /** FormFrame */
73     private FormFrame m_frame;
74
75     private ArrayList m_data = new ArrayList();
76     private ImpFormat m_format;
77     private JLabel[] m_labels;
78     private JTextField[] m_fields;
79     private int m_record = -1;
80     //
81
private static final String JavaDoc s_none = "----"; // no format indicator
82
//
83
private CPanel northPanel = new CPanel();
84     private JButton bFile = new JButton();
85     private JComboBox pickFormat = new JComboBox();
86     private CPanel centerPanel = new CPanel();
87     private BorderLayout centerLayout = new BorderLayout();
88     private JScrollPane rawDataPane = new JScrollPane();
89     private JTextArea rawData = new JTextArea();
90     private JScrollPane previewPane = new JScrollPane();
91     private CPanel previewPanel = new CPanel();
92     private ConfirmPanel confirmPanel = new ConfirmPanel(true);
93     private JLabel info = new JLabel();
94     private JLabel labelFormat = new JLabel();
95     private GridBagLayout previewLayout = new GridBagLayout();
96     private JButton bNext = new JButton();
97     private JButton bPrevious = new JButton();
98     private JLabel record = new JLabel();
99
100     /**
101      * Static Init
102      * @throws Exception
103      */

104     private void jbInit() throws Exception JavaDoc
105     {
106         CompiereColor.setBackground(this);
107         bFile.setText(Msg.getMsg(Env.getCtx(), "FileImportFile"));
108         bFile.setToolTipText(Msg.getMsg(Env.getCtx(), "FileImportFileInfo"));
109         bFile.addActionListener(this);
110         info.setText(" ");
111         labelFormat.setText(Msg.translate(Env.getCtx(), "AD_ImpFormat_ID"));
112         //
113
bNext.setToolTipText(Msg.getMsg(Env.getCtx(), "Next"));
114         bNext.setMargin(new Insets(2, 2, 2, 2));
115         bNext.setText(">");
116         bNext.addActionListener(this);
117         record.setText("-");
118         bPrevious.setToolTipText(Msg.getMsg(Env.getCtx(), "Previous"));
119         bPrevious.setMargin(new Insets(2, 2, 2, 2));
120         bPrevious.setText("<");
121         bPrevious.addActionListener(this);
122         //
123
northPanel.setBorder(BorderFactory.createEtchedBorder());
124         northPanel.add(bFile, null);
125         northPanel.add(info, null);
126         northPanel.add(labelFormat, null);
127         northPanel.add(pickFormat, null);
128         northPanel.add(bPrevious, null);
129         northPanel.add(record, null);
130         northPanel.add(bNext, null);
131         //
132
centerPanel.setLayout(centerLayout);
133         rawData.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 10));
134         rawData.setColumns(80);
135         rawData.setRows(4);
136         rawDataPane.getViewport().add(rawData, null);
137         centerPanel.add(rawDataPane, BorderLayout.NORTH);
138         centerPanel.add(previewPane, BorderLayout.CENTER);
139         //
140
previewPanel.setLayout(previewLayout);
141         previewPane.getViewport().add(previewPanel, null);
142         previewPane.setPreferredSize(new Dimension(700,80));
143         //
144
confirmPanel.addActionListener(this);
145     } // jbInit
146

147     /**
148      * Dispose
149      */

150     public void dispose()
151     {
152         m_frame.dispose();
153     } // dispose
154

155     /**
156      * Dynamic Init
157      */

158     private void dynInit()
159     {
160         // Load Formats
161
pickFormat.addItem(s_none);
162         String JavaDoc SQL = "SELECT Name from AD_ImpFormat";
163         try
164         {
165             PreparedStatement pstmt = DB.prepareStatement(SQL);
166             ResultSet rs = pstmt.executeQuery();
167             while (rs.next())
168                 pickFormat.addItem(rs.getString(1));
169             rs.close();
170             pstmt.close();
171         }
172         catch (SQLException e)
173         {
174             Log.error ("VFileImport.dynInit", e);
175         }
176         pickFormat.setSelectedIndex(0);
177         pickFormat.addActionListener(this);
178     } // dynInit
179

180     /*************************************************************************/
181
182     /**
183      * Action Listener
184      * @param e event
185      */

186     public void actionPerformed (ActionEvent e)
187     {
188         if (e.getSource() == bFile)
189             cmd_loadFile();
190         else if (e.getSource() == pickFormat)
191             cmd_loadFormat();
192         else if (e.getSource() == bNext)
193             cmd_applyFormat(true);
194         else if (e.getSource() == bPrevious)
195             cmd_applyFormat(false);
196         else if (e.getActionCommand().equals(ConfirmPanel.A_OK))
197         {
198             cmd_process();
199             dispose();
200         }
201         else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
202             dispose();
203     } // actionPerformed
204

205
206     /*************************************************************************/
207
208     /**
209      * Load File
210      */

211     private void cmd_loadFile()
212     {
213         String JavaDoc directory = org.compiere.Compiere.getCompiereHome() + File.separator + "data" + File.separator + "import";
214         Log.trace(Log.l3_Util, "VFileImport.cmd_loadFile", directory);
215
216         //
217
JFileChooser chooser = new JFileChooser(directory);
218         chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
219         chooser.setMultiSelectionEnabled(false);
220         chooser.setDialogTitle(Msg.getMsg(Env.getCtx(), "FileImportFileInfo"));
221         if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
222             return;
223         bFile.setText(chooser.getSelectedFile().getName());
224         setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
225         m_data.clear();
226         try
227         {
228             // see NaturalAccountMap
229
BufferedReader in = new BufferedReader(new FileReader(chooser.getSelectedFile()), 10240);
230             // not safe see p108 Network pgm
231
String JavaDoc s = null;
232             while ((s = in.readLine()) != null)
233             {
234                 m_data.add(s);
235                 if (m_data.size() < 100)
236                 {
237                     rawData.append(s);
238                     rawData.append("\n");
239                 }
240             }
241             in.close();
242             rawData.setCaretPosition(0);
243         }
244         catch (Exception JavaDoc e)
245         {
246             Log.error("VFileImport.cmd_loadFile", e);
247             bFile.setText(Msg.getMsg(Env.getCtx(), "FileImportFile"));
248         }
249         int length = m_data.get(1).toString().length();
250         info.setText(Msg.getMsg(Env.getCtx(), "Records") + "=" + m_data.size()
251             + ", " + Msg.getMsg(Env.getCtx(), "Length") + "=" + length + " ");
252         setCursor (Cursor.getDefaultCursor());
253     } // cmd_loadFile
254

255     /**
256      * Load Format
257      */

258     private void cmd_loadFormat()
259     {
260         // clear panel
261
previewPanel.removeAll();
262         //
263
String JavaDoc formatName = pickFormat.getSelectedItem().toString();
264         if (formatName.equals(s_none))
265             return;
266         m_format = ImpFormat.load (formatName);
267         if (m_format == null)
268         {
269             ADialog.error(m_WindowNo, this, "FileImportNoFormat", formatName);
270             return;
271         }
272
273         // pointers
274
int size = m_format.getRowCount();
275         m_labels = new JLabel[size];
276         m_fields = new JTextField[size];
277         for (int i = 0; i < size; i++)
278         {
279             ImpFormatRow row = m_format.getRow(i);
280             m_labels[i] = new JLabel (row.getColumnName());
281             previewPanel.add(m_labels[i], new GridBagConstraints(i, 0, 1, 1, 0.0, 0.0,
282                 GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
283             //
284
int length = row.getEndNo() - row.getStartNo();
285             if (length <= 5)
286                 length = 5;
287             else if (length > 20)
288                 length = 20;
289             m_fields[i] = new JTextField (length);
290             previewPanel.add(m_fields[i], new GridBagConstraints(i, 1, 1, 1, 0.0, 0.0,
291                 GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
292         }
293         m_record = -1;
294         record.setText("-");
295     } // cmd_format
296

297     /**
298      * Apply Current Pattern
299      * @param next next
300      */

301     private void cmd_applyFormat (boolean next)
302     {
303         if (m_format == null)
304             return;
305
306         // set position
307
if (next)
308             m_record++;
309         else
310             m_record--;
311         if (m_record < 0)
312             m_record = 0;
313         else if (m_record >= m_data.size())
314             m_record = m_data.size() - 1;
315         record.setText(" " + String.valueOf(m_record+1) + " ");
316         //
317
String JavaDoc[] info = m_format.parseLine(m_data.get(m_record).toString(), false, true, false); // no label, trace, no ignore
318
int size = m_format.getRowCount();
319         if (info.length != size)
320             Log.error("VFileImport.cmd_applyFormat - FormatElements=" + size + " != Fields=" + info.length);
321         for (int i = 0; i < size; i++)
322         {
323             m_fields[i].setText(info[i]);
324             m_fields[i].setCaretPosition(0);
325         }
326     } // cmd_applyFormat
327

328     /*************************************************************************/
329
330     /**
331      * Process File
332      */

333     private void cmd_process()
334     {
335         Log.trace(Log.l3_Util, "VFileImport.cmd_process");
336         /** @todo import process should run on separate thread ! */
337         if (m_format == null)
338         {
339             ADialog.error(m_WindowNo, this, "FileImportNoFormat");
340             return;
341         }
342
343         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
344         confirmPanel.setEnabled(false);
345         // For all rows - update/insert DB table
346
int row = 0;
347         int imported = 0;
348         for (row = 0; row < m_data.size(); row++)
349             if (m_format.updateDB(Env.getCtx(), m_data.get(row).toString()))
350                 imported++;
351         setCursor(Cursor.getDefaultCursor());
352         //
353
ADialog.info(m_WindowNo, this, "FileImportR/I", row + " / " + imported + "#");
354     } // cmd_process
355

356 } // FileImport
357
Popular Tags