Eclipse RCP:在编辑器窗口中打开相同的编辑器 [英] Eclipse RCP : have the same editor open in editor window
问题描述
现在或当前的编辑器可以使用rcp3 eclipse在同一编辑器窗口中重新打开
Existing or current editor can re-open in same editor window using rcp3 eclipse
我创建了多个编辑器,打开成功,但有些问题像
一样编辑器名称
可以在编辑器窗口中显示
i am create multiple editor which open successfully but some problem like same editor name can display on editor window
我喜欢 Setfocus在现有或当前编辑器(重用编辑器显示匹配
) 和
相同的编辑器名称不能在编辑器窗口中显示。
i got like Setfocus on existing or current editor(Reuse editors to show matches. ) and same editor name can't display on editor window.
包名称:rcp_demo.Editor
类名:UserCommand.java,UserEditor.java和UserEditorInput.java
class name: UserCommand.java, UserEditor.java and UserEditorInput.java
类名:EmpCommand.java,EmployeeEditor.java和
EmployeeEditorInput.java
class name: EmpCommand.java, EmployeeEditor.java and EmployeeEditorInput.java
package rcp_demo.Editor;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
public class UserCommand extends AbstractHandler{
public static final String ID = "rcp_demo.Editor.UserCommand";
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage page = window.getActivePage();
UserEditorInput input = new UserEditorInput();
try {
if(page.getActivePart().getTitle().toString().equals("Student_Editor"))
{
page.findEditor(input);
System.out.println("Student Editor exist..........");
}
else
{
page.openEditor(input, UserEditor.ID);
System.out.println("Student Editor open");
}
} catch (PartInitException e) {
System.out.println("Error:" + this.getClass().getName() + ":" + e);
e.printStackTrace();
throw new ExecutionException("Error open UserEditor");
}
return null;
}
}
plugin.xml
编辑器列表
<extension
point="org.eclipse.ui.editors">
<editor
class="rcp_demo.Editor.UserEditor"
default="false"
id="rcp_demo.Editor.user"
name="Student_Editor">
</editor>
<editor
class="rcp_demo.Editor.EmployeeEditor"
default="false"
id="rcp_demo.Editor.emp"
name="Employee_Editor">
</editor>
</extension>
命令列表
<extension
point="org.eclipse.ui.commands">
<command
defaultHandler="rcp_demo.Editor.UserCommand"
id="rcp_demo.Editor.UserCommand"
name="Call UserEditor">
</command>
<command
defaultHandler="rcp_demo.Editor.EmpCommand"
id="rcp_demo.Editor.EmpCommand"
name="call EmpEditor">
</command>
</extension>
输出前
学生编辑器打开
员工编辑器打开
学生编辑器打开
员工编辑器打开
(之后)我所需的输出
学生编辑器打开
员工编辑器打开
学生编辑器存在..........
Student Editor exist..........
存在员工编辑.........
Employee Editor exist.........
学生编辑器存在..........
Student Editor exist..........
员工编辑器存在.........
Employee Editor exist.........
所有准备好的开放编辑器不能第二次打开...
all ready open editor can not second time open...
推荐答案
我已经解决了这个问题
包名称:rcp_demo.Editor
package name:rcp_demo.Editor
类名:EmpCommand.java,EmployeeEditor.java和EmployeeEditorInput.java
class name: EmpCommand.java, EmployeeEditor.java and EmployeeEditorInput.java
package rcp_demo.Editor;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
public class EmpCommand extends AbstractHandler {
public static final String Id = "rcp_demo.Editor.EmpCommand";
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage page = window.getActivePage();
IEditorReference[] editors = page.getEditorReferences();
EmployeeEditorInput input = new EmployeeEditorInput();
//All Comments are easily understand
//public class EmployeeEditorInput implements IEditorInput{}
for (int i=0; i<editors.length; i++) {
//List out all Exist editor
//compare with EmployeeEditor.Id="rcp_demo.Editor.emp";
if (editors[i].getId().equals(EmployeeEditor.Id)) {
//public class EmployeeEditor extends EditorPart
//{
// public static final String Id="rcp_demo.Editor.emp";
// public void createPartControl(Composite parent) {.....}
//}
page.activate(editors[i].getEditor(true));
System.out.println("set focus an existing editor(Employee)");
return null;
}
}
try {
//open new Editor like EmployeeEditor.Id="rcp_demo.Editor.emp";
page.openEditor(input,EmployeeEditor.Id);
System.out.println("open Editor(Employee) ");
} catch (PartInitException e) {
e.printStackTrace();
}
return null;
}
}
这篇关于Eclipse RCP:在编辑器窗口中打开相同的编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!