KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.swing.border.*;
20 import java.util.*;
21 import java.sql.*;
22
23 import org.compiere.util.*;
24 import org.compiere.model.*;
25 import org.compiere.swing.*;
26
27 /**
28  * Bean Shell Editor
29  *
30  * @author Jorg Janke
31  * @version $Id: ScriptEditor.java,v 1.7 2003/02/14 06:44:13 jjanke Exp $
32  */

33 public class ScriptEditor extends JFrame implements ActionListener
34 {
35     /**
36      * Minimum Constructor
37      */

38     public ScriptEditor()
39     {
40         this (Msg.getMsg(Env.getCtx(), "Script"), null, 0);
41     } // ScriptEditor
42

43     /**
44      * Constructor
45      *
46      * @param title Field Name
47      * @param script The Script
48      */

49     public ScriptEditor (String JavaDoc title, Scriptlet script, int WindowNo)
50     {
51         super(title);
52         m_WindowNo = WindowNo;
53         if (m_WindowNo == 0)
54             m_WindowNo = Env.createWindowNo(this);
55         Log.trace(Log.l1_User, "ScriptEditor", "Window=" + m_WindowNo);
56         try
57         {
58             jbInit();
59             setScript (script);
60             dynInit();
61             AEnv.showCenterScreen(this);
62             toFront();
63         }
64         catch(Exception JavaDoc ex)
65         {
66             Log.error("ScriptEditor", ex);
67         }
68     } // ScriptEditor
69

70     /** The Script */
71     private Scriptlet m_script;
72     /** WindowNo */
73     private int m_WindowNo;
74     /** Original Script */
75     private String JavaDoc m_origScript;
76     // --
77

78     private CPanel mainPanel = new CPanel();
79     private BorderLayout borderLayout1 = new BorderLayout();
80     private JScrollPane editorPane = new JScrollPane();
81     private JTextArea editor = new JTextArea();
82     private JScrollPane variablesPane = new JScrollPane();
83     private JTextPane variables = new JTextPane();
84     private TitledBorder titledBorder1;
85     private TitledBorder titledBorder2;
86     private CPanel northPanel = new CPanel();
87     private CPanel southPanel = new CPanel();
88     private BorderLayout southLayout = new BorderLayout();
89     private CPanel okPanel = new CPanel();
90     private JButton bOK = ConfirmPanel.createOKButton(true);
91     private JButton bCancel = ConfirmPanel.createCancelButton(true);
92     private CPanel resultPanel = new CPanel();
93     private JButton bProcess = ConfirmPanel.createProcessButton(true);
94     private JLabel lResult = new JLabel();
95     private JTextField fResult = new JTextField();
96     private CPanel resultVariablePanel = new CPanel();
97     private BorderLayout northLayout = new BorderLayout();
98     private JLabel lResultVariable = new JLabel();
99     private JTextField fResultVariable = new JTextField();
100     private CPanel helpPanel = new CPanel();
101     private JButton bHelp = ConfirmPanel.createHelpButton(true);
102     private GridBagLayout resultVariableLayout = new GridBagLayout();
103     private FlowLayout okLayout = new FlowLayout();
104     private GridBagLayout resultLayout = new GridBagLayout();
105     private JSplitPane centerPane = new JSplitPane();
106
107     /**
108      * Static Layout
109      * @throws Exception
110      */

111     void jbInit() throws Exception JavaDoc
112     {
113         this.setIconImage(Env.getImage("Bean16.gif"));
114         this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
115         titledBorder1 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(148, 145, 140)),
116             Msg.getMsg(Env.getCtx(), "ScriptVariables"));
117         titledBorder2 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(148, 145, 140)),
118             Msg.getMsg(Env.getCtx(), "ScriptEditor"));
119         mainPanel.setLayout(borderLayout1);
120         editor.setLineWrap(true);
121         editor.setTabSize(4);
122         editor.setWrapStyleWord(true);
123         bOK.addActionListener(this);
124         bCancel.addActionListener(this);
125         bHelp.addActionListener(this);
126         bProcess.addActionListener(this);
127         variables.setBackground(Color.lightGray);
128         variables.setEditable(false);
129         variables.setContentType("text/html");
130         variablesPane.setBorder(titledBorder1);
131         editorPane.setBorder(titledBorder2);
132         centerPane.setPreferredSize(new Dimension(500, 500));
133         centerPane.setLeftComponent(editorPane);
134         centerPane.setRightComponent(variablesPane);
135         southPanel.setLayout(southLayout);
136         resultPanel.setLayout(resultLayout);
137         lResult.setText(Msg.getMsg(Env.getCtx(), "ScriptResult"));
138         fResult.setBackground(Color.lightGray);
139         fResult.setEditable(false);
140         fResult.setText("");
141         northPanel.setLayout(northLayout);
142         lResultVariable.setText(Msg.getMsg(Env.getCtx(), "ScriptResultVariable"));
143         fResultVariable.setBackground(Color.lightGray);
144         fResultVariable.setEditable(false);
145         resultVariablePanel.setLayout(resultVariableLayout);
146         okPanel.setLayout(okLayout);
147         northPanel.add(resultVariablePanel, BorderLayout.CENTER);
148         getContentPane().add(mainPanel);
149         editorPane.getViewport().add(editor, null);
150         variablesPane.getViewport().add(variables, null);
151         mainPanel.add(northPanel, BorderLayout.NORTH);
152         mainPanel.add(southPanel, BorderLayout.SOUTH);
153         southPanel.add(okPanel, BorderLayout.EAST);
154         okPanel.add(bCancel, null);
155         okPanel.add(bOK, null);
156         southPanel.add(resultPanel, BorderLayout.CENTER);
157         resultPanel.add(bProcess, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
158             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
159         resultPanel.add(lResult, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
160             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
161         resultPanel.add(fResult, new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0
162             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
163         mainPanel.add(centerPane, BorderLayout.CENTER);
164         resultVariablePanel.add(lResultVariable, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
165             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
166         resultVariablePanel.add(fResultVariable, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
167             ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
168         northPanel.add(helpPanel, BorderLayout.EAST);
169         helpPanel.add(bHelp, null);
170         centerPane.setDividerLocation(350);
171     } // jbInit
172

173     /**
174      * Set Script
175      * @param script The Script
176      */

177     public void setScript (Scriptlet script)
178     {
179         if (script == null)
180             m_script = new Scriptlet (Scriptlet.VARIABLE, ";", Env.getCtx(), m_WindowNo);
181         else
182             m_script = script;
183         //
184
fResultVariable.setText(m_script.getVariable());
185         m_origScript = m_script.getScript();
186         editor.setText(m_script.getScript());
187         //
188
StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<HTML><BODY>");
189         HashMap ctx = m_script.getEnvironment();
190         String JavaDoc[] pp = new String JavaDoc[ctx.size()];
191         ctx.keySet().toArray(pp);
192         Arrays.sort(pp);
193         for (int i = 0; i < pp.length; i++)
194         {
195             String JavaDoc key = pp[i].toString();
196             Object JavaDoc value = ctx.get(key);
197             sb.append("<font color=").append('"').append(getColor(value)).append('"').append(">")
198                 .append(key)
199                 .append(" (")
200                 .append(value)
201                 .append(")</font><br>");
202         }
203         sb.append("</BODY></HTML>");
204         variables.setText(sb.toString());
205         variables.setCaretPosition(0);
206     } // setScript
207

208     /**
209      * Get Color Code
210      * @param value the object
211      * @return HTML color code
212      */

213     private String JavaDoc getColor (Object JavaDoc value)
214     {
215         if (value instanceof String JavaDoc)
216             return "#009900"; // "green";
217
else if (value instanceof Integer JavaDoc)
218             return "#0000FF"; // "blue";
219
else if (value instanceof Double JavaDoc)
220             return "#00FFFF"; // "cyan";
221
else if (value instanceof Timestamp)
222             return "#FF00FF"; // "magenta";
223
else if (value instanceof Boolean JavaDoc)
224             return "#FF9900"; // "orange";
225
return "#FF0000"; // "red";
226
} // getColor
227

228     /**
229      * Dynamic Init
230      */

231     private void dynInit()
232     {
233
234     } // dynInit
235

236
237     /**
238      * Action Listener
239      * @param e
240      */

241     public void actionPerformed(ActionEvent e)
242     {
243         if (e.getSource() == bOK)
244             dispose();
245         else if (e.getSource() == bCancel)
246         {
247             m_script.setScript(m_origScript);
248             dispose();
249         }
250
251         else if (e.getSource() == bProcess)
252             actionProcess();
253         else if (e.getSource() == bHelp)
254         {
255             Help h = new Help (this ,
256                 Msg.getMsg(Env.getCtx(), "ScriptHelp"),
257                 getClass().getResource("Script.html"));
258             h.setVisible(true);
259         }
260     } // actionPerformed
261

262     /**
263      * Process Script
264      */

265     private void actionProcess()
266     {
267         m_script.setScript(editor.getText());
268         Exception JavaDoc e = m_script.execute();
269         if (e != null)
270             ADialog.error(m_WindowNo, this, "ScriptError", e.toString());
271         Object JavaDoc result = m_script.getResult(false);
272         fResult.setText(result == null ? "" : result.toString());
273     } // actionProcess
274

275     /*************************************************************************/
276
277     /**
278      * Start ScriptEditor
279      *
280      * @param header Title
281      * @param script ScriptCode
282      * @param editable
283      * @return updated Script
284      */

285     public static String JavaDoc start (String JavaDoc header, String JavaDoc script, boolean editable, int WindowNo)
286     {
287         Scriptlet scr = new Scriptlet (Scriptlet.VARIABLE, script, Env.getCtx(), WindowNo);
288         ScriptEditor se = new ScriptEditor (header, scr, WindowNo);
289         return scr.getScript();
290     } // start
291

292 } // ScriptEditor
293
Popular Tags