2016-12-02 3 views
0

私のウィザードのフィールドの一部を注入しようとしています。Eclipse RCP IWizardのインスタンスに注入

私は成功し、次のヘルパークラスを使用して独自のOSGi DSを注入することができますようにPartServiceやMApplicationなどRCP生態系から

public class UtilRCP { 

public static void inject(Plugin plugin, Object object) { 

IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(plugin.getBundle().getBundleContext()); 
ContextInjectionFactory.inject(object, serviceContext); 
    } 
} 

その他のサービスには、(ヌル/実際の値が見つかりませんでした注入することに失敗します引数 "MApplication")。ここで

はコード

public class MyWizard extends MyAbstractWizard implements IImportWizard { 

    private MyWizardPage page; 
    @Inject 
    private EPartService partService; 
    @Inject 
    private DatabaseProvider databaseProvider; 
    @Inject 
    private MApplication application; 

    public MyWizard() { 
     System.err.println("Create"); 
     System.err.println(databaseProvider); 
     System.err.println(partService); 
     System.err.println(application); 
    } 

    @Override 
    public void init(IWorkbench workbench, IStructuredSelection selection) { 

     UtilRCP.inject(Activator.getDefault(), this); 
     System.err.println("Init"); 
     System.err.println(databaseProvider); 
     System.err.println(partService); 
     System.err.println(application); 
    } 

    @Override 
    public void addPages() { 

     super.addPages(); 
     page = new MyWizardPage(); 
     addPage(page); 
    } 

    @Override 
    public boolean performFinish() { 

     return true; 
    } 
} 

答えて

1

サービスコンテキストは、非常に限られた内容を持っており、このような使用には適していないです。あなたが使用してIWorkbenchオブジェクトからワークベンチのコンテキストを取得することができ、このような3.xスタイルウィザードで

:ダイアログがアクティブなとき、アクティブな「部分」は存在しないこと

@Override 
public void init(IWorkbench workbench, IStructuredSelection selection) { 

    IEclipseContext context = (IEclipseContext)workbench.getService(IEclipseContext.class); 

は注意(ダイアログ理由一部ではありません)。これは、さまざまなAPIで問題を引き起こす可能性があります。特に、アプリケーション(ワークベンチ)パーツサービスは、アクティブな部分がないと不平を言う例外を与えます。

あなたは明示的に使用して、トップレベルウィンドウの一部のサービスを取得することにより、作業部のサービスを受けることができます:私はの3.x RCPのトップレベルのウィンドウIDが「IDEWindow」であると考えてい

@Inject 
MApplication application; 
@Inject 
EModelService modelService; 

MWindow window = (MWindow)modelService.find("top level window id", application); 

EPartService partService = window.getContext().get(EPartService.class); 

を。

そのE4アプリケーションならば、あなたはあなたのApplication.e4xmlメインウィンドウIDを見つけることができます。

+0

それは魅力のように機能します、ありがとうございます! 呼び出すことによってコンテキストを取得するのがなぜ違うのか説明できますか? IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(plugin.getBundle()。getBundleContext()); および IEclipseContext serviceContext = workbench.getService(IEclipseContext.class); – kerner1000

+0

PartServiceを使用すると、次の例外が発生します。 java.lang.IllegalStateException:アプリケーションにアクティブなウィンドウがありません \t at org.eclipse.e4.ui.internal.workbench.ApplicationPartServiceImpl.getActiveWindowService(ApplicationPartServiceImpl.java:43 ) – kerner1000

+0

各部分に1つ、アプリケーションに1つ、多くの部分に多くの異なるコンテキストがあります。サービスコンテキストは、OSGiサービスの特殊なコンテキストであり、他の用途には適していません。 –