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

📄 htmleditor.java

📁 Memoranda( 从前以jNotes2而闻名) 是一个日志管理和个人项目管理工具
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
package net.sf.memoranda.ui.htmleditor;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Cursor;import java.awt.Dimension;import java.awt.Font;import java.awt.Point;import java.awt.Rectangle;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.event.ActionEvent;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.Reader;import java.io.StringReader;import java.io.StringWriter;import java.net.MalformedURLException;import java.net.URL;import java.util.Enumeration;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.BorderFactory;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JEditorPane;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JTabbedPane;import javax.swing.JToolBar;import javax.swing.KeyStroke;import javax.swing.border.BevelBorder;import javax.swing.border.Border;import javax.swing.event.CaretEvent;import javax.swing.event.CaretListener;import javax.swing.event.UndoableEditEvent;import javax.swing.event.UndoableEditListener;import javax.swing.text.AbstractDocument;import javax.swing.text.AttributeSet;import javax.swing.text.Document;import javax.swing.text.Element;import javax.swing.text.SimpleAttributeSet;import javax.swing.text.StyleConstants;import javax.swing.text.StyledEditorKit;import javax.swing.text.html.HTML;import javax.swing.text.html.HTMLDocument;import javax.swing.text.html.HTMLEditorKit;import javax.swing.text.html.StyleSheet;import javax.swing.undo.CannotRedoException;import javax.swing.undo.CannotUndoException;import javax.swing.undo.UndoManager;import net.sf.memoranda.ui.htmleditor.util.Local;/** *   */public class HTMLEditor extends JPanel {	public HTMLEditorPane editor = new HTMLEditorPane("");    JScrollPane jScrollPane1 = new JScrollPane();    public HTMLEditorKit editorKit = new HTMLEditorKit();    public HTMLDocument document = null;	boolean bold = false;	boolean italic = false;	boolean under = false;	boolean list = false;	String currentTagName = "BODY";	Element currentParaElement = null;	Border border1, border2;	Class cl = net.sf.memoranda.ui.htmleditor.HTMLEditor.class;	String imagesDir = null;	String imagesPath = null;	public void setImagesDir(String path) {		imagesDir = path;	}	public String getImagesDir() {		return imagesDir;	}	abstract class HTMLEditorAction extends AbstractAction {		HTMLEditorAction(String name, ImageIcon icon) {			super(name, icon);			super.putValue(Action.SHORT_DESCRIPTION, name);		}		HTMLEditorAction(String name) {			super(name);			super.putValue(Action.SHORT_DESCRIPTION, name);		}	}	public Action boldAction =		new HTMLEditorAction(			Local.getString("Bold"),			new ImageIcon(cl.getResource("resources/icons/bold.png"))) {		public void actionPerformed(ActionEvent e) {			boldActionB_actionPerformed(e);		}	};	public Action italicAction =		new HTMLEditorAction(			Local.getString("Italic"),			new ImageIcon(cl.getResource("resources/icons/italic.png"))) {		public void actionPerformed(ActionEvent e) {			italicActionB_actionPerformed(e);		}	};	public Action underAction =		new HTMLEditorAction(			Local.getString("Underline"),			new ImageIcon(cl.getResource("resources/icons/underline.png"))) {		public void actionPerformed(ActionEvent e) {			underActionB_actionPerformed(e);		}	};	public Action ulAction =		new HTMLEditorAction(			Local.getString("Unordered list"),			new ImageIcon(				cl.getResource("resources/icons/listunordered.png"))) {		public void actionPerformed(ActionEvent e) {			ulActionB_actionPerformed(e);		}	};	public Action olAction =		new HTMLEditorAction(			Local.getString("Ordered list"),			new ImageIcon(cl.getResource("resources/icons/listordered.png"))) {		public void actionPerformed(ActionEvent e) {			olActionB_actionPerformed(e);		}	};	public Action lAlignAction =		new HTMLEditorAction(			Local.getString("Align left"),			new ImageIcon(cl.getResource("resources/icons/alignleft.png"))) {		public void actionPerformed(ActionEvent e) {			lAlignActionB_actionPerformed(e);		}	};	public Action cAlignAction =		new HTMLEditorAction(			Local.getString("Align center"),			new ImageIcon(cl.getResource("resources/icons/aligncenter.png"))) {		public void actionPerformed(ActionEvent e) {			cAlignActionB_actionPerformed(e);		}	};	public Action rAlignAction =		new HTMLEditorAction(			Local.getString("Align right"),			new ImageIcon(cl.getResource("resources/icons/alignright.png"))) {		public void actionPerformed(ActionEvent e) {			rAlignActionB_actionPerformed(e);		}	};	/*	 * public Action jAlignAction = new AbstractAction() { public void	 * actionPerformed(ActionEvent e) { jAlignActionB_actionPerformed(e); }	 */	public Action imageAction =		new HTMLEditorAction(			Local.getString("Insert image"),			new ImageIcon(cl.getResource("resources/icons/image.png"))) {		public void actionPerformed(ActionEvent e) {			imageActionB_actionPerformed(e);		}	};	public Action tableAction =		new HTMLEditorAction(			Local.getString("Insert table"),			new ImageIcon(cl.getResource("resources/icons/table.png"))) {		public void actionPerformed(ActionEvent e) {			tableActionB_actionPerformed(e);		}	};	public Action linkAction =		new HTMLEditorAction(			Local.getString("Insert hyperlink"),			new ImageIcon(cl.getResource("resources/icons/link.png"))) {		public void actionPerformed(ActionEvent e) {			linkActionB_actionPerformed(e);		}	};	public Action propsAction =		new HTMLEditorAction(			Local.getString("Object properties"),			new ImageIcon(cl.getResource("resources/icons/properties.png"))) {		public void actionPerformed(ActionEvent e) {			propsActionB_actionPerformed(e);		}	};	public Action selectAllAction =		new HTMLEditorAction(Local.getString("Select all")) {		public void actionPerformed(ActionEvent e) {			editor.selectAll();		}	};	public Action insertHRAction =		new HTMLEditorAction(			Local.getString("Insert horizontal rule"),			new ImageIcon(cl.getResource("resources/icons/hr.png"))) {		public void actionPerformed(ActionEvent e) {			/*			 * String elName =			 * document.getParagraphElement(editor.getCaretPosition()).getName();			 * HTML.Tag tag = HTML.getTag(elName); if			 * (elName.toUpperCase().equals("P-IMPLIED")) tag =			 * HTML.Tag.IMPLIED; HTMLEditorKit.InsertHTMLTextAction hta = new			 * HTMLEditorKit.InsertHTMLTextAction("insertHR", " <hr> ", tag,			 * HTML.Tag.HR);			 */			try {				editorKit.insertHTML(					document,					editor.getCaretPosition(),					"<hr>",					0,					0,					HTML.Tag.HR);			} catch (Exception ex) {				ex.printStackTrace();			}		}	};	CharTablePanel charTablePanel = new CharTablePanel(editor);	boolean charTableShow = false;	public JTabbedPane toolsPanel = new JTabbedPane();	public boolean toolsPanelShow = false;	public void showToolsPanel() {		if (toolsPanelShow)			return;		this.add(toolsPanel, BorderLayout.SOUTH);		toolsPanelShow = true;	}	public void hideToolsPanel() {		if (!toolsPanelShow)			return;		this.remove(charTablePanel);		toolsPanelShow = false;	}	void addCharTablePanel() {		showToolsPanel();		toolsPanel.addTab(Local.getString("Characters"), charTablePanel);	}	void removeCharTablePanel() {		toolsPanel.remove(charTablePanel);		if (toolsPanel.getTabCount() == 0)			hideToolsPanel();	}	public Action insCharAction =		new HTMLEditorAction(			Local.getString("Insert character"),			new ImageIcon(cl.getResource("resources/icons/char.png"))) {		public void actionPerformed(ActionEvent e) {			if (!charTableShow) {				addCharTablePanel();				charTableShow = true;				insCharActionB.setBorder(border2);			} else {				removeCharTablePanel();				charTableShow = false;				insCharActionB.setBorder(border1);			}			insCharActionB.setBorderPainted(charTableShow);		}	};	public Action findAction =		new HTMLEditorAction(			Local.getString("Find & Replace"),			new ImageIcon(cl.getResource("resources/icons/find.png"))) {		public void actionPerformed(ActionEvent e) {			doFind();		}	};	public InsertTableCellAction insertTableCellAction =		new InsertTableCellAction();	public InsertTableRowAction insertTableRowAction =		new InsertTableRowAction();	public BreakAction breakAction = new BreakAction();	public Action cutAction = new HTMLEditorKit.CutAction();	/*	 * new AbstractAction() { public void actionPerformed(ActionEvent e) { if	 * (editor.getSelectedText() == null) return; doCopy();	 * editor.replaceSelection(""); }	 *  	 */	public Action styleCopyAction = new HTMLEditorKit.CopyAction();	//new DefaultEditorKit.CopyAction();	public Action copyAction = styleCopyAction;	/*	 * new AbstractAction() { public void actionPerformed(ActionEvent e) { if	 * (editor.getSelectedText() == null) return; doCopy(); }	 */	public Action stylePasteAction = new HTMLEditorKit.PasteAction();		public Action pasteAction = //new HTMLEditorKit.PasteAction();	new AbstractAction() {		public void actionPerformed(ActionEvent e) {				//editor.paste();	doPaste();		}	};	private void doCopy() {		/*		 * java.awt.datatransfer.Clipboard clip =		 * java.awt.Toolkit.getDefaultToolkit().getSystemClipboard(); try {		 * String text = editor.getSelectedText();		 * //.getText(editor.getSelectionStart(),		 * editor.getSelectionEnd()-editor.getSelectionStart());		 * clip.setContents(new java.awt.datatransfer.StringSelection(text),		 * null); } catch (Exception e) { e.printStackTrace();		 */		Element el = document.getParagraphElement(editor.getSelectionStart());		if (el.getName().toUpperCase().equals("P-IMPLIED"))			el = el.getParentElement();		String elName = el.getName();		StringWriter sw = new StringWriter();		String copy;		java.awt.datatransfer.Clipboard clip =			java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();		try {			editorKit.write(				sw,				document,				editor.getSelectionStart(),				editor.getSelectionEnd() - editor.getSelectionStart());			copy = sw.toString();			copy = copy.split("<" + elName + "(.*?)>")[1];			copy = copy.split("</" + elName + ">")[0];			clip.setContents(				new java.awt.datatransfer.StringSelection(copy.trim()),				null);		} catch (Exception ex) {			ex.printStackTrace();		}	}	private void doPaste() {		Clipboard clip =			java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();		try {			Transferable content = clip.getContents(this);			if (content == null)				return;			String txt =				content					.getTransferData(new DataFlavor(String.class, "String"))					.toString();			document.replace(				editor.getSelectionStart(),				editor.getSelectionEnd() - editor.getSelectionStart(),				txt,				editorKit.getInputAttributes());			//editor.replaceSelection(content.getTransferData(new			// DataFlavor(String.class, "String")).toString());			//editor.paste();			//insertHTML(content.getTransferData(new DataFlavor(String.class,			// "String")).toString(), editor.getCaretPosition());			/*			 * Element el =			 * document.getParagraphElement(editor.getCaretPosition());			 * insertTextInElement(el, content.getTransferData(new			 * DataFlavor(String.class, "String")).toString(),			 */		} catch (Exception ex) {			ex.printStackTrace();		}	}	/*	 * private void insertTextInElement(Element el, String text, int pos) {	 * String elName = el.getName(); StringWriter sw = new StringWriter();	 * String copy1; String copy2; try { StringWriter sw1 = new StringWriter();	 * editorKit.write(sw1, document, el.getStartOffset(), pos -	 * el.getStartOffset()); copy1 = sw1.toString(); StringWriter sw2 = new	 * StringWriter(); editorKit.write(sw2, document, pos, el.getEndOffset() -	 * pos); copy2 = sw2.toString(); String copy = copy1+text+copy2; ?)>")[1];	 * copy = copy.split(" </" + elName + "> ")[0]; document.setInnerHTML(el,	 * copy); } catch (Exception ex) { ex.printStackTrace(); }	 */	public Action zoomInAction = new AbstractAction() {		public void actionPerformed(ActionEvent e) {			doZoom(true);		}	};	public Action zoomOutAction = new AbstractAction() {

⌨️ 快捷键说明

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