JFace Viewer final

来源:互联网 发布:淘宝要好评话语 编辑:程序博客网 时间:2024/05/18 15:28
 

5.2. Text Viewers

 

TextViewer类封装了StyledText控件 (参见 Figure 5-11 TextViewer 层次图)。文本可以拥有很多不同的风格,像文字颜色,背景色,是否粗体等。文本查看器使用文字框控件,为客户程序提供了文档模型,并管理文档模型到风格化文字信息的转换。

 

 

Figure 5-11. TextViewer 层次图

 

 


常用API包括:

addTextListener(ITextListener) 为查看器添加文本监听器。

 

appendVerifyKeyListener(VerifyKeyListener) 添加键盘校验监听器到查看器的键盘监听器列表中。

 

canDoOperation(int) 返回指定操作码对应的操作是否可以执行。

 

changeTextPresentation(TextPresentation, boolean) 应用颜色信息,颜色信息被编码在指定的文本表达中 。

 

doOperation(int) 执行执行操作码对应的操作。

 

enableOperation(int, boolean)设置指定的文本操作是否可用。

 

getSelectedRange() 返回与查看器文档相符的当前选择范围。

 

getSelection() 返回当前选择内容。

 

getTextWidget() 返回查看器文本控件。

 

isEditable() 返回文本是否可以被编辑。

 

refresh() 使用查看器模型中的信息,刷新查看器。

 

setDocument(IDocument) 设置指定的文档作为文本查看器的模型,并作相应的展示更新。Sets the given document as the text viewer's model and updates the presentation accordingly.

 

setEditable(boolean) 设置是否可编辑。

 

setInput(Object)设置 或者清空查看器的输入内容。 TextViewer 实现该方法使用输入对象调用 setDocument(IDocument) 方法。如果输入对象是IDocument则输入该对象,如果不是,则输入null

 

setRedraw(boolean)启用或禁用文本查看器的重画功能。

 

setSelectedRange(int, int) 设置选择内容为指定的范围。

 

setSelection(ISelection, boolean) 设置新的选择内容,并可选择是否使其可见。

 

setTextColor(Color)应用指定的颜色。

 

 

setTextColor(Color, int, int, boolean) 为指定的部分应用指定的颜色。

 

setTextHover(ITextHover, String)设置查看器的文本漂浮窗的内容。

 

下面的例子创建了文本查看器:(参见 Figure 5-12)。

 

import org.eclipse.jface.text.*;import org.eclipse.swt.*;import org.eclipse.swt.custom.*;import org.eclipse.swt.graphics.*;import org.eclipse.swt.layout.*;import org.eclipse.swt.widgets.*;public class TextViewerExample {   public static void main(String[] args) {      Display display = new Display();      Shell shell = new Shell(display);      shell.setText("Text Viewer Example");      shell.setBounds(100, 100, 225, 125);      shell.setLayout(new FillLayout());      final TextViewer textViewer =         new TextViewer(shell, SWT.MULTI | SWT.V_SCROLL);      String string = "This is plain text/n"         + "This is bold text/n"         + "This is red text";      Document document = new Document(string);      textViewer.setDocument(document);      TextPresentation style = new TextPresentation();      style.addStyleRange(         new StyleRange(19, 17, null, null, SWT.BOLD));      Color red = new Color(null, 255, 0, 0);      style.addStyleRange(         new StyleRange(37, 16, red, null));      textViewer.changeTextPresentation(style, true);      shell.open();      while (!shell.isDisposed()) {         if (!display.readAndDispatch()) display.sleep();      }      display.dispose();   }}

 

Figure 5-12. TextViewer example.

 


创建文本查看器后,创建Document对象保存文本信息,并被赋于文本查看器。然后创建TextPresentation 对象,使其持有风格范围信息。设置了两种风格范围:一种是文字粗体,一种是文字颜色为红色。 StyleRange 构造方法的第一个参数是文本中要应用此风格的第一个字符的序列号。第二个参数是要应用此风格的字符的个数。最后,风格对象被赋于查看器。

 

 

5.3. 总结

JFace 查看器在 Eclipse 插件开发中使用的很广泛。查看器对基础Eclipse控件提供了面向对象的封装,可以很容易的对高层次的域对象进行封装,而不是处理基本的文字,数字和图片。同样的,文本查看器可以很容易的处理需要使用复杂风格的文本。在第 7 章, 视图 将进行更详细的介绍。 

参考书目: 

 

本章源代码(www.qualityeclipse.com/projects/source-ch-04-and-05.zip).

Gauthier, Laurent, "使用SWT/JFace创建并发布表格编辑器," Mirasol Op'nWorks, 2003,7,3 (www.eclipse.org/articles/Article-Table-viewer/table_viewer.html).

 

Grindstaff, Chris, "怎样使用JFace树查看器," ,2002,5,5 (www.eclipse.org/articles/treeviewer-cg/TreeViewerArticle.htm).

 




原创粉丝点击