📄 toolloader.java
字号:
/*
* ToolLoader.java
*
* Created on 2007年5月12日, 下午11:23
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package utils;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import plugin.*;
/**
*
* @author Virlene Cheng
*/
public class ToolLoader
{
public static final String CONFIG_TOOL_FILE = "Module.Toolbar.xml";
public static final String NODE_TOOL = "tool";
public static final String TOOL_NAME = "name";
public static final String TOOL_PACKAGE = "package";
public static final String TOOL_CLASS = "class";
private JToolBar toolBar;
private IGUIResource guiRes;
private IDBResource dbRes;
private Hashtable<JButton, IAppPlugin> map = new Hashtable<JButton, IAppPlugin>();
/** Creates a new instance of ToolLoader */
public ToolLoader(IGUIResource guiRes, IDBResource dbRes)
{
this.guiRes = guiRes;
this.dbRes = dbRes;
//获取菜单栏
this.toolBar = guiRes.getToolsBar();
}
/**
* 将默认Xml文件中所配置的菜单树添加到菜单栏上
* @param xmlFileName Xml文件
*/
public void addToolWithDefaultXml()
{
buildTools(ToolLoader.CONFIG_TOOL_FILE);
}
/**
* 将Xml文件中所配置的菜单树添加到菜单栏上
* @param xmlFileName Xml文件
*/
public void addToolWithXml(String xmlFileName)
{
buildTools(xmlFileName);
}
/**
* 根据Xml文件名建立菜单
* @param xmlFileName Xml文件名
*/
private void buildTools(String xmlFileName)
{
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFileName);
Element root = doc.getDocumentElement();
//获取子节点
NodeList list = root.getElementsByTagName(ToolLoader.NODE_TOOL);
for (int i = 0; i < list.getLength(); i++)
{
assembleTool(list.item(i));
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
/**
* 使用按钮装配工具项
* @param node 工具项节点
* @return 是否装配成功
*/
private boolean assembleTool(Node node)
{
try
{
//获取工具项属性
String toolName = node.getAttributes().getNamedItem(ToolLoader.TOOL_NAME).getNodeValue();
String packageFile = node.getAttributes().getNamedItem(ToolLoader.TOOL_PACKAGE).getNodeValue();
String className = node.getAttributes().getNamedItem(ToolLoader.TOOL_CLASS).getNodeValue();
//设置工具按钮属性
JButton button = new JButton();
button.setText(toolName);
button.setFont(new Font("新宋体", Font.PLAIN,12));
//读取插件
if(!loadPlugin(button, packageFile, className))
{
return false;
}
//为工具项添加事件
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
IAppPlugin appPlugin = map.get(event.getSource());
appPlugin.runApp();
}
});
toolBar.add(button);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
/**
* 读取插件
* @param packageFile 插件所在的jar文件
* @param className 插件的完整类名
* @return 是否读取成功
*/
private boolean loadPlugin(JButton button, String packageFile, String className)
{
try
{
//利用反射装载插件类
URL[] urls = new URL[1];
urls[0] = (new File(packageFile)).toURI().toURL();
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass(className);
//将反射出的Object对象转化为IAppPlugin
Object obj = cls.newInstance();
IAppPlugin appPlugin = (IAppPlugin)obj;
//为插件装载资源
appPlugin.loadResource(guiRes, dbRes);
//插件初始化
appPlugin.initPlugin();
//加入Hashtable
map.put(button, appPlugin);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -