RCP表单编辑器---》一打开编辑器
Posted alwaysmove
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RCP表单编辑器---》一打开编辑器相关的知识,希望对你有一定的参考价值。
1、创建JsEditor.java
package com.testrcp.myrcp.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 = "com.testrcp.myrcp.editors.JsEditor"; private Text text; //编辑器中的内容是否被修改的标志 private boolean bDirty = false; public JsEditor() { super(); } //初始化编辑器 public void init(IEditorSite site, IEditorInput input)throws PartInitException{ this.setSite(site);//设置site this.setInput(input);//设置输入的IEditorInput对象 this.setPartName(input.getName());//设置编辑器上方显示的名称 } //创建编辑器中的控件 public void createPartControl(Composite parent) { text = new Text(parent, SWT.NONE); //当文本框修改时,设定内容被修改过 text.addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent e) { if (!isDirty()) {//如果未修改 setDirty(true);//设置修改 //更改编辑器的状态 firePropertyChange(IEditorPart.PROP_DIRTY); } } }); } //编辑器关闭时,保存编辑器内容时所调用的方法 public void doSave(IProgressMonitor monitor) { //将保存状态显示在状态栏中 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) { } } //另存为调用的方法 public void doSaveAs() { } //判断是否被修改过 public boolean isDirty() { return bDirty; } //是否允许保存 public boolean isSaveAsAllowed() { return true; } //设置焦点 public void setFocus() { text.setFocus(); } //设置编辑器内容被修改过 public void setDirty(boolean b) { bDirty = b; }
2、创建JsEditorInput.java
package com.testrcp.myrcp.editors; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IPersistableElement; import formeditorrcp.Activator; public class JsEditorInput implements IEditorInput { //输入的字符 private String input ; public JsEditorInput ( String input ){ this.input = input ; } //是否将编辑器保存在最近访问记录中 public boolean exists() { return true; } //输入内容的图标 public ImageDescriptor getImageDescriptor() { return Activator.getImageDescriptor("icons/samples.gif"); } //输入信息的名称 public String getName() { return input; } //是否可以持久化该编辑器 public IPersistableElement getPersistable() { return null; } //设置编辑器标签中显示提示信息 public String getToolTipText() { return input; } //返回与该输入相关的类的对象 public Object getAdapter(Class adapter) { return null; } }
3、创建OpenEditorView.java
package com.testrcp.myrcp.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.internal.part.NullEditorInput; import org.eclipse.ui.part.ViewPart; import com.testrcp.myrcp.editors.JsEditor; import com.testrcp.myrcp.editors.JsEditorInput; public class OpenEditorView extends ViewPart { public static final String ID = "com.testrcp.myrcp.views.OpenEditorView"; public List list; public OpenEditorView() { super(); } public void createPartControl(Composite parent) { list = new List(parent, SWT.NONE); list.add("Editor"); list.add("MutiPage Editor"); list.add("Form Editor"); list.add("Master/Detail Page"); list.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { String select = list.getSelection()[0]; // 获得当前激活的IWorkbenchPage对象 IWorkbenchPage page = getViewSite().getWorkbenchWindow().getActivePage(); try { if (select.equals("Editor")) {// 如果选中的"Editor"一项 // 创建输入的内容对象 JsEditorInput editor = new JsEditorInput(select); // 打开该编辑器 page.openEditor(editor, JsEditor.ID); }else if (select.equals("MutiPage Editor")) { //page.openEditor(new NullEditorInput(), MutiEditorSample.ID); }else if (select.equals("Form Editor")) {// 如果选中的"Editor"一项 //page.openEditor(new NullEditorInput(), MyMutiForm.ID); } }catch (PartInitException ee) { ee.printStackTrace(); } } }); } public void setFocus() { list.setFocus(); } }
4、在plugin.xml中增加如下代码
<extension point="org.eclipse.ui.views"> <view allowMultiple="false" class="com.testrcp.myrcp.views.OpenEditorView" icon="icons/alt_window16.gif" id="com.testrcp.myrcp.views.OpenEditorView" name="打开编辑器"/> </extension>
5、在透视图中,增加如下代码
public void createInitialLayout(IPageLayout layout) { String editorArea = layout.getEditorArea(); //添加树视图 layout.addView(OpenEditorView.ID, IPageLayout.LEFT, 0.2f, editorArea); }
效果如图:
以上是关于RCP表单编辑器---》一打开编辑器的主要内容,如果未能解决你的问题,请参考以下文章
Jacoco和Tycho surefire的Eclipse RCP插件代码介绍