RCP添加一个编辑器视图

RCP提供专门的编辑器视图,里面内置了很多编辑器的专用用户,如果修改后,视图中名称显示命名,提示已修改要进行保存。

1、plugin.xml中添加视图view扩展点,和org.eclipse.ui.editors扩展点

 

2、添加以下3个编辑器相关的类

 A、JSEditor

package myviewrcp2.editors;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

public class JSEditor extends EditorPart{

	//对应plugin.xml指定的id
	public static final String ID = "myviewrcp2.editors.JSEditor";
	private Text text;
	//编辑器中的内容是否被修改的标志
	private boolean bDity = false;
	
	public JSEditor() {
		super();
	}
	
	//编辑器关闭时,保存编辑器内容时所调用的方法
	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub
		try {
			monitor.beginTask("保存文件....", 100);
			for(int i= 0;i<10 && monitor.isCanceled();i++) {
				Thread.sleep(500);
				monitor.worked(10);
				double d = (i + 1)/10d;
				monitor.subTask("已完成"+d * 100 + "%");//显示任务状态
			}
			monitor.done();
			if(monitor.isCanceled()) {
				throw new InterruptedException("取消保存");
			}
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
	}

	//另存为调用的方法
	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub
		
	}

	//初始化编辑器
	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		// TODO Auto-generated method stub
		this.setSite(site);//设置site
		this.setInput(input);//设置输入的IEditorInput 对象
		this.setPartName(input.getName()); //设置编辑器上方显示的名称
	}

	//判断是否被修改过
	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return bDity;
	}

	//是否允许保存
	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return true;
	}

	//创建编辑器中的控件
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		text = new Text(parent,SWT.NONE);
		//当文本框修改时,设定内容被修改过
		text.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent arg0) {
				// TODO Auto-generated method stub
				if(!isDirty()) {
					setDirty(true); //设置修改
					//更改编辑器的状态
					firePropertyChange(IEditorPart.PROP_DIRTY);
				}
			}
		});
	}

	//设置焦点
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		text.setFocus();
	}
	
	public void setDirty(boolean b) {
		bDity = b;
	}

}

 B、JsEditorContributor

package myviewrcp2.editors;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.EditorActionBarContributor;

public class JsEditorContributor extends EditorActionBarContributor{
	private Action action1;
	private Action action2;
	public JsEditorContributor() {
		super();
		makeActions();

	}
	public void makeActions() {
		action1 = new Action() {
			public void run() {
				
			}
		};
		action1.setText("Action 1");
		action1.setToolTipText("Action 1 tooltip");
		//action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_DEF_VIEW));
		
		action2 = new Action() {
			public void run() {
				
			}
		};
		action2.setText("Action 2");
		action2.setToolTipText("Action 2 tooltip");
		//action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_WARN_TSK));
	}
	
	//创建菜单
	@Override
	public void contributeToMenu(IMenuManager menuManager) {
		MenuManager editMenu = new MenuManager("编辑器菜单");
		editMenu.add(action1);
		editMenu.add(action2);
		menuManager.add(editMenu);
	}
	
	//覆盖父类的方法,创建工具栏
	@Override
	public void contributeToToolBar(IToolBarManager toolBarManager) {
		toolBarManager.add(action1);
		toolBarManager.add(action2);
	}
}

  C、JsEditorInput

package myviewrcp2.editors;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;

public class JsEditorInput implements IEditorInput{

	//输入字符
	private String input;
	
	public JsEditorInput(String input) {
		this.input = input;
	}
	
	//返回与该输入相关的类的对象
	@Override
	public <T> T getAdapter(Class<T> arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	//是否将编辑器保存在最近访问记录中
	@Override
	public boolean exists() {
		// TODO Auto-generated method stub
		return true;
	}

	//输入内容的图标
	@Override
	public ImageDescriptor getImageDescriptor() {
		// TODO Auto-generated method stub
		return null;
	}

	//输入信息的名称
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return input;
	}

	//是否可以持久化该编辑器
	@Override
	public IPersistableElement getPersistable() {
		// TODO Auto-generated method stub
		return null;
	}

	//设置编辑器标签中显示提示信息
	@Override
	public String getToolTipText() {
		// TODO Auto-generated method stub
		return input;
	}
	
}

3、添加了一个打开编辑器的视图类OpenEditorView

package myviewrcp2.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

import myviewrcp2.editors.JSEditor;
import myviewrcp2.editors.JsEditorInput;

public class OpenEditorView extends ViewPart{

	public static final String ID = "myviewrcp2.views.OpenEditorView";
	public List list;
	
	public OpenEditorView() {
		super();
	}
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		list = new List(parent,SWT.NONE);
		list.add("Editor");
		list.add("MutiPage Editor");
		list.add("Form Editor");
		list.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				String select = list.getSelection()[0];
				//获得当前激活IWorkbenchPage对象
				IWorkbenchPage page = getViewSite().getWorkbenchWindow().getActivePage();
				if(select.equals("Editor")) { //如果选中“Editor”项
					//创建输入的内容对象
					JsEditorInput editor = new JsEditorInput(select);
					try {
						//打开该编辑器
						page.openEditor(editor, JSEditor.ID);
					}catch(PartInitException ee) {
						ee.printStackTrace();
					}
				}
			}
		});
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		list.setFocus();
	}

}

4、修改透视图显示

5、效果

 

posted @ 2022-11-11 10:29  信铁寒胜  阅读(57)  评论(0编辑  收藏  举报