GEF-whole-upload 实验随笔一:搭建编辑器

看了dengues,突发奇想,如果能用gef-whole-upload的例子,也开发一个类似的studio,那对于学习和工作,帮助应该都很大

那么,从跟着gef-whole-upload开始,算是实验课吧,就记录在这里吧

首先,建立一个plugin工程,设置为rcp模式

1.new->plugin project

2.选择hello rcp

3.出来了一个包,里面存放着Activator.java(控制插件的生命周期),Application.java(控制程序执行的各个方面),ApplicationActionBar.java(应用程序的按钮等),ApplicationWorkbenchWindowAdvisor.java(显示程序启动的方式及初始状态等),ApplicationWorkbenchAdvisor(这个不知道是用来做什么的),perspective.java(程序的属性)

4.在extensions中加入org.eclipse.ui.editors插件,用来新建一个编辑器,并设置id作为该编辑器的标示符,设置一个class:editor.class,放在xxx.ui包中

5.在Application中实现IPlatformRunable接口及IApplication接口,在start方法中加入

Application中的start方法
 1 public Object start(IApplicationContext context) throws Exception
2 {
3 Display display = PlatformUI.createDisplay();
4 try
5 {
6 int returnCode = PlatformUI.createAndRunWorkbench(display,
7 new ApplicationWorkbenchAdvisor());
8 if (returnCode == PlatformUI.RETURN_RESTART)
9 return IApplication.EXIT_RESTART;
10 else
11 return IApplication.EXIT_OK;
12 } finally
13 {
14 display.dispose();
15 }
16
17 }

并且在run方法中加入

Application中的run方法
 1 public Object run(Object args) throws Exception
2 {
3 // TODO Auto-generated method stub
4 Display display = PlatformUI.createDisplay();
5 try
6 {
7 int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
8 if (returnCode == PlatformUI.RETURN_RESTART)
9 return IPlatformRunnable.EXIT_RESTART;
10 return IPlatformRunnable.EXIT_OK;
11 }
12 finally
13 {
14 display.dispose();
15 }
16 }

6.定义一个画图的按钮:EditorAction

画图动作
public class EditorAction extends Action implements ISelectionListener,
IWorkbenchAction
{
private final IWorkbenchWindow window;
private IStructuredSelection selection;

public static final String ID = "com.newland.editor";

public EditorAction(IWorkbenchWindow window)
{
this.window = window;
setId(ID);
setText("&design");
setToolTipText("Create a Newland File");
window.getSelectionService().addSelectionListener(this);
}

@Override
public void dispose()
{
// TODO Auto-generated method stub
window.getSelectionService().removeSelectionListener(this);
}

@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection)
{
// TODO Auto-generated method stub

}

}

 

7.在ApplicationActionBarAdvisor中加入按钮

ApplicationActionBarAdvisor中加入按钮
 1 protected void makeActions(IWorkbenchWindow window)
2 {
3 exitAction = ActionFactory.QUIT.create(window);
4 register(exitAction);
5 aboutAction = ActionFactory.ABOUT.create(window);
6 register(aboutAction);
7 editorAction = new EditorAction(window);
8 register(editorAction);
9
10 }
11
12 protected void fillMenuBar(IMenuManager menuBar)
13 {
14 MenuManager fileMenu = new MenuManager("&New", "New");
15 fileMenu.add(editorAction);
16 fileMenu.add(new Separator());
17 fileMenu.add(exitAction);
18
19 MenuManager helpMenu = new MenuManager("&Help","help");
20 helpMenu.add(aboutAction);
21
22 menuBar.add(fileMenu);
23 menuBar.add(helpMenu);
24 }

8.设置编辑器的输入类:新建类:EditorInput

编辑器的输入类
 1 private IPath path;
2
3 public NewlandEditorInput(IPath path)
4 {
5 // TODO Auto-generated constructor stub
6 this.path = path;
7 }
8 @Override
9 public boolean exists()
10 {
11 // TODO Auto-generated method stub
12 return path.toFile().exists();
13 }
14
15 @Override
16 public ImageDescriptor getImageDescriptor()
17 {
18 // TODO Auto-generated method stub
19 return null;
20 }
21
22 @Override
23 public String getName()
24 {
25 // TODO Auto-generated method stub
26 return path.toString();
27 }
28
29 @Override
30 public IPersistableElement getPersistable()
31 {
32 // TODO Auto-generated method stub
33 return null;
34 }
35
36 @Override
37 public String getToolTipText()
38 {
39 // TODO Auto-generated method stub
40 return path.toString();
41 }
42
43 @Override
44 public Object getAdapter(Class adapter)
45 {
46 // TODO Auto-generated method stub
47 return null;
48 }
49
50 @Override
51 public IPath getPath()
52 {
53 // TODO Auto-generated method stub
54 return path;
55 }
56
57 public int hashCode()
58 {
59 return path.hashCode();
60 }

9.设置打开编辑器:在editorAction中加入下面代码

打开编辑器
public void run()
{
String path = openFileDialog();
if (path != null)
{
IEditorInput input = new NewlandEditorInput(new Path(path));
IWorkbenchPage page = window.getActivePage();
try
{
page.openEditor(input, NewlandEditor.ID,true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

private String openFileDialog()
{
// TODO Auto-generated method stub
FileDialog dialog = new FileDialog(window.getShell(),SWT.OPEN);
dialog.setText("Design Files");
dialog.setFilterExtensions(new String[] {".design"});
return dialog.open();
}

10.在Perspective.java中设置可见

在Perspective.java中设置可见
1 public void createInitialLayout(IPageLayout layout)
2 {
3 layout.setEditorAreaVisible(true);
4 }

11.设置gef命令堆栈的存放位置

editor中的构造方法加入
setEditDomain(new DefaultEditDomain(this));

 

这样一个编辑器基本搭建好了

posted @ 2011-12-16 04:21  Sonald  阅读(383)  评论(0编辑  收藏  举报