用于基于SWT的应用程序的RichText编辑器组件
生活随笔
收集整理的这篇文章主要介绍了
用于基于SWT的应用程序的RichText编辑器组件
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
本文将完成使用SWT实现我们自己的RichText编辑器组件的任务。 在为我的一位客户开发基于桌面的应用程序时,我遇到了这样一个可视化组件的需求,并希望添加一项功能,以允许用户使用粗体,斜体,删除线等功能来写富文本注释。 那时,我开始研究网络,以便找到一个开放源代码库,该库可以使我免于从头开始部署它的工作,而我遇到了“已完成”实现的列表 。 让我们在这里列出我对这种组件的需求:
- 它应该是本地SWT组件,而不是Eclipse View或Editor,并且必须可嵌入任何SWT组合中。
- 它应允许使用粗体,斜体,删除线等基本格式。
- 它应该支持剪贴板的基本操作,例如复制,剪切和粘贴。
- 它应该使用基本HTML输出文本。
- 它需要能够解析所生成的基本HTML,以允许版本。
下一个称为RichStringBuilder,它将用作帮助程序类,以将StyledText组件的内容格式化为基本HTML:
import java.util.Stack;public final class RichStringBuilder {public static final String LINE_DELIMITER = "<br/>";private StringBuilder builder;private Stack fontStyleStack;public RichStringBuilder() {builder = new StringBuilder();fontStyleStack = new Stack();}public RichStringBuilder append(String text) {builder.append(text);return this;}public RichStringBuilder appendLineBreak() {builder.append(LINE_DELIMITER);return this;}public RichStringBuilder startParagraph() {builder.append("<p>");return this;}public RichStringBuilder startFontStyle(FontStyle fontStyle) {fontStyleStack.push(fontStyle);internalStartFontStyle(fontStyle);return this;}public RichStringBuilder startFontStyles(FontStyle... fontStyles) {for (FontStyle fs : fontStyles) {startFontStyle(fs);}return this;}public RichStringBuilder endFontStyles(int count) {for (int i = 0;i < count;i++) {endStyle();}return this;}public RichStringBuilder endStyle() {if (fontStyleStack.size() > 0) {FontStyle style = fontStyleStack.pop();internalEndFontStyle(style);}return this;}public RichStringBuilder endParagraph() {flushStyles();builder.append("</p>");return this;}public void flushStyles() {while (fontStyleStack.size() > 0) {endStyle();}}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (null == o) return false;if (!(o instanceof RichStringBuilder)) return false;return ((RichStringBuilder) o).builder.equals(builder);}@Overridepublic int hashCode() {return builder.hashCode();}@Overridepublic String toString() {return builder.toString();}private void internalStartFontStyle(FontStyle fontStyle) {switch (fontStyle) {case BOLD:builder.append("<b>");break;case ITALIC:builder.append("<i>");break;case STRIKE_THROUGH:builder.append("<del>");break;case UNDERLINE:builder.append("<ins>");break;}}private void internalEndFontStyle(FontStyle fontStyle) {switch (fontStyle) {case BOLD:builder.append("</b>");break;case ITALIC:builder.append("</i>");break;case STRIKE_THROUGH:builder.append("</del>");break;case UNDERLINE:builder.append("</ins>");break;}}}第三个是基于SAX的内容处理程序,它将基本HTML解析为StyledText控件时将启动事件:
import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.List; import java.util.Stack;import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory;import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyleRange; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler;public final class RichTextParser {public static RichTextParser parse(String formattedText)throws ParserConfigurationException, SAXException, IOException {return new RichTextParser(formattedText);}private StringBuilder text = new StringBuilder();private List styleRanges = new ArrayList();private RichTextParser(String formattedText)throws ParserConfigurationException, SAXException, IOException {StringReader reader = new StringReader(formattedText);SAXParserFactory factory = SAXParserFactory.newInstance();SAXParser parser = factory.newSAXParser();DefaultHandler handler = new RichTextContentHandler();parser.parse(new InputSource(reader), handler);}public String getText() {return text.toString();}public StyleRange[] getStyleRanges() {return styleRanges.toArray(new StyleRange[styleRanges.size()]);}private class RichTextContentHandler extends DefaultHandler {private Stack<List> stylesStack = new Stack<List>();private String lastTextChunk = null;@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {lastTextChunk = new String(ch, start, length);}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {// If there is not any previous text chunk parsed then returnif (lastTextChunk == null) return;// If the tag found is not a supported one then returnif (!"p".equals(qName) || !"b".equals(qName) || !"i".equals(qName) ||!"ins".equals(qName) || !"del".equals(qName)) {return;}List lastStyles = lastFontStyles(true);if (lastStyles != null) {StyleRange range = transform(lastStyles);range.start = currentIndex() + 1;range.length = lastTextChunk.length();styleRanges.add(range);}text.append(lastTextChunk);lastTextChunk = null;}@Overridepublic void startElement(String uri, String localName, String qName,Attributes atts) throws SAXException {// If the tag found is not a supported one then returnif (!"p".equals(qName) || !"b".equals(qName) || !"i".equals(qName) ||!"ins".equals(qName) || !"del".equals(qName)) {return;}List lastStyles = lastFontStyles(false);if (lastTextChunk == null) {if (lastStyles == null) {lastStyles = new ArrayList();stylesStack.add(lastStyles);}} else {if (lastStyles != null) {StyleRange range = transform(lastStyles);range.start = currentIndex() + 1;range.length = lastTextChunk.length();styleRanges.add(range);}text.append(lastTextChunk);lastTextChunk = null;}if ("b".equals(qName)) {lastStyles.add(FontStyle.BOLD);} else if ("i".equals(qName)) {lastStyles.add(FontStyle.ITALIC);} else if ("ins".equals(qName)) {lastStyles.add(FontStyle.UNDERLINE);} else {lastStyles.add(FontStyle.STRIKE_THROUGH);}}private StyleRange transform(List styles) {StyleRange range = new StyleRange();range.start = currentIndex() + 1;range.length = lastTextChunk.length();for (FontStyle fs : styles) {if (FontStyle.BOLD == fs) {range.fontStyle = (range.fontStyle & SWT.BOLD);} else if (FontStyle.ITALIC == fs) {range.fontStyle = (range.fontStyle & SWT.ITALIC);} else if (FontStyle.STRIKE_THROUGH == fs) {range.strikeout = true;} else if (FontStyle.UNDERLINE == fs) {range.underline = true;}}return range;}private List lastFontStyles(boolean remove) {List lastStyles = null;if (stylesStack.size() > 0) {if (remove) {lastStyles = stylesStack.pop();} else {lastStyles = stylesStack.peek();}}return lastStyles;}private int currentIndex() {return text.length() - 1;}}} 结论 实现您自己的SWT RichText控件可能不是满足您需求的最佳选择,您将需要权衡这样做的利弊,以及是否有必要投资其中一种现成的商业解决方案。 但是,我想通过本文演示如何在SWT对话框和视图中嵌入您自己的(简单且轻量级的)富文本编辑器,与从中获得的好处相比,它很容易实现,并且需要花费很少的精力。 参考:来自Code Nibbles博客的JCG合作伙伴 Alonso Dominguez的RichText编辑器组件,用于基于SWT的应用程序 。翻译自: https://www.javacodegeeks.com/2012/07/richtext-editor-component-for-swt-based.html
总结
以上是生活随笔为你收集整理的用于基于SWT的应用程序的RichText编辑器组件的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 广东为什么叫岭南 广东叫岭南的原因分析
- 下一篇: 登录:应用程序错误通知