2017-03-07 26 views
0

如果您懶得閱讀所有這些,通常問題是:如何將Composite父項傳遞給現有部分,假定這些部分是由Eclipse創建的,而不是手動創建的。Eclipse 4 - 將部分添加到SashForm

我正在開發一個Eclipse 4插件。我在不同的課程中有不同的部分(視圖),通過事件進行交流。一切都很好,並分開,直到我不得不實施包含2個零件的SashForm。我看過一些例子,但他們大多顯示簡單的用法SashForm。 我想要的是保持這種模塊化的所有依賴注入和自動事件接收(通過註釋)。 但我無法弄清楚如何做到這一點。 我最好的嘗試是使用IEclipseContext這樣的:

@Inject 
private EventPool eventPool; 

@Inject 
private EventBroker eventBroker; 

@Inject 
private CMTreeContentProvider contentProvider; 

@Inject 
private LoginService loginService; 

@PostConstruct 
public void createPartControl(Composite shell) { 
    SashForm form = new SashForm(shell, SWT.HORIZONTAL); 
    form.setLayout(new FillLayout()); 
    renderCMTree(form); 
    renderProjects(form); 
} 

private void renderCMTree(SashForm form) { 
    Composite child = new Composite(form, SWT.NONE); 
    child.setLayout(new FillLayout()); 
    IEclipseContext context = EclipseContextFactory.create(); 
    context.set(Composite.class, child); 
    context.set(EventPool.class, eventPool); 
    context.set(EventBroker.class, eventBroker); 
    context.set(CMTreeContentProvider.class, contentProvider); 
    ContextInjectionFactory.make(CMTreeView.class, context); 
    context.dispose(); 
} 

private void renderProjects(SashForm form) { 
    Composite child = new Composite(form, SWT.NONE); 
    child.setLayout(new FillLayout()); 
    IEclipseContext context = EclipseContextFactory.create(); 
    context.set(Composite.class, child); 
    context.set(EventPool.class, eventPool); 
    context.set(EventBroker.class, eventBroker); 
    ContextInjectionFactory.make(CMTicketView.class, context); 
    context.dispose(); 
} 

正如你所看到的,我必須手動注入通過上下文所有的依賴。使用這種方法,消耗事件在CMTreeViewCMTicketView內不起作用(但是,我手動訂閱事件)。我可以忍受這一點,但也許有更好的方式來保持模塊化?我真的不希望這個類的長度超過2000行。

+0

爲什麼不在Application.e4xmi中使用'Part Sash Container'來包含這兩部分? –

+0

@ greg-449感謝您的評論。我試圖使用它,但無法使其工作。我見過的有關'PartSashContainer'的所有例子似乎都使用RCP模型,但我開發了一個插件。是否可以添加'PartSashContainer'作爲一個視圖,使其可以通過Window - > Show view訪問? – iozee

+0

我使用Feautre名稱的「描述符」爲模型片段添加視圖。我可以以相同的方式添加PartSashContainer嗎? – iozee

回答

1

你應該真的嘗試在Application.e4xmi中使用類似'Part Sash Container'的東西來包含這些部分。

如果你必須使用ContextInjectionFactory.make創造的東西,你可以用它使用兩個IEclipseContext參數的形式在主背景加上只包含你的價值觀輔環境經過:

@Inject 
IEclipseContext context; // injected existing context 


IEclipseContext staticContext = EclipseContextUtil.createContext(); 

// Only need to put your own values in staticContext 
staticContext.set(Composite.class, child); 

// Pass in both contexts to make 
ContextInjectionFactory.make(CMTreeView.class, context, staticContext); 

兩種情況下會使用以查找注入CMTreeView所需的值。

注意:您應該使用IEventBrokerEventBroker這是一個內部類。

+0

感謝您的回答。但是我沒有一個Application.e4xmi文件,因爲我沒有開發一個RCP應用程序,而是一個插件,所以我只有Model Fragments。猜猜我最好從我的帖子中刪除'eclipse-rcp'標籤。我一定會試一試,因爲我非常喜歡這種e4風格的注射和單一責任。 – iozee

+0

根據主應用程序的設計,您仍可以在碎片中使用零件窗框容器。答案的其餘部分不依賴於使用零件窗框容器。 –