Eclipse的flow插件开发
工作需要,让研究eclipse的flow插件开发,通过搜索资料发现在05-06年期间eclipse插件开发曾经火过一段时间,很多优秀的教程都是在那时出现的.不过现在貌似已经没落了.想想也是eclipse插件的整个体系比较庞大,同时也有很多优秀框架.但是学插件干嘛呢?一般公司不会用,除非专业出这样产品的公司,这样算下来市场的需求量确实是少之又少, 同事又缺乏像样的教程和书籍(英文的很多,中文的稀少……)。可是公司既然这样要求了也只有硬着头皮上了。下面介绍正文:
前面可以参见《八进制》的博客
http://bjzhanghao.cnblogs.com/archive/2005/02/19/106000.html
05年的教程,不得不说八进制写的很好.
实现了基本的添加,删除,拖拽,连线,属性显示等功能.但缺少非常关键的save与load方法(绘制的图像保存为xml,或图片,读取是回显上次编辑的内容)。
下面就将先实现save方法(将编辑后的东东保存为xml和png图片):
1.在实现GraphicalEditorWithPalette的类中找到doSave()方法,每次保存是都会调用该方法
2.要保存为图片,并且在本目录下,首先要获取本次打开的文件
IFile file =((IFileEditorInput)getEditorInput()).getFile();
File currentFile = file.getLocation().toFile();
String fileName = currentFile.getName();
String filePath = currentFile.getPath();
关于getEditorInput有这样一句解释:
当你在Resource Navigator里双击打开一个文件时,这个文件就和editor关联上了,因此可以通过editorInput得到文件。
3.保存为图片,添加export方法
public void export(GraphicalViewer viewer, String location, int format)
{
try
{
IFigure figure = ((AbstractGraphicalEditPart) viewer.getRootEditPart()).getFigure();
File file = new File(location);
if (file.exists())
{
if (!MessageDialog.openQuestion(null, "系统提示",
"该文件已经存在. 要重新覆盖它吗 ?"))
{
return;
}
}
else
{
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
if (figure instanceof Viewport)
{
// Reinit the figure
((Viewport) figure).setViewLocation(0, 0);
}
Dimension size = figure.getPreferredSize();
Image image = new Image(Display.getDefault(), size.width, size.height);
GC gc = new GC(image);
SWTGraphics graphics = new SWTGraphics(gc);
figure.paint(graphics);
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] {image.getImageData()};
loader.save(fos, format);
fos.close();
}
catch (Exception e)
{
//记录该异常
// ModelerPlugin.displayDialog(null, "An error occured during export. See the error log for more details.", IStatus.ERROR);
// ModelerPlugin.log(e);
}finally{
}
}
4.添加保存xml的方法xmlSave():
public void xmlSave(IProgressMonitor progressMonitor) {
editorSaving = true;
Platform.run(new SafeRunnable() {
public void run() throws Exception {
//以下存储xml文件
Document document = DocumentHelper.createDocument();
Element logicDiagramElement = document.addElement("LogicDiagram");
/*中间是你的xml*/
try{
OutputFormat format = OutputFormat.createPrettyPrint();
// IFile file = ((IFileEditorInput)getEditorInput()).getFile();
// final IFile file = (IFile) ((DiagramEditorInput)getEditorInput()).getFile();
IFile file =((IFileEditorInput)getEditorInput()).getFile();
File f = file.getLocation().toFile();
System.out.println("fileName:"+f.getName());
XMLWriter output = new XMLWriter(new FileOutputStream( f ), format);
output.write( document );
output.close();
} catch(IOException e){
System.out.println(e.getMessage());
}
getCommandStack().markSaveLocation();
}
});
editorSaving = false;
}
5.在doSave()中调用:
//保存为 xml
xmlSave(monitor);
//保存为图片export(getGraphicalViewer(),filePath+".png",SWT.IMAGE_PNG);
问题:保存xml使用的是dom4j这个外部jar,但是插件的寻包机制又和普通项目不同,所以需要处理一下:
a. 在项目下新建lib目录存放外部jar
b. MANIFEST.MF的Runtime页签,在右下角的classpath中添lib/dom4j-1.6.1.jar
c. 在左上角exported package中添加需要用到的包
d. 添加选中工程,在buildPath中添加lib中的jar
在当前工程目录下添加lib文件夹,将用到的第三方jar包放入lib文件夹中。在runtime右下角的classpath中添加/lib/*.jar,最后在runtime左上角的exported package中添加需要用到的包