2017-07-05 6 views
0

特定のパースペクティブに切り替えるためのコマンド/ハンドラを提供しようとしています。プログラムでパースペクティブを開く

私は以下のクラスを思い付いた:

public class OpenPerspectiveHandler { 

    private static final Logger logger = Logger.getLogger(OpenPerspectiveHandler.class); 
    @Inject 
    private MApplication application; 
    @Inject 
    private EModelService modelService; 
    @Inject 
    private EPartService partService; 
    private final String perspectiveId; 

    public OpenPerspectiveHandler(String perspectiveId) { 
     super(); 
     this.perspectiveId = perspectiveId; 
    } 

    public void changePerspective(String perspectiveId) { 

     Optional<MPerspective> perspective = findPerspective(); 
     if(perspective.isPresent()) { 
      partService.switchPerspective(perspective.get()); 
     } else { 
      logger.debug("Perspective not found (" + perspectiveId + ")"); 
     } 
    } 

    @Execute 
    public void execute() { 

     changePerspective(perspectiveId); 
    } 

    private Optional<MPerspective> findPerspective() { 

     MUIElement element = modelService.find(perspectiveId, application); 
     if(element instanceof MPerspective) { 
      return Optional.of((MPerspective)element); 
     } else { 
      logger.debug("Wrong type " + element); 
     } 
     return Optional.empty(); 
    } 

    @Override 
    public String toString() { 

     return "OpenPerspectiveHandler [application=" + application + ", modelService=" + modelService + ", partService=" + partService + ", perspectiveId=" + perspectiveId + "]"; 
    } 
} 

興味深いことに、これは一度だけ動作します。この問題を回避するには、MPerspectiveが見つかったらキャッシュし、もう一度modelService.find(perspectiveId, application)を使用しないでください。

なぜ1回だけ動作しますか? modelService.find(perspectiveId, application)は、最初の実行後にnullを返します。

EDIT:

別のアプローチは、(グレッグ-449により示唆されるように)以下の通りです:

public class OpenPerspectiveHandler { 

    private static final Logger logger = Logger.getLogger(OpenPerspectiveHandler.class); 
    private final String perspectiveId; 

    public OpenPerspectiveHandler(String perspectiveId) { 
     super(); 
     this.perspectiveId = perspectiveId; 
    } 

    @Execute 
    public void changePerspective(MApplication application, EModelService modelService, EPartService partService) { 

     Optional<MPerspective> perspective = findPerspective(application, modelService); 
     if(perspective.isPresent()) { 
      partService.switchPerspective(perspective.get()); 
     } else { 
      logger.debug("Perspective not found (" + perspectiveId + ")"); 
     } 
    } 

    private Optional<MPerspective> findPerspective(MApplication application, EModelService modelService) { 

     MUIElement element = modelService.find(perspectiveId, application); 
     if(element instanceof MPerspective) { 
      return Optional.of((MPerspective)element); 
     } else { 
      logger.debug("Wrong type " + element); 
     } 
     return Optional.empty(); 
    } 
} 

しかし、このアプローチはまた、一度だけ視点を変更します。 modelService.find(perspectiveId, application);は、最初の実行後にnullを返します。

+1

さて?たとえば、モデルサービスが機能していることがわかりますか?ハンドラーでフィールド注入を使用するのは最良ではありません**常に@Executeメソッドの引数としてすべてを注入することに注意してください。パーツサービスのようなものは、状況に応じて変わることがあります。 –

+0

'modelService.find(perspectiveId、application)'は、最初の実行後に 'null'を返します。 – kerner1000

+0

@Executeメソッドへの注入に関するヒントをありがとう。更新された質問をご覧ください。 – kerner1000

答えて

0

EPartServiceは、ハンドラが実行されるコンテキストによって異なります。アクティブなウィンドウがある場合にのみ機能するアプリケーションパートサービスがある場合があります。

次のようなものを使用して、そのウィンドウの一部のサービスを受けることができます。

二度目に動作していないこれのどの部分
MWindow window = (MWindow)modelService.find("top window id", application); 

IEclipseContext windowContext = window.getContext(); 

EPartService windowPartService = windowContext.get(EPartService.class); 
+0

この方法を使用すると同じ問題が発生します。 windowPartServiceは問題ではなく、 'MPerspective'オブジェクトは' null'です( 'modelService.find(perspectiveId、application);')。 – kerner1000

+0

多分この '!MESSAGE Perspective with name'に関連しています 'Identify' and id '[..] .ui.perspective。'ローカルコピーに作成されました。' – kerner1000

+0

これはちょうどe4ですか、それともe3コンポーネントと混ざっていますか?そのメッセージは[このバグ]のように聞こえるため(https://bugs.eclipse.org/bugs/show_bug.cgi?id = 489816) –

関連する問題