站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题Eclipse Form程序设计指南(4)

Eclipse Form程序设计指南(4)

摘要: Eclipse Form程序设计指南(4)

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 FormToolkit的createFormText()方法创建FromText控件

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

(3)URL转换为超链接

l 下面是将URL转换为超链接的例子,和上面例子的唯一区别是FromText的setText()方法的第三个参数设置为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>可以包含

  • 标记

    n

  • 可以包含普通的文本、
    、图像()和超链接(

    n 标记不允许嵌套

    n

    有vspace属性,表示是否要加垂直空白(缺省是true)

    n

  • 有下列属性:

    u vspace:同

    u style:bullet(缺省)、text和image值之一

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

    u indent:bullet内容缩进的大小(像素为单位)

    u bindent:bullet自身缩进的大小(像素为单位)

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

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

    n :使包含的文本变粗体

    n
    :强制换行

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

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

                  StringBuffer buf = new StringBuffer();
                  buf.append("<form>");
                  buf.append("

    ");

                  buf.append("Here is some plain text for the text to render; ");
                  buf.append("this text is at http://www.eclipse.org web site.");
                  buf.append("

    ");
                  buf.append("

    ");

                  buf.append("color=\"header\" font=\"header\">This text is in header font and color.");
                  buf.append("

    ");
                  buf.append("

    This line will contain some bold and some text\">source text. ");

                  buf.append("We can also add  an image. ");
                  buf.append("

    ");
                  buf.append("
  • A default (bulleted) list item.
  • ");
                  buf.append("
  • Another bullet list item.
  • ");
                  buf.append("
  • A list item with text.
  • ");
                  buf.append("
  • Another list item with text
  • ");
                  buf.append("
  • List item with an image bullet>");
  •               buf.append("
  • A list item with text.
  • ");
                  buf.append("
  • A list item with text.
  • ");
                  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 下面是上面例子呈现的内容

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

    n 标记不能嵌套

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

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

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

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

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

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