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

📄 htmleditor.java

📁 大家共享愉快, 共享愉快, 共享愉快, 共享愉快,共享愉快
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
 * 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.grid.ed;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import org.compiere.*;
import org.compiere.apps.*;
import org.compiere.swing.*;
import org.compiere.util.*;

/**
 *	HTML Editor.
 *	(Thanks to Kim Topley "Core Swing Programming" for action menu organization)
 *  @author Jorg Janke
 *  @version $Id: HTMLEditor.java,v 1.9 2005/11/14 02:10:57 jjanke Exp $
 */
public class HTMLEditor extends CDialog
{
	/**
	 * 	HTML Editor
	 *	@param owner owner
	 *	@param htmlText text
	 */
	public HTMLEditor (Frame owner, String title, String htmlText, boolean editable)
	{
		super (owner, title == null ? Msg.getMsg(Env.getCtx(), "Editor") : title, true);
		init(owner, htmlText, editable);
	}	//	HTMLEditor


	/**
	 * 	HTML Editor
	 *	@param owner owner
	 *	@param htmlText text
	 */
	public HTMLEditor (Dialog owner, String title, String htmlText, boolean editable)
	{
		super (owner, title == null ? Msg.getMsg(Env.getCtx(), "Editor") : title, true);
		init(owner, htmlText, editable);
	}	//	HTMLEditor

	/**
	 * 	Init
	 *	@param owner owner
	 *	@param htmlText text
	 */
	private void init (Window owner, String htmlText, boolean editable)
	{
		try
		{
			jbInit();
		}
		catch (Exception e)
		{
			log.log(Level.SEVERE, "HTMLEditor", e);
		}
		setHtmlText(htmlText);
		editorPane.setEditable(editable);
		AEnv.showCenterWindow(owner, this);
	}	//	init

	/**	Logger				*/
	private CLogger	log = CLogger.getCLogger(getClass());
	/**	The HTML Text		*/
	private String	m_text;
	
	//
	private CPanel mainPanel = new CPanel();
	private BorderLayout mainLayout = new BorderLayout();
	//
	private JScrollPane editorScrollPane = new JScrollPane();
	private JTextPane editorPane = new JTextPane();
	//
	private ConfirmPanel confirmPanel = new ConfirmPanel (true);
	
	//	Tool Bar
	private JToolBar toolBar = new JToolBar();
	private JButton bImport = AEnv.getButton("Import");
	private JButton bExport = AEnv.getButton("Export");
	private JButton bBold = AEnv.getButton("Bold");
	private JButton bItalic = AEnv.getButton("Italic");
	private JButton bUnderline = AEnv.getButton("Underline");
	
	//	Menu Bar
	private JMenuBar menuBar = new JMenuBar();
	
	private static String NAME_SIZE 	= Msg.getMsg(Env.getCtx(),"Size");
	private static String NAME_HEADING 	= Msg.getMsg(Env.getCtx(), "Heading"); 
	//	Font Size Sub-Menu
	private static HTMLEditor_MenuAction[] sizeMenu = new HTMLEditor_MenuAction[] {
		new HTMLEditor_MenuAction(NAME_SIZE + "  8",  "font-size-8"),
		new HTMLEditor_MenuAction(NAME_SIZE + " 10", "font-size-10"),
		new HTMLEditor_MenuAction(NAME_SIZE + " 12", "font-size-12"),
		new HTMLEditor_MenuAction(NAME_SIZE + " 14", "font-size-14"),
		new HTMLEditor_MenuAction(NAME_SIZE + " 16", "font-size-16"),
		new HTMLEditor_MenuAction(NAME_SIZE + " 18", "font-size-18"),
		new HTMLEditor_MenuAction(NAME_SIZE + " 24", "font-size-24"),
		new HTMLEditor_MenuAction(NAME_SIZE + " 36", "font-size-36"),
		new HTMLEditor_MenuAction(NAME_SIZE + " 48", "font-size-48")					
	};
	//	Font Family Sub-Menu
	private static HTMLEditor_MenuAction[] familyMenu = new HTMLEditor_MenuAction[] {
		new HTMLEditor_MenuAction("Sans Serif", "font-family-SansSerif"),
		new HTMLEditor_MenuAction("Monospaced", "font-family-Monospaced"),
		new HTMLEditor_MenuAction("Serif", "font-family-Serif")
	};
	//	Font Size Sub-Menu
	private static HTMLEditor_MenuAction[] styleMenu = new HTMLEditor_MenuAction[] {
		new HTMLEditor_MenuAction(Msg.getMsg(Env.getCtx(), "Bold"), "font-bold"),
		new HTMLEditor_MenuAction(Msg.getMsg(Env.getCtx(), "Italic"), "font-italic"),
		new HTMLEditor_MenuAction(Msg.getMsg(Env.getCtx(), "Underline"), "font-underline")
		// default-typed?
	};
	//	Heading Sub-Menu
	private static HTMLEditor_MenuAction[] headingMenu = new HTMLEditor_MenuAction[] {
		new HTMLEditor_MenuAction(NAME_HEADING + " 1", "Heading 1"),
		new HTMLEditor_MenuAction(NAME_HEADING + " 2", "Heading 2"),
		new HTMLEditor_MenuAction(NAME_HEADING + " 3", "Heading 3"),
		new HTMLEditor_MenuAction(NAME_HEADING + " 4", "Heading 4"),
		new HTMLEditor_MenuAction(NAME_HEADING + " 5", "Heading 5")
	};
	//	Font Menu
	private static HTMLEditor_MenuAction[] fontMenu = new HTMLEditor_MenuAction[] {
		new HTMLEditor_MenuAction(NAME_SIZE, sizeMenu),
		new HTMLEditor_MenuAction(Msg.getMsg(Env.getCtx(), "FontFamily"), familyMenu),
		new HTMLEditor_MenuAction(Msg.getMsg(Env.getCtx(), "FontStyle"), styleMenu),
		new HTMLEditor_MenuAction(NAME_HEADING, headingMenu)
	};
	// Alignment Menu
	private static HTMLEditor_MenuAction[] alignMenu = new HTMLEditor_MenuAction[] {
		new HTMLEditor_MenuAction(Msg.getMsg(Env.getCtx(), "Left"), "left-justify"),
		new HTMLEditor_MenuAction(Msg.getMsg(Env.getCtx(), "Center"), "center-justify"),
		new HTMLEditor_MenuAction(Msg.getMsg(Env.getCtx(), "Right"), "right-justify")
	};
	// Other HTML Menu
	private static HTMLEditor_MenuAction[] htmlMenu = new HTMLEditor_MenuAction[] {
		new HTMLEditor_MenuAction("Paragraph", "Paragraph"),
		new HTMLEditor_MenuAction("Table", "InsertTable"),
		new HTMLEditor_MenuAction("Table Row", "InsertTableRow"),
		new HTMLEditor_MenuAction("Table Cell", "InsertTableDataCell"),
		new HTMLEditor_MenuAction("Unordered List", "InsertUnorderedList"),
		new HTMLEditor_MenuAction("Unordered List Item", "InsertUnorderedListItem"),
		new HTMLEditor_MenuAction("Ordered List", "InsertOrderedList"),
		new HTMLEditor_MenuAction("Ordered List Item", "InsertOrderedListItem"),
		new HTMLEditor_MenuAction("Preformatted Paragraph", "InsertPre"),
		new HTMLEditor_MenuAction("Horizontal Rule", "InsertHR")
	};

	//	Insert HTML Actions
	private static HTMLEditorKit.InsertHTMLTextAction[] extraActions = 
		new HTMLEditorKit.InsertHTMLTextAction[] 
	{
		new HTMLEditorKit.InsertHTMLTextAction("Heading 1", "<h1>h1</h1>",
				HTML.Tag.BODY, HTML.Tag.H1),
		new HTMLEditorKit.InsertHTMLTextAction("Heading 2", "<h2>h2</h2>",
				HTML.Tag.BODY, HTML.Tag.H2),
		new HTMLEditorKit.InsertHTMLTextAction("Heading 3", "<h2>h3</h2>",
				HTML.Tag.BODY, HTML.Tag.H3),
		new HTMLEditorKit.InsertHTMLTextAction("Heading 4", "<h2>h4</h2>",
				HTML.Tag.BODY, HTML.Tag.H4),
		new HTMLEditorKit.InsertHTMLTextAction("Heading 5", "<h2>h5</h2>",
				HTML.Tag.BODY, HTML.Tag.H5),
		//
		new HTMLEditorKit.InsertHTMLTextAction("Paragraph", "<p>p</p>",
				HTML.Tag.BODY, HTML.Tag.P)
	};


	/**
	 * 	Static Init
	 *	@throws Exception
	 */
	private void jbInit() throws Exception
	{
		//	ToolBar
		bImport.setToolTipText(Msg.getMsg(Env.getCtx(), "Import"));
		bImport.addActionListener(this);
		bExport.setToolTipText(Msg.getMsg(Env.getCtx(), "Export"));
		bExport.addActionListener(this);
		//
		bBold.setToolTipText(Msg.getMsg(Env.getCtx(), "Bold"));
		bItalic.setToolTipText(Msg.getMsg(Env.getCtx(), "Italic"));
		bUnderline.setToolTipText(Msg.getMsg(Env.getCtx(), "Underline"));
		
		toolBar.add(bImport, null);
		toolBar.add(bExport, null);
		toolBar.addSeparator();
		toolBar.add(bBold, null);
		toolBar.add(bItalic, null);
		toolBar.add(bUnderline, null);
		toolBar.addSeparator();

		//	Editor
		editorPane.setContentType("text/html");
		//	Set Menu (content type must be set)
		setJMenuBar(menuBar);
		createMenuBar();

		//	General Layout
		mainPanel.setLayout(mainLayout);
		getContentPane().add(mainPanel, BorderLayout.CENTER);
		mainPanel.add(toolBar,  BorderLayout.NORTH);
		mainPanel.add(editorScrollPane, BorderLayout.CENTER);
		//	Size 600x600
		editorScrollPane.setPreferredSize(new Dimension(600,600));
		editorScrollPane.getViewport().add(editorPane, null);
		mainPanel.add(confirmPanel, BorderLayout.SOUTH);
		confirmPanel.addActionListener(this);
	}	//	setHTMLText

	/**
	 * Create Menu Bar
	 */
	private void createMenuBar() 
	{
		//	Build Lookup
		Action[] actionArray = editorPane.getActions();
		Hashtable<Object,Action> actions = new Hashtable<Object,Action>();
		for (int i = 0; i < actionArray.length; i++)
		{
			Object name = actionArray[i].getValue(Action.NAME);
		//	System.out.println (name);
			actions.put(name, actionArray[i]);
		}
		for (int i = 0; i < extraActions.length; i++)
		{
			Object name = extraActions[i].getValue(Action.NAME);
			actions.put(name, extraActions[i]);
		}

		//	Add the font menu
		JMenu menu = buildMenu(Msg.getMsg(Env.getCtx(), "Font"), fontMenu, actions);

⌨️ 快捷键说明

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