KickJava   Java API By Example, From Geeks To Geeks.

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


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;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import javax.swing.*;
19
20 import org.compiere.util.*;
21 import org.compiere.plaf.*;
22
23 /**
24  * Application Task
25  *
26  * @author Jorg Janke
27  * @version $Id: ATask.java,v 1.3 2002/08/12 01:55:13 danb Exp $
28  */

29 public class ATask extends JFrame implements ActionListener
30 {
31     /**
32      * Start Application Task
33      * @param title
34      * @param command
35      */

36     static public void start (final String JavaDoc title, final String JavaDoc command)
37     {
38         new Thread JavaDoc()
39         {
40             public void run()
41             {
42                 new ATask(title, command);
43             }
44         }.start();
45     } // start
46

47     /*************************************************************************/
48
49     /** @todo Write to process (currently just reads) */
50
51     /**
52      * IDE Constructor.
53      * You need to set the Title and call executeCommand manually
54      */

55     public ATask()
56     {
57         this("","");
58     } // ATask
59

60     /**
61      * Full Constructor
62      * @param title
63      * @param command
64      */

65     public ATask (String JavaDoc title, String JavaDoc command)
66     {
67         super (title);
68         Log.trace(Log.l1_User, "ATask", title + " - " + command);
69         try
70         {
71             jbInit();
72         }
73         catch(Exception JavaDoc e)
74         {
75             Log.error("ATask",e);
76         }
77         AEnv.showCenterScreen(this);
78         executeCommand (command);
79     } // ATask
80

81     private Task m_task = null;
82
83
84     private ConfirmPanel confirmPanel = new ConfirmPanel(true);
85     private JScrollPane infoScrollPane = new JScrollPane();
86     private JTextArea info = new JTextArea();
87
88     /**
89      * Static Layout
90      * @throws Exception
91      */

92     private void jbInit() throws Exception JavaDoc
93     {
94         CompiereColor.setBackground(this);
95         info.setEditable(false);
96         info.setBackground(CompierePLAF.getFieldBackground_Inactive());
97         infoScrollPane.getViewport().add(info, null);
98         infoScrollPane.setPreferredSize(new Dimension(500,300));
99         this.getContentPane().add(infoScrollPane, BorderLayout.CENTER);
100         this.getContentPane().add(confirmPanel, BorderLayout.SOUTH);
101         //
102
confirmPanel.addActionListener(this);
103         confirmPanel.getOKButton().setEnabled(false);
104     } // jbInit
105

106     /**
107      * Execute Command
108      * @param command
109      */

110     public void executeCommand (String JavaDoc command)
111     {
112         String JavaDoc cmd = Msg.parseTranslation(Env.getCtx(), command);
113         Log.trace(Log.l3_Util, "ATask.executeCommand", cmd);
114         if (command == null || command.equals(""))
115             return;
116
117         if (m_task != null && m_task.isAlive())
118             m_task.interrupt();
119
120         m_task = new Task(cmd);
121         m_task.start();
122
123         while (true)
124         {
125             // Give it a bit of time
126
try
127             {
128                 Thread.sleep(500);
129             }
130             catch (InterruptedException JavaDoc ioe)
131             {
132                 Log.error("ATask.executeCommand", ioe);
133             }
134             // Info to user
135
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
136             sb.append(m_task.getOut()).append("\n-------\n")
137                 .append(m_task.getErr()).append("\n-------");
138             info.setText(sb.toString());
139
140             // Are we done?
141
if (!m_task.isAlive())
142             {
143                 confirmPanel.getCancelButton().setEnabled(false);
144                 confirmPanel.getOKButton().setEnabled(true);
145                 break;
146             }
147         }
148         Log.trace(Log.l3_Util, "ATask.executeCommand - done");
149     } // executeCommand
150

151     /**
152      * Action Listener
153      * @param e
154      */

155     public void actionPerformed (ActionEvent e)
156     {
157         if (m_task != null && m_task.isAlive())
158             m_task.interrupt();
159         dispose();
160     } // actionPerformed
161

162 } // ATask
163
Popular Tags