KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > AMenuStartItem


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-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps;
15
16 import java.awt.event.*;
17 import java.sql.*;
18 import javax.swing.*;
19
20 import org.compiere.apps.form.*;
21 import org.compiere.util.*;
22
23
24 /**
25  * Start Menu Item & UpdateProgress Bar
26  *
27  * @author Jorg Janke
28  * @version $Id: AMenuStartItem.java,v 1.3 2003/09/29 01:04:42 jjanke Exp $
29  */

30 public class AMenuStartItem extends Thread JavaDoc implements ActionListener
31 {
32     /**
33      * Start Menu Item
34      *
35      * @param ID ID
36      * @param isMenu false if Workflow
37      * @param name Name
38      * @param menu Menu
39      */

40     public AMenuStartItem (int ID, boolean isMenu, String JavaDoc name, AMenu menu)
41     {
42         m_ID = ID;
43         m_isMenu = isMenu;
44         m_name = name;
45         m_menu = menu;
46         if (menu != null)
47             m_increment = (menu.progressBar.getMaximum()-menu.progressBar.getMinimum()) / 5;
48     } // UpdateProgress
49

50     /** The ID */
51     private int m_ID = 0;
52     private boolean m_isMenu = false;
53     private String JavaDoc m_name;
54     private AMenu m_menu;
55
56     // Reset Progress Bar
57
private Runnable JavaDoc m_resetPB = new Runnable JavaDoc()
58     {
59         public void run()
60         {
61             m_value = 0;
62             if (m_menu != null)
63                 m_menu.progressBar.setValue(0);
64         }
65     };
66     // Progress Bar tick
67
private Runnable JavaDoc m_tickPB = new Runnable JavaDoc()
68     {
69         public void run()
70         {
71             if (m_menu == null)
72                 return;
73             // 100/5 => 20 ticks - every .5 sec => 10 seconds loadtime
74
final int tick = 5;
75             if (m_menu.progressBar.getValue() < (m_menu.progressBar.getMaximum() - tick))
76                 m_menu.progressBar.setValue(m_menu.progressBar.getValue() + tick);
77         }
78     };
79     // Progress Bar max state
80
private Runnable JavaDoc m_updatePB = new Runnable JavaDoc()
81     {
82         public void run()
83         {
84             if (m_menu == null)
85                 return;
86             m_value += m_increment;
87             if (m_menu.progressBar.getValue() > m_value) // max value
88
m_menu.progressBar.setValue(m_value);
89         }
90     };
91     private int m_value = 0;
92     private int m_increment = 20;
93     private javax.swing.Timer JavaDoc m_timer = new javax.swing.Timer JavaDoc(500, this); // every 1/2 second
94

95
96     /**
97      * Start Menu Item
98      */

99     public void run()
100     {
101         if (m_menu != null)
102             m_menu.setBusy (true);
103         SwingUtilities.invokeLater(m_resetPB);
104         m_timer.start();
105         SwingUtilities.invokeLater(m_updatePB);
106         try
107         {
108             String JavaDoc SQL = "SELECT * FROM AD_Menu WHERE AD_Menu_ID=?";
109             if (!m_isMenu)
110                 SQL = "SELECT * FROM AD_WF_Node WHERE AD_WF_Node_ID=?";
111             PreparedStatement pstmt = DB.prepareStatement(SQL);
112             pstmt.setInt(1, m_ID);
113             ResultSet rs = pstmt.executeQuery();
114
115             SwingUtilities.invokeLater(m_updatePB);
116             while(rs.next()) // should only be one
117
{
118                 String JavaDoc Action = rs.getString("Action");
119                 String JavaDoc IsSOTrx = "Y";
120                 if (m_isMenu)
121                     IsSOTrx = rs.getString("IsSOTrx");
122                 int cmd;
123                 if (Action.equals("W")) // Window
124
{
125                     cmd = rs.getInt("AD_Window_ID");
126                     startWindow(0, cmd, IsSOTrx);
127                 }
128                 else if (Action.equals("P") || Action.equals("R")) // Process & Report
129
{
130                     cmd = rs.getInt("AD_Process_ID");
131                     startProcess(cmd, IsSOTrx);
132                 }
133                 else if (Action.equals("B")) // Workbench
134
{
135                     cmd = rs.getInt("AD_Workbench_ID");
136                     startWindow (cmd, 0, IsSOTrx);
137                 }
138                 else if (Action.equals("F")) // WorkFlow
139
{
140                     if (m_isMenu)
141                         cmd = rs.getInt("AD_Workflow_ID");
142                     else
143                         cmd = rs.getInt("Workflow_ID");
144                     if (m_menu != null)
145                         m_menu.startWorkFlow(cmd);
146                 }
147                 else if (Action.equals("T")) // Task
148
{
149                     cmd = rs.getInt("AD_Task_ID");
150                     startTask(cmd);
151                 }
152                 else if (Action.equals("X")) // Form
153
{
154                     cmd = rs.getInt("AD_Form_ID");
155                     startForm(cmd);
156                 }
157                 else
158                     Log.error ("AMenuStartItem.run - No valid Action in ID=" + m_ID);
159             } // for all records
160

161
162             SwingUtilities.invokeLater(m_updatePB);
163             rs.close();
164             pstmt.close();
165         }
166         catch (Exception JavaDoc e)
167         {
168             Log.error("AMenuStartItem.run ID=" + m_ID, e);
169             ADialog.error(0, null, "Error", Msg.parseTranslation(Env.getCtx(), e.getMessage()));
170         }
171
172         try {Thread.sleep(1000);} // 1 sec
173
catch (InterruptedException JavaDoc ie) {}
174
175         // ready for next
176
m_timer.stop();
177         SwingUtilities.invokeLater(m_resetPB);
178         if (m_menu != null)
179         {
180             m_menu.updateMemoryInfo();
181             m_menu.setBusy(false);
182         }
183     } // run
184

185
186     /**
187      * Actlion Listener for Timer
188      * @param e event
189      */

190     public void actionPerformed (ActionEvent e)
191     {
192         SwingUtilities.invokeLater(m_tickPB);
193     } // actionPerformed
194

195         /**
196          * Start Window
197          *
198          * @param AD_Workbench_ID workbench
199          * @param AD_Window_ID window
200          * @param IsSOTrx is SO trx
201          */

202         private void startWindow(int AD_Workbench_ID, int AD_Window_ID, String JavaDoc IsSOTrx)
203         {
204             SwingUtilities.invokeLater(m_updatePB); // 1
205
AWindow frame = new AWindow();
206             boolean isSO = false;
207             if (IsSOTrx != null && IsSOTrx.equals("Y"))
208                 isSO = true;
209             boolean OK = false;
210             if (AD_Workbench_ID != 0)
211                 OK = frame.initWorkbench(AD_Workbench_ID, isSO);
212             else
213                 OK = frame.initWindow(AD_Window_ID, null, isSO); // No Query Value
214
if (!OK)
215                 return;
216
217             SwingUtilities.invokeLater(m_updatePB); // 2
218
frame.pack();
219
220             // Center the window
221
SwingUtilities.invokeLater(m_updatePB); // 3
222
AEnv.showCenterScreen(frame);
223 // if (wfPanel.isVisible())
224
// m_WF_Window = frame; // maintain one reference
225
frame = null;
226         } // startWindow
227

228         /**
229          * Start Process.
230          * Start/show Process Dialog which calls ProcessCtl
231          * @param AD_Process_ID process
232          * @param IsSOTrx is SO trx
233          */

234         private void startProcess (int AD_Process_ID, String JavaDoc IsSOTrx)
235         {
236             SwingUtilities.invokeLater(m_updatePB); // 1
237
boolean isSO = false;
238             if (IsSOTrx != null && IsSOTrx.equals("Y"))
239                 isSO = true;
240             m_timer.stop();
241             ProcessDialog pd = new ProcessDialog (AD_Process_ID, isSO);
242             if (!pd.init())
243                 return;
244             m_timer.start();
245
246             SwingUtilities.invokeLater(m_updatePB); // 2
247
pd.pack();
248             // Center the window
249
SwingUtilities.invokeLater(m_updatePB); // 3
250
AEnv.showCenterScreen(pd);
251         } // startProcess
252

253     /**
254      * Start OS Task
255      * @param AD_Task_ID task
256      */

257     private void startTask (int AD_Task_ID)
258     {
259         SwingUtilities.invokeLater(m_updatePB); // 1
260
// Get Command
261
String JavaDoc command = null;
262         String JavaDoc SQL = "SELECT OS_Command FROM AD_Task WHERE AD_Task_ID=?";
263         try
264         {
265             PreparedStatement pstmt = DB.prepareStatement(SQL);
266             pstmt.setInt(1, AD_Task_ID);
267             ResultSet rs = pstmt.executeQuery();
268             if (rs.next())
269                 command = rs.getString(1);
270             rs.close();
271             pstmt.close();
272         }
273         catch (SQLException e)
274         {
275             Log.error ("AMenuStartItem.startTask", e);
276         }
277         if (command == null)
278             return;
279
280         SwingUtilities.invokeLater(m_updatePB); // 2
281
ATask.start(m_name, command);
282     } // startTask
283

284     /**
285      * Start Form
286      * @param AD_Form_ID form
287      */

288     private void startForm (int AD_Form_ID)
289     {
290         FormFrame ff = new FormFrame();
291         SwingUtilities.invokeLater(m_updatePB); // 1
292
ff.openForm(AD_Form_ID);
293         SwingUtilities.invokeLater(m_updatePB); // 2
294
ff.pack();
295         // Center the window
296
SwingUtilities.invokeLater(m_updatePB); // 3
297
AEnv.showCenterScreen(ff);
298     } // startForm
299

300 } // StartItem
301
Popular Tags