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

📄 htmleditor.java

📁 Memoranda( 从前以jNotes2而闻名) 是一个日志管理和个人项目管理工具
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	}	public void ulActionB_actionPerformed(ActionEvent e) {		String parentname =			document				.getParagraphElement(editor.getCaretPosition())				.getParentElement()				.getName();		HTML.Tag parentTag = HTML.getTag(parentname);		HTMLEditorKit.InsertHTMLTextAction ulAction =			new HTMLEditorKit.InsertHTMLTextAction(				"insertUL",				"<ul><li></li></ul>",				parentTag,				HTML.Tag.UL);		ulAction.actionPerformed(e);		//removeIfEmpty(document.getParagraphElement(editor.getCaretPosition()-1));		list = true;		/*		 * Element pEl =		 * document.getParagraphElement(editor.getCaretPosition());		 * StringWriter sw = new StringWriter(); try { editorKit.write(sw,		 * document, pEl.getStartOffset(),		 * pEl.getEndOffset()-pEl.getStartOffset()); String copy =		 * sw.toString(); String elName = pEl.getName(); copy =		 * copy.substring(copy.indexOf(" <"+elName)); copy =		 * copy.substring(0,copy.indexOf(" </"+elName)+elName.length()+3);		 * document.setOuterHTML(pEl, " <ul><li> "+copy+" </li></ul> ");		 * System.out.println(copy); } catch (Exception ex){		 * ex.printStackTrace();		 */	}	public void olActionB_actionPerformed(ActionEvent e) {		String parentname =			document				.getParagraphElement(editor.getCaretPosition())				.getParentElement()				.getName();		HTML.Tag parentTag = HTML.getTag(parentname);		HTMLEditorKit.InsertHTMLTextAction olAction =			new HTMLEditorKit.InsertHTMLTextAction(				"insertOL",				"<ol><li></li></ol>",				parentTag,				HTML.Tag.OL);		olAction.actionPerformed(e);		//removeIfEmpty(document.getParagraphElement(editor.getCaretPosition()-1));		list = true;	}	void removeIfEmpty(Element elem) {		if (elem.getEndOffset() - elem.getStartOffset() < 2) {			try {				document.remove(elem.getStartOffset(), elem.getEndOffset());			} catch (Exception ex) {				//ex.printStackTrace();			}		}	}	class ParaBreakAction extends AbstractAction {		ParaBreakAction() {			super("ParaBreakAction");		}		public void actionPerformed(ActionEvent e) {			Element elem =				document.getParagraphElement(editor.getCaretPosition());			String elName = elem.getName().toUpperCase();			String parentname = elem.getParentElement().getName();			HTML.Tag parentTag = HTML.getTag(parentname);			if (parentname.toUpperCase().equals("P-IMPLIED"))				parentTag = HTML.Tag.IMPLIED;			if (parentname.toLowerCase().equals("li")) {				// HTML.Tag listTag =				// HTML.getTag(elem.getParentElement().getParentElement().getName());				if (elem.getEndOffset() - elem.getStartOffset() > 1) {					try {						document.insertAfterEnd(							elem.getParentElement(),							"<li></li>");						editor.setCaretPosition(							elem.getParentElement().getEndOffset());					} catch (Exception ex) {						ex.printStackTrace();					}					/*					 * HTMLEditorKit.InsertHTMLTextAction liAction = new					 * HTMLEditorKit.InsertHTMLTextAction("insertLI", " <li>					 * </li> ", parentTag, HTML.Tag.LI);					 */				} else {					try {						document.remove(editor.getCaretPosition(), 1);					} catch (Exception ex) {						ex.printStackTrace();					}					Element listParentElement =						elem							.getParentElement()							.getParentElement()							.getParentElement();					HTML.Tag listParentTag =						HTML.getTag(listParentElement.getName());					String listParentTagName = listParentTag.toString();					if (listParentTagName.toLowerCase().equals("li")) {						Element listAncEl =							listParentElement.getParentElement();						try {							editorKit.insertHTML(								document,								listAncEl.getEndOffset(),								"<li><p></p></li>",								3,								0,								HTML.Tag.LI);						} catch (Exception ex) {							ex.printStackTrace();						}					} else {						HTMLEditorKit.InsertHTMLTextAction pAction =							new HTMLEditorKit.InsertHTMLTextAction(								"insertP",								"<p></p>",								listParentTag,								HTML.Tag.P);						pAction.actionPerformed(e);					}				}			} else if (				(elName.equals("PRE"))					|| (elName.equals("ADDRESS"))					|| (elName.equals("BLOCKQUOTE"))) {				if (editor.getCaretPosition() > 0)					removeIfEmpty(						document.getParagraphElement(							editor.getCaretPosition() - 1));				HTMLEditorKit.InsertHTMLTextAction pAction =					new HTMLEditorKit.InsertHTMLTextAction(						"insertP",						"<p></p>",						parentTag,						HTML.Tag.P);				System.out.println("PRE");				pAction.actionPerformed(e);			} else if (elName.equals("P-IMPLIED")) {				/*				 * HTML.Tag sParentTag =				 * HTML.getTag(elem.getParentElement().getParentElement().getName());				 * if (editor.getCaretPosition() > 0)				 * removeIfEmpty(document.getParagraphElement(editor.getCaretPosition() -				 * 1)); HTMLEditorKit.InsertHTMLTextAction pAction = new				 * HTMLEditorKit.InsertHTMLTextAction("insertP", " <p></p> ",				 * sParentTag, HTML.Tag.P);				 * System.out.println(sParentTag.toString());				 */				try {					System.out.println("IMPLIED");					document.insertAfterEnd(elem.getParentElement(), "<p></p>");					editor.setCaretPosition(						elem.getParentElement().getEndOffset());				} catch (Exception ex) {					ex.printStackTrace();				}			} else {				//removeIfEmpty(editor.getStyledDocument().getParagraphElement(editor.getCaretPosition()-1));				/*				 * HTMLEditorKit.InsertHTMLTextAction pAction = new				 * HTMLEditorKit.InsertHTMLTextAction("insertP"," <p></p> ",				 * HTML.Tag.BODY, HTML.Tag.P);				 */				//HTMLEditorKit.InsertBreakAction iba = new				// HTMLEditorKit.InsertBreakAction();				//iba.actionPerformed(e);				editor.replaceSelection("\n");				editorKit.getInputAttributes().removeAttribute(					HTML.Attribute.ID);				editorKit.getInputAttributes().removeAttribute(					HTML.Attribute.CLASS);			}			//System.out.println(e.getWhen());		}	}	class BreakAction extends AbstractAction {		BreakAction() {			super(				Local.getString("Insert break"),				new ImageIcon(cl.getResource("resources/icons/break.png")));		}		public void actionPerformed(ActionEvent e) {			String elName =				document					.getParagraphElement(editor.getCaretPosition())					.getName();			/*			 * if ((elName.toUpperCase().equals("PRE")) ||			 * (elName.toUpperCase().equals("P-IMPLIED"))) {			 * editor.replaceSelection("\r"); return;			 */			HTML.Tag tag = HTML.getTag(elName);			if (elName.toUpperCase().equals("P-IMPLIED"))				tag = HTML.Tag.IMPLIED;			HTMLEditorKit.InsertHTMLTextAction hta =				new HTMLEditorKit.InsertHTMLTextAction(					"insertBR",					"<br>",					tag,					HTML.Tag.BR);			hta.actionPerformed(e);			//insertHTML("<br>",editor.getCaretPosition());		}	}	class InsertTableRowAction extends AbstractAction {		InsertTableRowAction() {			super(Local.getString("Insert table row"));			this.putValue(				Action.ACCELERATOR_KEY,				KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_MASK));		}		public void actionPerformed(ActionEvent e) {			String trTag = "<tr>";			Element tr =				document					.getParagraphElement(editor.getCaretPosition())					.getParentElement()					.getParentElement();			for (int i = 0; i < tr.getElementCount(); i++)				if (tr.getElement(i).getName().toUpperCase().equals("TD"))					trTag += "<td><p></p></td>";			trTag += "</tr>";			/*			 * HTMLEditorKit.InsertHTMLTextAction hta = new			 * HTMLEditorKit.InsertHTMLTextAction("insertTR",trTag,			 * HTML.Tag.TABLE, HTML.Tag.TR);			 */			try {				document.insertAfterEnd(tr, trTag);				//editorKit.insertHTML(document, editor.getCaretPosition(),				// trTag, 3, 0, HTML.Tag.TR);			} catch (Exception ex) {				ex.printStackTrace();			}		}		public boolean isEnabled() {			if (document == null)				return false;			return document				.getParagraphElement(editor.getCaretPosition())				.getParentElement()				.getName()				.toUpperCase()				.equals("TD");		}		public void update() {			this.setEnabled(isEnabled());		}	}	class InsertTableCellAction extends AbstractAction {		InsertTableCellAction() {			super(Local.getString("Insert table cell"));			this.putValue(				Action.ACCELERATOR_KEY,				KeyStroke.getKeyStroke(					KeyEvent.VK_ENTER,					KeyEvent.CTRL_MASK + KeyEvent.SHIFT_MASK));		}		public void actionPerformed(ActionEvent e) {			String tdTag = "<td><p></p></td>";			Element td =				document					.getParagraphElement(editor.getCaretPosition())					.getParentElement();			try {				document.insertAfterEnd(td, tdTag);			} catch (Exception ex) {				ex.printStackTrace();			}		}		public boolean isEnabled() {			if (document == null)				return false;			return document				.getParagraphElement(editor.getCaretPosition())				.getParentElement()				.getName()				.toUpperCase()				.equals("TD");		}		public void update() {			this.setEnabled(isEnabled());		}	}	public void lAlignActionB_actionPerformed(ActionEvent e) {		HTMLEditorKit.AlignmentAction aa =			new HTMLEditorKit.AlignmentAction(				"leftAlign",				StyleConstants.ALIGN_LEFT);		aa.actionPerformed(e);	}	public void cAlignActionB_actionPerformed(ActionEvent e) {		HTMLEditorKit.AlignmentAction aa =			new HTMLEditorKit.AlignmentAction(				"centerAlign",				StyleConstants.ALIGN_CENTER);		aa.actionPerformed(e);	}	public void rAlignActionB_actionPerformed(ActionEvent e) {		HTMLEditorKit.AlignmentAction aa =			new HTMLEditorKit.AlignmentAction(				"rightAlign",				StyleConstants.ALIGN_RIGHT);		aa.actionPerformed(e);	}	public void jAlignActionB_actionPerformed(ActionEvent e) {		HTMLEditorKit.AlignmentAction aa =			new HTMLEditorKit.AlignmentAction(				"justifyAlign",				StyleConstants.ALIGN_JUSTIFIED);		aa.actionPerformed(e);	}	public void insertHTML(String html, int location) {		//assumes editor is already set to "text/html" type		try {			HTMLEditorKit kit = (HTMLEditorKit) editor.getEditorKit();			Document doc = editor.getDocument();			StringReader reader = new StringReader(html);			kit.read(reader, doc, location);		} catch (Exception ex) {			ex.printStackTrace();		}	}	public void imageActionB_actionPerformed(ActionEvent e) {		/*		 * JFileChooser chooser = new JFileChooser();		 * chooser.setFileHidingEnabled(false); chooser.setDialogTitle("Choose		 * image file"); chooser.setAcceptAllFileFilterUsed(false);		 * chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);		 * chooser.addChoosableFileFilter(new		 * net.sf.memoranda.ui.htmleditor.filechooser.ImageFilter());		 * chooser.setAccessory(new		 * net.sf.memoranda.ui.htmleditor.filechooser.ImagePreview(chooser));		 *  		 */		ImageDialog dlg = new ImageDialog(null);		Dimension dlgSize = dlg.getPreferredSize();		Dimension frmSize = this.getSize();		Point loc = this.getLocationOnScreen();		dlg.setLocation(			(frmSize.width - dlgSize.width) / 2 + loc.x,			(frmSize.height - dlgSize.height) / 2 + loc.y);		//dlg.setLocation(imageActionB.getLocationOnScreen());		dlg.setModal(true);		dlg.setVisible(true);		if (!dlg.CANCELLED) {			String parentname =				document					.getParagraphElement(editor.getCaretPosition())					.getParentElement()					.getName();			//HTML.Tag parentTag = HTML.getTag(parentname);			String urlString = dlg.fileField.getText();			String path = urlString;			if (imagesDir != null) {				try {					URL url = new URL(urlString);					if (!url.getProtocol().startsWith("http"))						path = imagesDir + "/" + url.getFile();				} catch (MalformedURLException e1) {				}			}			try {				String imgTag =					"<img src=\""						+ path						+ "\" alt=\""						+ dlg.altField.getText()						+ "\" ";				String w = dlg.widthField.getText();				try {					Integer.parseInt(w, 10);					imgTag += " width=\"" + w + "\" ";				} catch (Exception ex) {					ex.printStackTrace();				}				String h = dlg.heightField.getText();				try {					Integer.parseInt(h, 10);					imgTag += " height=\"" + h + "\" ";				} catch (Exception ex) {					ex.printStackTrace();				}				String hs = dlg.hspaceField.getText();				try {					Integer.parseInt(hs, 10);					imgTag += " hspace=\"" + hs + "\" ";				} catch (Exception ex) {					ex.printStackTrace();				}				String vs = dlg.vspaceField.getText();				try {					Integer.parseInt(vs, 10);					imgTag += " vspace=\"" + vs + "\" ";				} catch (Exception ex) {					ex.printStackTrace();				}				String b = dlg.borderField.getText();				try {					Integer.parseInt(b, 10);					imgTag += " border=\"" + b + "\" ";				} catch (Exception ex) {

⌨️ 快捷键说明

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