⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scripteditor.java

📁 大家共享愉快, 共享愉快, 共享愉快, 共享愉快,共享愉快
💻 JAVA
字号:
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
 * Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
 * are Copyright (C) 1999-2005 Jorg Janke.
 * All parts are Copyright (C) 1999-2005 ComPiere, Inc.  All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.apps;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import javax.swing.*;
import javax.swing.border.*;

import org.compiere.model.*;
import org.compiere.swing.*;
import org.compiere.util.*;

/**
 *  Bean Shell Editor
 *
 *  @author     Jorg Janke
 *  @version    $Id: ScriptEditor.java,v 1.12 2005/12/09 05:17:57 jjanke Exp $
 */
public class ScriptEditor extends CFrame implements ActionListener
{
	/**
	 *  Minimum Constructor
	 */
	public ScriptEditor()
	{
		this (Msg.getMsg(Env.getCtx(), "Script"), null, 0);
	}   //  ScriptEditor

	/**
	 *  Constructor
	 *
	 *  @param title Field Name
	 *  @param script The Script
	 */
	public ScriptEditor (String title, Scriptlet script, int WindowNo)
	{
		super(title);
		m_WindowNo = WindowNo;
		if (m_WindowNo == 0)
			m_WindowNo = Env.createWindowNo(this);
		log.info("Window=" + m_WindowNo);
		try
		{
			jbInit();
			setScript (script);
			dynInit();
			AEnv.showCenterScreen(this);
			toFront();
		}
		catch(Exception ex)
		{
			log.log(Level.SEVERE, "", ex);
		}
	}   //  ScriptEditor

	/** The Script      */
	private Scriptlet   m_script;
	/** WindowNo        */
	private int         m_WindowNo;
	/** Original Script */
	private String      m_origScript;
	/**	Logger			*/
	private static CLogger log = CLogger.getCLogger(ScriptEditor.class);
	//  --

	private CPanel mainPanel = new CPanel();
	private BorderLayout borderLayout1 = new BorderLayout();
	private JScrollPane editorPane = new JScrollPane();
	private JTextArea editor = new JTextArea();
	private JScrollPane variablesPane = new JScrollPane();
	private JTextPane variables = new JTextPane();
	private TitledBorder titledBorder1;
	private TitledBorder titledBorder2;
	private CPanel northPanel = new CPanel();
	private CPanel southPanel = new CPanel();
	private BorderLayout southLayout = new BorderLayout();
	private CPanel okPanel = new CPanel();
	private JButton bOK = ConfirmPanel.createOKButton(true);
	private JButton bCancel = ConfirmPanel.createCancelButton(true);
	private CPanel resultPanel = new CPanel();
	private JButton bProcess = ConfirmPanel.createProcessButton(true);
	private JLabel lResult = new JLabel();
	private JTextField fResult = new JTextField();
	private CPanel resultVariablePanel = new CPanel();
	private BorderLayout northLayout = new BorderLayout();
	private JLabel lResultVariable = new JLabel();
	private JTextField fResultVariable = new JTextField();
	private CPanel helpPanel = new CPanel();
	private JButton bHelp = ConfirmPanel.createHelpButton(true);
	private GridBagLayout resultVariableLayout = new GridBagLayout();
	private FlowLayout okLayout = new FlowLayout();
	private GridBagLayout resultLayout = new GridBagLayout();
	private JSplitPane centerPane = new JSplitPane();

	/**
	 *  Static Layout
	 *  @throws Exception
	 */
	void jbInit() throws Exception
	{
		this.setIconImage(Env.getImage("Script16.gif"));
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		titledBorder1 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(148, 145, 140)),
			Msg.getMsg(Env.getCtx(), "ScriptVariables"));
		titledBorder2 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(148, 145, 140)),
			Msg.getMsg(Env.getCtx(), "ScriptEditor"));
		mainPanel.setLayout(borderLayout1);
		editor.setLineWrap(true);
		editor.setTabSize(4);
		editor.setWrapStyleWord(true);
		bOK.addActionListener(this);
		bCancel.addActionListener(this);
		bHelp.addActionListener(this);
		bProcess.addActionListener(this);
		variables.setBackground(Color.lightGray);
		variables.setEditable(false);
		variables.setContentType("text/html");
		variablesPane.setBorder(titledBorder1);
		editorPane.setBorder(titledBorder2);
		centerPane.setPreferredSize(new Dimension(500, 500));
		centerPane.setLeftComponent(editorPane);
		centerPane.setRightComponent(variablesPane);
		southPanel.setLayout(southLayout);
		resultPanel.setLayout(resultLayout);
		lResult.setText(Msg.getMsg(Env.getCtx(), "ScriptResult"));
		fResult.setBackground(Color.lightGray);
		fResult.setEditable(false);
		fResult.setText("");
		northPanel.setLayout(northLayout);
		lResultVariable.setText(Msg.getMsg(Env.getCtx(), "ScriptResultVariable"));
		fResultVariable.setBackground(Color.lightGray);
		fResultVariable.setEditable(false);
		resultVariablePanel.setLayout(resultVariableLayout);
		okPanel.setLayout(okLayout);
		northPanel.add(resultVariablePanel, BorderLayout.CENTER);
		getContentPane().add(mainPanel);
		editorPane.getViewport().add(editor, null);
		variablesPane.getViewport().add(variables, null);
		mainPanel.add(northPanel, BorderLayout.NORTH);
		mainPanel.add(southPanel,  BorderLayout.SOUTH);
		southPanel.add(okPanel,  BorderLayout.EAST);
		okPanel.add(bCancel, null);
		okPanel.add(bOK, null);
		southPanel.add(resultPanel,  BorderLayout.CENTER);
		resultPanel.add(bProcess,   new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
			,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		resultPanel.add(lResult,   new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
			,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		resultPanel.add(fResult,   new GridBagConstraints(2, 0, 1, 1, 1.0, 0.0
			,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		mainPanel.add(centerPane,  BorderLayout.CENTER);
		resultVariablePanel.add(lResultVariable,   new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
			,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		resultVariablePanel.add(fResultVariable,   new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
			,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		northPanel.add(helpPanel,  BorderLayout.EAST);
		helpPanel.add(bHelp, null);
		centerPane.setDividerLocation(350);
	}   //  jbInit

	/**
	 *  Set Script
	 *  @param script The Script
	 */
	public void setScript (Scriptlet script)
	{
		if (script == null)
			m_script = new Scriptlet (Scriptlet.VARIABLE, ";", Env.getCtx(), m_WindowNo);
		else
			m_script = script;
		//
		fResultVariable.setText(m_script.getVariable());
		m_origScript = m_script.getScript();
		editor.setText(m_script.getScript());
		//
		StringBuffer sb = new StringBuffer("<HTML><BODY>");
		HashMap<String,Object> ctx = m_script.getEnvironment();
		String[] pp = new String[ctx.size()];
		ctx.keySet().toArray(pp);
		Arrays.sort(pp);
		for (int i = 0; i < pp.length; i++)
		{
			String key = pp[i].toString();
			Object value = ctx.get(key);
			sb.append("<font color=").append('"').append(getColor(value)).append('"').append(">")
				.append(key)
				.append(" (")
				.append(value)
				.append(")</font><br>");
		}
		sb.append("</BODY></HTML>");
		variables.setText(sb.toString());
		variables.setCaretPosition(0);
	}   //  setScript

	/**
	 *  Get Color Code
	 *  @param value the object
	 *  @return HTML color code
	 */
	private String getColor (Object value)
	{
		if (value instanceof String)
			return "#009900";   //  "green";
		else if (value instanceof Integer)
			return "#0000FF";   //  "blue";
		else if (value instanceof Double)
			return "#00FFFF";   //  "cyan";
		else if (value instanceof Timestamp)
			return "#FF00FF";   //  "magenta";
		else if (value instanceof Boolean)
			return "#FF9900";   //  "orange";
		return     "#FF0000";   //  "red";
	}   //  getColor

	/**
	 *  Dynamic Init
	 */
	private void dynInit()
	{

	}   //  dynInit


	/**
	 *  Action Listener
	 *  @param e
	 */
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == bOK)
			dispose();
		else if (e.getSource() == bCancel)
		{
			m_script.setScript(m_origScript);
			dispose();
		}

		else if (e.getSource() == bProcess)
			actionProcess();
		else if (e.getSource() == bHelp)
		{
			Help h = new Help (this ,
				Msg.getMsg(Env.getCtx(), "ScriptHelp"),
				getClass().getResource("Script.html"));
			h.setVisible(true);
		}
	}	//  actionPerformed

	/**
	 *  Process Script
	 */
	private void actionProcess()
	{
		/** Example:
		import org.compiere.util.DB;
		import java.sql.*;
		PreparedStatement pstmt =DB.prepareStatement("select Name, Password from AD_User where Name like 'Super%'");
		ResultSet rs = pstmt.executeQuery();
		if (rs.next())
		{
		result = rs.getString("Name") + "; password= " + rs.getString("Password");
		}
		**/
		
		MUser user = MUser.get(Env.getCtx());
		if (!user.isAdministrator())
		{
			fResult.setText("Not Administrator");
			return;
		}
		//
		m_script.setScript(editor.getText());
		Exception e = m_script.execute();
		if (e != null)
			ADialog.error(m_WindowNo, this, "ScriptError", e.toString());
		Object result = m_script.getResult(false);
		fResult.setText(result == null ? "" : result.toString());
	}   //  actionProcess

	/*************************************************************************/

	/**
	 *  Start ScriptEditor
	 *
	 *  @param header   Title
	 *  @param script   ScriptCode
	 *  @param editable
	 *  @return updated Script
	 */
	public static String start (String header, String script, boolean editable, int WindowNo)
	{
		Scriptlet scr = new Scriptlet (Scriptlet.VARIABLE, script, Env.getCtx(), WindowNo);
		ScriptEditor se = new ScriptEditor (header, scr, WindowNo);
		return scr.getScript();
	}   //  start

}   //  ScriptEditor

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -