2017-01-12 74 views
8

現有的或當前編輯器可以重新打開相同的編輯RCP3蝕的Eclipse RCP:在使用編輯器窗口中相同的編輯器窗口中打開

我是創建成功地打開多個編輯器,但像 相同的編輯器的名字 一些問題可以顯示在編輯器窗口

我得到了像SETFOCUS現有或當前編輯器(編輯重用展示比賽。 )和 相同的編輯器的名稱不能在編輯器窗口中顯示。

包名稱:rcp_demo.Editor

類名稱:UserCommand.java,UserEditor.java和UserEditorInput.java

類名稱:EmpCommand.java,EmployeeEditor.java和 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> 

之前輸出


學生編輯器中打開

員工編輯器中打開

學生編輯器中打開

員工編輯器中打開


(之後)我需要的輸出


學生編輯器中打開

員工編輯器中打開

學生編輯器存在..........

員工編輯存在.........

學生編輯存在..........

員工編輯存在.........


都準備好打開編輯器無法第二次開...

+0

很抱歉,但我不明白你的要求。 –

+0

謝謝重播...完整描述這個問題.. –

+0

對不起,但我仍然不明白問題是什麼。如果你想打開另一個編輯器,你爲什麼要檢查部分標題? –

回答

2

我有解決這個問題


包名稱:rcp_demo.Editor

類名:EmpCommand.java,EmployeeEditor.ja VA和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; 
    } 
} 
相關問題