Open Java Project

Keep it simple, Stupid, Less is more

BlogJava 首页 新随笔 联系 聚合 管理
  20 Posts :: 0 Stories :: 12 Comments :: 0 Trackbacks

5FromText控件

1)概述

l         虽然使用Label、超链接(或图像链接)以及TableWrapLayout布局就能创建丰富的Form内容,但是要接近Web外观还是很有限的

l         Eclipse From提供了FromText控件来创建Rich文本,作为上述的补充,有三种形式:

n         按纯文本呈现

n         将文本中URL转换为超链接呈现

n         XML标记解析呈现

2)纯文本

l         下面是按纯文本呈现的例子(等同于Label

              FormText rtext = toolkit.createFormText(body, true);
              td = new TableWrapData(TableWrapData.FILL);
              td.colspan = 2;
              rtext.setLayoutData(td);
              String data = "Here is some plain text for the text to render.";
              rtext.setText(data, false, false);

l         FormToolkitcreateFormText()方法创建FromText控件

l         FromTextsetText()方法指定要呈现的文本,后面两个参数指定是否要按XML标记进行解析和是否要将文本中的URL转换为超链接

3URL转换为超链接

l         下面是将URL转换为超链接的例子,和上面例子的唯一区别是FromTextsetText()方法的第三个参数设置为True

              FormText rtext = toolkit.createFormText(body, true);
              td = new TableWrapData(TableWrapData.FILL);
              td.colspan = 2;
              rtext.setLayoutData(td);
              Display display = FormSamplesPlugin.getDefault().getWorkbench().getDisplay();
              FormColors formColors = new FormColors(display);
              Color activeLinkColor = formColors.createColor("activeLink", 175,225,200);
              HyperlinkSettings linkSettings = new HyperlinkSettings(display);
              linkSettings.setActiveForeground(activeLinkColor);
              linkSettings.setHyperlinkUnderlineMode(HyperlinkSettings.UNDERLINE_HOVER);
              rtext.setHyperlinkSettings(linkSettings);
              String data = "Here is some plain text for the text to render; this text is at http://www.eclipse.org/ web site.";
              rtext.setText(data, false, true);
              rtext.addHyperlinkListener(new HyperlinkAdapter() {
                public void linkActivated(HyperlinkEvent e) {
                       System.out.println("Link active: "+e.getHref());
                }
              });

l         既然能够将URL转换成超链接,FromText同样提供addHyperlinkListener()方法来监听超链接事件

l         FromText还提供setHyperlinkSettings()方法来设置超链接的属性(注意,要在setText()方法之前设置才有效)

l         超链接的属性由HyperlinkSettings对象管理,包括颜色和下划线模式的设置

l         例中使用了Eclipse From提供的FormColors对象(颜色管理器)来管理颜色,createColor()方法创建一种新的颜色,并提供了key值,以便以后可以使用getColor(key)访问

l         这里是使用FormColors的一个简单例子,通常一个插件应该只有一个颜色管理器(FormColors),可以使用Singlton模式来访问FormColors对象

4解析XML标记

l         解析XML标记是FormText最强大的用法,但是FormText不完全支持所有的标记,下面是FormText支持的标记,而且用法有些不同:

n         根标记必须是<form>

n         <form>可以包含<p><li>标记

n         <p><li>可以包含普通的文本、<br><b><span>、图像(<img>)和超链接(<a>

n         标记不允许嵌套

n         <p>vspace属性,表示是否要加垂直空白(缺省是true

n         <li>有下列属性:

u       vspace:同<p>

u       stylebullet缺省)、textimage值之一

u       value:当style=bullet时,无效;当style=text时,指定作为bullet的文本;当style=image时,指定作为bullet的图像(key值)

u       indentbullet内容缩进的大小(像素为单位)

u       bindentbullet自身缩进的大小(像素为单位)

n         <img>显示图像,其属性href指定的是一个key值,该值和FormTextsetImage()方法中key参数指定的值是对应的

n         <a>显示超链接,其属性href指定URL并通过FormText添加监听器来监听超链接的点击事件,<a>还有nowarp属性,指定是否允许超链接自动换行

n         <b>:使包含的文本变粗体

n         <br>:强制换行

n         <span>:使包含的文本具有特定的颜色(color属性)和字体(font属性)这些属性的值也是一个key值,和FormTextsetColor()setFont()方法中key参数指定的值是对应的

l         下面是一个解析XML标记的例子:

              StringBuffer buf = new StringBuffer();
              buf.append("<form>");
              buf.append("<p>");
              buf.append("Here is some plain text for the text to render; ");
              buf.append("this text is at <a href=\"http://www.eclipse.org\" nowrap=\"true\">http://www.eclipse.org</a> web site.");
              buf.append("</p>");
              buf.append("<p>");
              buf.append("<span color=\"header\" font=\"header\">This text is in header font and color.</span>");
              buf.append("</p>");
              buf.append("<p>This line will contain some <b>bold</b> and some <span font=\"text\">source</span> text. ");
              buf.append("We can also add <img href=\"image\"/> an image. ");
              buf.append("</p>");
              buf.append("<li>A default (bulleted) list item.</li>");
              buf.append("<li>Another bullet list item.</li>");
              buf.append("<li style=\"text\" value=\"1.\">A list item with text.</li>");
              buf.append("<li style=\"text\" value=\"2.\">Another list item with text</li>");
              buf.append("<li style=\"image\" value=\"image\">List item with an image bullet</li>");
              buf.append("<li style=\"text\" bindent=\"20\" indent=\"40\" value=\"3.\">A list item with text.</li>");
              buf.append("<li style=\"text\" bindent=\"20\" indent=\"40\" value=\"4.\">A list item with text.</li>");
              buf.append("</form>");
              FormText rtext = toolkit.createFormText(body, true);
              td = new TableWrapData(TableWrapData.FILL);
              td.colspan = 2;
              rtext.setLayoutData(td);
              rtext.setImage("image", FormSamplesPlugin.imageDescriptorFromPlugin("FormSamples","images/aspect.gif").createImage());
              rtext.setColor("header", toolkit.getColors().getColor(FormColors.TITLE));
              rtext.setFont("header", JFaceResources.getHeaderFont());
              rtext.setFont("text", JFaceResources.getTextFont());
              rtext.setText(buf.toString(), true, false);
              rtext.addHyperlinkListener(new HyperlinkAdapter() {
                public void linkActivated(HyperlinkEvent e) {
                       System.out.println("Link active: "+e.getHref());
                }
              });

l         下面是上面例子呈现的内容

 o_1.JPG

l         就像上面提到的FormTextXML标记的支持是有限的,例如:

n         标记不能嵌套

n         只支持粗体(<b>),而不支持斜体

n         文本内容不能被选取,等等

l         所以对于FormText的一些限制,需要考虑其它的替代方法:

n         如果需要具有更为复杂的格式化能力,可以使用Browser控件

n         如果需要具有编辑和字体、颜色风格的能力,可以使用StyleText控件

n         如果需要文本能够自动换行,可以使用具有SWT.WARP风格的SWT Label控件

posted on 2005-07-04 18:37 nelson_tu 阅读(2557) 评论(2)  编辑  收藏 所属分类: Eclipse开发

Feedback

# re: Eclipse Form程序设计指南(4)[未登录] 2008-03-22 18:42 新手
有没有master/details模式的呀?  回复  更多评论
  

# re: Eclipse Form程序设计指南(4)[未登录] 2008-03-24 15:57 新手
FormPage里有两个Master又怎么样呢  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: