使用 IBM Lotus Sametime V7.5.1 中的扩展点构建插件,让您能够显示缩略词的全写。
缩略词是我们日常语言的一部分。这个世界似乎很忙碌,我们在书写电子邮件或即时消息时总是喜欢使用一些缩略词来节省按键次数。常见流行的缩略词有 IMHO(窃以为)、TTYL(稍后再聊)和 BRB(马上回来)等,而新的缩略词也在不断出现。
本文将向您展示如何为 IBM Lotus Sametime Connect V7.5.1 创建有用的 Acronym Expander 插件。您还将了解如何使用 com.ibm.collaboration.realtime.messages.MessageHandlerListener 扩展点创建侦听类,该类用于在 Lotus Sametime 消息被发送到 Lotus Sametime 用户之前侦听该消息。此侦听类可以提供中介功能;本文中的中介功能是扩展缩略词。
首先,我们来看一下成品。图 1 显示了发送方视图中的一个 Lotus Sametime Connect 聊天窗口。注意,发送消息中的 IBM 就是一个缩略词。
图 2 显示了启用 Acronym Expander 插件后所看到的情况(前提是词典中定义了 IBM 这个缩略词)。注意,缩略词 IBM 已经被扩展成了 International Business Machines。
注:此缩略词扩展发生在接收方一端,而不是发送方一端。
开发 Acronym Expander 插件的第一步是创建 Eclipse 插件项目,用于保存 Lotus Sametime 插件。首先,选择 File - New - Project 启动 New Project 向导。在 New Project 向导中选择 Plug-in Project 并单击 Next。
接下来,在 Project Name 域中键入 project 名称:com.devworks.example.acronym。
接着会出现一个屏幕,您可以在其中编辑插件属性。确保选中 “This plug-in will make contributions to the UI” 选项,然后单击 Finish。
单击 Finish 后,可能会询问您是否要切换到 Plug-in Development Perspective,如图 3 所示。单击 Yes 切换到 Plug-in Development Perspective。
要开发 Lotus Sametime V7.5.1 插件,必须将目标平台从 Eclipse 更改为 Lotus Sametime。为此,将 Eclipse 首选项更改为 Target Platform。选择 Window - Preferences。出现 Preferences 对话框(如图 4 所示)。在左侧窗格中选择 Plug-in Development - Target Platform。指定 Lotus Sametime Connect 的安装目录为 Target Platform. 的位置;在本例中,指定为 C:\Program Files\IBM\Sametime Connect。单击 OK。
Target platform. 已经指定完毕,接着就可以扩展 Lotus Sametime Connect 了。
可以开始构建 Acronym Expander 插件了。您需要一种方法来定义缩略词及其相关的扩展含义。
要在缩略词词典中输入所需的缩略词(Acronym Expander 插件使用该词典来扩展缩略词),需要使用 Eclipse Preferences 页面。Acronym Expander Preferences 页面操作如图 5 所示。
单击 Acronym Expander Preferences 页面上的 New 按钮后,将出现一个消息框(如图 6 所示),您可以在其中输入新的缩略词及其扩展形式。如您所见,条目的格式为 ACRONYM = Expanded 形式。
我们创建了一个 com.devworks.example.acronym.preferences.PreferenceConstants 类来具体化 Preferences 页面使用的字符串字段名称。选择 File - New Class 来创建这个类,将出现一个 New Java Class 对话框,如图 7 所示。填入包名 com.devworks.example.acronym.preferences 和 Java 类名 PreferenceConstants。
com.devworks.example.acronym.preferences.PreferenceConstants 类将出现在 package explorer 中。将自动生成的代码替换为如下所示的代码:
public class PreferenceConstants {在这个代码片段中,常量 ACRONYM_LIST 是用于获取和设置首选存储中的数据的关键字。com.devworks.example.acronym.preferences.AcronymPreferencePage 类和 com.example.acronym.preferences.AcronymListStore 类都可以使用此关键字。
使用 Plug-in Manifest Editor 定义一个新扩展
要添加一个专用于 Acronym Expander 的 Eclipse Preferences 页面,需要使用 Eclipse 扩展点。您可以使用 Plug-in Manifest Editor 来定义这个扩展点。要打开 Plug-in Manifest Editor,打开 META-INF/MANIFEST.MF 文件并单击 Extensions 附签。Extensions 窗口如图 8 所示。
现在单击 Extensions 附签上的 Add 按钮,将出现 New Extension 向导,如图 9 所示。取消选定 “Show only extension points from the required plug-ins” 选项。在 Extension Point 筛选器中,键入 org.eclipse.ui.preferencePages。从下方窗格中选择 org.eclipse.ui.preferencePages,然后单击 Finish。
可能会询问您是否要将插件 org.eclipse.ui 添加到插件依赖项列表中,如图 10 所示。单击 Yes 添加。如果没有询问您,则说明它已经存在于插件依赖项列表中了。
保存并关闭 Plug-in Manifest Editor。
现在定制 org.eclipse.ui.preferencePages 扩展点(由 New Extension 向导创建)的扩展。为此需要编辑 plugin.xml 文件。plugin.xml 文件包含到 org.eclipse.ui.preferencePages 扩展点的扩展:
编辑此扩展,如清单 1 所示。
如清单 1 所示,指定使用 org.eclipse.ui.preferencePages 扩展点。此扩展点提供了一种方式让插件将 Eclipse Preference 页面提供给 Preferences 窗口。在扩展点定义中,您指定了一个惟一的 ID 用于表示页面(com.devworks.example.acronym.preferences.AcronymPreferencePage)和向用户显示的名称(我们选择为 Preference 页面显示 Acronym Expander)。您还指定 com.devworks.example.acronym.preferences.AcronymPreferencePage 为实现 org.eclipse.ui.IWorkbenchPreferencePage 接口的类。当您扩展 org.eclipse.ui.preferencePages 扩展点时需要负责实现该接口。类层次结构如图 11 所示。
com.devworks.example.acronym.preferences.AcronymPreferencePage 类如清单 2 所示。如您所见,在 createFieldEditors 方法中,您创建了自己的列表编辑器 com.devworks.example.acronym.preferences.AcronymListEditor。
public class AcronymPreferencePage
extends FieldEditorPreferencePage
implements IWorkbenchPreferencePage {
public AcronymPreferencePage() {
super(GRID);
setPreferenceStore(Activator.getDefault().getPreferenceStore());
}
public void createFieldEditors() {
AcronymListEditor editor = new AcronymListEditor(PreferenceConstants.ACRONYM_LIST,
"Acronyms", getFieldEditorParent());
addField(editor);
}
public void init(IWorkbench workbench) {}
} |
com.devworks.example.acronym.preferences.AcronymListEditor 类扩展了 Eclipse 类 org.eclipse.jface.preference.ListEditor。而 ListEditor 则是扩展了 org.eclipse.jface.preference.FieldEditor 类。FieldEditor 是一个抽象类,表示用户可以在 Preferences 页面修改的值。图 12 是 AcronymListEditor 类的一个 UML 类图,描绘了该类与 Eclipse 父类以及一个可以验证输入的检验器类之间的关系。
ListEditor 是一个抽象类,表示一个数据列表。AcronymListEditor 类是 ListEditor 的扩展。注意图 13 中指向 AcronymListEditor 域的箭头。
清单 3 是 AcronymListEditor 的类定义。在这段代码中,内部类 MyInputValidator 负责验证缩略词列表的输入。为简单起见,我们选择不验证列表中的条目,每次运行检验器时返回空值。
public class AcronymListEditor extends ListEditor {
public class MyInputValidator implements IInputValidator {
public String isValid(String arg0) {
return null;
}
}
protected void adjustForNumColumns(int arg0) {
// TODO Auto-generated method stub
super.adjustForNumColumns(arg0);
}
public AcronymListEditor(String arg0, String arg1, Composite arg2) {
super(arg0, arg1, arg2);
adjustForNumColumns(2);
}
protected String createList(String[] arg0) {
String encodedArray = "";
if (arg0 != null) {
int size = arg0.length;
for (int i=0; i |
清单 4 中的代码负责收集 Sametime 用户的输入。
protected String getNewInputObject() {
InputDialog dialog = new InputDialog(new Shell(),"Enter an Acronym",
"Enter an Acronym followed by
the series of words which represent that acronym. \n \n \t Example: IBM = International
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
Business Machines","", new MyInputValidator());
dialog.create();
dialog.open();
String text = dialog.getValue();
return text;
}
|
缩略词数据存储为字符串。清单 5 中的代码将缩略词数据从字符串转换为字符串数组,以便将来审查某个特定词是否为缩略词。
protected String[] parseString(String arg0) {
StringTokenizer stok = new StringTokenizer(arg0, ";");
int arraySize = stok.countTokens();
String[] decodedArray = new String[arraySize];
for (int i=0; i |
创建 Preferences 页面后,需要一种方法来初始化这个页面。为此,可以使用 org.eclipse.core.runtime.preferences 扩展点。利用此扩展点可以指定使用 com.devworks.example.acronym.preferences.PreferenceInitializer 类来初始化 preferences 页面,如清单 6 中的扩展点声明所示。
复制前述的扩展定义并粘贴到 plugin.xml 文件中。因为 org.eclipse.core.runtime.preferences 扩展点依赖于 org.eclipse.ui 插件,所以无需使用 Plug-in Manifest Editor 编辑插件依赖项列表。您已经将 org.eclipse.ui 插件添加到了插件依赖项列表中。
第一次打开 Preferences 页面时会调用清单 7 中所示的 com.devworks.example.acronym.preferences.PreferenceInitializer 类。
public class PreferenceInitializer extends AbstractPreferenceInitializer {
public void initializeDefaultPreferences() {
IPreferenceStore store = Activator.getDefault()
.getPreferenceStore();
store.setDefault(PreferenceConstants.ACRONYM_LIST,
"");
}
}
|
这个 PreferenceInitializer 类用于加载带默认值的 Preferences 页面。在本例中,我们决定不设默认值。如果没有为 Preference 设定值,就会使用默认值。例如,我们希望为所有的 Lotus Sametime 用户预先加载缩略词词典。我们可以在 PreferenceInitalizer 类的 setDefault 方法中设置这个预先加载词典的值。随后,只要用户修改词典,就会使用该词典替换 PreferenceInitializer 中定义的默认值。换言之,某个特定域的用户值为空时就会使用默认值;在本例中,我们并没有加载任何默认值;而是决定让 Lotus Sametime Connect 用户加载自己的缩略词列表。
使用 Acronym Expander 操作启用 Acronym Expander
Lotus Sametime Connect 用户可以随意打开或关闭 AcronymExpander 插件。如图 14 所示,Acronym Expander 按钮已被添加到 Lotus Sametime 工具栏上。
为了允许来回切换 Acronym Expander 的功能,我们使用清单 8 中所示的 com.ibm.collaboration.realtime.chatwindow.chatAction 扩展点。
要定义 com.ibm.collaboration.realtime.chatwindow.chatAction 扩展,请重复 “使用 Plug-in Manifest Editor 定义新扩展” 一节中的步骤,只是这次需要指定 com.ibm.collaboration.realtime.chatwindow.chatAction 为扩展点。别忘了让向导将 com.ibm.collaboration.realtime.chatwindow 插件添加到插件依赖项列表中。
在扩展点定义中,您会看到引用了图像 enabled.gif。单击此图像可以启用 Acronym Expander 功能。处理单击此图像的类为 com.devworks.example.acronym.AcronymChatWindowAction。扩展点定义中也 引用了 com.devworks.example.acronym.AcronymChatWindowAction 类(如清单 9 所示)。
public class AcronymChatWindowAction extends ChatWindowAction {
public void run() {
//Change the status of isEnbaled, and get the value after the status change.
boolean isEnabled = AcronymExpanderHandler.toggleEnabled();
if (isEnabled)
MessageDialog.openInformation(null, "Acronym Expander Status",
"Acronym Expansion is now Enabled");
else
MessageDialog.openInformation(null, "Acronym Expander Status",
"Acronym Expansion is now Disabled");
}
}
|
当用户单击 Acronym Expander 按钮时,将调用前述类中的 run 方法。单击按钮时,我们通过调用 AcronymExpanderHandler 类(即将介绍)的 toggleEnabled 方法来切换 Acronym Expander 的状态。然后用一个消息框显示 Acronym Expander 插件的当前状态。
使用 MessageHandlerListener 扩展点截取消息
com.ibm.collaboration.realtime.messages.MessageHandlerListener 扩展点让用户能够创建一个侦听器,用于在 Lotus Sametime 消息被发送到 Lotus Sametime 用户之前侦听和(如果这样编程的话)转交该消息。要在消息被发送到用户之前截取这些消息,必须指定一个扩展于 com.ibm.collaboration.realtime.messages.MessageHandlerPreCallback 类的扩展点类。
扩展点定义(如清单 10 所示)指定了 com.ibm.devworks.example.acronym.AcronymExpanderCallback 类的用法。
同样,按 “使用 Plug-in Manifest Editor 定义新扩展” 一节中的步骤将前述的扩展添加到 plugin.xml 文件中。别忘了让向导将 com.ibm.collaboration.realtime.messages 插件添加到插件依赖项列表中。
稍后您所需要的 com.ibm.collaboration.realtime.messages.MessageHandlerPreCallback 类需要使用 com.ibm.collaboration.realtime.magiccarpet 插件。此时,您还需要将 com.ibm.collaboration.realtime.magiccarpet 插件添加到插件依赖项列表中。为此,通过打开 META-INF/MANIFEST.MF 来打开 Plug-in Manifest Editor。选择 Dependencies 附签并单击 Add 按钮(如图 15 所示)。
出现 Plug-in Selection 对话框后,在文本框中键入 com.ibm.collaboration.realtime.maggiccarpet 筛选掉其他插件。选择 com.ibm.collaboration.realtime.maggiccarpet 插件,然后单击 OK。
保存并关闭 Plug-in Manifest Editor。
AcronymExpanderCallback 类如清单 11 所示。需注意此类的构造函数使用 com.ibm.collaboration.realtime.messages.MessageHandler 对象作为参数的方式。AcronymExpanderCallback 类由 MessageHandlerListener 扩展点所引用。AcronymExpanderCallback 类则反过来使用 AcronymExpanderHandler,后者如图 16 所示。
public class AcronymExpanderCallback extends MessageHandlerPreCallback {
public AcronymExpanderCallback() {
super(new AcronymExpanderHandler());
}
}
|
要捕获发送到 AcronymExpanderCallback 类的消息,需要提供自己的 com.ibm.collaboration.realtime.messages.MessageHandler 类的子类。
此类的扩展是 com.devworks.example.acronym.AcronymExpanderHandler 类,清单 12 中给出了其部分内容。
public class AcronymExpanderHandler extends DefaultMessageHandler {
private static boolean isEnabled = true;
public static boolean toggleEnabled() {
isEnabled = !isEnabled;
return isEnabled;
}
|
单击 Acronym Expander 按钮启用或禁用 Acronym Expander 插件功能时,将调用 AcronymExpandHandler 类的 toggleEnabled 方法。
同样在 AcronymExpanderHandler 类中,还重写了父类的 handleMessage 方法,该方法使用 ImTextReceivedMessage 和 ManyToManyTextReceivedMessage 对象作为参数(如清单 13 所示)。在这些重写的方法中,调用了 expandAcronyms(ImMessage) 方法,稍后将对它进行介绍。
public void handleMessage(ImTextReceivedMessage message)
{
expandAcronyms(message);
}
public void handleMessage(ManyToManyTextReceivedMessage message)
{
expandAcronyms(message);
}
public void handleDefaultMessage(Message arg0) {
}
|
如前所述,当用户看见消息时,缩略词已经完成扩展。前一节中的 handleMessage 方法所调用的 expandAcronyms 方法依赖于 com.devworks.example.acronym.AcronymListStore 类(下一节中将对它进行介绍)以扩展缩略词。在 AcronymExpanderHandler 类的 expandAcronyms 方法中,首先检查是否启用了缩略词扩展(根据 isEnabled Boolean 变量的值确定)。接下来,如果启用了缩略词扩展,则使用 AcronymListStore 类的 expandMessage 方法扩展缩略词(见下一节,“AcronymListStore”)。然后使用扩展后的缩略词文本设置(“重置” 可能更适当)消息,如清单 14 所示。
public void expandAcronyms(ImMessage message) {
if (isEnabled) {
String text = message.getText();
if (text != null) {
String acronym = AcronymListStore.expandMessage(text);
message.setText(acronym);
}
}
}
} |
AcronymListStore 类拥有两个静态方法,expandMessage 和 getAcronymMap。清单 15 中所示的 getAcronymMap 方法用于获取首选项存储中缩略词列表的 HashMap 表示。
public static HashMap getAcronymMap() {
String acronymListString = Activator.getDefault().getPreferenceStore()
.getString(PreferenceConstants.ACRONYM_LIST);
HashMap acronymMap = new HashMap();
StringTokenizer stok = new StringTokenizer(acronymListString, ";");
int arraySize = stok.countTokens();
for (int i=0; i |
在设计中,我们考虑过对存储缩略词信息的 HashMap 进行缓存,但是我们选择不缓存,因为希望支持缩略词词典的动态更新。
清单 16 中所示的 expandMessage 方法用于扩展缩略词。此方法迭代消息字符串的标记过的词并查看 acronymMap 对象是否包含检查的词。如果有,则检索到缩略词关联的扩展文本并连接到发给用户的消息字符串。分析(并替换,如有必要)完消息中所有的词后,控制消息返回给即时消息传递客户机,以便呈现给用户。
public static String expandMessage(String unexpandedMessage) {
HashMap acronymMap = getAcronymMap();
StringTokenizer stok = new StringTokenizer(unexpandedMessage, " <>!.?,:", true);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
String expandedString = "";
int bracketCount = 0;
while (stok.hasMoreTokens()) {
String preString = stok.nextToken();
if (preString.equals("<"))
bracketCount++;
if ((acronymMap.containsKey(preString))&&(bracketCount==0))
expandedString = expandedString.concat((String)acronymMap.get(preString));
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
else
expandedString = expandedString.concat(preString);
if (preString.equals(">"))
bracketCount--;
}
System.out.println(expandedString);
return expandedString.trim();
}
|
要使用 Eclipse 运行带有 Acronym Expander 插件的 Lotus Sametime V7.5.1,转到 Run 菜单选择 Run。将出现 Run 向导。使用 Run 向导创建一个新配置。为此,右击 Eclipse Application 并选择 New,如图 17 所示。
接受默认选项,然后单击 Run 运行配置。
在本文中,您了解了如何在 IBM Lotus Sametime Connect 中在即时消息被呈现给用户之前截取消息。通过使用 com.ibm.collaboration.realtime.messages.MessageHandlerListener 扩展点使截取成为可能。使用此扩展点可以构建一个实用的 Lotus Sametime 插件,让您能够显示缩略词的扩展形式。当然,您所执行的这类中介作用并不仅限于缩略词扩展。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/14751907/viewspace-417686/ ,如需转载,请注明出处,否则将追究法律责任。