1 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 33 public class ScriptEditor extends JFrame implements ActionListener 34 { 35 38 public ScriptEditor() 39 { 40 this (Msg.getMsg(Env.getCtx(), "Script"), null, 0); 41 } 43 49 public ScriptEditor (String 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 ex) 65 { 66 Log.error("ScriptEditor", ex); 67 } 68 } 70 71 private Scriptlet m_script; 72 73 private int m_WindowNo; 74 75 private String m_origScript; 76 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 111 void jbInit() throws Exception 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 } 173 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 fResultVariable.setText(m_script.getVariable()); 185 m_origScript = m_script.getScript(); 186 editor.setText(m_script.getScript()); 187 StringBuffer sb = new StringBuffer ("<HTML><BODY>"); 189 HashMap ctx = m_script.getEnvironment(); 190 String [] pp = new String [ctx.size()]; 191 ctx.keySet().toArray(pp); 192 Arrays.sort(pp); 193 for (int i = 0; i < pp.length; i++) 194 { 195 String key = pp[i].toString(); 196 Object 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 } 208 213 private String getColor (Object value) 214 { 215 if (value instanceof String ) 216 return "#009900"; else if (value instanceof Integer ) 218 return "#0000FF"; else if (value instanceof Double ) 220 return "#00FFFF"; else if (value instanceof Timestamp) 222 return "#FF00FF"; else if (value instanceof Boolean ) 224 return "#FF9900"; return "#FF0000"; } 228 231 private void dynInit() 232 { 233 234 } 236 237 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 } 262 265 private void actionProcess() 266 { 267 m_script.setScript(editor.getText()); 268 Exception e = m_script.execute(); 269 if (e != null) 270 ADialog.error(m_WindowNo, this, "ScriptError", e.toString()); 271 Object result = m_script.getResult(false); 272 fResult.setText(result == null ? "" : result.toString()); 273 } 275 276 277 285 public static String start (String header, String 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 } 292 } | Popular Tags |