KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.*;
21
22 import org.compiere.util.*;
23 import org.compiere.model.*;
24 import org.compiere.apps.*;
25 import org.compiere.plaf.*;
26 import org.compiere.swing.*;
27
28 /**
29  * Displays valid Document Action Options based on context
30  *
31  * @author Jorg Janke
32  * @version $Id: VDocAction.java,v 1.8 2003/10/10 20:54:14 jjanke Exp $
33  */

34 public class VDocAction extends JDialog implements ActionListener
35 {
36     /**
37      * Constructor
38      * started from APanel
39      * @param WindowNo
40      * @param mTab
41      * @param button
42      */

43     public VDocAction(int WindowNo, MTab mTab, VButton button)
44     {
45         super(Env.getWindow(WindowNo), Msg.translate(Env.getCtx(), "DocAction"), true);
46         Log.trace(Log.l3_Util, "VDocAction");
47         m_WindowNo = WindowNo;
48         m_mTab = mTab;
49         //
50
try
51         {
52             jbInit();
53         }
54         catch(Exception JavaDoc ex)
55         {
56             Log.error("VDocAction", ex);
57         }
58
59         // dynamic init preparation
60
m_AD_Table_ID = Env.getContextAsInt(Env.getCtx(), WindowNo, "BaseTable_ID");
61         if (s_value == null)
62             readReference();
63         //
64
dynInit();
65         //
66
AEnv.positionCenterWindow(Env.getWindow(WindowNo), this);
67     } // VDocAction
68

69     //
70
private int m_WindowNo = 0;
71     private int m_AD_Table_ID;
72     private boolean m_OKpressed = false;
73     private MTab m_mTab;
74     //
75
private static String JavaDoc[] s_value = null;
76     private static String JavaDoc[] s_name;
77     private static String JavaDoc[] s_description;
78     //
79
private CPanel mainPanel = new CPanel();
80     private BorderLayout mainLayout = new BorderLayout();
81     private CPanel northPanel = new CPanel();
82     private CComboBox actionCombo = new CComboBox();
83     private JLabel actionLabel = new JLabel();
84     private JScrollPane centerPane = new JScrollPane();
85     private JTextArea message = new JTextArea();
86     private FlowLayout northLayout = new FlowLayout();
87     private ConfirmPanel confirmPanel = new ConfirmPanel(true);
88
89     /**
90      * Static Init
91      * @throws Exception
92      */

93     void jbInit() throws Exception JavaDoc
94     {
95         CompiereColor.setBackground(this);
96         mainPanel.setLayout(mainLayout);
97         actionLabel.setText(Msg.translate(Env.getCtx(), "DocAction"));
98         actionCombo.addActionListener(this);
99         message.setLineWrap(true);
100         message.setPreferredSize(new Dimension(350, 35));
101         message.setWrapStyleWord(true);
102         message.setBackground(CompierePLAF.getFieldBackground_Inactive());
103         message.setEditable(false);
104         northPanel.setLayout(northLayout);
105         northLayout.setAlignment(FlowLayout.RIGHT);
106         getContentPane().add(mainPanel);
107         mainPanel.add(northPanel, BorderLayout.NORTH);
108         northPanel.add(actionLabel, null);
109         northPanel.add(actionCombo, null);
110         mainPanel.add(centerPane, BorderLayout.CENTER);
111         centerPane.getViewport().add(message, null);
112         //
113
mainPanel.add(confirmPanel, BorderLayout.SOUTH);
114         confirmPanel.addActionListener(this);
115     } // jbInit
116

117     /**
118      * Dynamic Init - determine valid DocActions based on DocStatus for the different documents.
119      * <pre>
120      * DocStatus (131)
121             ?? Unknown
122             AP * Approved
123             CH Changed
124             CL * Closed
125             CO * Completed
126             DR Drafted
127             IN Inactive
128             NA Not Approved
129             PE Posting Error
130             PO * Posted
131             PR * Printed
132             RE Reversed
133             TE Transfer Error
134             TR * Transferred
135             VO * Voided
136             XX Being Processed
137      *
138      * DocAction (135)
139             -- <None>
140             AP * Approve
141             CL * Close
142             CO * Complete
143             PO * Post
144             PR * Print
145             RA Reverse - Accrual
146             RC Reverse - Correction
147             RE RE-activate
148             RJ Reject
149             TR * Transfer
150             VO * Void
151             XL Unlock
152      * </pre>
153      */

154     private void dynInit()
155     {
156         String JavaDoc DocStatus = (String JavaDoc)m_mTab.getValue("DocStatus");
157         String JavaDoc DocAction = (String JavaDoc)m_mTab.getValue("DocAction");
158         String JavaDoc Processing = (String JavaDoc)m_mTab.getValue("Processing");
159         String JavaDoc OrderType = Env.getContext(Env.getCtx(), m_WindowNo, "OrderType");
160         String JavaDoc IsSOTrx = Env.getContext(Env.getCtx(), m_WindowNo, "IsSOTrx");
161
162         if (DocStatus == null)
163         {
164             message.setText("*** ERROR ***");
165             return;
166         }
167
168         Log.trace(Log.l5_DData, "DocStatus=" + DocStatus + ", DocAction="
169             + DocAction + ", OrderType=" + OrderType + ", IsSOTrx=" + IsSOTrx
170             + ", Processing=" + Processing + ", AD_Table_ID=" + m_AD_Table_ID);
171
172
173         String JavaDoc[] options = new String JavaDoc[s_value.length];
174         int index = 0;
175         //
176

177         /*******************
178          * General Actions
179          */

180
181         // Locked
182
if (Processing != null && Processing.equals("Y"))
183         {
184             options[index++] = "XL"; // Unlock
185
}
186
187         // Approval required .. NA
188
else if (DocStatus.equals("NA"))
189         {
190             options[index++] = "AP"; // Approve -> IP
191
options[index++] = "RJ"; // Reject -> NA
192
options[index++] = "VO"; // Void -> VO
193
}
194         // Draft .. DR
195
else if (DocStatus.equals("DR"))
196         {
197             options[index++] = "CO"; // Complete -> CO
198
options[index++] = "VO"; // Void -> VO
199
}
200         // In Process .. IP
201
else if (DocStatus.equals("IP"))
202         {
203             options[index++] = "CO"; // Complete -> CO
204
options[index++] = "VO"; // Void -> VO
205
}
206         // Complete .. CO
207
else if (DocStatus.equals("CO"))
208         {
209             options[index++] = "CL"; // Close -> CL
210
}
211         // Closed, Voided, REversed .. CL/VO/RE
212
else if (DocStatus.equals("CL") || DocStatus.equals("VO") || DocStatus.equals("RE"))
213             return;
214
215
216         /********************
217          * Order
218          */

219         if (m_AD_Table_ID == 259)
220         {
221             // Draft .. DR
222
if (DocStatus.equals("DR"))
223             {
224                 options[index++] = "PR";// Process -> IP
225
// Draft Sales Order Quote/Proposal - Process
226
if ("Y".equals(IsSOTrx)
227                     && ("OB".equals(OrderType) || "ON".equals(OrderType)))
228                     DocAction = "PR";
229             }
230             // Complete .. CO
231
else if (DocStatus.equals("CO"))
232                 options[index++] = "RE";// REactivate -> RE
233
}
234         /********************
235          * Invoice
236          */

237         else if (m_AD_Table_ID == 318)
238         {
239             // Complete .. CO
240
if (DocStatus.equals("CO"))
241                 options[index++] = "RC";// RevCorrect -> RC
242
}
243         /********************
244          * Payment
245          */

246         else if (m_AD_Table_ID == 335)
247         {
248             // Complete .. CO
249
if (DocStatus.equals("CO"))
250                 options[index++] = "RC";// RevCorrect -> RC
251
}
252
253
254         /**
255          * Fill actionCombo
256          */

257         for (int i = 0; i < index; i++)
258         {
259             // Serach for option and add it
260
boolean added = false;
261             for (int j = 0; j < s_value.length && !added; j++)
262                 if (options[i].equals(s_value[j]))
263                 {
264                     actionCombo.addItem(s_name[j]);
265                     added = true;
266                 }
267         }
268
269         // setDefault
270
if (DocAction.equals("--")) // If None, suggest closing
271
DocAction = "CL";
272         String JavaDoc defaultV = "";
273         for (int i = 0; i < s_value.length && defaultV.equals(""); i++)
274             if (DocAction.equals(s_value[i]))
275                 defaultV = s_name[i];
276         if (!defaultV.equals(""))
277             actionCombo.setSelectedItem(defaultV);
278     } // dynInit
279

280     /**
281      * Number of options available (to decide to display it)
282      * @return item count
283      */

284     public int getNumberOfOptions()
285     {
286         return actionCombo.getItemCount();
287     } // getNumberOfOptions
288

289     /**
290      * Should the process be started?
291      * @return OK pressed
292      */

293     public boolean getStartProcess()
294     {
295         return m_OKpressed;
296     } // getStartProcess
297

298
299     /**
300      * Fill Vector with DocAction Ref_List(135) values
301      */

302     private void readReference()
303     {
304         String JavaDoc SQL;
305         if (Env.isBaseLanguage(Env.getCtx(), "AD_Ref_List"))
306             SQL = "SELECT Value, Name, Description FROM AD_Ref_List "
307                 + "WHERE AD_Reference_ID=135 ORDER BY Name";
308         else
309             SQL = "SELECT l.Value, t.Name, t.Description "
310                 + "FROM AD_Ref_List l, AD_Ref_List_Trl t "
311                 + "WHERE l.AD_Ref_List_ID=t.AD_Ref_List_ID"
312                 + " AND t.AD_Language='" + Env.getAD_Language(Env.getCtx()) + "'"
313                 + " AND l.AD_Reference_ID=135 ORDER BY t.Name";
314
315         ArrayList v_value = new ArrayList();
316         ArrayList v_name = new ArrayList();
317         ArrayList v_description = new ArrayList();
318         try
319         {
320             PreparedStatement pstmt = DB.prepareStatement(SQL);
321             ResultSet rs = pstmt.executeQuery();
322             while (rs.next())
323             {
324                 String JavaDoc value = rs.getString(1);
325                 String JavaDoc name = rs.getString(2);
326                 String JavaDoc description = rs.getString(3);
327                 if (description == null)
328                     description = "";
329                 //
330
v_value.add(value);
331                 v_name.add(name);
332                 v_description.add(description);
333             }
334             rs.close();
335             pstmt.close();
336         }
337         catch (SQLException e)
338         {
339             Log.error("VDocAction.readReference", e);
340         }
341
342         // convert to arrays
343
int size = v_value.size();
344         s_value = new String JavaDoc[size];
345         s_name = new String JavaDoc[size];
346         s_description = new String JavaDoc[size];
347         for (int i = 0; i < size; i++)
348         {
349             s_value[i] = (String JavaDoc)v_value.get(i);
350             s_name[i] = (String JavaDoc)v_name.get(i);
351             s_description[i] = (String JavaDoc)v_description.get(i);
352         }
353     } // readReference
354

355     /**
356      * ActionListener
357      * @param e
358      */

359     public void actionPerformed(ActionEvent e)
360     {
361         if (e.getActionCommand().equals(ConfirmPanel.A_OK))
362         {
363             if (save())
364             {
365                 dispose();
366                 m_OKpressed = true;
367             }
368         }
369         else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
370             dispose();
371         else if (e.getSource() != actionCombo)
372             return;
373
374         /**
375          * ActionCombo: display the description for the selection
376          */

377         int index = getSelectedIndex();
378         // Display descriprion
379
if (index != -1)
380             message.setText(s_description[index]);
381     } // actionPerformed
382

383
384     /**
385      * Get index of selected choice
386      * @return index or -a
387      */

388     private int getSelectedIndex()
389     {
390         int index = -1;
391
392         // get Selection
393
String JavaDoc sel = (String JavaDoc)actionCombo.getSelectedItem();
394         if (sel == null)
395             return index;
396
397         // find it in vector
398
for (int i = 0; i < s_name.length && index == -1; i++)
399             if (sel.equals(s_name[i]))
400                 index = i;
401         //
402
return index;
403     } // getSelectedIndex
404

405     /**
406      * Save to Database
407      * @return true if saved to Tab
408      */

409     private boolean save()
410     {
411         int index = getSelectedIndex();
412         if (index == -1)
413             return false;
414
415         // Save Selection
416
m_mTab.setValue("DocAction", s_value[index]);
417         return true;
418     } // save
419

420 } // VDocAction
421
Popular Tags