JavaHelp是Sun编写的一个库,它在Swing应用程序中显示HTML帮助页面。 JavaHelp允许将Swing组件嵌入到其HTML页面中:
<html>
<object classid="java:javax.swing.JButton">
<param name="text" value="Wow, a Swing component in HTML HTML!">
</object>
</html>
这将在此进一步讨论: http://docs.oracle.com/cd/E19253-01/819-0913/dev/lwcomp.html
我正在Apache Felix中运行一个大型OSGi应用程序。如上所示,classid
属性指的是我要嵌入的Swing组件类的FQN。我想这是指我在自己的bundle中定义的Swing组件类。因为JavaHelp在它自己的bundle中运行,所以它不能引用我的bundle中的类。我只是在HTML页面中看到??
,表示无法找到该类。如何让JavaHelp包引用我的包中的类?
答案 0 :(得分:2)
这只是部分可能的。这就是原因。
要解决此问题,我们必须创建自己的HTMLEditorKit
拦截object
代码,然后从Component
代码object
创建classid
。 。这是看起来如何*。
public class OurHTMLEditorKit extends HTMLEditorKit {
public ViewFactory getViewFactory() {
return new HTMLEditorKit.HTMLFactory() {
public View create(Element elem) {
if (elem.getName().equalsIgnoreCase("object"))
return new InternalObjectView(elem);
else
return super.create(elem);
}
};
}
}
private static Object attemptToGetClass(final String className) {
try {
Class c = Class.forName(className);
Object o = c.newInstance();
return o;
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
private static class InternalObjectView extends ObjectView {
public InternalObjectView(Element elem) {
super(elem);
logger.info(elem.toString());
}
protected Component createComponent() {
AttributeSet attrs = getElement().getAttributes();
String classname = ((String) attrs.getAttribute(HTML.Attribute.CLASSID)).trim();
try {
Component comp = (Component) attemptToGetClass(classname);
setParameters(comp, attrs);
return comp;
} catch (Exception e) {
logger.warn(e.getMessage());
}
return getUnloadableRepresentation();
}
// Copied from javax.swing.text.html.ObjectView with modifications to how exceptions are reported
Component getUnloadableRepresentation() {
Component comp = new JLabel("??");
comp.setForeground(Color.red);
return comp;
}
private void setParameters(Component comp, AttributeSet attr) {
Class k = comp.getClass();
BeanInfo bi;
try {
bi = Introspector.getBeanInfo(k);
} catch (IntrospectionException ex) {
logger.warn("introspector failed, ex: "+ex);
return; // quit for now
}
PropertyDescriptor props[] = bi.getPropertyDescriptors();
for (int i=0; i < props.length; i++) {
// System.err.println("checking on props[i]: "+props[i].getName());
Object v = attr.getAttribute(props[i].getName());
if (v instanceof String) {
// found a property parameter
String value = (String) v;
Method writer = props[i].getWriteMethod();
if (writer == null) {
// read-only property. ignore
return; // for now
}
Class[] params = writer.getParameterTypes();
if (params.length != 1) {
// zero or more than one argument, ignore
return; // for now
}
Object [] args = { value };
try {
writer.invoke(comp, args);
} catch (Exception ex) {
logger.warn("Invocation failed: " + ex.getMessage());
// invocation code
}
}
}
}
}
但是使用JavaHelp,无法使用HTMLEditorKit
标记[2]注册我们的<viewregistry>
。由于OSGi环境,JavaHelp无法访问我们的HTMLEditorKit
**。
相反,唯一可能的方法是使用JEditorPane
创建HTMLEditorKit
,使用JTree
创建我们自己的TOC TOCView.parse()
,然后告诉JEditorPane
在JTree
选择更改时加载帮助页面。
*这似乎很长,但大部分代码都是从javax.swing.text.html.ObjectView
[1]复制的。我不得不从那里复制代码,因为getUnloadableRepresentation
和setParameters
是私有的,不受保护。
**由于Dynamic-ImportPackage
清单条目[3],这可能是可能的。但这需要跳过很多圈。首先,必须更改JavaHelp清单。其次,在Felix启动之后,必须告诉它允许使用dynamic-import
命令进行动态导入。