2017-10-05 5 views
0

で新しいタブ/パーツを追加します。のEclipse RCP 4は、私は、このコードとして新しいタブ/パーツを追加することができ、通常のコマンドハンドラではIEventBrokerのhandleEvent

@Execute 
    public void execute(Shell shell, EPartService partService, MApplication application,EModelService modelService) throws URISyntaxException{ 
     MPart part = MBasicFactory.INSTANCE.createPart(); 
     part.setLabel("New file "); 
     part.setCloseable(true); 
     part.setContributionURI("bundleclass://com.xxx.rcp.app.item_editor/com.xxx.rcp.app.item_editor.parts.ItemEditorPart"); 
     List<MPartStack> stacks = modelService.findElements(application, "com.xxx.rcp.app.partstack.2", MPartStack.class, null); 
     stacks.get(0).getChildren().add(part); 
     partService.showPart(part, PartState.ACTIVATE); 
    } 

今、私は新しいタブ/パーツを追加したいです1つのIEventBroker handleEventです。

まず、私はアクティベーターで話題を登録します。

@Override 
    public void start(BundleContext context) throws Exception { 
     IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(context); 
     IEventBroker eventBroker = serviceContext.get(IEventBroker.class); 
     eventBroker.subscribe("MY_TOPIC", ContextInjectionFactory.make(OpenItemEditorHandler2.class, serviceContext)); 
} 

その後、私はのhandleEventのタブ/パーツを追加します。

public class OpenItemEditorHandler2 implements EventHandler { 

// @Inject 
// private IEclipseContext serviceContext; 

// @Inject 
// EPartService partService; 

// @Inject 
// MApplication application; 

    @Inject 
    IEclipseContext serviceContext; 

// @Inject 
// EModelService modelService; 

// @Inject 
// private ECommandService commandService; 
// 
// @Inject 
// private EHandlerService handlerService; 

@Override 
    public void handleEvent(Event event) { 
MPart part = MBasicFactory.INSTANCE.createPart(); 
     part.setLabel("New file "); 
     part.setCloseable(true); 
     part.setContributionURI("bundleclass://com.xxx.rcp.app.item_editor/com.xxx.rcp.app.item_editor.parts.ItemEditorPart"); 
     // get the part stack and show created part 
     EModelService modelService = serviceContext.get(EModelService.class); 
     MApplication application = serviceContext.get(MApplication.class); 
     List<MPartStack> stacks = modelService.findElements(application, "com.xxx.rcp.app.partstack.2", MPartStack.class, null); 
} 

私はすべてのためにヌルでそれらのサービスにアクセスしたり、注入することはできません。どうして?私はオブジェクトOpenItemEditorHandler2Activatorに注射しました。

または、新しいタブ/パートを追加するための他の解決策についてのヒントを教えてください。

ありがとうございました!

答えて

1

EclipseContextFactory.getServiceContextによって返されたコンテキストは、それはあなたがあなたのクラスを作成し、これを使用することはできませんのでは、通常のE4サービスのほとんどが含まれていません、OSGiサービスがあります。つまり、アクティベータはあなたの購読を設定するのに適した場所ではありません。

適切なEclipseコンテキストにアクセスできる場所にサブスクリプションを設定する必要があります。 AddOnまたはRCP LifeCycleが適切かもしれません。あなたが持つかもしれないAddOnコンストラクタで

@Inject 
public MyAddon(IEclipseContext context, IEventBroker eventBroker) 
{ 
    eventBroker.subscribe("MY_TOPIC", ContextInjectionFactory.make(OpenItemEditorHandler2.class, context)); 
} 
+0

私は、アプリケーションモデルに新しいアドオンを追加しました。バンドルクラスでは、 'Activator'のようにコードを追加しました。しかし、 'handleEvent'は呼び出されませんでした。 'LifeCycle'については、これらのコードは別のRCPアプリのプラグインにあります。 'LifeCycle'を追加するには?ありがとう! – aviit

+0

メインのRCPプラグインでのみLifeCycleを使用できます。 AddOnのコードは、 'EclipseContextFactory.getServiceContext'を使用していない** IEclipseContextを直接注入するべきです。更新された回答を参照 –

+0

ありがとう、greg!コードが機能しました。しかし、問題があります。「MyAddon」では、これらのサービスをメソッド/コンストラクタに注入するだけで、フィールドに注入することができます。 – aviit

関連する問題