editor.java

来自「Java写的ERP系统」· Java 代码 · 共 243 行

JAVA
243
字号
/******************************************************************************
 * 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  Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.grid.ed;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import org.compiere.util.*;
import org.compiere.apps.*;
import org.compiere.plaf.*;
import org.compiere.swing.*;

/**
 *  Editor for Text (textArea) with HTML (textPane) View
 *
 * 	@author 	Jorg Janke
 * 	@version 	$Id: Editor.java,v 1.7 2002/01/19 05:27:55 jjanke Exp $
 */
public class Editor extends JDialog
	implements ChangeListener, ActionListener
{
	/**
	 *	Minimum constructor
	 */
	public Editor(Frame frame)
	{
		this (frame, Msg.getMsg(Env.getCtx(), "Editor"), "", true);
	}   //  Editor

	/**
	 *	Standard constructor
	 */
	public Editor(Frame frame, String header, String text, boolean editable)
	{
		super (frame, header, frame != null);
		Log.trace(Log.l3_Util, "Editor");
		try
		{
			jbInit();
			setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		}
		catch(Exception ex)
		{
			Log.error("Editor", ex);
		}
		//	Set Text
		m_text = text;
		textArea.setText(m_text);
		textArea.setEditable(editable);
		if (editable)
			textArea.setBackground(CompierePLAF.getFieldBackground_Normal());
		else
			textArea.setBackground(CompierePLAF.getFieldBackground_Inactive());
		textPane.setBackground(CompierePLAF.getFieldBackground_Inactive());
	}	//	Editor

	/**
	 *  IDE Constructor
	 */
	public Editor()
	{
		this (null);
	}	//	Editor

	private String m_text;

	private CPanel panel = new CPanel();
	private BorderLayout panelLayout = new BorderLayout();
	private JTabbedPane tabbedPane = new JTabbedPane();
	private JScrollPane paneText = new JScrollPane();
	private JScrollPane paneHTML = new JScrollPane();
	private JTextArea textArea = new JTextArea();
	private JTextPane textPane = new JTextPane();
	private JMenuBar menuBar = new JMenuBar();
	private JMenu mFile = new JMenu();
	private JMenuItem mImport = new JMenuItem();
	private JMenuItem mExport = new JMenuItem();
	private ConfirmPanel confirmPanel = new ConfirmPanel();

	/**
	 *	Static Init
	 */
	void jbInit() throws Exception
	{
		panel.setLayout(panelLayout);
		this.setJMenuBar(menuBar);
		textArea.setPreferredSize(new Dimension(300, 300));
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);
		textPane.setContentType("text/html");
		textPane.setEditable(false);
		mFile.setText("File");
		mImport.setText("Import");
		mImport.addActionListener(this);
		mExport.setText("Export");
		mExport.addActionListener(this);
		tabbedPane.addChangeListener(this);
		getContentPane().add(panel);
		panel.add(tabbedPane, BorderLayout.CENTER);
		tabbedPane.add(paneText, "Text");
		paneText.getViewport().add(textArea, null);
		tabbedPane.add(paneHTML, "HTML");
		this.getContentPane().add(confirmPanel, BorderLayout.SOUTH);
		paneHTML.getViewport().add(textPane, null);
		menuBar.add(mFile);
		mFile.add(mImport);
		mFile.add(mExport);
		confirmPanel.addActionListener(this);
	}	//	jbInit

	/**
	 *	Factory: Start Editor
	 */
	public static String startEditor(Container jc, String header, String text, boolean editable)
	{
		//	Find frame
		JFrame frame = Env.getFrame(jc);
		String hdr = header;
		if (hdr == null || hdr.length() == 0)
			hdr = Msg.getMsg(Env.getCtx(), "Editor");
		//	Start it
		Editor ed = new Editor(frame, hdr, text, editable);
		AEnv.showCenterWindow(frame, ed);
		String s = ed.getText();
		ed = null;
		return s;
	}	//	startEditor

	/**
	 *	ActionListener
	 */
	public void actionPerformed(ActionEvent e)
	{
		if (e.getActionCommand().equals(ConfirmPanel.A_OK))
		{
			m_text = textArea.getText();
			Log.trace(Log.l5_DData, "Editor.actionPerformed - OK - length=" + m_text.length());
			dispose();
		}
		else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
		{
			dispose();
		}
		else if (e.getSource() == mImport)
		{
			importText();
		}
		else if (e.getSource() == mExport)
		{
			exportText();
		}
	}	//	actionPerformed

	/**
	 *	Get Text
	 */
	public String getText()
	{
		return m_text;
	}	//	getText

	/**
	 *	Import Text
	 */
	private void importText()
	{
		JFileChooser jc = new JFileChooser();
		jc.setDialogTitle(Msg.getMsg(Env.getCtx(), "ImportText"));
		jc.setDialogType(JFileChooser.OPEN_DIALOG);
		jc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		//
		if (jc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
			return;

		StringBuffer sb = new StringBuffer();
		try
		{
			InputStreamReader in = new InputStreamReader (new FileInputStream (jc.getSelectedFile()));
			char[] cbuf = new char[1024];
			int count;
			while ((count = in.read(cbuf)) > 0)
				sb.append(cbuf, 0, count);
			in.close();
		}
		catch (Exception e)
		{
			Log.error("Editor.importText" + e.getMessage());
			return;
		}
		textArea.setText(sb.toString());
	}	//	importText

	/**
	 *	Export Text
	 */
	private void exportText()
	{
		JFileChooser jc = new JFileChooser();
		jc.setDialogTitle(Msg.getMsg(Env.getCtx(), "ExportText"));
		jc.setDialogType(JFileChooser.SAVE_DIALOG);
		jc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		//
		if (jc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION)
			return;

		try
		{
			BufferedWriter bout = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (jc.getSelectedFile())));
			bout.write(textArea.getText());
			bout.flush();
			bout.close();
		}
		catch (Exception e)
		{
			Log.error("Editor.exportText" + e.getMessage());
		}
	}	//	exportText

	/**
	 *	ChangeListener for TabbedPane
	 */
	public void stateChanged(ChangeEvent e)
	{
		if (tabbedPane.getSelectedIndex() == 1)		//	switch to HTML
			textPane.setText(textArea.getText());
	}	//	stateChanged

}	//	Editor

⌨️ 快捷键说明

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